Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork9.7k
[PropertyInfo] Add an extractor to guess if a property is initializable#26997
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
a95d0a8 to1f2103eCompare| */ | ||
| publicfunctionisInitializable(string$class,string$property,array$context =array()): ?bool | ||
| { | ||
| $this->assertIsString($class); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
What's the point of this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
I added this for consistency with other methods, I'm not sure of the origin intent...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Here is the original intent:#19437
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Right. So type hints are added now so these checks are redundant
| /** | ||
| * Is the property initializable? | ||
| */ | ||
| publicfunctionisInitializable(string$class,string$property,array$context =array()): ?bool; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
What's the use-case for returningnull? Also, shouldn't we explicit within the method name that we are talking about the constructor in here?isInitializableViaConstructor or something like that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
null means "we don't know"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
@teohhanhui is right. It means "this extractor cannot guess", try the next registered in the chain. It's mandatory to keep this behavior for consistency with existing interfaces and to provide an extension point.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
@sroze I prefer the current name,isInitializableViaConstructor is too verbose and brings nothing much.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
My 2cts are that I didn't understand whatisInitializable meant before reading more about your PR. I can tell you that if I saw this interface I'd think "wait. What is that about?". While theisInitializableViaConstructor clarifies it and is not too verbose.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
I wouldn't, the PhpDoc that you've put is enough (to me).
teohhanhuiJun 30, 2018 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
"Property initializable" on its own indeed makes no sense to me. PerhapsConstructorInitializableExtractorInterface::isPropertyInitializable?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
PropertyInitializableViaConstructorExtractorInterface::isInitializable?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
I guess we could just stick with the current names. Lol...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
lyrixx left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Looks good to me
| continue; | ||
| if ($property===$parameter->name) { | ||
| returnarray($this->extractFromReflectionType($parameter->getType())); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
what's the point of this change ? I know it does the same, but with a different style, but then ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Removing 2 lines and making the code more explicit, but yes it's not mandatory.
| returnnull; | ||
| } | ||
| if ($constructor =$reflectionClass->getConstructor()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Why don't you test if the constructor is instantiable first?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Good idea, I'll update the code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
done
| interface PropertyInitializableExtractorInterface | ||
| { | ||
| /** | ||
| * Is the property initializable? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
not sure this is useful :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
It's for consistency with other interfaces.
nicolas-grekas commentedJun 19, 2018
rebase needed (and some comments to address) |
88b2286 to17cf6a1Comparedunglas commentedJun 30, 2018
rebased and comments addressed |
| /** | ||
| * Is the property initializable? | ||
| */ | ||
| publicfunctionisInitializable(string$class,string$property,array$context =array()): ?bool; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
My 2cts are that I didn't understand whatisInitializable meant before reading more about your PR. I can tell you that if I saw this interface I'd think "wait. What is that about?". While theisInitializableViaConstructor clarifies it and is not too verbose.
Uh oh!
There was an error while loading.Please reload this page.
fced167 to6737e71Comparedunglas commentedJul 13, 2018
Rebased, I suggest to keep the current name as there are no consensus on another name. |
| 4.2.0 | ||
| ----- | ||
| * added`PropertyInitializableExtractorInterface` to test if a property can be initialized through the constructor and an implementation is`ReflectionExtractor` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
if a property can be initialized through the constructor (implemented byReflectionExtractor)
| if ($constructor =$reflectionClass->getConstructor()) { | ||
| foreach ($constructor->getParameters()as$parameter) { | ||
| if ($property ===$parameter->name) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
The fact that the constructor parameter name must match the property one should be documented somewhere.
| interface PropertyInitializableExtractorInterface | ||
| { | ||
| /** | ||
| * Is the property initializable? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
This is probably where we need more docs about the naming convention.
fabpot commentedSep 4, 2018
@dunglas Do you have time to have a look at my comments? |
d125f25 to4a0483dComparedunglas commentedSep 4, 2018
@fabpot done |
dunglas commentedSep 4, 2018
CI errors look unrelated. |
Uh oh!
There was an error while loading.Please reload this page.
a8fb40e to9d2ab9eComparefabpot commentedSep 4, 2018
Thank you@dunglas. |
… is initializable (dunglas)This PR was squashed before being merged into the 4.2-dev branch (closes#26997).Discussion----------[PropertyInfo] Add an extractor to guess if a property is initializable| Q | A| ------------- | ---| Branch? | master| Bug fix? | no| New feature? | yes| BC breaks? | no <!-- seehttps://symfony.com/bc -->| Deprecations? | no <!-- don't forget to update UPGRADE-*.md and src/**/CHANGELOG.md files -->| Tests pass? | yes <!-- please add some, will be required by reviewers -->| Fixed tickets | n/a <!-- #-prefixed issue number(s), if any -->| License | MIT| Doc PR | todoWhen dealing with value objects, being able to detect if a property can be initialized using the constructor is a very valuable information. It's mandatory to add a proper value object support in API Platform and in the Serializer component.Seeapi-platform/core#1749 andapi-platform/core#1843 for the related discussions, extended use cases and proof of concepts.This PR adds a new interface to guess if a property can be initialized through the constructor, and an implementation using the reflection (in `ReflectionExtractor`).Commits-------9d2ab9e [PropertyInfo] Add an extractor to guess if a property is initializable
nicolas-grekas commentedSep 5, 2018 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
This PR breaks the CI by breaking BC somehow, see deps=high failures (which mean that 4.1 is broken when using PropertyInfo master) |
nicolas-grekas commentedSep 5, 2018 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
(fixed in5557e38, we'll have to deal with this kind of BC breaks now that we put DI passes in components and not in bundles anymore.) |
dunglas commentedSep 5, 2018
Thanks for the fix@nicolas-grekas |
javiereguiluz commentedSep 7, 2018
I've createdsymfony/symfony-docs#10272 to document this new feature. Please, don't forget to create a doc issue for every new feature. Kévin, if you don't have time to contribute the docs, we'll need some code examples of using this feature in action. Thanks! |
…s initializable (dunglas, javiereguiluz)This PR was merged into the master branch.Discussion----------[PropertyInfo] Add an extractor to guess if a property is initializablesymfony/symfony#26997symfony/symfony#24571Closes#10272.Commits-------8415f96 Minor tweakseebca4a RSTf7ed144 [PropertyInfo] Add an extractor to guess if a property is initializable
Uh oh!
There was an error while loading.Please reload this page.
When dealing with value objects, being able to detect if a property can be initialized using the constructor is a very valuable information. It's mandatory to add a proper value object support in API Platform and in the Serializer component.
Seeapi-platform/core#1749 andapi-platform/core#1843 for the related discussions, extended use cases and proof of concepts.
This PR adds a new interface to guess if a property can be initialized through the constructor, and an implementation using the reflection (in
ReflectionExtractor).