|
11 | 11 | use PhpParser\Node\Scalar\String_; |
12 | 12 | use PhpParser\Node\Stmt\Class_; |
13 | 13 | use PhpParser\Node\Stmt\ClassMethod; |
| 14 | +use PHPStan\Reflection\ReflectionProvider; |
14 | 15 | use PHPStan\Type\ObjectType; |
15 | 16 | use Rector\Rector\AbstractRector; |
16 | 17 | use Rector\Symfony\Configs\NodeFactory\AutowiredParamFactory; |
| 18 | +use Rector\Symfony\Enum\SymfonyAttribute; |
| 19 | +use Rector\Symfony\Enum\SymfonyClass; |
17 | 20 | use Rector\ValueObject\MethodName; |
18 | 21 | use Rector\ValueObject\PhpVersionFeature; |
19 | 22 | use Rector\VersionBonding\Contract\MinPhpVersionInterface; |
|
28 | 31 | */ |
29 | 32 | final class ParameterBagToAutowireAttributeRector extends AbstractRector implements MinPhpVersionInterface |
30 | 33 | { |
31 | | - /** |
32 | | - * @var string |
33 | | - */ |
34 | | - private const PARAMETER_BAG_CLASS = 'Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface'; |
35 | | - |
36 | 34 | public function __construct( |
37 | | - private readonly AutowiredParamFactory $autowiredParamFactory |
| 35 | + private readonly AutowiredParamFactory $autowiredParamFactory, |
| 36 | + private readonly ReflectionProvider $reflectionProvider |
38 | 37 | ) { |
39 | 38 | } |
40 | 39 |
|
@@ -85,6 +84,10 @@ public function getNodeTypes(): array |
85 | 84 | */ |
86 | 85 | public function refactor(Node $node): ?Class_ |
87 | 86 | { |
| 87 | + if (! $this->reflectionProvider->hasClass(SymfonyAttribute::AUTOWIRE)) { |
| 88 | + return null; |
| 89 | + } |
| 90 | + |
88 | 91 | if ($node->isAnonymous()) { |
89 | 92 | return null; |
90 | 93 | } |
@@ -112,7 +115,7 @@ public function refactor(Node $node): ?Class_ |
112 | 115 | return null; |
113 | 116 | } |
114 | 117 |
|
115 | | - if (! $this->isObjectType($node->var, new ObjectType(self::PARAMETER_BAG_CLASS))) { |
| 118 | + if (! $this->isObjectType($node->var, new ObjectType(SymfonyClass::PARAMETER_BAG_INTERFACE))) { |
116 | 119 | return null; |
117 | 120 | } |
118 | 121 |
|
@@ -191,6 +194,6 @@ private function isParameterBagParam(Param $param): bool |
191 | 194 | return false; |
192 | 195 | } |
193 | 196 |
|
194 | | - return $this->isName($param->type, self::PARAMETER_BAG_CLASS); |
| 197 | + return $this->isName($param->type, SymfonyClass::PARAMETER_BAG_INTERFACE); |
195 | 198 | } |
196 | 199 | } |
0 commit comments