Skip to content

Commit 0b1523a

Browse files
authored
[CodeQuality] Skip has conflicted param name different object on different method on ControllerMethodInjectionToConstructorRector (#925)
* [CodeQuality] Skip has conflicted param name different object on different method on ControllerMethodInjectionToConstructorRector * [CodeQuality] Skip has conflicted param name different object on different method on ControllerMethodInjectionToConstructorRector
1 parent aa281cb commit 0b1523a

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Symfony\Tests\CodeQuality\Rector\Class_\ControllerMethodInjectionToConstructorRector\Fixture;
6+
7+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
8+
9+
class SkipConflictParamNameDifferentObject extends AbstractController
10+
{
11+
public function foo(Foo $service): void
12+
{
13+
dump($service);
14+
}
15+
16+
public function bar(Bar $service): void
17+
{
18+
dump($service);
19+
}
20+
}

rules/CodeQuality/Rector/Class_/ControllerMethodInjectionToConstructorRector.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,15 @@ public function refactor(Node $node): ?Node
199199
continue;
200200
}
201201

202+
if ($this->hasConflictedParamName(
203+
$node,
204+
$classMethod->name->toString(),
205+
$this->getName($param->var),
206+
$this->getName($param->type)
207+
)) {
208+
continue;
209+
}
210+
202211
unset($classMethod->params[$key]);
203212
$propertyMetadatas[] = new PropertyMetadata($this->getName($param->var), $paramType);
204213
}
@@ -243,6 +252,33 @@ private function shouldSkipClassMethod(ClassMethod $classMethod): bool
243252
return $this->parentClassMethodTypeOverrideGuard->hasParentClassMethod($classMethod);
244253
}
245254

255+
private function hasConflictedParamName(
256+
Class_ $class,
257+
string $currentClassMethodName,
258+
string $paramName,
259+
string $paramType
260+
): bool {
261+
foreach ($class->getMethods() as $classMethod) {
262+
if ($this->isName($classMethod, $currentClassMethodName)) {
263+
continue;
264+
}
265+
266+
foreach ($classMethod->getParams() as $param) {
267+
if (! $param->var instanceof Variable) {
268+
continue;
269+
}
270+
271+
if (! $this->isName($param->var, $paramName)) {
272+
continue;
273+
}
274+
275+
return $param->type instanceof FullyQualified && ! $this->isName($param->type, $paramType);
276+
}
277+
}
278+
279+
return false;
280+
}
281+
246282
/**
247283
* @param string[] $paramNamesToReplace
248284
*/

0 commit comments

Comments
 (0)