diff --git a/composer.json b/composer.json index b3702ed7..7e1d90e1 100644 --- a/composer.json +++ b/composer.json @@ -10,6 +10,7 @@ "require-dev": { "doctrine/doctrine-bundle": "^2.14", "doctrine/orm": "^2.20", + "phpecs/phpecs": "^2.1", "phpstan/extension-installer": "^1.4", "phpstan/phpstan": "^2.1.14", "phpstan/phpstan-deprecation-rules": "^2.0", @@ -17,7 +18,7 @@ "phpunit/phpunit": "^11.5", "rector/rector-src": "dev-main", "rector/type-perfect": "^2.1", - "phpecs/phpecs": "^2.1", + "symplify/phpstan-extensions": "^12.0", "symplify/phpstan-rules": "^14.6.9", "symplify/vendor-patches": "^11.3", "tomasvotruba/class-leak": "^2.0", diff --git a/phpstan.neon b/phpstan.neon index 9078e5f6..8c19526f 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -4,6 +4,7 @@ includes: parameters: level: 8 + errorFormat: symplify treatPhpDocTypesAsCertain: false diff --git a/rules/Bundle210/Rector/Class_/EventSubscriberInterfaceToAttributeRector.php b/rules/Bundle210/Rector/Class_/EventSubscriberInterfaceToAttributeRector.php index b64686d2..7f942947 100644 --- a/rules/Bundle210/Rector/Class_/EventSubscriberInterfaceToAttributeRector.php +++ b/rules/Bundle210/Rector/Class_/EventSubscriberInterfaceToAttributeRector.php @@ -6,7 +6,6 @@ use PhpParser\Node; use PhpParser\Node\Arg; -use PhpParser\Node\ArrayItem; use PhpParser\Node\Attribute; use PhpParser\Node\AttributeGroup; use PhpParser\Node\Expr; @@ -18,7 +17,6 @@ use PhpParser\Node\Stmt\Return_; use PHPStan\Reflection\ReflectionProvider; use Rector\Doctrine\Enum\DoctrineClass; -use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\Rector\AbstractRector; use Rector\ValueObject\PhpVersionFeature; use Rector\VersionBonding\Contract\MinPhpVersionInterface; @@ -125,70 +123,77 @@ public function refactor(Node $node): ?Node return null; } - // $this->subscriberClass = $class; + foreach ($node->stmts as $key => $classStmt) { + if (! $classStmt instanceof ClassMethod) { + continue; + } - $getSubscribedEventsClassMethod = $node->getMethod('getSubscribedEvents'); - if (! $getSubscribedEventsClassMethod instanceof ClassMethod) { - return null; - } + if (! $this->isName($classStmt, 'getSubscribedEvents')) { + continue; + } - $stmts = (array) $getSubscribedEventsClassMethod->stmts; - if ($stmts === []) { - return null; - } + $getSubscribedEventsClassMethod = $classStmt; + if ($getSubscribedEventsClassMethod->stmts === []) { + continue; + } - if ( - ($stmts[0] instanceof Return_) && - $stmts[0]->expr instanceof Array_ - ) { - $this->handleArray($node, $stmts); - } + // $firstStmt = $getSubscribedEventsClassMethod->stmts[0]; + + // if ($firstStmt instanceof Return_ && $firstStmt->expr instanceof Array_ + // ) { + $this->refactorSubscriberArrayToClassAttributes($node, $getSubscribedEventsClassMethod); + // } + + $this->removeImplements( + $node, + [DoctrineClass::EVENT_SUBSCRIBER, DoctrineClass::EVENT_SUBSCRIBER_INTERFACE] + ); - $this->removeImplements($node, [DoctrineClass::EVENT_SUBSCRIBER, DoctrineClass::EVENT_SUBSCRIBER_INTERFACE]); - unset($node->stmts[$getSubscribedEventsClassMethod->getAttribute(AttributeKey::STMT_KEY)]); + // remove method + unset($node->stmts[$key]); - return $node; + return $node; + } + + return null; } - /** - * @param array $expressions - */ - private function handleArray(Class_ $class, array $expressions): void - { - foreach ($expressions as $expression) { - if (! $expression instanceof Return_ || - ! $expression->expr instanceof Array_ - ) { + private function refactorSubscriberArrayToClassAttributes( + Class_ $class, + ClassMethod $getSubscribedEventsClassMethod + ): void { + foreach ((array) $getSubscribedEventsClassMethod->stmts as $stmt) { + if (! $stmt instanceof Return_) { + continue; + } + + if (! $stmt->expr instanceof Array_) { continue; } - $arguments = $this->parseArguments($expression->expr); - $this->addAttribute($class, $arguments); + $arguments = $this->extractArrayItemsToExprs($stmt->expr); + $this->addClassAttribute($class, $arguments); } } /** * @return array */ - private function parseArguments(Array_ $array): array + private function extractArrayItemsToExprs(Array_ $array): array { - foreach ($array->items as $item) { - if ( - ! $item instanceof ArrayItem - ) { - continue; - } + $arguments = []; + foreach ($array->items as $item) { $arguments[] = $item->value; } - return $arguments ?? []; + return $arguments; } /** * @param array $arguments */ - private function addAttribute(Class_ $class, array $arguments): void + private function addClassAttribute(Class_ $class, array $arguments): void { foreach ($arguments as $argument) { $class->attrGroups[] = new AttributeGroup([new Attribute(