rectorphp/rector-doctrine

Add rule to update string events with constants

stefantalen opened this issue ยท 5 comments

Lets add a rule to replace event strings with the corresponding class constants (Just like the Criteria constants)

<?php

declare(strict_types=1);

namespace App\Subscriber\Doctrine;

use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\Event\PreUpdateEventArgs;
+use Doctrine\ORM\Events;

class DoctrineSubscriber implements EventSubscriber
{

    public function getSubscribedEvents(): array
    {
        return [   
-            'prePersist',
-            'preUpdate',
+            Events::prePersist,
+            Events::preUpdate,
        ];
    }

    public function prePersist(LifecycleEventArgs $args) {
        //...
    }
    public function preUpdate(PreUpdateEventArgs $eventArgs) {
        //...
    }
}

I'll see if I can implement this in the future

This might help: https://github.com/rectorphp/rector/blob/main/docs/rector_rules_overview.md#replacestringwithclassconstantrector

I just started playing around with this but I won't be able to use that rule since I'm not updating the arguments of getSubscribedEvents().

@TomasVotruba I tried using the StringToClassConstantRector, which works but I think the rule is too general. Is there some way to scope it to certain interfaces only?

@stefantalen That would require a standalone rule based on this one, as the context of global vs interface-based is a different one.

Closing for lack of feedback, to keep issue tracker focus on unclear issues that needs our attention.

Feel free to send PR with the rule when you find time ๐Ÿ‘

Thank you for understanding ๐Ÿ™‚