Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve static analysis on AttachEntityListenersListener #11272

Merged
merged 1 commit into from
Feb 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 15 additions & 6 deletions src/Tools/AttachEntityListenersListener.php
Expand Up @@ -5,25 +5,33 @@
namespace Doctrine\ORM\Tools;

use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
use Doctrine\ORM\Events;
use Doctrine\ORM\Mapping\Builder\EntityListenerBuilder;

use function assert;
use function ltrim;

/**
* Mechanism to programmatically attach entity listeners.
*/
class AttachEntityListenersListener
{
/** @var mixed[][] */
/**
* @var array<class-string, list<array{
* event: Events::*|null,
* class: class-string,
* method: string|null,
* }>>
*/
private array $entityListeners = [];

/**
* Adds an entity listener for a specific entity.
*
* @param string $entityClass The entity to attach the listener.
* @param string $listenerClass The listener class.
* @param string|null $eventName The entity lifecycle event.
* @param string|null $listenerCallback The listener callback method or NULL to use $eventName.
* @param class-string $entityClass The entity to attach the listener.
* @param class-string $listenerClass The listener class.
* @param Events::*|null $eventName The entity lifecycle event.
* @param non-falsy-string|null $listenerCallback The listener callback method or NULL to use $eventName.
*/
public function addEntityListener(
Copy link
Member Author

@greg0ire greg0ire Feb 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: it's unclear to me who/what calls this method. The class is not referenced at all in src, but it is referenced in the Symfony bundle. I suspect the method might only be there for test purposes. That class was introduced in #850

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possible. I remember when entitylisteners could only be configured with annotations in the entity class itself and there was no way to inject dependencies into the listener's constructor.

string $entityClass,
Expand All @@ -34,7 +42,7 @@ public function addEntityListener(
$this->entityListeners[ltrim($entityClass, '\\')][] = [
'event' => $eventName,
'class' => $listenerClass,
'method' => $listenerCallback ?: $eventName,
'method' => $listenerCallback ?? $eventName,
];
}

Expand All @@ -53,6 +61,7 @@ public function loadClassMetadata(LoadClassMetadataEventArgs $event): void
if ($listener['event'] === null) {
EntityListenerBuilder::bindEntityListener($metadata, $listener['class']);
} else {
assert($listener['method'] !== null);
$metadata->addEntityListener($listener['event'], $listener['class'], $listener['method']);
}
}
Expand Down