Completed
Push — master ( 5c7797...ceb7ac )
by Vasily
05:20
created

EventHandlers::unbind()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 8
nop 2
dl 0
loc 20
rs 9.2888
c 0
b 0
f 0
1
<?php
2
namespace PHPDaemon\Traits;
3
4
use PHPDaemon\Core\CallbackWrapper;
5
6
/**
7
 * Event handlers trait
8
 * @package PHPDaemon\Traits
9
 * @author  Vasily Zorin <[email protected]>
10
 */
11
trait EventHandlers
12
{
13
    /**
14
     * @var array Event handlers
15
     */
16
    protected $eventHandlers = [];
17
18
    /**
19
     * @var boolean Unshift $this to arguments of callback?
20
     */
21
    protected $addThisToEvents = true;
22
23
    /**
24
     * @var string Last called event name
25
     */
26
    protected $lastEventName;
27
28
    /**
29
     * Propagate event
30
     * @param  string $name Event name
31
     * @param  mixed ...$args Arguments
32
     * @return this
0 ignored issues
show
Documentation introduced by
Should the return type not be EventHandlers?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
33
     */
34 View Code Duplication
    public function event($name, ...$args)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
35
    {
36
        if ($this->addThisToEvents) {
37
            array_unshift($args, $this);
38
        }
39
        if (isset($this->eventHandlers[$name])) {
40
            $this->lastEventName = $name;
41
            foreach ($this->eventHandlers[$name] as $cb) {
42
                if ($cb(...$args) === true) {
43
                    return $this;
44
                }
45
            }
46
        }
47
        return $this;
48
    }
49
50
    /**
51
     * Propagate event
52
     * @param  string $name Event name
53
     * @param  mixed ...$args Arguments
54
     * @return this
0 ignored issues
show
Documentation introduced by
Should the return type not be EventHandlers?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
55
     */
56 View Code Duplication
    public function trigger($name, ...$args)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
57
    {
58
        if ($this->addThisToEvents) {
59
            array_unshift($args, $this);
60
        }
61
        if (isset($this->eventHandlers[$name])) {
62
            $this->lastEventName = $name;
63
            foreach ($this->eventHandlers[$name] as $cb) {
64
                if ($cb(...$args) === true) {
65
                    return $this;
66
                }
67
            }
68
        }
69
        return $this;
70
    }
71
72
    /**
73
     * Propagate event
74
     * @param  string $name Event name
75
     * @param  mixed ...$args Arguments
76
     * @return integer
77
     */
78
    public function triggerAndCount($name, ...$args)
79
    {
80
        if ($this->addThisToEvents) {
81
            array_unshift($args, $this);
82
        }
83
        $cnt = 0;
84
        if (isset($this->eventHandlers[$name])) {
85
            $this->lastEventName = $name;
86
            foreach ($this->eventHandlers[$name] as $cb) {
87
                if ($cb(...$args) !== 0) {
88
                    ++$cnt;
89
                }
90
            }
91
        }
92
        return $cnt;
93
    }
94
95
    /**
96
     * Use it to define event name, when one callback was bind to more than one events
97
     * @return string
98
     */
99
    public function getLastEventName()
100
    {
101
        return $this->lastEventName;
102
    }
103
104
    /**
105
     * Bind event or events
106
     * @alias EventHandlers::bind
107
     * @param string|array $event Event name
108
     * @param callable $cb Callback
109
     * @return this
0 ignored issues
show
Documentation introduced by
Should the return type not be EventHandlers?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
110
     */
111
    public function on($event, $cb)
112
    {
113
        return $this->bind($event, $cb);
114
    }
115
116
    /**
117
     * Bind event or events
118
     * @param string|array $event Event name
119
     * @param callable $cb Callback
120
     * @return this
0 ignored issues
show
Documentation introduced by
Should the return type not be EventHandlers?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
121
     */
122
    public function bind($event, $cb)
123
    {
124
        if ($cb !== null) {
125
            $cb = CallbackWrapper::wrap($cb);
126
        }
127
        $event = (array) $event;
128
        foreach ($event as $e) {
129
            CallbackWrapper::addToArray($this->eventHandlers[$e], $cb);
130
        }
131
        return $this;
132
    }
133
134
    /**
135
     * Unbind event(s) or callback from event(s)
136
     * @alias EventHandlers::unbind
137
     * @param string|array $event Event name
138
     * @param callable $cb Callback, optional
0 ignored issues
show
Documentation introduced by
Should the type for parameter $cb not be callable|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
139
     * @return this
0 ignored issues
show
Documentation introduced by
Should the return type not be EventHandlers?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
140
     */
141
    public function off($event, $cb = null)
142
    {
143
        return $this->unbind($event, $cb);
144
    }
145
146
    /**
147
     * Unbind event(s) or callback from event(s)
148
     * @param string|array $event Event name
149
     * @param callable $cb Callback, optional
0 ignored issues
show
Documentation introduced by
Should the type for parameter $cb not be callable|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
150
     * @return this
0 ignored issues
show
Documentation introduced by
Should the return type not be EventHandlers?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
151
     */
152
    public function unbind($event, $cb = null)
153
    {
154
        if ($cb !== null) {
155
            $cb = CallbackWrapper::wrap($cb);
156
        }
157
        $event = (array) $event;
158
        $success = true;
0 ignored issues
show
Unused Code introduced by
$success is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

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.

Loading history...
159
        foreach ($event as $e) {
160
            if (!isset($this->eventHandlers[$e])) {
161
                $success = false;
0 ignored issues
show
Unused Code introduced by
$success is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

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.

Loading history...
162
                continue;
163
            }
164
            if ($cb === null) {
165
                unset($this->eventHandlers[$e]);
166
                continue;
167
            }
168
            CallbackWrapper::removeFromArray($this->eventHandlers[$e], $cb);
169
        }
170
        return $this;
171
    }
172
173
    /**
174
     * Clean up all events
175
     * @return void
176
     */
177
    protected function cleanupEventHandlers()
178
    {
179
        $this->eventHandlers = [];
180
        //Daemon::log('clean up event handlers '.get_class($this). ' -- '.$this->attrs->server['REQUEST_URI']. PHP_EOL .Debug::backtrace());
181
    }
182
}
183