Skip to content

Commit fee9474

Browse files
authored
[CodeQuality] Skip Common Entity contains subnamespaces on ControllerMethodInjectionToConstructorRector (#878)
* [CodeQuality] Skip Common Entity contains subnamespaces on ControllerMethodInjectionToConstructorRector * [CodeQuality] Skip Common Entity contains subnamespaces on ControllerMethodInjectionToConstructorRector * [CodeQuality] Skip Common Entity contains subnamespaces on ControllerMethodInjectionToConstructorRector
1 parent 27d45a8 commit fee9474

2 files changed

Lines changed: 37 additions & 0 deletions

File tree

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 Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
8+
use Symfony\Component\HttpFoundation\Request;
9+
10+
/**
11+
* Entity or Document can be detected via ValueResolver that query in ValueResolver services
12+
*/
13+
final class SkipNamespacedDocumentOrEntity extends AbstractController
14+
{
15+
public function someAction(Request $request, \App\Entity\SomeEntity $entity)
16+
{
17+
}
18+
19+
public function some2Action(Request $request, \App\Document\SomeEntity $document)
20+
{
21+
}
22+
23+
public function some3Action(Request $request, \App\Model\SomeEntity $document)
24+
{
25+
}
26+
}

rules/CodeQuality/Rector/Class_/ControllerMethodInjectionToConstructorRector.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@
3131
*/
3232
final class ControllerMethodInjectionToConstructorRector extends AbstractRector
3333
{
34+
/**
35+
* @var string[]
36+
*/
37+
private const COMMON_ENTITY_CONTAINS_SUBNAMESPACES = ['\\Entity', '\\Document', '\\Model'];
38+
3439
public function __construct(
3540
private readonly ControllerAnalyzer $controllerAnalyzer,
3641
private readonly ControllerMethodAnalyzer $controllerMethodAnalyzer,
@@ -130,6 +135,12 @@ public function refactor(Node $node): ?Node
130135
continue;
131136
}
132137

138+
foreach (self::COMMON_ENTITY_CONTAINS_SUBNAMESPACES as $commonEntityContainsNamespace) {
139+
if (str_contains($this->getName($param->type), $commonEntityContainsNamespace)) {
140+
continue 2;
141+
}
142+
}
143+
133144
// @todo allow parameter converter
134145
unset($classMethod->params[$key]);
135146

0 commit comments

Comments
 (0)