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] Make ReflectionExtractor correctly extract nullability#40699

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
nicolas-grekas merged 1 commit intosymfony:4.4frommaxim-dovydenok:ticket_40659
May 7, 2021

Conversation

maxim-dovydenok
Copy link
Contributor

QA
Branch?4.4
Bug fix?yes
New feature?no
Deprecations?no
TicketsFix#40659
LicenseMIT
Doc PRno

When the property had a default value ReflectionExtractor was always returning isNullable: false. After PHP 7.4 we can get isNullable from the typehint.

@carsonbot
Copy link

Hey!

I see that this is your first PR. That is great! Welcome!

Symfony has acontribution guide which I suggest you to read.

In short:

  • Always add tests
  • Keep backward compatibility (seehttps://symfony.com/bc).
  • Bug fixes must be submitted against the lowest maintained branch where they apply (seehttps://symfony.com/releases)
  • Features and deprecations must be submitted against the 5.x branch.

Review the GitHub status checks of your pull request and try to solve the reported issues. If some tests are failing, try to see if they are failing because of this change.

When two Symfony core team members approve this change, it will be merged and you will become an official Symfony contributor!
If this PR is merged in a lower version branch, it will be merged up to all maintained branches within a few days.

I am going to sit back now and wait for the reviews.

Cheers!

Carsonbot

maxim-dovydenok reacted with thumbs up emoji

Copy link
Member

@derrabusderrabus left a comment

Choose a reason for hiding this comment

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

Thank you for working on this issue.

What bothers me about your solution is that you have taught a method namedextractFromDefaultValue() how to extract the desired information from the property type declaration. Now, the method doesn‘t do anymore what its name suggests.

Then again, there is a method namedextractFromReflectionType() that already does what you‘re trying to achieve.

@maxim-dovydenok
Copy link
ContributorAuthor

maxim-dovydenok commentedApr 4, 2021
edited
Loading

@derrabus, thank you for the review.

I agree that currentlyextractFromDefaultValue() knows more than only how to get the type from the default value. I did that because after PHP 7.4 it's no longer possible to correctly get property nullability from the default value.

Before this version, I tried to copy nullability from theextractFromReflectionType() result, but we can't do it since Type objects are not editable.

Then I was thinking about the following code, but it's harder to understand and we would also need to changeextractFromDefaultValue() method arguments. And even more, currently, we know that a nullability of allTypes in the$fromReflectionType is the same, but probably it can change in the future, so using the nullability of the first one is not the best option.

publicfunctiongetTypes($class,$property,array$context = []): ?array{...$isNullable =false;$fromReflectionType =null;if (\PHP_VERSION_ID >=70400) {try {$reflectionProperty =new \ReflectionProperty($class,$property);$type =$reflectionProperty->getType();if (null !==$type &&$fromReflectionType =$this->extractFromReflectionType($type,$reflectionProperty->getDeclaringClass())) {$isNullable =isset($fromReflectionType[0]) &&$fromReflectionType[0]->isNullable();            }        }catch (\ReflectionException$e) {// noop        }    }if ($fromDefaultValue =$this->extractFromDefaultValue($class,$property,$isNullable)) {return$fromDefaultValue;    }if ($fromReflectionType) {return$fromReflectionType;    }returnnull;}

Another solution would be to replace part of the current version ofgetTypes().

...if ($fromDefaultValue =$this->extractFromDefaultValue($class,$property)) {return$fromDefaultValue;}if (\PHP_VERSION_ID >=70400) {...return$this->extractFromReflectionType($type,$reflectionProperty->getDeclaringClass())) {    ...}returnnull;

With

...if ($fromPropertyDeclaration =$this->extractFromPropertyDeclaration($class,$property)) {return$fromPropertyDeclaration;}returnnull;

and

privatefunctionextractFromPropertyDeclaration(string$class,string$property): ?array{// calculate nullability// try to get from the default value// try to get from the property type, if PHP >= 7.4// else null}

What do you think about these solutions?

@derrabus
Copy link
Member

Okay, but if we have a typed property – why would we want to look at the default value at all? Wouldn't it be enough to prioritize the property type over the default value when extracting type information? Or am I missing something?

@maxim-dovydenok
Copy link
ContributorAuthor

maxim-dovydenok commentedApr 4, 2021
edited
Loading

I did some research on why typehint extraction is at the end. Typehint support was initially added in#34557, and it had the highest priority. And then in#38041 it was moved to the end of the method. After this issue was merged, in one of the comments, issue#38416 was mentioned.

I'm not sure if in this issue we should focus on property type priority versus constructor, mutator, and accessor. But I believe we can change the code so it uses property typehint on PHP 7.4 (and default value if there's no typehint) and a default value for older versions.

I don't think we're missing something, it's not possible to have a default value different from typehint.

derrabus, dpinheiro, and Kuramawers reacted with thumbs up emoji

@maxim-dovydenokmaxim-dovydenokforce-pushed theticket_40659 branch 2 times, most recently from649ca46 toa0d4d12CompareApril 4, 2021 15:51
@dpinheiro
Copy link

dpinheiro commentedApr 5, 2021
edited
Loading

I did some research on why typehint extraction is at the end. Typehint support was initially added in#34557, and it had the highest priority. And then in#38041 it was moved to the end of the method. After this issue was merged, in one of the comments, issue#38416 was mentioned.

I'm not sure if in this issue we should focus on property type priority versus constructor, mutator, and accessor. But I believe we can change the code so it uses property typehint on PHP 7.4 (and default value if there's no typehint) and a default value for older versions.

I don't think we're missing something, it's not possible to have a default value different from typehint.

I would like to take this opportunity to discuss this priority if possible. In my point of view#38041 introduced a BC break as serializer started having a different behaviour, and currently this in an impediment for me to update some of my projects, as described in#38416. Do you think we could change this order? I'm available to help with that

@derrabus
Copy link
Member

@dpinheiro I understand that the issue you've mentioned is important to you. But please let's leave the discussion on that ticket.

@nicolas-grekas
Copy link
Member

Thank you @shiftby.

@nicolas-grekasnicolas-grekas merged commitfab61ee intosymfony:4.4May 7, 2021
This was referencedMay 9, 2021
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@nicolas-grekasnicolas-grekasnicolas-grekas approved these changes

@derrabusderrabusderrabus approved these changes

@dunglasdunglasAwaiting requested review from dunglasdunglas is a code owner

Assignees
No one assigned
Projects
None yet
Milestone
4.4
Development

Successfully merging this pull request may close these issues.

5 participants
@maxim-dovydenok@carsonbot@derrabus@dpinheiro@nicolas-grekas

[8]ページ先頭

©2009-2025 Movatter.jp