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
@@ -0,0 +1,40 @@
<?php

namespace Rector\Symfony\Tests\CodeQuality\Rector\Class_\InlineClassRoutePrefixRector\Fixture;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Routing\Annotation\Route;

/**
* @Route(path="/city", name="some_org.")
*/
final class WithInvokeMethod extends Controller
{
/**
* @Route("/street", name="some")
*/
public function __invoke()
{
}
}

?>
-----
<?php

namespace Rector\Symfony\Tests\CodeQuality\Rector\Class_\InlineClassRoutePrefixRector\Fixture;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Routing\Annotation\Route;

final class WithInvokeMethod extends Controller
{
/**
* @Route("/city/street", name="some_org.some")
*/
public function __invoke()
{
}
}

?>
15 changes: 12 additions & 3 deletions rules/CodeQuality/Rector/Class_/InlineClassRoutePrefixRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTagRemover;
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
use Rector\Doctrine\NodeAnalyzer\AttrinationFinder;
use Rector\NodeAnalyzer\MagicClassMethodAnalyzer;
use Rector\Rector\AbstractRector;
use Rector\Symfony\Enum\FosAnnotation;
use Rector\Symfony\Enum\SymfonyAnnotation;
Expand Down Expand Up @@ -47,7 +48,8 @@ public function __construct(
private readonly PhpDocTagRemover $phpDocTagRemover,
private readonly DocBlockUpdater $docBlockUpdater,
private readonly ControllerAnalyzer $controllerAnalyzer,
private readonly AttrinationFinder $attrinationFinder
private readonly AttrinationFinder $attrinationFinder,
private readonly MagicClassMethodAnalyzer $magicClassMethodAnalyzer
) {
}

Expand Down Expand Up @@ -134,7 +136,7 @@ public function refactor(Node $node): ?Class_
$hasChanged = false;

foreach ($node->getMethods() as $classMethod) {
if (! $classMethod->isPublic() || $classMethod->isMagic()) {
if ($this->shouldSkipMethod($classMethod)) {
continue;
}

Expand Down Expand Up @@ -255,14 +257,21 @@ public function refactor(Node $node): ?Class_
return $node;
}

private function shouldSkipMethod(Node\Stmt\ClassMethod $classMethod): bool
{
return
!$classMethod->isPublic()
|| $this->magicClassMethodAnalyzer->isUnsafeOverridden($classMethod);
}

private function shouldSkipClass(Class_ $class): bool
{
if (! $this->controllerAnalyzer->isController($class)) {
return true;
}

foreach ($class->getMethods() as $classMethod) {
if (! $classMethod->isPublic() || $classMethod->isMagic()) {
if ($this->shouldSkipMethod($classMethod)) {
continue;
}

Expand Down