Skip to content
Merged
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
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@
"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",
"phpstan/phpstan-webmozart-assert": "^2.0",
"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",
Expand Down
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ includes:

parameters:
level: 8
errorFormat: symplify

treatPhpDocTypesAsCertain: false

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<int, Node\Stmt> $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<Expr>
*/
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<Expr> $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(
Expand Down