Skip to content

[PropertyInfo] Added support for extract type from default value #27570

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 21, 2019
Merged

[PropertyInfo] Added support for extract type from default value #27570

merged 1 commit into from
Feb 21, 2019

Conversation

tsantos84
Copy link
Contributor

@tsantos84 tsantos84 commented Jun 9, 2018

Q A
Branch? master
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets -
License MIT
Doc PR -

Added support for extract type from property's default value.

class Dummy
{
    private $age = 30;
}

$types = $propertyInfo->getTypes('Dummy', 'age');

/*
  Example Result
  --------------
  array(1) {
    [0] =>
    class Symfony\Component\PropertyInfo\Type (6) {
      private $builtinType          => string(3) "int"
      private $nullable             => bool(false)
      private $class                => 'Dummy'
      private $collection           => bool(false)
      private $collectionKeyType    => NULL
      private $collectionValueType  => NULL
    }
  }
*/

Copy link
Member

@fabpot fabpot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with a minor comment

return null;
}

$map = array(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about storing this as a static property on the class instead?

Copy link
Contributor Author

@tsantos84 tsantos84 Jun 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. Actually, quite obvious.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can even be a private const as it targets master.

'double' => Type::BUILTIN_TYPE_FLOAT,
);

$type = gettype($defaultValues[$property]);
Copy link
Member

@dunglas dunglas Jun 28, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's safer to pass (return null) if the type is null.
Most of the time, it will mean that the property isn't initialized (and it may be initialized in the constructor, with null values not allowed). Even when null is intended, it's very unlikely that it will be the only type allowed.

Returning an array means that next extractors in the chain will be skipped, it shouldn't be the case if the value is null (another extractor may find the relevant type).

@tsantos84
Copy link
Contributor Author

tsantos84 commented Jul 3, 2018

@dunglas, anymore work should be done here?

*/
public function testExtractWithDefaultValue($property, $type)
{
$this->assertEquals($type, $this->extractor->getTypes('Symfony\Component\PropertyInfo\Tests\Fixtures\DefaultValue', $property, array()));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DefaultValue::class

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, but there are a lot of FQN refers in the test class. It's ok?

@tsantos84
Copy link
Contributor Author

Hi @dunglas, Anymore work to do here?

Copy link
Contributor

@sroze sroze left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice 👍

@@ -42,6 +42,12 @@ class ReflectionExtractor implements PropertyListExtractorInterface, PropertyTyp
*/
public static $defaultArrayMutatorPrefixes = array('add', 'remove');

private static $mapTypes = array(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be a const right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure.

@tsantos84
Copy link
Contributor Author

Could someone point me how to fix the broken tests on PHP 7.1? I didn't find out how these last changes has impacted on other SF components.

@dunglas
Copy link
Member

dunglas commented Sep 18, 2018

Failures look unrelated with your changes.

@tsantos84
Copy link
Contributor Author

Nice. Thank you.

@fabpot
Copy link
Member

fabpot commented Oct 10, 2018

@tsantos84 Can you rebase this PR to get rid of the merge commits (this is a requirement before I can merge)? Thank you.

@tsantos84
Copy link
Contributor Author

@fabpot, I've got all this commits after rebasing the branch. Is this the expected result? Sounds weird for me. I've never use rebase before to be honest.

@tsantos84
Copy link
Contributor Author

Github had bad moments exactly when I've tried to call you to help me with rebase. I think its better to create a new branch and cherry pick only the necessary commits.

@xabbuh
Copy link
Member

xabbuh commented Nov 3, 2018

@tsantos84 I have rebased your PR. Can you double-check that I didn't mess up anything?

@tsantos84
Copy link
Contributor Author

All commits are there. Many thanks for help me in this PR.

@xabbuh
Copy link
Member

xabbuh commented Nov 5, 2018

It could be worth to add an entry to the component's changelog file.

@nicolas-grekas
Copy link
Member

@tsantos84 could you please add a note in the changelog file of the component as suggested by @xabbuh?

@tsantos84
Copy link
Contributor Author

Sure. Which version should I reference?

@tsantos84
Copy link
Contributor Author

@nicolas-grekas?

@chalasr
Copy link
Member

chalasr commented Dec 12, 2018

@tsantos84 4.3.0 is the target here.

@tsantos84
Copy link
Contributor Author

Done, @dunglas. Sorry for the very long time since your request for these changes.

@fabpot
Copy link
Member

fabpot commented Feb 21, 2019

Thank you @tsantos84.

@fabpot fabpot merged commit f6510cd into symfony:master Feb 21, 2019
fabpot added a commit that referenced this pull request Feb 21, 2019
…ault value (tsantos84)

This PR was squashed before being merged into the 4.3-dev branch (closes #27570).

Discussion
----------

[PropertyInfo] Added support for extract type from default value

| Q             | A
| ------------- | ---
| Branch?       | master |
| Bug fix?      | no
| New feature?  | yes |
| BC breaks?    | no   |
| Deprecations? | no |
| Tests pass?   | yes |
| Fixed tickets | - |
| License       | MIT |
| Doc PR        | - |

Added support for extract type from property's default value.

```php

class Dummy
{
    private $age = 30;
}

$types = $propertyInfo->getTypes('Dummy', 'age');

/*
  Example Result
  --------------
  array(1) {
    [0] =>
    class Symfony\Component\PropertyInfo\Type (6) {
      private $builtinType          => string(3) "int"
      private $nullable             => bool(false)
      private $class                => 'Dummy'
      private $collection           => bool(false)
      private $collectionKeyType    => NULL
      private $collectionValueType  => NULL
    }
  }
*/
```

Commits
-------

f6510cd [PropertyInfo] Added support for extract type from default value
@tsantos84 tsantos84 deleted the extract-property-type-from-default-value branch February 21, 2019 14:15
@nicolas-grekas nicolas-grekas modified the milestones: next, 4.3 Apr 30, 2019
@fabpot fabpot mentioned this pull request May 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants