Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[EventDispatcher] Add type-hints to EventDispatcherInterface #31984

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

Merged
merged 1 commit into from
Jun 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[EventDispatcher] Add type-hints to EventDispatcherInterface.
  • Loading branch information
derrabus committed Jun 10, 2019
commit 2bc94721b1cb844e62ccf3ffd3f7d7c3e3ee92a1
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function __construct(EventDispatcherInterface $dispatcher, Stopwatch $sto
/**
* {@inheritdoc}
*/
public function addListener($eventName, $listener, $priority = 0)
public function addListener(string $eventName, $listener, int $priority = 0)
{
$this->dispatcher->addListener($eventName, $listener, $priority);
}
Expand All @@ -69,7 +69,7 @@ public function addSubscriber(EventSubscriberInterface $subscriber)
/**
* {@inheritdoc}
*/
public function removeListener($eventName, $listener)
public function removeListener(string $eventName, $listener)
{
if (isset($this->wrappedListeners[$eventName])) {
foreach ($this->wrappedListeners[$eventName] as $index => $wrappedListener) {
Expand All @@ -95,15 +95,15 @@ public function removeSubscriber(EventSubscriberInterface $subscriber)
/**
* {@inheritdoc}
*/
public function getListeners($eventName = null)
public function getListeners(string $eventName = null)
{
return $this->dispatcher->getListeners($eventName);
}

/**
* {@inheritdoc}
*/
public function getListenerPriority($eventName, $listener)
public function getListenerPriority(string $eventName, $listener)
{
// we might have wrapped listeners for the event (if called while dispatching)
// in that case get the priority by wrapper
Expand All @@ -121,7 +121,7 @@ public function getListenerPriority($eventName, $listener)
/**
* {@inheritdoc}
*/
public function hasListeners($eventName = null)
public function hasListeners(string $eventName = null)
{
return $this->dispatcher->hasListeners($eventName);
}
Expand Down Expand Up @@ -272,7 +272,7 @@ public function reset()
*
* @return mixed
*/
public function __call($method, $arguments)
public function __call(string $method, array $arguments)
{
return $this->dispatcher->{$method}(...$arguments);
}
Expand All @@ -291,7 +291,7 @@ protected function afterDispatch(string $eventName, object $event)
{
}

private function preProcess($eventName)
private function preProcess(string $eventName): void
{
if (!$this->dispatcher->hasListeners($eventName)) {
$this->orphanedEvents[$this->currentRequestHash][] = $eventName;
Expand All @@ -309,7 +309,7 @@ private function preProcess($eventName)
}
}

private function postProcess($eventName)
private function postProcess(string $eventName): void
{
unset($this->wrappedListeners[$eventName]);
$skipped = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ class ExtractingEventDispatcher extends EventDispatcher implements EventSubscrib
public static $aliases = [];
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];
}
Expand Down
10 changes: 5 additions & 5 deletions 10 src/Symfony/Component/EventDispatcher/EventDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function dispatch($event, string $eventName = null): object
/**
* {@inheritdoc}
*/
public function getListeners($eventName = null)
public function getListeners(string $eventName = null)
{
if (null !== $eventName) {
if (empty($this->listeners[$eventName])) {
Expand All @@ -96,7 +96,7 @@ public function getListeners($eventName = null)
/**
* {@inheritdoc}
*/
public function getListenerPriority($eventName, $listener)
public function getListenerPriority(string $eventName, $listener)
{
if (empty($this->listeners[$eventName])) {
return;
Expand All @@ -121,7 +121,7 @@ public function getListenerPriority($eventName, $listener)
/**
* {@inheritdoc}
*/
public function hasListeners($eventName = null)
public function hasListeners(string $eventName = null)
{
if (null !== $eventName) {
return !empty($this->listeners[$eventName]);
Expand All @@ -139,7 +139,7 @@ public function hasListeners($eventName = null)
/**
* {@inheritdoc}
*/
public function addListener($eventName, $listener, $priority = 0)
public function addListener(string $eventName, $listener, int $priority = 0)
Copy link
Member Author

Choose a reason for hiding this comment

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

Note: I intentionally did not add callable $listener because this would cause a call like…

$dispatcher->addListener('some_event', ['Some\Class', 'someMethod']);

… to trigger autoloading for the class name that is passed here. This might have performance implications if we're adding a lot of static listeners.

{
$this->listeners[$eventName][$priority][] = $listener;
unset($this->sorted[$eventName], $this->optimized[$eventName]);
Expand All @@ -148,7 +148,7 @@ public function addListener($eventName, $listener, $priority = 0)
/**
* {@inheritdoc}
*/
public function removeListener($eventName, $listener)
public function removeListener(string $eventName, $listener)
{
if (empty($this->listeners[$eventName])) {
return;
Expand Down
27 changes: 10 additions & 17 deletions 27 src/Symfony/Component/EventDispatcher/EventDispatcherInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,11 @@ public function dispatch($event, string $eventName = null): object;
/**
* Adds an event listener that listens on the specified events.
*
* @param string $eventName The event to listen on
* @param callable $listener The listener
* @param int $priority The higher this value, the earlier an event
* listener will be triggered in the chain (defaults to 0)
* @param callable $listener The listener
* @param int $priority The higher this value, the earlier an event
* 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.
Expand All @@ -48,40 +47,34 @@ public function addSubscriber(EventSubscriberInterface $subscriber);
/**
* 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);

/**
* 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
*/
public function getListeners($eventName = null);
public function getListeners(string $eventName = null);

/**
* Gets the listener priority for a specific event.
*
* 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
*/
public function getListenerPriority($eventName, $listener);
public function getListenerPriority(string $eventName, $listener);

/**
* 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
*/
public function hasListeners($eventName = null);
public function hasListeners(string $eventName = null);
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function dispatch($event, string $eventName = null): object
/**
* {@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.');
}
Expand All @@ -52,7 +52,7 @@ public function addSubscriber(EventSubscriberInterface $subscriber)
/**
* {@inheritdoc}
*/
public function removeListener($eventName, $listener)
public function removeListener(string $eventName, $listener)
{
throw new \BadMethodCallException('Unmodifiable event dispatchers must not be modified.');
}
Expand All @@ -68,23 +68,23 @@ public function removeSubscriber(EventSubscriberInterface $subscriber)
/**
* {@inheritdoc}
*/
public function getListeners($eventName = null)
public function getListeners(string $eventName = null)
{
return $this->dispatcher->getListeners($eventName);
}

/**
* {@inheritdoc}
*/
public function getListenerPriority($eventName, $listener)
public function getListenerPriority(string $eventName, $listener)
{
return $this->dispatcher->getListenerPriority($eventName, $listener);
}

/**
* {@inheritdoc}
*/
public function hasListeners($eventName = null)
public function hasListeners(string $eventName = null)
{
return $this->dispatcher->hasListeners($eventName);
}
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.