rectorphp/rector-doctrine

EventSubscriberInterfaceToAttributeRector does not take into account EventSubscriberInterface

Closed this issue · 3 comments

Rule definition: Replace EventSubscriberInterface with AsDoctrineListener attribute(s)

but only check for

if (!$this->hasImplements($node, 'Doctrine\\Common\\EventSubscriber')) {
            return null;
        }

while it could be EventSubscriberInterface

<?php

declare(strict_types=1);

namespace Atta\FailedMessagesBundle\EventSubscriber\Doctrine;

use Atta\FailedMessagesBundle\Behavior\UserOperationCreatableInterface;
use Atta\FailedMessagesBundle\Entity\UserOperation;
use Atta\FailedMessagesBundle\Service\OperationIdentifier;
use Atta\FailedMessagesBundle\Service\UserOperationManager;
use Doctrine\Bundle\DoctrineBundle\EventSubscriber\EventSubscriberInterface;
use Doctrine\ORM\Event\OnFlushEventArgs;
use Doctrine\ORM\Events;

class UserOperationCreatable implements EventSubscriberInterface
{
    public function __construct(
        private readonly OperationIdentifier $operationIdentifier,
        private readonly UserOperationManager $userOperationManager,
    ) {
    }

    public function getSubscribedEvents(): array
    {
        return [
            Events::onFlush,
        ];
    }

    public function onFlush(OnFlushEventArgs $args): void
    {
        $em = $args->getEntityManager();
        $uow = $em->getUnitOfWork();

        foreach ($uow->getScheduledEntityInsertions() as $entity) {
            if ($entity instanceof UserOperationCreatableInterface) {
                $operationId = $this->operationIdentifier->getForObject($entity, true);
                $userOperation = $this->userOperationManager->create($entity, $operationId);
                $uow->computeChangeSet($em->getClassMetadata(UserOperation::class), $userOperation);
            }
        }
    }
}

rector.php

<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Doctrine\Set\DoctrineSetList;
use Rector\TypeDeclaration\Rector\ClassMethod\AddVoidReturnTypeWhereNoReturnRector;

return RectorConfig::configure()
    ->withPaths([
        __DIR__ . '/src',
        __DIR__ . '/tests',
    ])
    ->withPhpSets()
    ->withSets([
//        DoctrineSetList::DOCTRINE_CODE_QUALITY,
        DoctrineSetList::DOCTRINE_BUNDLE_210,
//        DoctrineSetList::DOCTRINE_DBAL_40,
//        DoctrineSetList::DOCTRINE_ORM_214,
//        DoctrineSetList::ANNOTATIONS_TO_ATTRIBUTES,
//        DoctrineSetList::GEDMO_ANNOTATIONS_TO_ATTRIBUTES,
    ])
    ->withRules([
        AddVoidReturnTypeWhereNoReturnRector::class,
    ])
;

Hi, thanks for reporting, you're right.

Could you add PR with a fix?