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] Added support for extracting type from constructor#25605

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

Merged
dunglas merged 1 commit intosymfony:masterfromlyrixx:property-info-constructor
Feb 19, 2018

Conversation

@lyrixx
Copy link
Member

QA
Branch?master
Bug fix?no
New feature?yes
BC breaks?no
Deprecations?no
Tests pass?yes
Fixed tickets-
LicenseMIT
Doc PR-

derrabus and Koc reacted with thumbs up emojidunglas and rvanlaak reacted with hooray emojijvasseur reacted with confused emoji
privatefunctionextractFromConstructor(string$class,string$property): ?array
{
try {
$class =new \ReflectionClass($class);

Choose a reason for hiding this comment

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

Instead of this:

$class =new \ReflectionClass($class);$constructor =$class->getConstructor();

maybe we can use this:

$constructor = (new \ReflectionClass($class))->getConstructor();

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

fixed.

returnnull;
}

foreach ($constructor->getParameters()as$name =>$parameter) {
Copy link
Member

@javiereguiluzjaviereguiluzDec 26, 2017
edited
Loading

Choose a reason for hiding this comment

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

Is the$name of the foreach different than the$parameter->name used below or we can reuse it?

Copy link
MemberAuthor

@lyrixxlyrixxDec 26, 2017
edited
Loading

Choose a reason for hiding this comment

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

$name === '$string' (note the leading$).

So I removed the$name assignment. Thanks

@lyrixxlyrixxforce-pushed theproperty-info-constructor branch from4bc0bdb toe1672aaCompareDecember 26, 2017 12:11
Copy link
Member

@javiereguiluzjaviereguiluz left a comment

Choose a reason for hiding this comment

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

Thanks for this contribution!

returnnull;
}

if (!$constructor) {
Copy link
Member

Choose a reason for hiding this comment

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

Should we take parent classes into account too?

Choose a reason for hiding this comment

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

@lyrixx WDYT? How does phpstorm handle overridden constructors? It could help decide.

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

I think we should take parents into account.
But I don't know how phpstorm handle that. I don't use it. You should know it ;)
I will check

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

Just checked, PHP storm is using parent constructor too. I will update the PR

damienalexandre reacted with thumbs up emoji
Copy link
MemberAuthor

Choose a reason for hiding this comment

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

Actually, it's already supported ;) I added a new test case to ensure it works as expected.

Choose a reason for hiding this comment

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

I was thinking about this situation:

class a{public$foo;function__construct(string$foo)  {$this->foo =$foo;  }}class bextends a{function__construct()  {parent::__construct('hello');  }}

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

Ah ok. I have added support for such case with a test case.

I guess the PR is now READY \o/

@nicolas-grekasnicolas-grekas added this to the4.1 milestoneDec 27, 2017
@nicolas-grekas
Copy link
Member

@jvasseur please comment when adding a 👎 , we're all curious about all opinions to enlighten decisions.

@lyrixxlyrixxforce-pushed theproperty-info-constructor branch frome1672aa to4dcdbb2CompareDecember 27, 2017 11:23
@jvasseur
Copy link
Contributor

Sorry. I'm mostly against this change since having a constructor argument with the same name as a property doesn't necessarily mean it will be saved inside that property. This feels less reliable that using the types from getter/setters and I don't fell fine having this guess inside the same extractor. Maybe putting it inside a separate extractor that could be assigned a to a lower priority.

@lyrixx
Copy link
MemberAuthor

@jvasseur I agree with you, and it already have a lower priority. So there are no issue ;)

@jvasseur
Copy link
Contributor

@lyrixx it has a lower priority inside the reflection extractor, but it still is the same extractor meaning you can't run another extractor between the accessor and the constructor guess.

@lyrixx
Copy link
MemberAuthor

You are right, indeed ;)

But I'm not sure it worth a dedicated extractor. Do youreally think it worth this complexity?

@jvasseur
Copy link
Contributor

Not necessary I am just thinking this is something that could append but I don't have a real case were it would append.

@lyrixxlyrixxforce-pushed theproperty-info-constructor branch from4dcdbb2 to7ba526bCompareDecember 28, 2017 15:02
@lyrixx
Copy link
MemberAuthor

👍

returnarray($this->extractFromReflectionType($parameter->getType()));
}

if ($reflectionClass->getParentClass()) {

Choose a reason for hiding this comment

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

$parent = to prevent creating two instances of ReflectionClass

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

done.

*/
publicfunctiongetTypes($class,$property,array$context =array())
{
if ($fromMutator =$this->extractFromMutator($class,$property)) {
Copy link
Member

Choose a reason for hiding this comment

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

Is is really necessary to change this (it will make it harder to merge bug fixes later)?

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

Nope, it was just done in order to optimize the code. I can revert it if you want

Choose a reason for hiding this comment

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

let's revert yes please :)

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

It's reverted

@dunglas
Copy link
Member

Shouldn't you also update thegetProperties() method to add properties accessible trough the constructor to the list (write only).

Also, if we start supporting this, it would be nice to also support constructors in the PhpDocExtractor (it is more powerful and precise than theReflectionExtractor).

@dunglas
Copy link
Member

dunglas commentedDec 29, 2017
edited
Loading

Regarding@jvasseur's comment, maybe can we add a flag in the constructor to disable this feature?

@javiereguiluz
Copy link
Member

@dunglas please, let's not complicate things with a constructor flag. I'd like to understand better the problem explained by@jvasseur. Imagine that this is your code:

private/* bool */$stock;publicfunction__construct(int$stock){$this->stock =$stock >10;}

The$stock property is private and it doesn't have any getter/setter. The type of the property would be guessed asint instead ofbool. Questions: 1) Is this code realistic or an academic example that nobody would use in practice? 2) Which parts of Symfony would stop working because of this wrong guess? Thanks!

Copy link
Member

@nicolas-grekasnicolas-grekas left a comment

Choose a reason for hiding this comment

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

apparently this needs to be opt-in, isn't it?

@lyrixx
Copy link
MemberAuthor

apparently this needs to be opt-in, isn't it?

What do you mean ?

*
* @return Type[]|null
*/
privatefunctionextractFromConstructor(string$class,string$property): ?array
Copy link
Contributor

Choose a reason for hiding this comment

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

why?array, and not?Type ?

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

Because it returns an array a not a Type

dunglas, lyrixx, and Taluu reacted with thumbs up emojimeyerbaptiste reacted with laugh emoji
Copy link
Member

@nicolas-grekasnicolas-grekas left a comment
edited
Loading

Choose a reason for hiding this comment

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

@dunglas, please advise: as is, context option, or constructor flag?

*/
publicfunctiongetTypes($class,$property,array$context =array())
{
if ($fromMutator =$this->extractFromMutator($class,$property)) {

Choose a reason for hiding this comment

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

let's revert yes please :)

@lyrixxlyrixxforce-pushed theproperty-info-constructor branch from2b1c2b3 to264f58fCompareFebruary 9, 2018 17:29
@dunglas
Copy link
Member

Both would be great (we already do that in the serializer component), the constructor flag allowing to set the default value, and the context option taking priority.
If we must choose, I tend to prefer the constructor flag.

@lyrixxlyrixxforce-pushed theproperty-info-constructor branch from264f58f toc5b3f45CompareFebruary 16, 2018 15:04
@lyrixx
Copy link
MemberAuthor

This PR is now ready

return$fromAccessor;
}

if (
Copy link
Member

Choose a reason for hiding this comment

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

$context['enable_constructor_extraction'] ?? $this->enableConstructorExtraction?

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

It's not the same, because if one want todisable it, with your code it will fallback on theenableConstructorExtraction value.

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

You are right. I fixed it.

@dunglas
Copy link
Member

Thanks@lyrixx.

@dunglasdunglas merged commitadcb25e intosymfony:masterFeb 19, 2018
dunglas added a commit that referenced this pull requestFeb 19, 2018
…constructor (lyrixx)This PR was merged into the 4.1-dev branch.Discussion----------[PropertyInfo] Added support for extracting type from constructor| Q             | A| ------------- | ---| Branch?       | master| Bug fix?      | no| New feature?  | yes| BC breaks?    | no| Deprecations? | no| Tests pass?   | yes| Fixed tickets | -| License       | MIT| Doc PR        | -Commits-------adcb25e [PropertyInfo] Added support for extracting type from constructor
@lyrixxlyrixx deleted the property-info-constructor branchFebruary 20, 2018 10:22
@fabpotfabpot mentioned this pull requestMay 7, 2018
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@xabbuhxabbuhxabbuh left review comments

@dunglasdunglasdunglas approved these changes

@javiereguiluzjaviereguiluzjaviereguiluz approved these changes

@nicolas-grekasnicolas-grekasnicolas-grekas approved these changes

@chalasrchalasrchalasr approved these changes

+2 more reviewers

@TaluuTaluuTaluu left review comments

@SimperfitSimperfitSimperfit approved these changes

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Projects

None yet

Milestone

4.1

Development

Successfully merging this pull request may close these issues.

10 participants

@lyrixx@nicolas-grekas@jvasseur@dunglas@javiereguiluz@Taluu@xabbuh@Simperfit@chalasr@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp