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

declare(strict_types=1);

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

use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;

final class InternalMethodCallArgRemoval extends AbstractController
{
#[Route('/some-action', name: 'some_action')]
public function someAction(LoggerInterface $logger)
{
$this->problematic($logger);
}

public function problematic(LoggerInterface $logger, ?string $optional = 'test')
{
$logger->log('level', 'value');
dump($optional);
}
}

?>
-----
<?php

declare(strict_types=1);

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

use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;

final class InternalMethodCallArgRemoval extends AbstractController
{
public function __construct(private readonly \Psr\Log\LoggerInterface $logger)
{
}
#[Route('/some-action', name: 'some_action')]
public function someAction()
{
$this->problematic();
}

public function problematic(?string $optional = 'test')
{
$this->logger->log('level', 'value');
dump($optional);
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Exception;
use PhpParser\Node;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Name\FullyQualified;
Expand Down Expand Up @@ -137,6 +138,9 @@ public function refactor(Node $node): ?Node
/** @var array<string, string[]> $methodParamNamesToReplace */
$methodParamNamesToReplace = [];

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

foreach ($node->getMethods() as $classMethod) {
if ($this->shouldSkipClassMethod($classMethod)) {
continue;
Expand Down Expand Up @@ -223,6 +227,7 @@ public function refactor(Node $node): ?Node
$paramsToRemove[] = [$classMethod, $key];
$propertyMetadatas[$paramName] = new PropertyMetadata($paramName, $paramType);
$methodParamNamesToReplace[$classMethod->name->toString()][] = $paramName;
$removedMethodArgPositions[$classMethod->name->toString()][] = $key;
}
}

Expand Down Expand Up @@ -254,9 +259,57 @@ public function refactor(Node $node): ?Node
$this->replaceParamUseWithPropertyFetch($classMethod, $methodParamNamesToReplace[$methodName]);
}

$this->updateCallSitesForRemovedParams($node, $removedMethodArgPositions);

return $node;
}

/**
* @param array<string, int[]> $removedArgPositionsByMethod
*/
private function updateCallSitesForRemovedParams(Class_ $class, array $removedArgPositionsByMethod): void
{
if ($removedArgPositionsByMethod === []) {
return;
}

foreach ($class->getMethods() as $classMethod) {
if ($classMethod->stmts === null) {
continue;
}

$this->traverseNodesWithCallable($classMethod->stmts, function (Node $node) use (
$removedArgPositionsByMethod
): ?MethodCall {
if (! $node instanceof MethodCall) {
return null;
}

if (! $node->var instanceof Variable) {
return null;
}

if (! $this->isName($node->var, 'this')) {
return null;
}

$methodName = $this->getName($node->name);
if ($methodName === null || ! isset($removedArgPositionsByMethod[$methodName])) {
return null;
}

$removedPositions = $removedArgPositionsByMethod[$methodName];
rsort($removedPositions);
foreach ($removedPositions as $removedPosition) {
unset($node->args[$removedPosition]);
}

$node->args = array_values($node->args);
return $node;
});
}
}

private function shouldSkipClassMethod(ClassMethod $classMethod): bool
{
if ($classMethod->isMagic() && ! $this->isName($classMethod->name, MethodName::INVOKE)) {
Expand Down
Loading