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 |
||
30 | class ObjectMapDispatcher implements \PEIP\INF\Dispatcher\ObjectMapDispatcher |
||
31 | { |
||
32 | protected $listeners = null, |
||
33 | $classDispatcher = null; |
||
34 | |||
35 | /** |
||
36 | * connects a Handler to an event on an object. |
||
37 | * |
||
38 | * @param string $name the event-name |
||
39 | * @param object $object arbitrary object to connect to |
||
40 | * @param callable|PEIP\INF\Handler\Handler $listener event-handler |
||
41 | * |
||
42 | * @return bool |
||
43 | */ |
||
44 | public function connect($name, $object, $listener) |
||
59 | |||
60 | /** |
||
61 | * Disconnects a Handler from an event on an object. |
||
62 | * |
||
63 | * @param string $name the event-name |
||
64 | * @param object $object arbitrary object to disconnect from |
||
65 | * @param \PEIP\INF\Handler\Handler $listener event-handler |
||
66 | * |
||
67 | * @return bool |
||
68 | */ |
||
69 | public function disconnect($name, $object, $listener) |
||
87 | |||
88 | public function disconnectAll($name, $object) |
||
97 | |||
98 | /** |
||
99 | * Checks wether an object has a listener for an event. |
||
100 | * |
||
101 | * @param string $name the event-name |
||
102 | * @param object $object object to check for listeners |
||
103 | * |
||
104 | * @return bool |
||
105 | */ |
||
106 | public function hasListeners($name, $object) |
||
118 | |||
119 | /** |
||
120 | * Checks wether an object has or had a listener for an event. |
||
121 | * |
||
122 | * @param string $name the event-name |
||
123 | * @param object $object object to check for listeners |
||
124 | * |
||
125 | * @return bool |
||
126 | */ |
||
127 | public function hadListeners($name, $object) |
||
133 | |||
134 | /** |
||
135 | * Returns all event-names an object has registered listeners for. |
||
136 | * |
||
137 | * @param object $object object to get event-names for |
||
138 | * |
||
139 | * @return string[] array of event-names |
||
140 | */ |
||
141 | public function getEventNames($object) |
||
150 | |||
151 | /** |
||
152 | * Notifies all listeners of a given event on an object. |
||
153 | * |
||
154 | * @param $name |
||
155 | * @param $object |
||
156 | * |
||
157 | * @return bool |
||
158 | */ |
||
159 | public function notify($name, $object) |
||
168 | |||
169 | /** |
||
170 | * Notifies all listeners of a given event on an object |
||
171 | * until one returns a non null value. |
||
172 | * |
||
173 | * @param $name |
||
174 | * @param $subject |
||
175 | * |
||
176 | * @return \PEIP\INF\Handler\Handler |
||
177 | */ |
||
178 | public function notifyUntil($name, $subject) |
||
186 | |||
187 | /** |
||
188 | * Returns all listeners of a given event on an object. |
||
189 | * |
||
190 | * @param string $name the event-name |
||
191 | * @param object $object object to check for listeners |
||
192 | * |
||
193 | * @return array array of listeners |
||
194 | */ |
||
195 | public function getListeners($name, $object) |
||
204 | |||
205 | /** |
||
206 | * Returns ObjectStorage object to store objects to lositen to in. |
||
207 | * creates ObjectStorage if not exists. |
||
208 | * |
||
209 | * @return ObjectStorage |
||
210 | */ |
||
211 | protected function doGetListeners() |
||
215 | |||
216 | /** |
||
217 | * Notifies all given listeners about an subject. |
||
218 | * |
||
219 | * @static |
||
220 | * |
||
221 | * @param $name |
||
222 | * @param $object |
||
223 | * |
||
224 | * @return bool|null |
||
225 | */ |
||
226 | protected static function doNotify(array $listeners, $subject) |
||
236 | |||
237 | /** |
||
238 | * Notifies all given listeners about an subject untill one returns a boolean true value. |
||
239 | * |
||
240 | * @static |
||
241 | * |
||
242 | * @param $name |
||
243 | * @param $object |
||
244 | * |
||
245 | * @return \PEIP\INF\Handler\Handler the listener which returned a boolean true value |
||
246 | */ |
||
247 | protected static function doNotifyUntil(array $listeners, $subject) |
||
261 | |||
262 | protected function getListenerHash($listener) |
||
272 | } |
||
273 |
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.