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

[Php80] Handle nested attribute with constant on AnnotationAttributeRector #1661

Merged
merged 7 commits into from
Jan 13, 2022

Conversation

Jean85
Copy link
Contributor

@Jean85 Jean85 commented Jan 11, 2022

This is a follow-up as suggested in rectorphp/rector#6928 (comment), follow up of #1659.

@TomasVotruba
Copy link
Member

Thanks 👍

Could you extract it to a new file? Each fixture should be as minimal as possible, as this annotation parsing deals with tokens and it's very detailed work. The smaller scope the better.

@Jean85
Copy link
Contributor Author

Jean85 commented Jan 11, 2022

@TomasVotruba done, but fixing this is definitely out of my league 😄

Copy link
Contributor Author

@Jean85 Jean85 left a comment

Choose a reason for hiding this comment

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

@TomasVotruba I reworked the example (it was wrong) and I attempted to fix the parsing. This is now ready for review.

use Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Source\GenericAnnotation;
use Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Source\ConstantReference;

#[GenericAnnotation(some: [ConstantReference::FIRST_NAME => ['foo' => ['bar' => ['baz']]], 0 => ConstantReference::LAST_NAME])]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I would love to obtain this:

Suggested change
#[GenericAnnotation(some: [ConstantReference::FIRST_NAME => ['foo' => ['bar' => ['baz']]], 0 => ConstantReference::LAST_NAME])]
#[GenericAnnotation(some: [ConstantReference::FIRST_NAME => ['foo' => ['bar' => ['baz']]], ConstantReference::LAST_NAME])]

... but it seems basically impossible, since there's no way to say to PHP "this element has no key", it does in fact have one, just an autogenerated-due-to-append one.

@samsonasik
Copy link
Member

@Jean85 as you provided a fix as well, could you update PR title? something like: [Php80] Handle nested attribute with constant on AnnotationAttributeRector ?

@Jean85 Jean85 changed the title Update fixture for #6928 [Php80] Handle nested attribute with constant on AnnotationAttributeRector Jan 13, 2022
@Jean85
Copy link
Contributor Author

Jean85 commented Jan 13, 2022

Done, thanks for the suggestion, I'm on mobile and I just copy pasted it 😂

Comment on lines +99 to +105
$tokenIterator->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);

if ($tokenIterator->isCurrentTokenTypes([Lexer::TOKEN_CLOSE_CURLY_BRACKET, Lexer::TOKEN_COMMA])) {
// it's a value, not a key
return [null, $key];
}

Copy link
Member

Choose a reason for hiding this comment

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

I often struggle with the right tokens to consume. This is amazing work 👍 Thank you

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you! The test suite was very comprehensive, and contributing was really easy! I struggled a little with the parsing too, but reviewing the fixture put me on the right track.

@TomasVotruba TomasVotruba merged commit a84d951 into rectorphp:main Jan 13, 2022
@TomasVotruba
Copy link
Member

Thank you 👏

@Jean85 Jean85 deleted the patch-1 branch January 13, 2022 21:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants