1 | <?php |
||
22 | trait BetterEmitter |
||
23 | { |
||
24 | /** |
||
25 | * @var PriorityCollection[] |
||
26 | */ |
||
27 | protected $listeners; |
||
28 | |||
29 | use EventEmitterTrait; |
||
30 | |||
31 | 11 | public function on($event, callable $listener, $condition = null, int $priority = 0) |
|
32 | { |
||
33 | 11 | $this->addListener($event, $this->getConditionalCallable($listener, $condition), $priority); |
|
34 | 11 | } |
|
35 | |||
36 | public function once($event, callable $listener, $condition = null, int $priority = 0) |
||
40 | |||
41 | public function removeListener($event, callable $listener) |
||
50 | |||
51 | 11 | public function emit($event, array $arguments = []) |
|
52 | { |
||
53 | 11 | foreach ($this->listeners($event) as $listener) { |
|
54 | 11 | if ($listener(...$arguments) === false) { |
|
55 | 11 | return false; |
|
56 | } |
||
57 | } |
||
58 | |||
59 | 11 | return true; |
|
60 | } |
||
61 | |||
62 | public function reference(callable $callable, int $position = 0) : callable |
||
66 | |||
67 | 11 | private function addListener($event, callable $listener, int $priority = 0) |
|
68 | { |
||
69 | 11 | if(!isset($this->listeners[$event])) { |
|
70 | 11 | $this->listeners[$event] = new PriorityCollection(); |
|
71 | } |
||
72 | |||
73 | 11 | $this->listeners[$event]->insert($listener, $priority); |
|
74 | 11 | } |
|
75 | |||
76 | 11 | private function getConditionalCallable(callable $listener, $condition) : callable |
|
77 | { |
||
78 | 11 | if ($condition === null) { |
|
79 | 11 | return $listener; |
|
80 | } |
||
81 | |||
82 | $condition = $this->emitterResolveCondition($condition); |
||
83 | return function (...$arguments) use ($listener, $condition) { |
||
84 | if ($condition(...$arguments)) { |
||
85 | $listener(...$arguments); |
||
86 | } |
||
87 | }; |
||
88 | } |
||
89 | |||
90 | private function getOnceCallable(callable $listener, $event) : callable |
||
98 | |||
99 | protected function emitterResolveCondition($condition) : callable |
||
109 | } |
||
110 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.