|
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
|
9 |
|
public function on($event, callable $listener, $condition = null, int $priority = 0) |
|
31
|
|
|
{ |
|
32
|
9 |
|
return $this->addListener($event, $this->getConditionalCallable($listener, $condition), $priority); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
2 |
|
public function once($event, callable $listener, $condition = null, int $priority = 0) |
|
36
|
|
|
{ |
|
37
|
2 |
|
return $this->on($event, $this->getOnceCallable($this->getConditionalCallable($listener, $condition), $event), null, $priority); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
3 |
|
public function removeListener($event, callable $listener) |
|
41
|
|
|
{ |
|
42
|
3 |
|
if (!isset($this->listeners[$event])) { |
|
43
|
|
|
return false; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
3 |
|
$this->listeners[$event]->remove($listener); |
|
47
|
|
|
|
|
48
|
3 |
|
return true; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
9 |
|
public function emit($event, array $arguments = []) |
|
52
|
|
|
{ |
|
53
|
9 |
|
foreach ($this->listeners($event) as $listener) { |
|
54
|
|
|
try { |
|
55
|
9 |
|
if ($listener(...$arguments) === false) { |
|
56
|
8 |
|
return false; |
|
57
|
|
|
} |
|
58
|
2 |
|
} catch (\Throwable $exception) { |
|
59
|
2 |
|
if($this->emit('exception', [ $exception, $event ])) { |
|
60
|
1 |
|
throw $exception; |
|
61
|
|
|
} |
|
62
|
8 |
|
return false; |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
8 |
|
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
|
9 |
|
private function addListener($event, callable $listener, int $priority = 0) |
|
75
|
|
|
{ |
|
76
|
9 |
|
if (!isset($this->listeners[$event])) { |
|
77
|
9 |
|
$this->listeners[$event] = new PriorityCollection(); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
9 |
|
$this->listeners[$event]->insert($listener, $priority); |
|
81
|
|
|
|
|
82
|
9 |
|
return $listener; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
9 |
|
private function getConditionalCallable(callable $listener, $condition) : callable |
|
86
|
|
|
{ |
|
87
|
9 |
|
if ($condition === null) { |
|
88
|
8 |
|
return $listener; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
2 |
|
$condition = with\predicate($condition); |
|
92
|
|
|
|
|
93
|
|
|
return function (...$arguments) use ($listener, $condition) { |
|
94
|
2 |
|
if ($condition(...$arguments)) { |
|
95
|
2 |
|
return (bool)$listener(...$arguments); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
2 |
|
return null; |
|
99
|
2 |
|
}; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
private function getOnceCallable(callable $listener, $event) : callable |
|
103
|
|
|
{ |
|
104
|
2 |
|
return $onceListener = function (...$arguments) use (&$onceListener, $event, $listener) { |
|
|
|
|
|
|
105
|
2 |
|
if(($result = $listener(...$arguments)) !== null) { |
|
106
|
1 |
|
$this->removeListener($event, $onceListener); |
|
107
|
1 |
|
return $result; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
2 |
|
return null; |
|
111
|
2 |
|
}; |
|
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
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.