[EventDispatcher] Add type-hints to EventDispatcherInterface.

This commit is contained in:
Alexander M. Turek 2019-06-10 22:45:58 +02:00
parent e8c2a1b807
commit 2bc94721b1
5 changed files with 29 additions and 36 deletions

View File

@ -53,7 +53,7 @@ class TraceableEventDispatcher implements EventDispatcherInterface, ResetInterfa
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function addListener($eventName, $listener, $priority = 0) public function addListener(string $eventName, $listener, int $priority = 0)
{ {
$this->dispatcher->addListener($eventName, $listener, $priority); $this->dispatcher->addListener($eventName, $listener, $priority);
} }
@ -69,7 +69,7 @@ class TraceableEventDispatcher implements EventDispatcherInterface, ResetInterfa
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function removeListener($eventName, $listener) public function removeListener(string $eventName, $listener)
{ {
if (isset($this->wrappedListeners[$eventName])) { if (isset($this->wrappedListeners[$eventName])) {
foreach ($this->wrappedListeners[$eventName] as $index => $wrappedListener) { foreach ($this->wrappedListeners[$eventName] as $index => $wrappedListener) {
@ -95,7 +95,7 @@ class TraceableEventDispatcher implements EventDispatcherInterface, ResetInterfa
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getListeners($eventName = null) public function getListeners(string $eventName = null)
{ {
return $this->dispatcher->getListeners($eventName); return $this->dispatcher->getListeners($eventName);
} }
@ -103,7 +103,7 @@ class TraceableEventDispatcher implements EventDispatcherInterface, ResetInterfa
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getListenerPriority($eventName, $listener) public function getListenerPriority(string $eventName, $listener)
{ {
// we might have wrapped listeners for the event (if called while dispatching) // we might have wrapped listeners for the event (if called while dispatching)
// in that case get the priority by wrapper // in that case get the priority by wrapper
@ -121,7 +121,7 @@ class TraceableEventDispatcher implements EventDispatcherInterface, ResetInterfa
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function hasListeners($eventName = null) public function hasListeners(string $eventName = null)
{ {
return $this->dispatcher->hasListeners($eventName); return $this->dispatcher->hasListeners($eventName);
} }
@ -272,7 +272,7 @@ class TraceableEventDispatcher implements EventDispatcherInterface, ResetInterfa
* *
* @return mixed * @return mixed
*/ */
public function __call($method, $arguments) public function __call(string $method, array $arguments)
{ {
return $this->dispatcher->{$method}(...$arguments); return $this->dispatcher->{$method}(...$arguments);
} }
@ -291,7 +291,7 @@ class TraceableEventDispatcher implements EventDispatcherInterface, ResetInterfa
{ {
} }
private function preProcess($eventName) private function preProcess(string $eventName): void
{ {
if (!$this->dispatcher->hasListeners($eventName)) { if (!$this->dispatcher->hasListeners($eventName)) {
$this->orphanedEvents[$this->currentRequestHash][] = $eventName; $this->orphanedEvents[$this->currentRequestHash][] = $eventName;
@ -309,7 +309,7 @@ class TraceableEventDispatcher implements EventDispatcherInterface, ResetInterfa
} }
} }
private function postProcess($eventName) private function postProcess(string $eventName): void
{ {
unset($this->wrappedListeners[$eventName]); unset($this->wrappedListeners[$eventName]);
$skipped = false; $skipped = false;

View File

@ -134,7 +134,7 @@ class ExtractingEventDispatcher extends EventDispatcher implements EventSubscrib
public static $aliases = []; public static $aliases = [];
public static $subscriber; public static $subscriber;
public function addListener($eventName, $listener, $priority = 0) public function addListener(string $eventName, $listener, int $priority = 0)
{ {
$this->listeners[] = [$eventName, $listener[1], $priority]; $this->listeners[] = [$eventName, $listener[1], $priority];
} }

View File

@ -70,7 +70,7 @@ class EventDispatcher implements EventDispatcherInterface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getListeners($eventName = null) public function getListeners(string $eventName = null)
{ {
if (null !== $eventName) { if (null !== $eventName) {
if (empty($this->listeners[$eventName])) { if (empty($this->listeners[$eventName])) {
@ -96,7 +96,7 @@ class EventDispatcher implements EventDispatcherInterface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getListenerPriority($eventName, $listener) public function getListenerPriority(string $eventName, $listener)
{ {
if (empty($this->listeners[$eventName])) { if (empty($this->listeners[$eventName])) {
return; return;
@ -121,7 +121,7 @@ class EventDispatcher implements EventDispatcherInterface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function hasListeners($eventName = null) public function hasListeners(string $eventName = null)
{ {
if (null !== $eventName) { if (null !== $eventName) {
return !empty($this->listeners[$eventName]); return !empty($this->listeners[$eventName]);
@ -139,7 +139,7 @@ class EventDispatcher implements EventDispatcherInterface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function addListener($eventName, $listener, $priority = 0) public function addListener(string $eventName, $listener, int $priority = 0)
{ {
$this->listeners[$eventName][$priority][] = $listener; $this->listeners[$eventName][$priority][] = $listener;
unset($this->sorted[$eventName], $this->optimized[$eventName]); unset($this->sorted[$eventName], $this->optimized[$eventName]);
@ -148,7 +148,7 @@ class EventDispatcher implements EventDispatcherInterface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function removeListener($eventName, $listener) public function removeListener(string $eventName, $listener)
{ {
if (empty($this->listeners[$eventName])) { if (empty($this->listeners[$eventName])) {
return; return;

View File

@ -30,12 +30,11 @@ interface EventDispatcherInterface extends ContractsEventDispatcherInterface
/** /**
* Adds an event listener that listens on the specified events. * Adds an event listener that listens on the specified events.
* *
* @param string $eventName The event to listen on * @param callable $listener The listener
* @param callable $listener The listener * @param int $priority The higher this value, the earlier an event
* @param int $priority The higher this value, the earlier an event * listener will be triggered in the chain (defaults to 0)
* listener will be triggered in the chain (defaults to 0)
*/ */
public function addListener($eventName, $listener, $priority = 0); public function addListener(string $eventName, $listener, int $priority = 0);
/** /**
* Adds an event subscriber. * Adds an event subscriber.
@ -48,40 +47,34 @@ interface EventDispatcherInterface extends ContractsEventDispatcherInterface
/** /**
* Removes an event listener from the specified events. * Removes an event listener from the specified events.
* *
* @param string $eventName The event to remove a listener from * @param callable $listener The listener to remove
* @param callable $listener The listener to remove
*/ */
public function removeListener($eventName, $listener); public function removeListener(string $eventName, $listener);
public function removeSubscriber(EventSubscriberInterface $subscriber); public function removeSubscriber(EventSubscriberInterface $subscriber);
/** /**
* Gets the listeners of a specific event or all listeners sorted by descending priority. * Gets the listeners of a specific event or all listeners sorted by descending priority.
* *
* @param string|null $eventName The name of the event
*
* @return array The event listeners for the specified event, or all event listeners by event name * @return array The event listeners for the specified event, or all event listeners by event name
*/ */
public function getListeners($eventName = null); public function getListeners(string $eventName = null);
/** /**
* Gets the listener priority for a specific event. * Gets the listener priority for a specific event.
* *
* Returns null if the event or the listener does not exist. * Returns null if the event or the listener does not exist.
* *
* @param string $eventName The name of the event * @param callable $listener The listener
* @param callable $listener The listener
* *
* @return int|null The event listener priority * @return int|null The event listener priority
*/ */
public function getListenerPriority($eventName, $listener); public function getListenerPriority(string $eventName, $listener);
/** /**
* Checks whether an event has any registered listeners. * Checks whether an event has any registered listeners.
* *
* @param string|null $eventName The name of the event
*
* @return bool true if the specified event has any listeners, false otherwise * @return bool true if the specified event has any listeners, false otherwise
*/ */
public function hasListeners($eventName = null); public function hasListeners(string $eventName = null);
} }

View File

@ -36,7 +36,7 @@ class ImmutableEventDispatcher implements EventDispatcherInterface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function addListener($eventName, $listener, $priority = 0) public function addListener(string $eventName, $listener, int $priority = 0)
{ {
throw new \BadMethodCallException('Unmodifiable event dispatchers must not be modified.'); throw new \BadMethodCallException('Unmodifiable event dispatchers must not be modified.');
} }
@ -52,7 +52,7 @@ class ImmutableEventDispatcher implements EventDispatcherInterface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function removeListener($eventName, $listener) public function removeListener(string $eventName, $listener)
{ {
throw new \BadMethodCallException('Unmodifiable event dispatchers must not be modified.'); throw new \BadMethodCallException('Unmodifiable event dispatchers must not be modified.');
} }
@ -68,7 +68,7 @@ class ImmutableEventDispatcher implements EventDispatcherInterface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getListeners($eventName = null) public function getListeners(string $eventName = null)
{ {
return $this->dispatcher->getListeners($eventName); return $this->dispatcher->getListeners($eventName);
} }
@ -76,7 +76,7 @@ class ImmutableEventDispatcher implements EventDispatcherInterface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getListenerPriority($eventName, $listener) public function getListenerPriority(string $eventName, $listener)
{ {
return $this->dispatcher->getListenerPriority($eventName, $listener); return $this->dispatcher->getListenerPriority($eventName, $listener);
} }
@ -84,7 +84,7 @@ class ImmutableEventDispatcher implements EventDispatcherInterface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function hasListeners($eventName = null) public function hasListeners(string $eventName = null)
{ {
return $this->dispatcher->hasListeners($eventName); return $this->dispatcher->hasListeners($eventName);
} }