Skip to content

Commit

Permalink
Add types to EntityListenerResolver (doctrine#9976)
Browse files Browse the repository at this point in the history
  • Loading branch information
derrabus authored Aug 8, 2022
1 parent 7abeb9b commit 41bca04
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 71 deletions.
39 changes: 18 additions & 21 deletions lib/Doctrine/ORM/Event/ListenersInvoker.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,16 @@
*/
class ListenersInvoker
{
public const INVOKE_NONE = 0;
public const INVOKE_LISTENERS = 1;
public const INVOKE_CALLBACKS = 2;
public const INVOKE_MANAGER = 4;
final public const INVOKE_NONE = 0;
final public const INVOKE_LISTENERS = 1;
final public const INVOKE_CALLBACKS = 2;
final public const INVOKE_MANAGER = 4;

/** @var EntityListenerResolver The Entity listener resolver. */
private $resolver;
/** The Entity listener resolver. */
private readonly EntityListenerResolver $resolver;

/**
* The EventManager used for dispatching events.
*
* @var EventManager
*/
private $eventManager;
/** The EventManager used for dispatching events. */
private readonly EventManager $eventManager;

public function __construct(EntityManagerInterface $em)
{
Expand All @@ -42,10 +38,9 @@ public function __construct(EntityManagerInterface $em)
* @param ClassMetadata $metadata The entity metadata.
* @param string $eventName The entity lifecycle event.
*
* @return int Bitmask of subscribed event systems.
* @psalm-return int-mask-of<self::INVOKE_*>
* @psalm-return int-mask-of<self::INVOKE_*> Bitmask of subscribed event systems.
*/
public function getSubscribedSystems(ClassMetadata $metadata, $eventName)
public function getSubscribedSystems(ClassMetadata $metadata, string $eventName): int
{
$invoke = self::INVOKE_NONE;

Expand All @@ -71,13 +66,15 @@ public function getSubscribedSystems(ClassMetadata $metadata, $eventName)
* @param string $eventName The entity lifecycle event.
* @param object $entity The Entity on which the event occurred.
* @param EventArgs $event The Event args.
* @param int $invoke Bitmask to invoke listeners.
* @psalm-param int-mask-of<self::INVOKE_*> $invoke
*
* @return void
* @psalm-param int-mask-of<self::INVOKE_*> $invoke Bitmask to invoke listeners.
*/
public function invoke(ClassMetadata $metadata, $eventName, $entity, EventArgs $event, $invoke)
{
public function invoke(
ClassMetadata $metadata,
string $eventName,
object $entity,
EventArgs $event,
int $invoke
): void {
if ($invoke & self::INVOKE_CALLBACKS) {
foreach ($metadata->lifecycleCallbacks[$eventName] as $callback) {
$entity->$callback($event);
Expand Down
35 changes: 6 additions & 29 deletions lib/Doctrine/ORM/Mapping/DefaultEntityListenerResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@

namespace Doctrine\ORM\Mapping;

use InvalidArgumentException;

use function get_class;
use function gettype;
use function is_object;
use function sprintf;
use function trim;

/**
Expand All @@ -18,12 +13,9 @@
class DefaultEntityListenerResolver implements EntityListenerResolver
{
/** @psalm-var array<class-string, object> Map to store entity listener instances. */
private $instances = [];
private array $instances = [];

/**
* {@inheritdoc}
*/
public function clear($className = null)
public function clear(?string $className = null): void
{
if ($className === null) {
$this->instances = [];
Expand All @@ -32,33 +24,18 @@ public function clear($className = null)
}

$className = trim($className, '\\');
if (isset($this->instances[$className])) {
unset($this->instances[$className]);
}
unset($this->instances[$className]);
}

/**
* {@inheritdoc}
*/
public function register($object)
public function register(object $object): void
{
if (! is_object($object)) {
throw new InvalidArgumentException(sprintf('An object was expected, but got "%s".', gettype($object)));
}

$this->instances[get_class($object)] = $object;
}

/**
* {@inheritdoc}
*/
public function resolve($className)
public function resolve(string $className): object
{
$className = trim($className, '\\');
if (isset($this->instances[$className])) {
return $this->instances[$className];
}

return $this->instances[$className] = new $className();
return $this->instances[$className] ??= new $className();
}
}
14 changes: 3 additions & 11 deletions lib/Doctrine/ORM/Mapping/EntityListenerResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,18 @@ interface EntityListenerResolver
* Clear all instances from the set, or a specific instance when given its identifier.
*
* @param string|null $className May be any arbitrary string. Name kept for BC only.
*
* @return void
*/
public function clear($className = null);
public function clear(?string $className = null): void;

/**
* Returns a entity listener instance for the given identifier.
*
* @param string $className May be any arbitrary string. Name kept for BC only.
*
* @return object An entity listener
*/
public function resolve($className);
public function resolve(string $className): object;

/**
* Register a entity listener instance.
*
* @param object $object An entity listener
*
* @return void
*/
public function register($object);
public function register(object $object): void;
}
3 changes: 0 additions & 3 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -503,9 +503,6 @@
</MissingConstructor>
</file>
<file src="lib/Doctrine/ORM/Mapping/DefaultEntityListenerResolver.php">
<DocblockTypeContradiction occurrences="1">
<code>is_object($object)</code>
</DocblockTypeContradiction>
<InvalidStringClass occurrences="1">
<code>new $className()</code>
</InvalidStringClass>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,4 @@ public function testClearAll(): void
self::assertNotSame($obj1, $this->resolver->resolve($className1));
self::assertNotSame($obj2, $this->resolver->resolve($className2));
}

public function testRegisterStringException(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('An object was expected, but got "string".');
$this->resolver->register('CompanyContractListener');
}
}

0 comments on commit 41bca04

Please sign in to comment.