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 NestedSet extends TreeStrategy implements Buildable, Extension, Delay |
||
12 | { |
||
13 | const MACRO_METHOD = 'nestedSet'; |
||
14 | |||
15 | /** |
||
16 | * @var string |
||
17 | */ |
||
18 | protected $left; |
||
19 | |||
20 | /** |
||
21 | * @var string |
||
22 | */ |
||
23 | protected $right; |
||
24 | |||
25 | /** |
||
26 | * @var string |
||
27 | */ |
||
28 | protected $root; |
||
29 | |||
30 | 84 | public static function enable() |
|
38 | |||
39 | /** |
||
40 | * @param string $field |
||
41 | * @param string $type |
||
42 | * @param callable|null $callback |
||
43 | * |
||
44 | * @throws InvalidMappingException |
||
45 | * @return $this |
||
46 | */ |
||
47 | 26 | View Code Duplication | public function left($field = 'left', $type = 'integer', callable $callback = null) |
57 | |||
58 | /** |
||
59 | * @param string $field |
||
60 | * @param string $type |
||
61 | * @param callable|null $callback |
||
62 | * |
||
63 | * @throws InvalidMappingException |
||
64 | * @return $this |
||
65 | */ |
||
66 | 26 | View Code Duplication | public function right($field = 'right', $type = 'integer', callable $callback = null) |
76 | |||
77 | /** |
||
78 | * @param string $field |
||
79 | * @param callable|null $callback |
||
80 | * |
||
81 | * @return $this |
||
82 | */ |
||
83 | 3 | public function root($field = 'root', callable $callback = null) |
|
91 | |||
92 | /** |
||
93 | * Execute the build process |
||
94 | */ |
||
95 | 24 | public function build() |
|
101 | |||
102 | /** |
||
103 | * Add default values to all required fields. |
||
104 | * |
||
105 | * @return void |
||
106 | */ |
||
107 | 24 | protected function defaults() |
|
119 | |||
120 | 24 | protected function getValues() |
|
129 | |||
130 | /** |
||
131 | * @return bool |
||
132 | */ |
||
133 | 24 | private function isMissingLeft() |
|
137 | |||
138 | /** |
||
139 | * @return bool |
||
140 | */ |
||
141 | 24 | private function isMissingRight() |
|
145 | } |
||
146 |
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.