1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Nucleus - XMPP Library for PHP |
4
|
|
|
* |
5
|
|
|
* Copyright (C) 2016, Some rights reserved. |
6
|
|
|
* |
7
|
|
|
* @author Kacper "Kadet" Donat <[email protected]> |
8
|
|
|
* |
9
|
|
|
* Contact with author: |
10
|
|
|
* Xmpp: [email protected] |
11
|
|
|
* E-mail: [email protected] |
12
|
|
|
* |
13
|
|
|
* From Kadet with love. |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
namespace Kadet\Xmpp\Utils; |
17
|
|
|
|
18
|
|
|
use Kadet\Xmpp\Utils\filter as with; |
19
|
|
|
use Evenement\EventEmitterTrait; |
20
|
|
|
|
21
|
|
|
trait BetterEmitter |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var PriorityCollection[] |
25
|
|
|
*/ |
26
|
|
|
protected $listeners; |
27
|
|
|
|
28
|
|
|
use EventEmitterTrait; |
29
|
|
|
|
30
|
17 |
|
public function on($event, callable $listener, $condition = null, int $priority = 0) |
31
|
|
|
{ |
32
|
17 |
|
return $this->addListener($event, $this->getConditionalCallable($listener, $condition), $priority); |
33
|
|
|
} |
34
|
|
|
|
35
|
5 |
|
public function once($event, callable $listener, $condition = null, int $priority = 0) |
36
|
|
|
{ |
37
|
5 |
|
return $this->on($event, $this->getOnceCallable($this->getConditionalCallable($listener, $condition), $event), null, $priority); |
38
|
|
|
} |
39
|
|
|
|
40
|
4 |
|
public function removeListener($event, callable $listener) |
41
|
|
|
{ |
42
|
4 |
|
if (!isset($this->listeners[$event])) { |
43
|
|
|
return false; |
44
|
|
|
} |
45
|
|
|
|
46
|
4 |
|
$this->listeners[$event]->remove($listener); |
47
|
|
|
|
48
|
4 |
|
return true; |
49
|
|
|
} |
50
|
|
|
|
51
|
14 |
|
public function emit($event, array $arguments = []) |
52
|
|
|
{ |
53
|
14 |
|
foreach ($this->listeners($event) as $listener) { |
54
|
|
|
try { |
55
|
14 |
|
if ($listener(...$arguments) === false) { |
56
|
13 |
|
return false; |
57
|
|
|
} |
58
|
2 |
|
} catch (\Throwable $exception) { |
59
|
2 |
|
if($this->emit('exception', [ $exception, $event ])) { |
60
|
1 |
|
throw $exception; |
61
|
|
|
} |
62
|
13 |
|
return false; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
13 |
|
return true; |
67
|
|
|
} |
68
|
|
|
|
69
|
1 |
|
public function reference(callable $callable, int $position = 0) : callable |
70
|
|
|
{ |
71
|
1 |
|
return \Kadet\Xmpp\Utils\helper\partial($callable, $this, $position); |
72
|
|
|
} |
73
|
|
|
|
74
|
17 |
|
private function addListener($event, callable $listener, int $priority = 0) |
75
|
|
|
{ |
76
|
17 |
|
if (!isset($this->listeners[$event])) { |
77
|
17 |
|
$this->listeners[$event] = new PriorityCollection(); |
78
|
|
|
} |
79
|
|
|
|
80
|
17 |
|
$this->listeners[$event]->insert($listener, $priority); |
81
|
|
|
|
82
|
17 |
|
return $listener; |
83
|
|
|
} |
84
|
|
|
|
85
|
17 |
|
private function getConditionalCallable(callable $listener, $condition) : callable |
86
|
|
|
{ |
87
|
17 |
|
if ($condition === null) { |
88
|
16 |
|
return $listener; |
89
|
|
|
} |
90
|
|
|
|
91
|
10 |
|
$condition = with\predicate($condition); |
92
|
|
|
|
93
|
|
|
return function (...$arguments) use ($listener, $condition) { |
94
|
7 |
|
if ($condition(...$arguments)) { |
95
|
7 |
|
return $listener(...$arguments); |
96
|
|
|
} |
97
|
|
|
|
98
|
5 |
|
return INF; |
99
|
10 |
|
}; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
private function getOnceCallable(callable $listener, $event) : callable |
103
|
|
|
{ |
104
|
5 |
|
return $onceListener = function (...$arguments) use (&$onceListener, $event, $listener) { |
|
|
|
|
105
|
2 |
|
if(($result = $listener(...$arguments)) !== INF) { |
106
|
2 |
|
$this->removeListener($event, $onceListener); |
107
|
2 |
|
return $result; |
108
|
|
|
} |
109
|
|
|
|
110
|
1 |
|
return null; |
111
|
5 |
|
}; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
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.