Skip to content

Commit 8f7905f

Browse files
authored
[CodeQuality] Skip got duplicated method after suffix removed on ActionSuffixRemoverRector (#827)
* [CodeQuality] Skip got duplicated method after suffix removed on ActionSuffixRemoverRector * Fix
1 parent 3e0e4aa commit 8f7905f

6 files changed

Lines changed: 36 additions & 9 deletions

File tree

config/sets/symfony/symfony3/symfony32.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
declare(strict_types=1);
44

5-
use Rector\Arguments\Rector\ClassMethod\ReplaceArgumentDefaultValueRector;
6-
use Rector\Arguments\ValueObject\ReplaceArgumentDefaultValue;
75
use Rector\Config\RectorConfig;
86

97
return static function (RectorConfig $rectorConfig): void {

rector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@
4747
])
4848
->withPhpSets()
4949
->withPreparedSets(
50+
deadCode: true,
5051
codeQuality: true,
5152
typeDeclarations: true,
52-
deadCode: true,
5353
privatization: true,
5454
naming: true,
5555
rectorPreset: true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Rector\Symfony\Tests\Rector\ClassMethod\ActionSuffixRemoverRector\Fixture;
4+
5+
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
6+
7+
class SkipMethodExistsAfterSuffixedRemoved extends Controller
8+
{
9+
public function indexAction()
10+
{
11+
$this->index();
12+
}
13+
14+
private function index()
15+
{
16+
}
17+
}

rules/CodeQuality/Rector/ClassMethod/ActionSuffixRemoverRector.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
use PhpParser\Node;
99
use PhpParser\Node\Identifier;
1010
use PhpParser\Node\Stmt\ClassMethod;
11+
use PHPStan\Reflection\ClassReflection;
1112
use Rector\Rector\AbstractRector;
13+
use Rector\Reflection\ReflectionResolver;
1214
use Rector\Symfony\Bridge\NodeAnalyzer\ControllerMethodAnalyzer;
1315
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
1416
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@@ -20,6 +22,7 @@ final class ActionSuffixRemoverRector extends AbstractRector
2022
{
2123
public function __construct(
2224
private readonly ControllerMethodAnalyzer $controllerMethodAnalyzer,
25+
private readonly ReflectionResolver $reflectionResolver
2326
) {
2427
}
2528

@@ -72,6 +75,13 @@ public function refactor(Node $node): ?Node
7275
return null;
7376
}
7477

78+
$classReflection = $this->reflectionResolver->resolveClassReflection($node);
79+
if ($classReflection instanceof ClassReflection
80+
&& $classReflection->hasNativeMethod(rtrim($node->name->toString(), 'Action'))
81+
) {
82+
return null;
83+
}
84+
7585
return $this->removeSuffix($node, 'Action');
7686
}
7787

rules/Symfony73/Rector/Class_/AddVoteArgumentToVoteOnAttributeRector.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use PhpParser\Node\Expr\Variable;
1010
use PhpParser\Node\Name;
1111
use PhpParser\Node\Name\FullyQualified;
12+
use PhpParser\Node\NullableType;
1213
use PhpParser\Node\Param;
1314
use PhpParser\Node\Stmt\Class_;
1415
use Rector\Rector\AbstractRector;
@@ -112,7 +113,7 @@ public function refactor(Node $node): ?Node
112113
$classMethod->params[] = new Param(
113114
new Variable('vote'),
114115
new ConstFetch(new Name('null')),
115-
new Node\NullableType(new FullyQualified(self::VOTE_CLASS))
116+
new NullableType(new FullyQualified(self::VOTE_CLASS))
116117
);
117118

118119
return $node;

rules/Symfony73/Rector/Class_/ConstraintOptionsToNamedArgumentsRector.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66

77
use PhpParser\Node;
88
use PhpParser\Node\Arg;
9-
use PhpParser\Node\Expr\Array_;
109
use PhpParser\Node\ArrayItem;
1110
use PhpParser\Node\Expr;
11+
use PhpParser\Node\Expr\Array_;
1212
use PhpParser\Node\Expr\New_;
13+
use PhpParser\Node\Identifier;
1314
use PhpParser\Node\Name;
1415
use PhpParser\Node\Name\FullyQualified;
1516
use Rector\PhpParser\Node\Value\ValueResolver;
@@ -79,14 +80,14 @@ public function refactor(Node $node): ?Node
7980
}
8081

8182
$argName = $node->args[0]->name;
82-
if ($argName !== null && $argName->name !== 'options') {
83+
if ($argName instanceof Identifier && $argName->name !== 'options') {
8384
return null;
8485
}
8586

8687
$array = $node->args[0]->value;
8788
$namedArgs = [];
8889

89-
foreach ($array->items as $key => $item) {
90+
foreach ($array->items as $item) {
9091
if (! $item instanceof ArrayItem) {
9192
continue;
9293
}
@@ -105,8 +106,8 @@ public function refactor(Node $node): ?Node
105106
continue;
106107
}
107108

108-
$arg = new Node\Arg($item->value);
109-
$arg->name = new Node\Identifier($keyValue);
109+
$arg = new Arg($item->value);
110+
$arg->name = new Identifier($keyValue);
110111

111112
$namedArgs[] = $arg;
112113
}

0 commit comments

Comments
 (0)