Skip to content
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

Allow usage of self:: accessor for constants #368

Merged

Conversation

bertrandseurot
Copy link
Contributor

@bertrandseurot bertrandseurot commented Oct 2, 2020

ex: @Annotation(self::VALUE)
Fixes #269

@bertrandseurot bertrandseurot force-pushed the allow-self-reference-for-constants branch from f4a499b to c3a707b Compare October 2, 2020 10:45
@stof
Copy link
Member

stof commented Oct 2, 2020

I think self should be forbidden when parsing an annotation on a trait, as self:: would resolve to the class using the trait in PHP, and we don't know that in the current code.

stof
stof previously requested changes Oct 2, 2020
tests/Doctrine/Tests/Common/Annotations/DocParserTest.php Outdated Show resolved Hide resolved
@bertrandseurot bertrandseurot force-pushed the allow-self-reference-for-constants branch 5 times, most recently from a1bd09d to 159936d Compare October 2, 2020 15:02
@bertrandseurot bertrandseurot force-pushed the allow-self-reference-for-constants branch from 159936d to dc1e9ea Compare October 2, 2020 15:15
@bertrandseurot
Copy link
Contributor Author

I think self should be forbidden when parsing an annotation on a trait, as self:: would resolve to the class using the trait in PHP, and we don't know that in the current code.

Seems to work in traits too. Am I missing something ?

@stof
Copy link
Member

stof commented Oct 2, 2020

@bertrandseurot if you use self:: in the code of a trait, it refers to the class using the trait, not to the trait itself, as trait are basically compiler-based copy/paste. See https://3v4l.org/BSPC3 for the proof.

So supporting self:: in annotations on traits would require that it works the same. If that's the case, this is fine.

@bertrandseurot
Copy link
Contributor Author

bertrandseurot commented Oct 5, 2020

@stof This will fails only if you try to directly parse a trait out of a class context :

$reflectionTrait = new \ReflectionClass( RandomTrait::class );
$reader = new AnnotationReader();
$reader->getMethodAnnotations( reflectionTrait->getMethod( 'randomMethod')  );

It coul'd be annoying if you wanted to do that in order to get the traits annotation cached, and use this cache later when parsing classes using this trait. But since the php Reflection API does not not allow to know if a property/method of a trait is overriden in the class, I think this strategy is currently not possible.

I confirm that it works in other cases (tested in testMethodAnnotationSupportsSelfAccessorForConstants() and testPropertyAnnotationSupportsSelfAccessorForConstants)

@bertrandseurot
Copy link
Contributor Author

Because of a force-push, this PR seems stuck in a "changes requested" status. Sorry for that, it's my first PR. Should I open another or it is not a problem ?

@greg0ire
Copy link
Member

It's not a problem and it's not because of a force-push. You need to get another review from @stof , or to get somebody from the team to dismiss his review.

@bertrandseurot
Copy link
Contributor Author

OK, thank you !

Copy link
Member

@beberlei beberlei left a comment

Choose a reason for hiding this comment

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

This looks good and brings annotations in line with how PHP 8 attributes work.

@beberlei beberlei merged commit fe25aa5 into doctrine:master Oct 17, 2020
@beberlei beberlei added this to the 1.11.0 milestone Oct 17, 2020
@bertrandseurot bertrandseurot deleted the allow-self-reference-for-constants branch October 19, 2020 15:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants