Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[PropertyInfo] ConstructorExtractor which has higher priority than PhpDocExtractor and ReflectionExtractor#30128

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

Closed
karser wants to merge3 commits intosymfony:4.2fromkarser:4-2-constructor-extractor

Conversation

@karser
Copy link
Contributor

QA
Branch?4.2
Bug fix?yes
New feature?no
BC breaks?hopefully no
Deprecations?no
Tests pass?yes
Fixed tickets#30053
LicenseMIT

The discussion is in previous PR#30056
In short, when using PhpDocExtractor, it ignores the constructor argument type, althoughargument types from the constructor are the only types that are valid for the class instantiation.

This PR adds a separate extractor -ConstructorExtractor which is called first (-999) and it attempts to extract the type from constructor only, either from PhpDoc or using reflection.
I addedgetTypesFromConstructor toPhpDocExtractor andReflectionExtractor - they implementConstructorArgumentTypeExtractorInterface interface.ConstructorExtractor aggregates those extractors using compiler pass.

So the flow of control looks like this:

PropertyInfoExtractor::getTypes:    - ConstructorExtractor::getTypes        - PhpDocExtractor::getTypesFromConstructor        - ReflectionExtractor::getTypesFromConstructor    - PhpDocExtractor::getTypes    - ReflectionExtractor::getTypes

@nicolas-grekasnicolas-grekas added this to the4.2 milestoneFeb 10, 2019
*
* @return DocBlock
*/
privatefunctionfilterDocBlockParams(DocBlock$docBlock,$allowedParam)
Copy link
Contributor

Choose a reason for hiding this comment

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

You should use type hints and return types wherever possible as this is for 4.2 and new code if I am right

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

@OskarStark I applied the type hints in#30335

@jvasseur
Copy link
Contributor

The problem with this solution is that by replacing the property types with the ones from the constructor we will break updating the entity with the setter (when usingobject_to_populate for example) because the type will be wrong there instead.

I think the only way to solve this problem without breaking anything would be to hold constructor argument metadata separately from property metadata. Maybe we could go with theConstructorArgumentTypeExtractorInterface interface introduced in this PR but instead of using aConstructorExtractor we could call thegetTypesFromConstructor directly from the denormalizer.

@karser
Copy link
ContributorAuthor

@jvasseur
How should we approachthese controversial cases?
We could restrict inferring the property type from constructor to the original case only (see#30053) where the property is private and doesn't have getter/setter.
If we did that, would it still break updating the entity with the setter?

@jvasseur
Copy link
Contributor

@karser I think we souldn't infer the property type from constructor arguments at all, this would leave the property type guessing only to for properties (and setter/getter) and provide a completely separate API for getting constructor types.

I was originaly not sure about guessing property types from constructor arguments (#25605) but didn't have an example ready to be sure if there would be a problem or not, I guess now we have one.

rvanlaak reacted with thumbs up emoji

@fabpot
Copy link
Member

As discussed in the PR for 3.4, this is a new feature and as such it should target master.

*
* @return Type[]|null
*/
publicfunctiongetTypesFromConstructor($class,$property);
Copy link
Contributor

Choose a reason for hiding this comment

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

you should add typehints and return type

*
* @return \ReflectionParameter|null
*/
privatefunctiongetReflectionParameterFromConstructor($property,\ReflectionMethod$reflectionConstructor)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
privatefunction getReflectionParameterFromConstructor($property,\ReflectionMethod$reflectionConstructor)
privatefunction getReflectionParameterFromConstructor(string$property,\ReflectionMethod$reflectionConstructor): ?\ReflectionParameter


publicfunctiontestGetTypes()
{
$this->assertEquals([newType(Type::BUILTIN_TYPE_STRING)],$this->extractor->getTypes('Foo','bar', []));
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you useassertSame() here?

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

I can't, assertSameasserts that two variables reference the same object.
image

* @param int $date Timestamp
* @param \DateTimeInterface $dateObject
*/
publicfunction__construct(\DateTimeZone$timezone,$date,$dateObject,\DateTime$dateTime)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
publicfunction __construct(\DateTimeZone$timezone,$date,$dateObject,\DateTime$dateTime)
publicfunction __construct(\DateTimeZone$timezone,int$date,\DateTimeInterface$dateObject,\DateTime$dateTime)

Copy link
Contributor

Choose a reason for hiding this comment

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

but$dateObject isn't used´ .... 🤔

Copy link
ContributorAuthor

@karserkarserFeb 21, 2019
edited
Loading

Choose a reason for hiding this comment

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

@OskarStark That's on purpose, they are used for testing phpdoc types extraction from constructor args even if no such property exists.

OskarStark reacted with thumbs up emoji
@karser
Copy link
ContributorAuthor

closing this in favor of#30335

@karserkarser closed thisFeb 21, 2019
@karserkarser deleted the 4-2-constructor-extractor branchFebruary 21, 2019 16:49
fabpot added a commit that referenced this pull requestAug 26, 2020
…riority than PhpDocExtractor and ReflectionExtractor (karser)This PR was merged into the 5.2-dev branch.Discussion----------[PropertyInfo] ConstructorExtractor which has higher priority than PhpDocExtractor and ReflectionExtractor| Q             | A| ------------- | ---| Branch?       | master| Bug fix?      | yes| New feature?  | yes| BC breaks?    | hopefully no| Deprecations? | no| Tests pass?   | yes| Fixed tickets |#30053| License       | MITSupersedes#30056#30128In short, when using PhpDocExtractor, it ignores the constructor argument type, although `argument types from the constructor are the only types that are valid for the class instantiation`.This PR adds a separate extractor - `ConstructorExtractor` which is called first (-999) and it attempts to extract the type from constructor only, either from PhpDoc or using reflection.I added `getTypesFromConstructor` to `PhpDocExtractor` and `ReflectionExtractor` - they implement `ConstructorArgumentTypeExtractorInterface` interface. `ConstructorExtractor` aggregates those extractors using compiler pass.So the flow of control looks like this:```PropertyInfoExtractor::getTypes:    - ConstructorExtractor::getTypes        - PhpDocExtractor::getTypesFromConstructor        - ReflectionExtractor::getTypesFromConstructor    - PhpDocExtractor::getTypes    - ReflectionExtractor::getTypes```Commits-------5049e25 Added ConstructorExtractor which has higher priority than PhpDocExtractor and ReflectionExtractor
fabpot added a commit that referenced this pull requestJan 6, 2025
…xtractor` class (HypeMC)This PR was merged into the 7.3 branch.Discussion----------[FrameworkBundle][PropertyInfo] Wire the `ConstructorExtractor` class| Q             | A| ------------- | ---| Branch?       | 7.3| Bug fix?      | no| New feature?  | yes| Deprecations? | no| Tickets       | -| License       | MIT| Doc PR        | -A sort of followup to#30335.The original PR removed the wiring to prevent creating a BC break, see#30128 (comment) &#30335 (comment).This PR proposes adding a new `framework.property_info.with_constructor_extractor` configuration to allow optionally enabling the framework integration. The configuration defaults to `false` to prevent creating a BC break. Only when it's set to `true` will the `ConstructorExtractor` be registered in the container.Commits-------2b392b1 [FrameworkBundle][PropertyInfo] Wire the `ConstructorExtractor` class
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@OskarStarkOskarStarkOskarStark left review comments

Assignees

No one assigned

Projects

None yet

Milestone

4.2

Development

Successfully merging this pull request may close these issues.

6 participants

@karser@jvasseur@fabpot@OskarStark@nicolas-grekas@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp