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 ClassMetadata |
||
23 | { |
||
24 | /** |
||
25 | * @var string |
||
26 | */ |
||
27 | private $namespace; |
||
28 | |||
29 | /** |
||
30 | * @var string |
||
31 | */ |
||
32 | private $class; |
||
33 | |||
34 | /** |
||
35 | * @var string |
||
36 | */ |
||
37 | private $fqcn; |
||
38 | |||
39 | /** |
||
40 | * @var ClassMetadata |
||
41 | */ |
||
42 | private $parent; |
||
43 | |||
44 | /** |
||
45 | * @var TraitMetadata[] |
||
46 | */ |
||
47 | private $traits; |
||
48 | |||
49 | /** |
||
50 | * @var bool |
||
51 | */ |
||
52 | private $final; |
||
53 | |||
54 | /** |
||
55 | * @var bool |
||
56 | */ |
||
57 | private $abstract; |
||
58 | |||
59 | /** |
||
60 | * @var MethodMetadata[] |
||
61 | */ |
||
62 | private $methods; |
||
63 | |||
64 | /** |
||
65 | * @var string |
||
66 | */ |
||
67 | private $filename; |
||
68 | |||
69 | /** |
||
70 | * @var Class_ |
||
71 | */ |
||
72 | private $ast; |
||
73 | |||
74 | /** |
||
75 | * ClassMetadata constructor. |
||
76 | * |
||
77 | * @param string $namespace |
||
78 | * @param string $class |
||
79 | * @param ClassMetadata|null $parent |
||
80 | * @param bool $final |
||
81 | * @param bool $abstract |
||
82 | * @param MethodMetadata[] $methods |
||
83 | * @param string|null $filename |
||
84 | * @param Class_ $ast |
||
85 | */ |
||
86 | public function __construct($namespace, $class, ClassMetadata $parent = null, array $traits = [], $final = false, $abstract = false, array $methods = [], $filename = null, Class_ $ast = null) |
||
109 | |||
110 | /** |
||
111 | * @return string |
||
112 | */ |
||
113 | public function getNamespace() |
||
117 | |||
118 | /** |
||
119 | * @return string |
||
120 | */ |
||
121 | public function getClass() |
||
125 | |||
126 | /** |
||
127 | * @return string |
||
128 | */ |
||
129 | public function getFqcn() |
||
133 | |||
134 | /** |
||
135 | * @return bool |
||
136 | */ |
||
137 | public function isAutoloadable() |
||
141 | |||
142 | /** |
||
143 | * Check if class inherits some other class. |
||
144 | * |
||
145 | * @return bool |
||
146 | */ |
||
147 | public function hasParent() |
||
151 | |||
152 | /** |
||
153 | * @return ClassMetadata|null |
||
154 | */ |
||
155 | public function getParent() |
||
159 | |||
160 | /** |
||
161 | * @return bool |
||
162 | */ |
||
163 | public function hasTraits() |
||
167 | |||
168 | /** |
||
169 | * @return TraitMetadata[] |
||
170 | */ |
||
171 | public function getTraits() |
||
175 | |||
176 | /** |
||
177 | * @return bool |
||
178 | */ |
||
179 | public function isFinal() |
||
183 | |||
184 | /** |
||
185 | * @return bool |
||
186 | */ |
||
187 | public function isAbstract() |
||
191 | |||
192 | /** |
||
193 | * @return MethodMetadata[] |
||
194 | */ |
||
195 | public function getMethods() |
||
199 | |||
200 | /** |
||
201 | * Check if class has public method, with optional inheritance tree and trait traverse. |
||
202 | * |
||
203 | * @param string $name |
||
204 | * @param bool $traverse |
||
205 | * |
||
206 | * @return bool |
||
207 | */ |
||
208 | public function hasPublicMethod($name, $traverse = true) |
||
236 | |||
237 | /** |
||
238 | * Get public method for class, with optional inheritance tree and trait traverse. |
||
239 | * |
||
240 | * @param string $name |
||
241 | * @param bool $traverse |
||
242 | * |
||
243 | * @return MethodMetadata |
||
244 | * |
||
245 | * @throws \RunOpenCode\AbstractBuilder\Exception\RuntimeException |
||
246 | */ |
||
247 | public function getPublicMethod($name, $traverse = true) |
||
280 | |||
281 | /** |
||
282 | * @return string |
||
283 | */ |
||
284 | public function getFilename() |
||
288 | |||
289 | /** |
||
290 | * @return Class_ |
||
291 | */ |
||
292 | public function getAst() |
||
296 | |||
297 | /** |
||
298 | * {@inheritdoc} |
||
299 | */ |
||
300 | public function __toString() |
||
304 | |||
305 | /** |
||
306 | * Initialize new, non-existing class. |
||
307 | * |
||
308 | * @param string $fqcn |
||
309 | * |
||
310 | * @return ClassMetadata|static $this |
||
311 | */ |
||
312 | public static function create($fqcn) |
||
320 | |||
321 | /** |
||
322 | * Clones original metadata object, with possible values overwrite |
||
323 | * |
||
324 | * @param ClassMetadata $original |
||
325 | * @param array $overwrite |
||
326 | * |
||
327 | * @return ClassMetadata|static $this |
||
328 | */ |
||
329 | public static function clone(ClassMetadata $original, array $overwrite = []) |
||
346 | } |
||
347 |
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.