Skip to content
Open
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
Expand Up @@ -13,6 +13,7 @@
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Type\ObjectType;
use Rector\Contract\Rector\ConfigurableRectorInterface;
use Rector\NodeManipulator\ClassDependencyManipulator;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PostRector\ValueObject\PropertyMetadata;
Expand All @@ -32,13 +33,18 @@
/**
* @see \Rector\Symfony\Tests\CodeQuality\Rector\Class_\ControllerMethodInjectionToConstructorRector\ControllerMethodInjectionToConstructorRectorTest
*/
final class ControllerMethodInjectionToConstructorRector extends AbstractRector
final class ControllerMethodInjectionToConstructorRector extends AbstractRector implements ConfigurableRectorInterface
{
/**
* @var string[]
*/
private const COMMON_ENTITY_CONTAINS_SUBNAMESPACES = ["\\Entity", "\\Document", "\\Model"];

/**
* @var string[]
*/
private array $skipAllowedKnownObjects = [];

public function __construct(
private readonly ControllerAnalyzer $controllerAnalyzer,
private readonly ControllerMethodAnalyzer $controllerMethodAnalyzer,
Expand Down Expand Up @@ -159,6 +165,7 @@ public function refactor(Node $node): ?Node
Throwable::class,
Exception::class,
...$entityClasses,
...$this->skipAllowedKnownObjects,
]
)) {
continue;
Expand Down Expand Up @@ -275,4 +282,9 @@ private function replaceParamUseWithPropertyFetch(ClassMethod $classMethod, arra
return new PropertyFetch(new Variable('this'), $propertyName);
});
}

public function configure(array $configuration): void
{
$this->skipAllowedKnownObjects = $configuration;
}
}
4 changes: 4 additions & 0 deletions src/Enum/SymfonyAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,8 @@ final class SymfonyAttribute
* @var string
*/
public const REQUIRED = 'Symfony\Contracts\Service\Attribute\Required';
/**
* @var string
*/
public const AS_CONTROLLER = 'Symfony\Component\HttpKernel\Attribute\AsController';
}
12 changes: 9 additions & 3 deletions src/TypeAnalyzer/ControllerAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@
namespace Rector\Symfony\TypeAnalyzer;

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Stmt\Class_;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Type\ObjectType;
use PHPStan\Type\ThisType;
use PHPStan\Type\TypeWithClassName;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Php80\NodeAnalyzer\PhpAttributeAnalyzer;
use Rector\Reflection\ReflectionResolver;
use Rector\Symfony\Enum\SymfonyAttribute;
use Rector\Symfony\Enum\SymfonyClass;

final readonly class ControllerAnalyzer
final class ControllerAnalyzer
{
public function __construct(
private ReflectionResolver $reflectionResolver,
Expand Down Expand Up @@ -73,6 +74,12 @@ private function isControllerClassReflection(ClassReflection $classReflection):
return true;
}

foreach ($classReflection->getAttributes() as $attribute) {
if ($attribute->getName() === SymfonyAttribute::AS_CONTROLLER) {
return true;
}
}

return $classReflection->is(SymfonyClass::ABSTRACT_CONTROLLER);
}

Expand All @@ -82,7 +89,6 @@ private function isControllerClass(Class_ $class): bool
if (! $classReflection instanceof ClassReflection) {
return false;
}

return $this->isControllerClassReflection($classReflection);
}
}
Loading