Skip to content

Commit ec51521

Browse files
Fix rule when method is __invoke (#884)
* Fix rule when method is __invoke * Use method isUnsafeOverridden and fix TU --------- Co-authored-by: Johan MARTIN <johan.martin@kosmos.fr>
1 parent eb0c034 commit ec51521

2 files changed

Lines changed: 52 additions & 3 deletions

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Rector\Symfony\Tests\CodeQuality\Rector\Class_\InlineClassRoutePrefixRector\Fixture;
4+
5+
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
6+
use Symfony\Component\Routing\Annotation\Route;
7+
8+
/**
9+
* @Route(path="/city", name="some_org.")
10+
*/
11+
final class WithInvokeMethod extends Controller
12+
{
13+
/**
14+
* @Route("/street", name="some")
15+
*/
16+
public function __invoke()
17+
{
18+
}
19+
}
20+
21+
?>
22+
-----
23+
<?php
24+
25+
namespace Rector\Symfony\Tests\CodeQuality\Rector\Class_\InlineClassRoutePrefixRector\Fixture;
26+
27+
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
28+
use Symfony\Component\Routing\Annotation\Route;
29+
30+
final class WithInvokeMethod extends Controller
31+
{
32+
/**
33+
* @Route("/city/street", name="some_org.some")
34+
*/
35+
public function __invoke()
36+
{
37+
}
38+
}
39+
40+
?>

rules/CodeQuality/Rector/Class_/InlineClassRoutePrefixRector.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTagRemover;
1616
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
1717
use Rector\Doctrine\NodeAnalyzer\AttrinationFinder;
18+
use Rector\NodeAnalyzer\MagicClassMethodAnalyzer;
1819
use Rector\Rector\AbstractRector;
1920
use Rector\Symfony\Enum\FosAnnotation;
2021
use Rector\Symfony\Enum\SymfonyAnnotation;
@@ -47,7 +48,8 @@ public function __construct(
4748
private readonly PhpDocTagRemover $phpDocTagRemover,
4849
private readonly DocBlockUpdater $docBlockUpdater,
4950
private readonly ControllerAnalyzer $controllerAnalyzer,
50-
private readonly AttrinationFinder $attrinationFinder
51+
private readonly AttrinationFinder $attrinationFinder,
52+
private readonly MagicClassMethodAnalyzer $magicClassMethodAnalyzer
5153
) {
5254
}
5355

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

136138
foreach ($node->getMethods() as $classMethod) {
137-
if (! $classMethod->isPublic() || $classMethod->isMagic()) {
139+
if ($this->shouldSkipMethod($classMethod)) {
138140
continue;
139141
}
140142

@@ -255,14 +257,21 @@ public function refactor(Node $node): ?Class_
255257
return $node;
256258
}
257259

260+
private function shouldSkipMethod(Node\Stmt\ClassMethod $classMethod): bool
261+
{
262+
return
263+
!$classMethod->isPublic()
264+
|| $this->magicClassMethodAnalyzer->isUnsafeOverridden($classMethod);
265+
}
266+
258267
private function shouldSkipClass(Class_ $class): bool
259268
{
260269
if (! $this->controllerAnalyzer->isController($class)) {
261270
return true;
262271
}
263272

264273
foreach ($class->getMethods() as $classMethod) {
265-
if (! $classMethod->isPublic() || $classMethod->isMagic()) {
274+
if ($this->shouldSkipMethod($classMethod)) {
266275
continue;
267276
}
268277

0 commit comments

Comments
 (0)