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,58 @@
<?php

declare(strict_types=1);

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

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

class DifferentParameterTypeSameNameUsed extends AbstractController
{
public function one(Foo $service)
{
dump($service);
}

public function two(Foo $service)
{
dump($service);
}

public function three(Bar $service)
{
dump($service);
}
}

?>
-----
<?php

declare(strict_types=1);

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

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

class DifferentParameterTypeSameNameUsed extends AbstractController
{
public function __construct(private readonly \Rector\Symfony\Tests\CodeQuality\Rector\Class_\ControllerMethodInjectionToConstructorRector\Fixture\Foo $service)
{
}
public function one()
{
dump($this->service);
}

public function two()
{
dump($this->service);
}

public function three(Bar $service)
{
dump($service);
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,12 @@ public function refactor(Node $node): ?Node
}
}

/** @var array<array{ClassMethod, int}> $paramsToRemove */
$paramsToRemove = [];

/** @var array<string, string[]> $methodParamNamesToReplace */
$methodParamNamesToReplace = [];

foreach ($node->getMethods() as $classMethod) {
if ($this->shouldSkipClassMethod($classMethod)) {
continue;
Expand Down Expand Up @@ -213,8 +219,10 @@ public function refactor(Node $node): ?Node
continue;
}

unset($classMethod->params[$key]);
$propertyMetadatas[] = new PropertyMetadata($this->getName($param->var), $paramType);
$paramName = $this->getName($param->var);
$paramsToRemove[] = [$classMethod, $key];
$propertyMetadatas[$paramName] = new PropertyMetadata($paramName, $paramType);
$methodParamNamesToReplace[$classMethod->name->toString()][] = $paramName;
}
}

Expand All @@ -223,9 +231,9 @@ public function refactor(Node $node): ?Node
return null;
}

$paramNamesToReplace = [];
foreach ($propertyMetadatas as $propertyMetadata) {
$paramNamesToReplace[] = $propertyMetadata->getName();
// defer param removal to after collection to avoid mutation during iteration
foreach ($paramsToRemove as [$methodToModify, $paramKey]) {
unset($methodToModify->params[$paramKey]);
}

// 1. update constructor
Expand All @@ -238,7 +246,12 @@ public function refactor(Node $node): ?Node
continue;
}

$this->replaceParamUseWithPropertyFetch($classMethod, $paramNamesToReplace);
$methodName = $classMethod->name->toString();
if (! isset($methodParamNamesToReplace[$methodName])) {
continue;
}

$this->replaceParamUseWithPropertyFetch($classMethod, $methodParamNamesToReplace[$methodName]);
}

return $node;
Expand Down
Loading