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 |
||
22 | class ClassNode |
||
23 | { |
||
24 | private $parentClass = 'stdClass'; |
||
25 | private $interfaces = array(); |
||
26 | private $properties = array(); |
||
27 | private $unextendableMethods = array(); |
||
28 | |||
29 | /** |
||
30 | * @var MethodNode[] |
||
31 | */ |
||
32 | private $methods = array(); |
||
33 | |||
34 | public function getParentClass() |
||
38 | |||
39 | /** |
||
40 | * @param string $class |
||
41 | */ |
||
42 | public function setParentClass($class) |
||
46 | |||
47 | /** |
||
48 | * @return string[] |
||
49 | */ |
||
50 | public function getInterfaces() |
||
54 | |||
55 | /** |
||
56 | * @param string $interface |
||
57 | */ |
||
58 | public function addInterface($interface) |
||
66 | |||
67 | /** |
||
68 | * @param string $interface |
||
69 | * |
||
70 | * @return bool |
||
71 | */ |
||
72 | public function hasInterface($interface) |
||
76 | |||
77 | public function getProperties() |
||
81 | |||
82 | View Code Duplication | public function addProperty($name, $visibility = 'public') |
|
94 | |||
95 | /** |
||
96 | * @return MethodNode[] |
||
97 | */ |
||
98 | public function getMethods() |
||
102 | |||
103 | public function addMethod(MethodNode $method) |
||
113 | |||
114 | public function removeMethod($name) |
||
118 | |||
119 | /** |
||
120 | * @param string $name |
||
121 | * |
||
122 | * @return MethodNode|null |
||
123 | */ |
||
124 | public function getMethod($name) |
||
128 | |||
129 | /** |
||
130 | * @param string $name |
||
131 | * |
||
132 | * @return bool |
||
133 | */ |
||
134 | public function hasMethod($name) |
||
138 | |||
139 | /** |
||
140 | * @return string[] |
||
141 | */ |
||
142 | public function getUnextendableMethods() |
||
146 | |||
147 | /** |
||
148 | * @param string $unextendableMethod |
||
149 | */ |
||
150 | public function addUnextendableMethod($unextendableMethod) |
||
157 | |||
158 | /** |
||
159 | * @param string $method |
||
160 | * @return bool |
||
161 | */ |
||
162 | public function isExtendable($method) |
||
166 | } |
||
167 |
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.