1 | <?php |
||
21 | trait BetterEmitter |
||
22 | { |
||
23 | /** |
||
24 | * @var PriorityCollection[] |
||
25 | */ |
||
26 | protected $listeners; |
||
27 | |||
28 | use EventEmitterTrait; |
||
29 | |||
30 | public function on($event, callable $listener, $condition = null, int $priority = 0) |
||
31 | { |
||
32 | return $this->addListener($event, $this->getConditionalCallable($listener, $condition), $priority); |
||
33 | } |
||
34 | |||
35 | public function once($event, callable $listener, $condition = null, int $priority = 0) |
||
36 | { |
||
37 | return $this->on($event, $this->getOnceCallable($this->getConditionalCallable($listener, $condition), $event), null, $priority); |
||
38 | } |
||
39 | |||
40 | public function removeListener($event, callable $listener) |
||
41 | { |
||
42 | if (!isset($this->listeners[$event])) { |
||
43 | return false; |
||
44 | } |
||
45 | |||
46 | $this->listeners[$event]->remove($listener); |
||
47 | |||
48 | return true; |
||
49 | } |
||
50 | |||
51 | public function emit($event, array $arguments = []) |
||
61 | |||
62 | public function reference(callable $callable, int $position = 0) : callable |
||
66 | |||
67 | private function addListener($event, callable $listener, int $priority = 0) |
||
68 | { |
||
69 | if (!isset($this->listeners[$event])) { |
||
70 | $this->listeners[$event] = new PriorityCollection(); |
||
71 | } |
||
72 | |||
73 | $this->listeners[$event]->insert($listener, $priority); |
||
74 | |||
75 | return $listener; |
||
76 | } |
||
77 | |||
78 | private function getConditionalCallable(callable $listener, $condition) : callable |
||
79 | { |
||
80 | if ($condition === null) { |
||
81 | return $listener; |
||
82 | } |
||
83 | |||
84 | $condition = with\predicate($condition); |
||
85 | |||
86 | return function (...$arguments) use ($listener, $condition) { |
||
87 | if ($condition(...$arguments)) { |
||
88 | return (bool)$listener(...$arguments); |
||
89 | } |
||
90 | |||
91 | return null; |
||
92 | }; |
||
93 | } |
||
94 | |||
95 | private function getOnceCallable(callable $listener, $event) : callable |
||
106 | } |
||
107 |
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.