Skip to content

Commit 95262bd

Browse files
committed
refactor EventDispatcher
1 parent dd201d1 commit 95262bd

1 file changed

Lines changed: 21 additions & 42 deletions

File tree

src/core/EventDispatcher.php

Lines changed: 21 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -6,51 +6,44 @@
66

77
class EventDispatcher
88
{
9-
/** @var self|null Singleton instance of the EventDispatcher */
9+
/** Singleton instance of the EventDispatcher */
1010
private static ?self $instance = null;
1111

12-
/** @var array<string, array<int, callable>> */
12+
/** @var array<string, array<int, callable(): mixed>> */
1313
protected array $listeners = [];
1414

15-
/**
16-
* Singleton instance of the EventDispatcher.
17-
*
18-
* @return self
19-
*/
15+
/** Singleton instance of the EventDispatcher. */
2016
public static function getInstance(): self
2117
{
22-
if (self::$instance === null) {
18+
if (!self::$instance) {
2319
self::$instance = new self();
2420
}
21+
2522
return self::$instance;
2623
}
2724

2825
/**
2926
* Register a callback for an event.
30-
*
3127
* @param string $event Event name
32-
* @param callable $callback Callback function
28+
* @param callable(): mixed $callback Callback function
3329
*/
3430
public function on(string $event, callable $callback): void
3531
{
36-
if (isset($this->listeners[$event]) === false) {
37-
$this->listeners[$event] = [];
38-
}
32+
$this->listeners[$event] ??= [];
3933
$this->listeners[$event][] = $callback;
4034
}
4135

4236
/**
4337
* Trigger an event with optional arguments.
44-
*
4538
* @param string $event Event name
4639
* @param mixed ...$args Arguments to pass to the callbacks
47-
*
4840
* @return mixed
4941
*/
5042
public function trigger(string $event, ...$args)
5143
{
5244
$result = null;
53-
if (isset($this->listeners[$event]) === true) {
45+
46+
if (isset($this->listeners[$event])) {
5447
foreach ($this->listeners[$event] as $callback) {
5548
$result = call_user_func_array($callback, $args);
5649

@@ -60,27 +53,24 @@ public function trigger(string $event, ...$args)
6053
}
6154
}
6255
}
56+
6357
return $result;
6458
}
6559

6660
/**
6761
* Check if an event has any registered listeners.
68-
*
6962
* @param string $event Event name
70-
*
7163
* @return bool True if the event has listeners, false otherwise
7264
*/
7365
public function hasListeners(string $event): bool
7466
{
75-
return isset($this->listeners[$event]) === true && count($this->listeners[$event]) > 0;
67+
return isset($this->listeners[$event]) && count($this->listeners[$event]) > 0;
7668
}
7769

7870
/**
7971
* Get all listeners registered for a specific event.
80-
*
8172
* @param string $event Event name
82-
*
83-
* @return array<int, callable> Array of callbacks registered for the event
73+
* @return array<int, callable(): mixed> Array of callbacks registered for the event
8474
*/
8575
public function getListeners(string $event): array
8676
{
@@ -89,7 +79,6 @@ public function getListeners(string $event): array
8979

9080
/**
9181
* Get a list of all events that have registered listeners.
92-
*
9382
* @return array<int, string> Array of event names
9483
*/
9584
public function getAllRegisteredEvents(): array
@@ -99,41 +88,31 @@ public function getAllRegisteredEvents(): array
9988

10089
/**
10190
* Remove a specific listener for an event.
102-
*
10391
* @param string $event the event name
104-
* @param callable $callback the exact callback to remove
105-
*
106-
* @return void
92+
* @param callable(): mixed $callback the exact callback to remove
10793
*/
10894
public function removeListener(string $event, callable $callback): void
10995
{
110-
if (isset($this->listeners[$event]) === true && count($this->listeners[$event]) > 0) {
111-
$this->listeners[$event] = array_filter($this->listeners[$event], function ($listener) use ($callback) {
112-
return $listener !== $callback;
113-
});
96+
if ($this->hasListeners($event)) {
97+
$this->listeners[$event] = array_filter(
98+
$this->listeners[$event],
99+
static fn(callable $listener): bool => $listener !== $callback,
100+
);
101+
114102
$this->listeners[$event] = array_values($this->listeners[$event]); // Re-index the array
115103
}
116104
}
117105

118106
/**
119107
* Remove all listeners for a specific event.
120-
*
121108
* @param string $event the event name
122-
*
123-
* @return void
124109
*/
125110
public function removeAllListeners(string $event): void
126111
{
127-
if (isset($this->listeners[$event]) === true) {
128-
unset($this->listeners[$event]);
129-
}
112+
unset($this->listeners[$event]);
130113
}
131114

132-
/**
133-
* Remove the current singleton instance of the EventDispatcher.
134-
*
135-
* @return void
136-
*/
115+
/** Remove the current singleton instance of the EventDispatcher. */
137116
public static function resetInstance(): void
138117
{
139118
self::$instance = null;

0 commit comments

Comments
 (0)