Skip to content

Commit 40e67e0

Browse files
authored
[CodeQuality] Do not replace variable assign on ControllerMethodInjectionToConstructorRector (#883)
* [CodeQuality] Do not replace variable assign on ControllerMethodInjectionToConstructorRector * fix
1 parent c301b38 commit 40e67e0

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
final class DoNotReplaceVariableAssign extends AbstractController
10+
{
11+
public function someAction(\Psr\Log\LoggerInterface $logger)
12+
{
13+
$logger = $this->modify($logger);
14+
}
15+
}
16+
17+
?>
18+
-----
19+
<?php
20+
21+
declare(strict_types=1);
22+
23+
namespace Rector\Symfony\Tests\CodeQuality\Rector\Class_\ControllerMethodInjectionToConstructorRector\Fixture;
24+
25+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
26+
27+
final class DoNotReplaceVariableAssign extends AbstractController
28+
{
29+
public function __construct(private readonly \Psr\Log\LoggerInterface $logger)
30+
{
31+
}
32+
public function someAction()
33+
{
34+
$logger = $this->modify($this->logger);
35+
}
36+
}
37+
38+
?>

rules/CodeQuality/Rector/Class_/ControllerMethodInjectionToConstructorRector.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use PhpParser\Node\Stmt\ClassMethod;
1515
use Rector\Doctrine\NodeAnalyzer\AttributeFinder;
1616
use Rector\NodeManipulator\ClassDependencyManipulator;
17+
use Rector\NodeTypeResolver\Node\AttributeKey;
1718
use Rector\PhpParser\Node\Value\ValueResolver;
1819
use Rector\PostRector\ValueObject\PropertyMetadata;
1920
use Rector\Rector\AbstractRector;
@@ -190,7 +191,10 @@ public function refactor(Node $node): ?Node
190191

191192
if (! $this->isNames($node, $paramNamesToReplace)) {
192193
return null;
194+
}
193195

196+
if ($node->getAttribute(AttributeKey::IS_BEING_ASSIGNED) === true) {
197+
return null;
194198
}
195199

196200
$propertyName = $this->getName($node);

0 commit comments

Comments
 (0)