8000 [EventDispatcher] Add return type declarations by derrabus · Pull Request #33248 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[EventDispatcher] Add return type declarations #33248

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

Closed
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
*/
class MergeDoctrineCollectionListener implements EventSubscriberInterface
{
public static function getSubscribedEvents()
public static function getSubscribedEvents(): array
{
// Higher priority than core MergeCollectionListener so that this one
// is called before
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Bridge/Monolog/Handler/ConsoleHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public function onTerminate(ConsoleTerminateEvent $event)
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents()
public static function getSubscribedEvents(): array
{
return [
ConsoleEvents::COMMAND => ['onCommand', 255],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function addCommandData(ConsoleEvent $event)
}
}

public static function getSubscribedEvents()
public static function getSubscribedEvents(): array
{
return [
ConsoleEvents::COMMAND => ['addCommandData', 1],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function onKernelFinishRequest(FinishRequestEvent $event)
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents()
public static function getSubscribedEvents(): array
{
return [
KernelEvents::REQUEST => [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function onConsoleTerminate(ConsoleTerminateEvent $event)
$this->logger->debug('Command "{command}" exited with code "{code}"', ['command' => $inputString, 'code' => $exitCode]);
}

public static function getSubscribedEvents()
public static function getSubscribedEvents(): array
{
return [
ConsoleEvents::ERROR => ['onConsoleError', -128],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,23 +53,23 @@ public function __construct(EventDispatcherInterface $dispatcher, Stopwatch $sto
/**
* {@inheritdoc}
*/
public function addListener(string $eventName, $listener, int $priority = 0)
public function addListener(string $eventName, $listener, int $priority = 0): void
{
$this->dispatcher->addListener($eventName, $listener, $priority);
}

/**
* {@inheritdoc}
*/
public function addSubscriber(EventSubscriberInterface $subscriber)
public function addSubscriber(EventSubscriberInterface $subscriber): void
{
$this->dispatcher->addSubscriber($subscriber);
}

/**
* {@inheritdoc}
*/
public function removeListener(string $eventName, $listener)
public function removeListener(string $eventName, $listener): void
{
if (isset($this->wrappedListeners[$eventName])) {
foreach ($this->wrappedListeners[$eventName] as $index => $wrappedListener) {
Expand All @@ -81,29 +81,29 @@ public function removeListener(string $eventName, $listener)
}
}

return $this->dispatcher->removeListener($eventName, $listener);
$this->dispatcher->removeListener($eventName, $listener);
}

/**
* {@inheritdoc}
*/
public function removeSubscriber(EventSubscriberInterface $subscriber)
public function removeSubscriber(EventSubscriberInterface $subscriber): void
{
return $this->dispatcher->removeSubscriber($subscriber);
$this->dispatcher->removeSubscriber($subscriber);
}

/**
* {@inheritdoc}
*/
public function getListeners(string $eventName = null)
public function getListeners(string $eventName = null): array
{
return $this->dispatcher->getListeners($eventName);
}

/**
* {@inheritdoc}
*/
public function getListenerPriority(string $eventName, $listener)
public function getListenerPriority(string $eventName, $listener): ?int
{
// 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(string $eventName, $listener)
/**
* {@inheritdoc}
*/
public function hasListeners(string $eventName = null)
public function hasListeners(string $eventName = null): bool
{
return $this->dispatcher->hasListeners($eventName);
}
Expand Down Expand Up @@ -170,10 +170,7 @@ public function dispatch($event, string $eventName = null): object
return $event;
}

/**
* @return array
*/
public function getCalledListeners(Request $request = null)
public function getCalledListeners(Request $request = null): array
{
if (null === $this->callStack) {
return [];
Expand All @@ -191,10 +188,7 @@ public function getCalledListeners(Request $request = null)
return $called;
}

/**
* @return array
*/
public function getNotCalledListeners(Request $request = null)
public function getNotCalledListeners(Request $request = null): array
{
try {
$allListeners = $this->getListeners();
Expand Down Expand Up @@ -250,7 +244,7 @@ public function getOrphanedEvents(Request $request = null): array
return array_merge(...array_values($this->orphanedEvents));
}

public function reset()
public function reset(): void
{
$this->callStack = null;
$this->orphanedEvents = [];
Expand All @@ -260,9 +254,6 @@ public function reset()
/**
* Proxies all method calls to the original event dispatcher.
*
* @param string $method The method name
* @param array $arguments The method arguments
*
* @return mixed
*/
public function __call(string $method, array $arguments)
Expand All @@ -273,14 +264,14 @@ public function __call(string $method, array $arguments)
/**
* Called before dispatching the event.
*/
protected function beforeDispatch(string $eventName, object $event)
protected function beforeDispatch(string $eventName, object $event): void
{
}

/**
* Called after dispatching the event.
*/
protected function afterDispatch(string $eventName, object $event)
protected function afterDispatch(string $eventName, object $event): void
{
}

Expand Down Expand Up @@ -341,7 +332,7 @@ private function postProcess(string $eventName): void
}
}

private function sortNotCalledListeners(array $a, array $b)
private function sortNotCalledListeners(array $a, array $b): int
{
if (0 !== $cmp = strcmp($a['event'], $b['event'])) {
return $cmp;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function setHotPathEvents(array $hotPathEvents, $tagName = 'container.hot
return $this;
}

public function process(ContainerBuilder $container)
public function process(ContainerBuilder $container): void
{
if (!$container->hasDefinition($this->dispatcherService) && !$container->hasAlias($this->dispatcherService)) {
return;
Expand Down Expand Up @@ -134,7 +134,7 @@ class ExtractingEventDispatcher extends EventDispatcher implements EventSubscrib
public static $aliases = [];
public static $subscriber;

public function addListener(string $eventName, $listener, int $priority = 0)
public function addListener(string $eventName, $listener, int $priority = 0): void
{
$this->listeners[] = [$eventName, $listener[1], $priority];
}
Expand Down
18 changes: 9 additions & 9 deletions 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(string $eventName = null)
public function getListeners(string $eventName = null): array
{
if (null !== $eventName) {
if (empty($this->listeners[$eventName])) {
Expand All @@ -96,7 +96,7 @@ public function getListeners(string $eventName = null)
/**
* {@inheritdoc}
*/
public function getListenerPriority(string $eventName, $listener)
public function getListenerPriority(string $eventName, $listener): ?int
{
if (empty($this->listeners[$eventName])) {
return null;
Expand All @@ -123,7 +123,7 @@ public function getListenerPriority(string $eventName, $listener)
/**
* {@inheritdoc}
*/
public function hasListeners(string $eventName = null)
public function hasListeners(string $eventName = null): bool
{
if (null !== $eventName) {
return !empty($this->listeners[$eventName]);
Expand All @@ -141,7 +141,7 @@ public function hasListeners(string $eventName = null)
/**
* {@inheritdoc}
*/
public function addListener(string $eventName, $listener, int $priority = 0)
public function addListener(string $eventName, $listener, int $priority = 0): void
{
$this->listeners[$eventName][$priority][] = $listener;
unset($this->sorted[$eventName], $this->optimized[$eventName]);
Expand All @@ -150,7 +150,7 @@ public function addListener(string $eventName, $listener, int $priority = 0)
/**
* {@inheritdoc}
*/
public function removeListener(string $eventName, $listener)
public function removeListener(string $eventName, $listener): void
{
if (empty($this->listeners[$eventName])) {
return;
Expand Down Expand Up @@ -179,7 +179,7 @@ public function removeListener(string $eventName, $listener)
/**
* {@inheritdoc}
*/
public function addSubscriber(EventSubscriberInterface $subscriber)
public function addSubscriber(EventSubscriberInterface $subscriber): void
{
foreach ($subscriber->getSubscribedEvents() as $eventName => $params) {
if (\is_string($params)) {
Expand All @@ -197,7 +197,7 @@ public function addSubscriber(EventSubscriberInterface $subscriber)
/**
* {@inheritdoc}
*/
public function removeSubscriber(EventSubscriberInterface $subscriber)
public function removeSubscriber(EventSubscriberInterface $subscriber): void
{
foreach ($subscriber->getSubscribedEvents() as $eventName => $params) {
if (\is_array($params) && \is_array($params[0])) {
Expand All @@ -220,7 +220,7 @@ public function removeSubscriber(EventSubscriberInterface $subscriber)
* @param string $eventName The name of the event to dispatch
* @param object $event The event object to pass to the event handlers/listeners
*/
protected function callListeners(iterable $listeners, string $eventName, object $event)
protected function callListeners(iterable $listeners, string $eventName, object $event): void
{
$stoppable = $event instanceof Event || $event instanceof StoppableEventInterface;

Expand All @@ -235,7 +235,7 @@ protected function callListeners(iterable $listeners, string $eventName, object
/**
* Sorts the internal list of listeners for the given event by priority.
*/
private function sortListeners(string $eventName)
private function sortListeners(string $eventName): void
{
krsort($this->listeners[$eventName]);
$this->sorted[$eventName] = [];
Expand Down
18 changes: 7 additions & 11 deletions src/Symfony/Component/EventDispatcher/EventDispatcherInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,47 +34,43 @@ public function dispatch($event, string $eventName = null): object;
* @param int $priority The higher this value, the earlier an event
* listener will be triggered in the chain (defaults to 0)
*/
public function addListener(string $eventName, $listener, int $priority = 0);
public function addListener(string $eventName, $listener, int $priority = 0): void;

/**
* Adds an event subscriber.
*
* The subscriber is asked for all the events it is
* interested in and added as a listener for these events.
*/
public function addSubscriber(EventSubscriberInterface $subscriber);
public function addSubscriber(EventSubscriberInterface $subscriber): void;

/**
* Removes an event listener from the specified events.
*
* @param callable $listener The listener to remove
*/
public function removeListener(string $eventName, $listener);
public function removeListener(string $eventName, $listener): void;

public function removeSubscriber(EventSubscriberInterface $subscriber);
public function removeSubscriber(EventSubscriberInterface $subscriber): void;

/**
* Gets the listeners of a specific event or all listeners sorted by descending priority.
*
* @return array The event listeners for the specified event, or all event listeners by event name
*/
public function getListeners(string $eventName = null);
public function getListeners(string $eventName = null): array;

/**
* Gets the listener priority for a specific event.
*
* Returns null if the event or the listener does not exist.
*
* @param callable $listener The listener
*
* @return int|null The event listener priority
*/
public function getListenerPriority(string $eventName, $listener);
public function getListenerPriority(string $eventName, $listener): ?int;

/**
* Checks whether an event has any registered listeners.
*
* @return bool true if the specified event has any listeners, false otherwise
*/
public function hasListeners(string $eventName = null);
public function hasListeners(string $eventName = null): bool;
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ interface EventSubscriberInterface
* * ['eventName' => 'methodName']
* * ['eventName' => ['methodName', $priority]]
* * ['eventName' => [['methodName1', $priority], ['methodName2']]]
*
* @return array The event names to listen to
*/
public static function getSubscribedEvents();
public static function getSubscribedEvents(): array;
}
Loading
0