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
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Rector\Symfony\Tests\CodeQuality\Rector\Class_\EventSubscriberToAttributeRector\Fixture;

class DropDefaultPriorityEventSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
{
public function callMe()
{
}
/**
* @return array<string, mixed>
*/
public static function getSubscribedEvents(): array
{
return ['some_event' => ['callMe', 0]];
}
}

?>
-----
<?php

namespace Rector\Symfony\Tests\CodeQuality\Rector\Class_\EventSubscriberToAttributeRector\Fixture;

class DropDefaultPriorityEventSubscriber
{
#[\Symfony\Component\EventDispatcher\Attribute\AsEventListener(event: 'some_event')]
public function callMe()
{
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Scalar\Int_;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Class_;
Expand Down Expand Up @@ -237,7 +238,13 @@ private function resolveMethodsWithPriorities(Expr $expr): ?array
return null;
}

$methodsWithPriorities[] = [$methodName->value->value, $priority?->value];
// priority 0 is the default, drop it to keep the attribute clean
$priorityExpr = $priority?->value;
if ($priorityExpr instanceof Int_ && $priorityExpr->value === 0) {
$priorityExpr = null;
}

$methodsWithPriorities[] = [$methodName->value->value, $priorityExpr];
}

return $methodsWithPriorities;
Expand Down
Loading