From fff0d657d73d82e902559fbe8fd88d9cff65f4db Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Fri, 8 May 2026 15:29:19 +0200 Subject: [PATCH 1/3] skip no-route in GetRequestRector --- .../GetRequestRector/Fixture/fixture.php.inc | 8 ++++++++ .../GetRequestRector/Fixture/fixture2.php.inc | 12 ++++++++++-- .../Fixture/skip_non_routed.php.inc | 13 +++++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 rules-tests/Symfony30/Rector/ClassMethod/GetRequestRector/Fixture/skip_non_routed.php.inc diff --git a/rules-tests/Symfony30/Rector/ClassMethod/GetRequestRector/Fixture/fixture.php.inc b/rules-tests/Symfony30/Rector/ClassMethod/GetRequestRector/Fixture/fixture.php.inc index 98088ed76..5298c38df 100644 --- a/rules-tests/Symfony30/Rector/ClassMethod/GetRequestRector/Fixture/fixture.php.inc +++ b/rules-tests/Symfony30/Rector/ClassMethod/GetRequestRector/Fixture/fixture.php.inc @@ -3,9 +3,13 @@ namespace Rector\Symfony\Tests\Symfony30\Rector\ClassMethod\GetRequestRector\Fixture; use Symfony\Bundle\FrameworkBundle\Controller\Controller; +use Symfony\Component\Routing\Annotation\Route; class ClassWithNamedService extends Controller { + /** + * @Route("/some-action", name="some_action") + */ public function someAction() { $this->getRequest()->getSomething(); @@ -19,9 +23,13 @@ class ClassWithNamedService extends Controller namespace Rector\Symfony\Tests\Symfony30\Rector\ClassMethod\GetRequestRector\Fixture; use Symfony\Bundle\FrameworkBundle\Controller\Controller; +use Symfony\Component\Routing\Annotation\Route; class ClassWithNamedService extends Controller { + /** + * @Route("/some-action", name="some_action") + */ public function someAction(\Symfony\Component\HttpFoundation\Request $request) { $request->getSomething(); diff --git a/rules-tests/Symfony30/Rector/ClassMethod/GetRequestRector/Fixture/fixture2.php.inc b/rules-tests/Symfony30/Rector/ClassMethod/GetRequestRector/Fixture/fixture2.php.inc index 0f68fdb17..a8d7d2297 100644 --- a/rules-tests/Symfony30/Rector/ClassMethod/GetRequestRector/Fixture/fixture2.php.inc +++ b/rules-tests/Symfony30/Rector/ClassMethod/GetRequestRector/Fixture/fixture2.php.inc @@ -3,10 +3,14 @@ namespace Rector\Symfony\Tests\Symfony30\Rector\ClassMethod\GetRequestRector\Fixture; use Symfony\Bundle\FrameworkBundle\Controller\Controller; +use Symfony\Component\Routing\Annotation\Route; use Symfony\TestCase\Request; -class ClassWithParameterPresent extends Controller +final class ClassWithParameterPresent extends Controller { + /** + * @Route("/some-action", name="some_action") + */ public function someAction(Request $request) { $this->getRequest()->getSomething(); @@ -20,10 +24,14 @@ class ClassWithParameterPresent extends Controller namespace Rector\Symfony\Tests\Symfony30\Rector\ClassMethod\GetRequestRector\Fixture; use Symfony\Bundle\FrameworkBundle\Controller\Controller; +use Symfony\Component\Routing\Annotation\Route; use Symfony\TestCase\Request; -class ClassWithParameterPresent extends Controller +final class ClassWithParameterPresent extends Controller { + /** + * @Route("/some-action", name="some_action") + */ public function someAction(Request $request, \Symfony\Component\HttpFoundation\Request $mainRequest) { $mainRequest->getSomething(); diff --git a/rules-tests/Symfony30/Rector/ClassMethod/GetRequestRector/Fixture/skip_non_routed.php.inc b/rules-tests/Symfony30/Rector/ClassMethod/GetRequestRector/Fixture/skip_non_routed.php.inc new file mode 100644 index 000000000..783dc666a --- /dev/null +++ b/rules-tests/Symfony30/Rector/ClassMethod/GetRequestRector/Fixture/skip_non_routed.php.inc @@ -0,0 +1,13 @@ +getRequest()->getSomething(); + } +} From 235d6407406e46e661109e8d8539a3412084293e Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Fri, 8 May 2026 15:34:32 +0200 Subject: [PATCH 2/3] require Route annotation --- .../Fixture/with_fqn_route.php.inc | 37 +++++++++++++++++++ ...iceSettersToSettersAutodiscoveryRector.php | 2 +- .../Rector/ClassMethod/GetRequestRector.php | 26 +++++++++++++ .../RemoveServiceFromSensioRouteRector.php | 5 +-- ...SensioRouteAnnotationWithSymfonyRector.php | 7 ++-- ...sureTwigExtensionToNativeMethodsRector.php | 3 +- .../Class_/CommandHelpToAttributeRector.php | 3 +- ...onstraintOptionsToNamedArgumentsRector.php | 3 +- src/Enum/SensioAnnotation.php | 10 +++++ 9 files changed, 85 insertions(+), 11 deletions(-) create mode 100644 rules-tests/Symfony30/Rector/ClassMethod/GetRequestRector/Fixture/with_fqn_route.php.inc create mode 100644 src/Enum/SensioAnnotation.php diff --git a/rules-tests/Symfony30/Rector/ClassMethod/GetRequestRector/Fixture/with_fqn_route.php.inc b/rules-tests/Symfony30/Rector/ClassMethod/GetRequestRector/Fixture/with_fqn_route.php.inc new file mode 100644 index 000000000..8302a4c11 --- /dev/null +++ b/rules-tests/Symfony30/Rector/ClassMethod/GetRequestRector/Fixture/with_fqn_route.php.inc @@ -0,0 +1,37 @@ +getRequest()->getSomething(); + } +} + +?> +----- +getSomething(); + } +} + +?> diff --git a/rules/Configs/Rector/Closure/ServiceSettersToSettersAutodiscoveryRector.php b/rules/Configs/Rector/Closure/ServiceSettersToSettersAutodiscoveryRector.php index 92af8194d..14aec1010 100644 --- a/rules/Configs/Rector/Closure/ServiceSettersToSettersAutodiscoveryRector.php +++ b/rules/Configs/Rector/Closure/ServiceSettersToSettersAutodiscoveryRector.php @@ -218,7 +218,7 @@ private function createAbsolutePathConcat(string $classFilePath): Concat { $relativeDirectoryPath = $this->filesystem->makePathRelative( dirname($classFilePath), - dirname($this->getFile()->getFilePath()) + dirname((string) $this->getFile()->getFilePath()) ); $distConstFetch = new ConstFetch(new Name('__DIR__')); diff --git a/rules/Symfony30/Rector/ClassMethod/GetRequestRector.php b/rules/Symfony30/Rector/ClassMethod/GetRequestRector.php index 04996a91a..e70beae64 100644 --- a/rules/Symfony30/Rector/ClassMethod/GetRequestRector.php +++ b/rules/Symfony30/Rector/ClassMethod/GetRequestRector.php @@ -4,6 +4,7 @@ namespace Rector\Symfony\Symfony30\Rector\ClassMethod; +use PhpParser\Comment\Doc; use PhpParser\Node; use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Expr\Variable; @@ -16,6 +17,8 @@ use Rector\PhpParser\Node\BetterNodeFinder; use Rector\Rector\AbstractRector; use Rector\Symfony\Bridge\NodeAnalyzer\ControllerMethodAnalyzer; +use Rector\Symfony\Enum\SensioAnnotation; +use Rector\Symfony\Enum\SymfonyAnnotation; use Rector\Symfony\Enum\SymfonyClass; use Rector\Symfony\TypeAnalyzer\ControllerAnalyzer; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; @@ -118,6 +121,10 @@ private function resolveUniqueName(ClassMethod $classMethod, string $name): stri private function isActionWithGetRequestInBody(ClassMethod $classMethod): bool { + if (! $this->hasRouteDocblock($classMethod)) { + return false; + } + if (! $this->controllerMethodAnalyzer->isAction($classMethod)) { return false; } @@ -247,4 +254,23 @@ private function refactorClassMethod(ClassMethod $classMethod): null|ClassMethod return $classMethod; } + + private function hasRouteDocblock(ClassMethod $classMethod): bool + { + // need a @Route docblock + $doc = $classMethod->getDocComment(); + if (! $doc instanceof Doc) { + return false; + } + + if (str_contains($doc->getText(), '@Route')) { + return true; + } + + if (str_contains($doc->getText(), SymfonyAnnotation::ROUTE)) { + return true; + } + + return str_contains($doc->getText(), SensioAnnotation::ROUTE); + } } diff --git a/rules/Symfony34/Rector/ClassMethod/RemoveServiceFromSensioRouteRector.php b/rules/Symfony34/Rector/ClassMethod/RemoveServiceFromSensioRouteRector.php index a9240b632..ecb665a1a 100644 --- a/rules/Symfony34/Rector/ClassMethod/RemoveServiceFromSensioRouteRector.php +++ b/rules/Symfony34/Rector/ClassMethod/RemoveServiceFromSensioRouteRector.php @@ -12,6 +12,7 @@ use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory; use Rector\Comments\NodeDocBlock\DocBlockUpdater; use Rector\Rector\AbstractRector; +use Rector\Symfony\Enum\SensioAnnotation; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -79,9 +80,7 @@ public function refactor(Node $node): ?Node return null; } - $doctrineAnnotationTagValueNode = $phpDocInfo->getByAnnotationClass( - 'Sensio\Bundle\FrameworkExtraBundle\Configuration\Route' - ); + $doctrineAnnotationTagValueNode = $phpDocInfo->getByAnnotationClass(SensioAnnotation::ROUTE); if (! $doctrineAnnotationTagValueNode instanceof DoctrineAnnotationTagValueNode) { return null; } diff --git a/rules/Symfony34/Rector/ClassMethod/ReplaceSensioRouteAnnotationWithSymfonyRector.php b/rules/Symfony34/Rector/ClassMethod/ReplaceSensioRouteAnnotationWithSymfonyRector.php index 4eed82287..413ab7a99 100644 --- a/rules/Symfony34/Rector/ClassMethod/ReplaceSensioRouteAnnotationWithSymfonyRector.php +++ b/rules/Symfony34/Rector/ClassMethod/ReplaceSensioRouteAnnotationWithSymfonyRector.php @@ -15,6 +15,7 @@ use Rector\Comments\NodeDocBlock\DocBlockUpdater; use Rector\Configuration\RenamedClassesDataCollector; use Rector\Rector\AbstractRector; +use Rector\Symfony\Enum\SensioAnnotation; use Rector\Symfony\Enum\SymfonyAnnotation; use Rector\Symfony\PhpDocNode\SymfonyRouteTagValueNodeFactory; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; @@ -28,8 +29,6 @@ */ final class ReplaceSensioRouteAnnotationWithSymfonyRector extends AbstractRector { - private const string SENSIO_ROUTE_NAME = 'Sensio\Bundle\FrameworkExtraBundle\Configuration\Route'; - public function __construct( private readonly SymfonyRouteTagValueNodeFactory $symfonyRouteTagValueNodeFactory, private readonly PhpDocTagRemover $phpDocTagRemover, @@ -100,7 +99,7 @@ public function refactor(Node $node): ?Node return null; } - $sensioDoctrineAnnotationTagValueNodes = $phpDocInfo->findByAnnotationClass(self::SENSIO_ROUTE_NAME); + $sensioDoctrineAnnotationTagValueNodes = $phpDocInfo->findByAnnotationClass(SensioAnnotation::ROUTE); // nothing to find if ($sensioDoctrineAnnotationTagValueNodes === []) { @@ -125,7 +124,7 @@ public function refactor(Node $node): ?Node } $this->renamedClassesDataCollector->addOldToNewClasses([ - self::SENSIO_ROUTE_NAME => SymfonyAnnotation::ROUTE, + SensioAnnotation::ROUTE => SymfonyAnnotation::ROUTE, ]); $this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node); diff --git a/rules/Symfony61/Rector/Class_/MagicClosureTwigExtensionToNativeMethodsRector.php b/rules/Symfony61/Rector/Class_/MagicClosureTwigExtensionToNativeMethodsRector.php index 0146a9ba9..1d5d8e5b9 100644 --- a/rules/Symfony61/Rector/Class_/MagicClosureTwigExtensionToNativeMethodsRector.php +++ b/rules/Symfony61/Rector/Class_/MagicClosureTwigExtensionToNativeMethodsRector.php @@ -182,6 +182,7 @@ private function isNonStaticExternalClassCallable(ArrayCallable $arrayCallable, return false; } - return ! $classReflection->getMethod($methodName, $scope)->isStatic(); + return ! $classReflection->getMethod($methodName, $scope) + ->isStatic(); } } diff --git a/rules/Symfony73/Rector/Class_/CommandHelpToAttributeRector.php b/rules/Symfony73/Rector/Class_/CommandHelpToAttributeRector.php index 0c0e09a09..4551de8c5 100644 --- a/rules/Symfony73/Rector/Class_/CommandHelpToAttributeRector.php +++ b/rules/Symfony73/Rector/Class_/CommandHelpToAttributeRector.php @@ -176,7 +176,8 @@ function (Node $node) use (&$helpString): int|null|Expr { return null; } - $argExpr = $node->getArgs()[0]->value; + $argExpr = $node->getArgs()[0] + ->value; $resolvedValue = $this->resolveStringExpr($argExpr); if ($resolvedValue === null) { return null; diff --git a/rules/Symfony73/Rector/Class_/ConstraintOptionsToNamedArgumentsRector.php b/rules/Symfony73/Rector/Class_/ConstraintOptionsToNamedArgumentsRector.php index 1ec616ef9..e55ec4474 100644 --- a/rules/Symfony73/Rector/Class_/ConstraintOptionsToNamedArgumentsRector.php +++ b/rules/Symfony73/Rector/Class_/ConstraintOptionsToNamedArgumentsRector.php @@ -125,7 +125,8 @@ public function refactor(Node $node): ?Node $array = $node->args[0]->value; $namedArgs = []; - $oldTokens = $this->getFile()->getOldTokens(); + $oldTokens = $this->getFile() + ->getOldTokens(); foreach ($array->items as $item) { if (! $item instanceof ArrayItem) { diff --git a/src/Enum/SensioAnnotation.php b/src/Enum/SensioAnnotation.php new file mode 100644 index 000000000..fdc91e1b1 --- /dev/null +++ b/src/Enum/SensioAnnotation.php @@ -0,0 +1,10 @@ + Date: Fri, 8 May 2026 13:38:11 +0000 Subject: [PATCH 3/3] [rector] Rector fixes --- .../Closure/ServiceSettersToSettersAutodiscoveryRector.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rules/Configs/Rector/Closure/ServiceSettersToSettersAutodiscoveryRector.php b/rules/Configs/Rector/Closure/ServiceSettersToSettersAutodiscoveryRector.php index 14aec1010..92af8194d 100644 --- a/rules/Configs/Rector/Closure/ServiceSettersToSettersAutodiscoveryRector.php +++ b/rules/Configs/Rector/Closure/ServiceSettersToSettersAutodiscoveryRector.php @@ -218,7 +218,7 @@ private function createAbsolutePathConcat(string $classFilePath): Concat { $relativeDirectoryPath = $this->filesystem->makePathRelative( dirname($classFilePath), - dirname((string) $this->getFile()->getFilePath()) + dirname($this->getFile()->getFilePath()) ); $distConstFetch = new ConstFetch(new Name('__DIR__'));