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 |
||
12 | trait DNode |
||
13 | { |
||
14 | /** |
||
15 | * @var array Associative array of callback functions registered by callRemote() |
||
16 | */ |
||
17 | protected $callbacks = []; |
||
18 | |||
19 | /** |
||
20 | * @var callable (string packet) |
||
21 | */ |
||
22 | protected $emitCallback; |
||
23 | |||
24 | /** |
||
25 | * @var array Associative array of persistent callback functions registered by callRemote() |
||
26 | */ |
||
27 | protected $persistentCallbacks = []; |
||
28 | |||
29 | /** |
||
30 | * @var boolean If true, callRemote() will register callbacks as persistent ones |
||
31 | */ |
||
32 | protected $persistentMode = false; |
||
33 | |||
34 | /** |
||
35 | * @var integer Incremental counter of callback functions registered by callRemote() |
||
36 | */ |
||
37 | protected $counter = 0; |
||
38 | |||
39 | /** |
||
40 | * @var array Associative array of registered remote methods (received in 'methods' call) |
||
41 | */ |
||
42 | protected $remoteMethods = []; |
||
43 | |||
44 | /** |
||
45 | * @var array Associative array of local methods, set by defineLocalMethods() |
||
46 | */ |
||
47 | protected $localMethods = []; |
||
48 | |||
49 | /** |
||
50 | * @var boolean Was this object cleaned up? |
||
51 | */ |
||
52 | protected $cleaned = false; |
||
53 | |||
54 | /** |
||
55 | * @var boolean Should __call method call parent::__call()? |
||
56 | */ |
||
57 | protected $magicCallParent = false; |
||
58 | |||
59 | /** |
||
60 | * Ensures that the variable passed by reference holds a valid callback-function |
||
61 | * If it doesn't, its value will be reset to null |
||
62 | * @param mixed &$arg Argument |
||
63 | * @return boolean |
||
64 | */ |
||
65 | public static function ensureCallback(&$arg) |
||
80 | |||
81 | /** |
||
82 | * Encodes value into JSON for debugging purposes |
||
83 | * @param mixed $m Data |
||
84 | * @return void |
||
85 | */ |
||
86 | public static function toJsonDebug($m) |
||
91 | |||
92 | /** |
||
93 | * Recursion handler for toJsonDebug() |
||
94 | * @param array &$a Data |
||
95 | * @return void |
||
96 | */ |
||
97 | public static function toJsonDebugResursive(&$m) |
||
117 | |||
118 | /** |
||
119 | * Default onHandshake() method |
||
120 | * @return void |
||
121 | */ |
||
122 | public function onHandshake() |
||
126 | |||
127 | /** |
||
128 | * Defines local methods |
||
129 | * @param array $arr Associative array of callbacks (methodName => callback) |
||
130 | * @return void |
||
131 | */ |
||
132 | protected function defineLocalMethods($arr = []) |
||
150 | |||
151 | /** |
||
152 | * Calls a remote method |
||
153 | * @param string $method Method name |
||
154 | * @param mixed ...$args Arguments |
||
155 | * @return this |
||
156 | */ |
||
157 | public function callRemote($method, ...$args) |
||
162 | |||
163 | |||
164 | /** |
||
165 | * Export object methods |
||
166 | * @param $object |
||
167 | * @return array |
||
168 | */ |
||
169 | public static function exportObjectMethods($object) { |
||
179 | |||
180 | /** |
||
181 | * Calls a remote method with array of arguments |
||
182 | * @param string $method Method name |
||
183 | * @param array $args Arguments |
||
184 | * @return this |
||
185 | */ |
||
186 | public function callRemoteArray($method, $args) |
||
207 | |||
208 | /** |
||
209 | * Extracts callback functions from array of arguments |
||
210 | * @param array &$args Arguments |
||
211 | * @param array &$list Output array for 'callbacks' property |
||
212 | * @param array &$path Recursion path holder |
||
213 | * @return void |
||
214 | */ |
||
215 | protected function extractCallbacks(&$args, &$list, &$path) |
||
251 | |||
252 | /** |
||
253 | * Sends a packet |
||
254 | * @param array $pct Data |
||
255 | * @return void |
||
256 | */ |
||
257 | protected function sendPacket($pct) |
||
269 | |||
270 | /** |
||
271 | * Encodes value into JSON |
||
272 | * @param mixed $m Value |
||
273 | * @return this |
||
274 | */ |
||
275 | public static function toJson($m) |
||
279 | |||
280 | /** |
||
281 | * Calls a local method |
||
282 | * @param string $method Method name |
||
283 | * @param mixed ...$args Arguments |
||
284 | * @return this |
||
285 | */ |
||
286 | public function callLocal(...$args) |
||
299 | |||
300 | /** |
||
301 | * Called when new packet is received |
||
302 | * @param array $pct Packet |
||
303 | * @return void |
||
304 | */ |
||
305 | public function onPacket($pct) |
||
354 | |||
355 | /** |
||
356 | * Sets value by materialized path |
||
357 | * @param array &$m |
||
358 | * @param array $path |
||
359 | * @param mixed $val |
||
360 | * @return void |
||
361 | */ |
||
362 | protected static function setPath(&$m, $path, $val) |
||
369 | |||
370 | /** |
||
371 | * Finds value by materialized path |
||
372 | * @param array &$m |
||
373 | * @param array $path |
||
374 | * @return mixed Value |
||
375 | */ |
||
376 | protected static function &getPath(&$m, $path) |
||
383 | |||
384 | /** |
||
385 | * Called when session is finished |
||
386 | * @return void |
||
387 | */ |
||
388 | public function onFinish() |
||
393 | |||
394 | /** |
||
395 | * Swipes internal structures |
||
396 | * @return void |
||
397 | */ |
||
398 | public function cleanup() |
||
406 | |||
407 | /** |
||
408 | * Magic __call method |
||
409 | * @param string $method Method name |
||
410 | * @param array $args Arguments |
||
411 | * @throws UndefinedMethodCalled if method name not start from 'remote_' |
||
412 | * @return mixed |
||
413 | */ |
||
414 | public function __call($method, $args) |
||
424 | |||
425 | /** |
||
426 | * Called when new frame is received |
||
427 | * @param string $data Frame's contents |
||
428 | * @param integer $type Frame's type |
||
429 | * @return void |
||
430 | */ |
||
431 | public function onFrame($data, $type) |
||
440 | |||
441 | /** |
||
442 | * Handler of the 'methods' method |
||
443 | * @param array $methods Associative array of methods |
||
444 | * @return void |
||
445 | */ |
||
446 | protected function methodsMethod($methods) |
||
450 | } |
||
451 |
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.