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:
Complex classes like DNode often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use DNode, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | trait DNode { |
||
15 | /** |
||
16 | * @var array Associative array of callback functions registered by callRemote() |
||
17 | */ |
||
18 | protected $callbacks = []; |
||
19 | |||
20 | /** |
||
21 | * @var array Associative array of persistent callback functions registered by callRemote() |
||
22 | */ |
||
23 | protected $persistentCallbacks = []; |
||
24 | |||
25 | /** |
||
26 | * @var boolean If true, callRemote() will register callbacks as persistent ones |
||
27 | */ |
||
28 | protected $persistentMode = false; |
||
29 | |||
30 | /** |
||
31 | * @var integer Incremental counter of callback functions registered by callRemote() |
||
32 | */ |
||
33 | protected $counter = 0; |
||
34 | |||
35 | /** |
||
36 | * @var array Associative array of registered remote methods (received in 'methods' call) |
||
37 | */ |
||
38 | protected $remoteMethods = []; |
||
39 | |||
40 | /** |
||
41 | * @var array Associative array of local methods, set by defineLocalMethods() |
||
42 | */ |
||
43 | protected $localMethods = []; |
||
44 | |||
45 | /** |
||
46 | * @var boolean Was this object cleaned up? |
||
47 | */ |
||
48 | protected $cleaned = false; |
||
49 | |||
50 | /** |
||
51 | * @var boolean Should __call method call parent::__call()? |
||
52 | */ |
||
53 | protected $magicCallParent = false; |
||
54 | |||
55 | /** |
||
56 | * Default onHandshake() method |
||
57 | * @return void |
||
58 | */ |
||
59 | public function onHandshake() { |
||
62 | |||
63 | /** |
||
64 | * Defines local methods |
||
65 | * @param array $arr Associative array of callbacks (methodName => callback) |
||
66 | * @return void |
||
67 | */ |
||
68 | protected function defineLocalMethods($arr = []) { |
||
85 | |||
86 | /** |
||
87 | * Calls a local method |
||
88 | * @param string $method Method name |
||
|
|||
89 | * @param mixed ...$args Arguments |
||
90 | * @return this |
||
91 | */ |
||
92 | public function callLocal() { |
||
105 | |||
106 | |||
107 | /** |
||
108 | * Ensures that the variable passed by reference holds a valid callback-function |
||
109 | * If it doesn't, its value will be reset to null |
||
110 | * @param mixed &$arg Argument |
||
111 | * @return boolean |
||
112 | */ |
||
113 | protected static function ensureCallback(&$arg) { |
||
127 | |||
128 | /** |
||
129 | * Extracts callback functions from array of arguments |
||
130 | * @param array &$args Arguments |
||
131 | * @param array &$list Output array for 'callbacks' property |
||
132 | * @param array &$path Recursion path holder |
||
133 | * @return void |
||
134 | */ |
||
135 | protected function extractCallbacks(&$args, &$list, &$path) { |
||
170 | |||
171 | /** |
||
172 | * Calls a remote method |
||
173 | * @param string $method Method name |
||
174 | * @param mixed ...$args Arguments |
||
175 | * @return this |
||
176 | */ |
||
177 | public function callRemote() { |
||
186 | |||
187 | /** |
||
188 | * Calls a remote method with array of arguments |
||
189 | * @param string $method Method name |
||
190 | * @param array $args Arguments |
||
191 | * @return this |
||
192 | */ |
||
193 | public function callRemoteArray($method, $args) { |
||
213 | |||
214 | /** |
||
215 | * Handler of the 'methods' method |
||
216 | * @param array $methods Associative array of methods |
||
217 | * @return void |
||
218 | */ |
||
219 | protected function methodsMethod($methods) { |
||
222 | |||
223 | /** |
||
224 | * Encodes value into JSON |
||
225 | * @param mixed $m Value |
||
226 | * @return this |
||
227 | */ |
||
228 | public static function toJson($m) { |
||
231 | |||
232 | /** |
||
233 | * Recursion handler for toJsonDebug() |
||
234 | * @param array &$a Data |
||
235 | * @return void |
||
236 | */ |
||
237 | public static function toJsonDebugResursive(&$m) { |
||
257 | |||
258 | /** |
||
259 | * Encodes value into JSON for debugging purposes |
||
260 | * @param mixed $m Data |
||
261 | * @return void |
||
262 | */ |
||
263 | public static function toJsonDebug($m) { |
||
267 | |||
268 | /** |
||
269 | * Sends a packet |
||
270 | * @param array $pct Data |
||
271 | * @return void |
||
272 | */ |
||
273 | protected function sendPacket($pct) { |
||
282 | |||
283 | /** |
||
284 | * Called when session is finished |
||
285 | * @return void |
||
286 | */ |
||
287 | public function onFinish() { |
||
291 | |||
292 | /** |
||
293 | * Swipes internal structures |
||
294 | * @return void |
||
295 | */ |
||
296 | public function cleanup() { |
||
303 | |||
304 | |||
305 | /** |
||
306 | * Sets value by materialized path |
||
307 | * @param array &$m |
||
308 | * @param array $path |
||
309 | * @param mixed $val |
||
310 | * @return void |
||
311 | */ |
||
312 | protected static function setPath(&$m, $path, $val) { |
||
318 | |||
319 | /** |
||
320 | * Finds value by materialized path |
||
321 | * @param array &$m |
||
322 | * @param array $path |
||
323 | * @return mixed Value |
||
324 | */ |
||
325 | protected static function &getPath(&$m, $path) { |
||
331 | |||
332 | /** |
||
333 | * Magic __call method |
||
334 | * @param string $method Method name |
||
335 | * @param array $args Arguments |
||
336 | * @throws UndefinedMethodCalled if method name not start from 'remote_' |
||
337 | * @return mixed |
||
338 | */ |
||
339 | public function __call($method, $args) { |
||
350 | |||
351 | /** |
||
352 | * Called when new packet is received |
||
353 | * @param array $pct Packet |
||
354 | * @return void |
||
355 | */ |
||
356 | public function onPacket($pct) { |
||
399 | |||
400 | /** |
||
401 | * Called when new frame is received |
||
402 | * @param string $data Frame's contents |
||
403 | * @param integer $type Frame's type |
||
404 | * @return void |
||
405 | */ |
||
406 | public function onFrame($data, $type) { |
||
414 | } |
||
415 |
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
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.