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 |
||
| 12 | trait EventHandlers { |
||
| 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() { |
|
| 50 | |||
| 51 | /** |
||
| 52 | * Propagate event |
||
| 53 | * @param string $name Event name |
||
| 54 | * @param mixed ...$args Arguments |
||
| 55 | * @return this |
||
| 56 | */ |
||
| 57 | View Code Duplication | public function trigger() { |
|
| 73 | |||
| 74 | /** |
||
| 75 | * Propagate event |
||
| 76 | * @param string $name Event name |
||
| 77 | * @param mixed ...$args Arguments |
||
| 78 | * @return integer |
||
| 79 | */ |
||
| 80 | public function triggerAndCount() { |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Use it to define event name, when one callback was bind to more than one events |
||
| 100 | * @return string |
||
| 101 | */ |
||
| 102 | public function getLastEventName() { |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Bind event or events |
||
| 108 | * @param string|array $event Event name |
||
| 109 | * @param callable $cb Callback |
||
| 110 | * @return this |
||
| 111 | */ |
||
| 112 | public function bind($event, $cb) { |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Bind event or events |
||
| 125 | * @alias EventHandlers::bind |
||
| 126 | * @param string|array $event Event name |
||
| 127 | * @param callable $cb Callback |
||
| 128 | * @return this |
||
| 129 | */ |
||
| 130 | public function on($event, $cb) { |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Unbind event(s) or callback from event(s) |
||
| 136 | * @param string|array $event Event name |
||
| 137 | * @param callable $cb Callback, optional |
||
| 138 | * @return this |
||
| 139 | */ |
||
| 140 | public function unbind($event, $cb = null) { |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Unbind event(s) or callback from event(s) |
||
| 162 | * @alias EventHandlers::unbind |
||
| 163 | * @param string|array $event Event name |
||
| 164 | * @param callable $cb Callback, optional |
||
| 165 | * @return this |
||
| 166 | */ |
||
| 167 | public function off($event, $cb) { |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Clean up all events |
||
| 173 | * @return void |
||
| 174 | */ |
||
| 175 | protected function cleanupEventHandlers() { |
||
| 179 | } |
||
| 180 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.