Skip to content

Commit 7d80f74

Browse files
committed
fix(rename): Enhance parameter renaming logic for promoted variables
- Add check to skip renaming for promoted parameters in function-like nodes. - Improve handling of variable nodes to prevent unnecessary renaming.
1 parent 2bbd5a1 commit 7d80f74

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

src/Rector/FunctionLike/RenameGarbageParamNameRector.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,18 @@
2525
use PhpParser\Node\Expr\Variable;
2626
use PhpParser\Node\FunctionLike;
2727
use PhpParser\Node\Param;
28+
use PhpParser\Node\Stmt\ClassMethod;
2829
use PhpParser\Node\Stmt\Foreach_;
2930
use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode;
31+
use PHPStan\Reflection\ClassReflection;
3032
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
3133
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
3234
use Rector\BetterPhpDocParser\ValueObject\PhpDocAttributeKey;
3335
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
3436
use Rector\DeadCode\NodeAnalyzer\ExprUsedInNodeAnalyzer;
3537
use Rector\PhpParser\Enum\NodeGroup;
3638
use Rector\PhpParser\Node\BetterNodeFinder;
39+
use Rector\PHPStan\ScopeFetcher;
3740
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
3841

3942
/**
@@ -204,6 +207,9 @@ private function refactorForeach(Foreach_ $foreachNode): ?Foreach_
204207
)) ? $foreachNode : null;
205208
}
206209

210+
/**
211+
* @throws \Rector\Exception\ShouldNotHappenException
212+
*/
207213
private function refactorFunctionLike(FunctionLike $functionLikeNode): ?FunctionLike
208214
{
209215
$hasChanged = false;
@@ -214,6 +220,8 @@ private function refactorFunctionLike(FunctionLike $functionLikeNode): ?Function
214220
foreach ($functionLikeNode->getParams() as $paramNode) {
215221
if (
216222
!$paramNode->var instanceof Variable
223+
|| $paramNode->isPromoted()
224+
|| $this->hasPrototypeMethod($functionLikeNode)
217225
|| $this->isUsedVariable($functionLikeNode, $paramNode->var)
218226
) {
219227
continue;
@@ -235,6 +243,28 @@ private function refactorFunctionLike(FunctionLike $functionLikeNode): ?Function
235243
return $hasChanged ? $functionLikeNode : null;
236244
}
237245

246+
/**
247+
* @throws \Rector\Exception\ShouldNotHappenException
248+
*/
249+
private function hasPrototypeMethod(FunctionLike $functionLikeNode): bool
250+
{
251+
if (!$functionLikeNode instanceof ClassMethod) {
252+
return false;
253+
}
254+
255+
$classReflection = ScopeFetcher::fetch($functionLikeNode)->getClassReflection();
256+
257+
if (!$classReflection instanceof ClassReflection) {
258+
return false;
259+
}
260+
261+
try {
262+
return $classReflection->getNativeReflection()->getMethod($this->getName($functionLikeNode))->hasPrototype();
263+
} catch (\ReflectionException $reflectionException) {
264+
return false;
265+
}
266+
}
267+
238268
private function isUsedVariable(FunctionLike $node, Variable $variableNode): bool
239269
{
240270
// Skip abstract, interface, empty body function like and foreach.

tests/Rector/FunctionLike/RenameGarbageParamNameRector/Fixture/skips.php.inc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,23 @@ interface Bar
1212
// Skip interface method.
1313
public function run($param): void;
1414
}
15+
16+
class Baz
17+
{
18+
// Skip promoted property parameter.
19+
public function __construct(private Bar $bar)
20+
{
21+
}
22+
}
23+
24+
use PhpParser\Node;
25+
use Rector\Rector\AbstractRector;
26+
27+
final class FooRector extends AbstractRector
28+
{
29+
// Skip has prototype method.
30+
public function refactor(Node $node): ?Node
31+
{
32+
return null;
33+
}
34+
}

0 commit comments

Comments
 (0)