-
Notifications
You must be signed in to change notification settings - Fork 91
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
chore(symfony-routing): replace Symfony route annotation by attribute one #557
chore(symfony-routing): replace Symfony route annotation by attribute one #557
Conversation
(i'm going to add a test for this) |
845d002
to
9dcc10d
Compare
497bb68
to
1cba32c
Compare
@@ -18,4 +19,10 @@ | |||
// @see Symfony 5.2+ https://github.com/symfony/doctrine-bridge/commit/02d2cf4743331e6b69ffd1d68e09b7e2dc417201#diff-1a16e2739e51eab000116d0542bd0226cea59a6d64711740ed7ce14769f95d1b | |||
new AnnotationToAttribute('Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity'), | |||
]); | |||
$rectorConfig->ruleWithConfiguration( | |||
RenameClassRector::class, | |||
[ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems not needed, just update the 2nd arg of AnnotationToAttribute
arg to Symfony\Component\Routing\Attribute\Route
above on line 18 should auto change to target attribute with new name
new AnnotationToAttribute('Symfony\Component\Routing\Annotation\Route', 'Symfony\Component\Routing\Attribute\Route'
test is needed tho
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/cc @scuben @JohJohan could you verify that "Symfony\Component\Routing\Attribute\Route" class exists in specific symfony version when upgrading from "Symfony\Component\Routing\Annotation\Route" thank you
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, rename seems still needed if it "already an attribute", but with "old name".
For other AnnotationToAttribute usage, the target and rename seems needed for this case, but need to ensure that the target attribute class already exists on specific symfony version when attribute introduced
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Symfony\Component\Routing\Attribute\Route
is available since symfony 6.4 AFAIK
I'm going to have a look at your feedbacks when at home.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's what I mean, if that target class introduced in later version, eg transforming annotation is start from symfony 5.2, while the new target attribute class only exists since 6.4, then that RenameClassRector need to be placed in specific setlists, the issue is, that need to be checked that rename attribute, not rename annotation
Ok, I am checking, the PHP 8 route attribute support exists since symfony 5.2, but the new class: "Symfony\Component\Routing\Attribute\Route" not yet exists, ref:
only the class: "Symfony\Component\Routing\Annotation\Route". Then, start from symfony 6.4, the "Symfony\Component\Routing\Attribute\Route" exists, but the so the original "Symfony\Component\Routing*Annotation*\Route" is still valid, which means the mapping is not necessarily needed. If this is needed, to avoid invalid rename, as it only should be used in the attribute part, I think custom rule is needed which the step is:
https://github.com/rectorphp/rector-symfony/blob/main/config/sets/symfony/symfony64.php which the "Symfony\Component\Routing\Attribute\Route" exists. |
I give it a try very soon, thanks for all the details ! |
c0fc2eb
to
2407797
Compare
if ($node instanceof Attribute && $node->name !== null && (string) $node->name === self::ANNOTATION_ROUTE) { | ||
// there I'd like to modify the UseUse node representing my current node->name | ||
// AFAIK the node I'm returning from this method is only the Attribute node | ||
// maybe should I retrieve the global AST from somewhere and manually get the UseUse node to edit the value | ||
// Or maybe there is a more elegant way to do that ? | ||
|
||
return null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would like some help on this part. I think this if statement is enough to identify that the #[Route] attribute is used in the file.
But I'm stuck to modify the related UseUse node of this attribute. Have you any recommendations on this ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to import to use statement, that will be taken care by : $rectorConfig->importNames()
when enabled.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh I see what you mean, in that case, $rectorConfig->importNames()
is working on RenameClassRector
, but not this custom rector, you may need to lookup of what AnnotationToAttributeRector implementation, set Name node, and add Name node to import nodes collector to be imported later
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use Rector\PostRector\Collector\UseNodesToAddCollector
service for that, eg:
$this->useNodesToAddCollector->addUseImport(new FullyQualifiedObjectType($namespacedAttrName));
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After some thinking, replace use statement directly is unnecessary on this case, if that needed, you can do:
$rectorConfig->importNames();
$rectorConfig->removeUnusedImports();
That will be safer imo, and that will be replaced.
The improvement for this case need to be verified carefully, and that need separate PR.
@samsonasik I started an implementation of the behaviour you described but I'm stuck. I can't figure out how to modify the use statement related to the #[Route] attribute node |
rules/Symfony64/Rector/Class_/FixImportWhileUsingRouteAttributeButAnnotationImportRector.php
Outdated
Show resolved
Hide resolved
|
||
public function getNodeTypes(): array | ||
{ | ||
return [Attribute::class]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use ClassMethod
instead, and rename its attrGroups
so it only change on ClassMethod
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The #Route[] attribute can be specified on a method but also on a class in Symfony
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
then, you need to define in Class_
and ClassMethod
2407797
to
2a86bfc
Compare
…by attribute one Replace Symfony\Component\Routing\Annotation\Route by Symfony\Component\Routing\Attribute\Route
2a86bfc
to
6c57a74
Compare
} | ||
|
||
?> | ||
----- |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The second part start from -----
can be removed if equal content
...ngeRouteAttributeFromAnnotationSubnamespaceRector/Fixture/use-annotation-should-skip.php.inc
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, thank you
Thank you @mcsky |
Thanks @mcsky 🙏 |
Integrate in Symfony upgrade the replacement of Symfony\Component\Routing\Annotation\Route by Symfony\Component\Routing\Attribute\Route
see this issue
Closes rectorphp/rector#8384