Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
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 |
||
|
|||
33 | */ |
||
34 | View Code Duplication | public function event($name, ...$args) |
|
49 | |||
50 | /** |
||
51 | * Propagate event |
||
52 | * @param string $name Event name |
||
53 | * @param mixed ...$args Arguments |
||
54 | * @return this |
||
55 | */ |
||
56 | View Code Duplication | public function trigger($name, ...$args) |
|
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) |
||
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() |
||
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 |
||
110 | */ |
||
111 | public function on($event, $cb) |
||
115 | |||
116 | /** |
||
117 | * Bind event or events |
||
118 | * @param string|array $event Event name |
||
119 | * @param callable $cb Callback |
||
120 | * @return this |
||
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 |
||
139 | * @return this |
||
140 | */ |
||
141 | public function off($event, $cb = null) |
||
145 | |||
146 | /** |
||
147 | * Unbind event(s) or callback from event(s) |
||
148 | * @param string|array $event Event name |
||
149 | * @param callable $cb Callback, optional |
||
150 | * @return this |
||
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; |
||
159 | foreach ($event as $e) { |
||
160 | if (!isset($this->eventHandlers[$e])) { |
||
161 | $success = false; |
||
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() |
||
182 | } |
||
183 |
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.