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 |
||
29 | abstract class MapDispatcher extends \PEIP\ABS\Dispatcher\Dispatcher implements //PEIP\INF\Dispatcher\MapDispatcher, |
||
30 | \PEIP\INF\Event\Connectable |
||
31 | { |
||
32 | protected $listeners = []; |
||
33 | |||
34 | /** |
||
35 | * Connects a listener to a given event-name. |
||
36 | * |
||
37 | * @param string $name name of the event |
||
38 | * @param callable|PEIP\INF\Handler\Handler $listener listener to connect |
||
39 | * |
||
40 | * @return |
||
41 | */ |
||
42 | public function connect($name, $listener) |
||
52 | |||
53 | /** |
||
54 | * Disconnects a listener from a given event-name. |
||
55 | * |
||
56 | * @param string $name name of the event |
||
57 | * @param callable|PEIP\INF\Handler\Handler $listener listener to connect |
||
58 | * |
||
59 | * @return |
||
60 | */ |
||
61 | public function disconnect($name, $listener) |
||
76 | |||
77 | /** |
||
78 | * Disconnects a listener from a given event-name. |
||
79 | * |
||
80 | * @param string $name name of the event |
||
81 | * |
||
82 | * @return |
||
83 | */ |
||
84 | View Code Duplication | public function disconnectAll($name) |
|
93 | |||
94 | /** |
||
95 | * Checks wether any listener is registered for a given event-name. |
||
96 | * |
||
97 | * @param string $name name of the event |
||
98 | * |
||
99 | * @return bool wether any listener is registered for event-name |
||
100 | */ |
||
101 | View Code Duplication | public function hasListeners($name) |
|
109 | |||
110 | /** |
||
111 | * notifies all listeners on a event on a subject. |
||
112 | * |
||
113 | * @param string $name name of the event |
||
114 | * @param mixed $subject the subject |
||
115 | * |
||
116 | * @return bool success |
||
117 | */ |
||
118 | View Code Duplication | public function notify($name, $subject) |
|
128 | |||
129 | /** |
||
130 | * notifies all listeners on a event on a subject until one returns a boolean true value. |
||
131 | * |
||
132 | * @param string $name name of the event |
||
133 | * @param mixed $subject the subject |
||
134 | * |
||
135 | * @return \PEIP\INF\Handler\Handler listener which returned a boolean true value |
||
136 | */ |
||
137 | public function notifyUntil($name, $subject) |
||
143 | |||
144 | /** |
||
145 | * Returns all listeners registered for a given event-name. |
||
146 | * |
||
147 | * @param $name |
||
148 | * |
||
149 | * @return array array of \PEIP\INF\Handler\Handler instances |
||
150 | */ |
||
151 | public function getListeners($name) |
||
159 | } |
||
160 |
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.