Skip to content

Commit 7c2ee8a

Browse files
committed
skip constructor in ControllerMethodInjectionToConstructorRector
1 parent 0d0cd6c commit 7c2ee8a

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

rules-tests/CodeQuality/Rector/Class_/ControllerMethodInjectionToConstructorRector/Fixture/re_use_existing_service.php.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ use Symfony\Component\Routing\Annotation\Route;
3737

3838
final class ReUseExistingService extends AbstractController
3939
{
40-
public function __construct(private readonly \Psr\Log\LoggerInterface $logger)
40+
public function __construct(private LoggerInterface $logger)
4141
{
4242
}
4343

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Symfony\Tests\CodeQuality\Rector\Class_\ControllerMethodInjectionToConstructorRector\Fixture;
6+
7+
use Psr\Log\LoggerInterface;
8+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
9+
use Symfony\Component\Routing\Annotation\Route;
10+
11+
final class SkipConstructor extends AbstractController
12+
{
13+
private LoggerInterface $logger;
14+
15+
public function __construct(LoggerInterface $logger)
16+
{
17+
$this->logger = $logger;
18+
}
19+
20+
#[Route('/some-action', name: 'some_action')]
21+
public function someAction(
22+
\Symfony\Component\HttpFoundation\Request $request,
23+
) {
24+
$this->logger->log('level', 'value');
25+
}
26+
}

rules/CodeQuality/Rector/Class_/ControllerMethodInjectionToConstructorRector.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Rector\Symfony\Bridge\NodeAnalyzer\ControllerMethodAnalyzer;
1818
use Rector\Symfony\Enum\SymfonyClass;
1919
use Rector\Symfony\TypeAnalyzer\ControllerAnalyzer;
20+
use Rector\ValueObject\MethodName;
2021
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
2122
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
2223

@@ -101,6 +102,10 @@ public function refactor(Node $node): ?Node
101102
$propertyMetadatas = [];
102103

103104
foreach ($node->getMethods() as $classMethod) {
105+
if ($classMethod->isMagic() && ! $this->isName($classMethod->name, MethodName::INVOKE)) {
106+
continue;
107+
}
108+
104109
if (! $this->controllerMethodAnalyzer->isAction($classMethod)) {
105110
continue;
106111
}

0 commit comments

Comments
 (0)