Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php declare (strict_types=1);

namespace Rector\Symfony\Tests\Symfony30\Rector\ClassMethod\GetRequestRector\Fixture;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

final class SkipNonRouted extends Controller
{
public function someAction()
{
$this->getRequest()->getSomething();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Rector\Symfony\Tests\Symfony30\Rector\ClassMethod\GetRequestRector\Fixture;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

final class WithFqnRoute extends Controller
{
/**
* @\Symfony\Component\Routing\Annotation\Route("/some-action", name="some_action")
*/
public function someAction()
{
$this->getRequest()->getSomething();
}
}

?>
-----
<?php

namespace Rector\Symfony\Tests\Symfony30\Rector\ClassMethod\GetRequestRector\Fixture;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

final class WithFqnRoute extends Controller
{
/**
* @\Symfony\Component\Routing\Annotation\Route("/some-action", name="some_action")
*/
public function someAction(\Symfony\Component\HttpFoundation\Request $request)
{
$request->getSomething();
}
}

?>
26 changes: 26 additions & 0 deletions rules/Symfony30/Rector/ClassMethod/GetRequestRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -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 === []) {
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ private function isNonStaticExternalClassCallable(ArrayCallable $arrayCallable,
return false;
}

return ! $classReflection->getMethod($methodName, $scope)->isStatic();
return ! $classReflection->getMethod($methodName, $scope)
->isStatic();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
10 changes: 10 additions & 0 deletions src/Enum/SensioAnnotation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Rector\Symfony\Enum;

final class SensioAnnotation
{
public const string ROUTE = 'Sensio\Bundle\FrameworkExtraBundle\Configuration\Route';
}
Loading