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 |
||
11 | class StackCallbacks extends \SplStack |
||
12 | { |
||
13 | use \PHPDaemon\Traits\ClassWatchdog; |
||
14 | use \PHPDaemon\Traits\StaticObjectWatchdog; |
||
15 | |||
16 | /** |
||
17 | * Push callback to the bottom of stack |
||
18 | * @param callable $cb Callback |
||
19 | * @return void |
||
20 | */ |
||
21 | public function push($cb) |
||
25 | |||
26 | /** |
||
27 | * Push callback to the top of stack |
||
28 | * @param callable $cb Callback |
||
29 | * @return void |
||
30 | */ |
||
31 | public function unshift($cb) |
||
35 | |||
36 | /** |
||
37 | * Executes one callback from the top with given arguments |
||
38 | * @param mixed ...$args Arguments |
||
39 | * @return boolean |
||
40 | */ |
||
41 | View Code Duplication | public function executeOne() |
|
55 | |||
56 | /** |
||
57 | * Executes one callback from the top with given arguments without taking it out |
||
58 | * @param mixed ...$args Arguments |
||
59 | * @return boolean |
||
60 | */ |
||
61 | View Code Duplication | public function executeAndKeepOne() |
|
73 | |||
74 | /** |
||
75 | * Executes all callbacks with given arguments |
||
76 | * @param array $args |
||
77 | * @return int |
||
78 | */ |
||
79 | public function executeAll(...$args) |
||
97 | |||
98 | /** |
||
99 | * Return array |
||
100 | * @return array |
||
101 | */ |
||
102 | public function toArray() |
||
110 | |||
111 | /** |
||
112 | * Shifts all callbacks sequentially |
||
113 | * @return void |
||
114 | */ |
||
115 | public function reset() |
||
121 | } |
||
122 |
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.