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 TraitMetadata |
||
23 | { |
||
24 | /** |
||
25 | * @var string |
||
26 | */ |
||
27 | private $name; |
||
28 | |||
29 | /** |
||
30 | * @var TraitMetadata[] |
||
31 | */ |
||
32 | private $traits; |
||
33 | |||
34 | /** |
||
35 | * @var MethodMetadata[] |
||
36 | */ |
||
37 | private $methods; |
||
38 | |||
39 | /** |
||
40 | * @var Trait_ |
||
41 | */ |
||
42 | private $ast; |
||
43 | |||
44 | public function __construct($name, array $traits = [], array $methods = [], Trait_ $ast = null) |
||
56 | |||
57 | /** |
||
58 | * @return string |
||
59 | */ |
||
60 | public function getName() |
||
64 | |||
65 | /** |
||
66 | * @return string |
||
67 | */ |
||
68 | public function getFqcn() |
||
72 | |||
73 | /** |
||
74 | * @return bool |
||
75 | */ |
||
76 | public function hasTraits() |
||
80 | |||
81 | /** |
||
82 | * @return TraitMetadata[] |
||
83 | */ |
||
84 | public function getTraits() |
||
88 | |||
89 | /** |
||
90 | * @return MethodMetadata[] |
||
91 | */ |
||
92 | public function getMethods() |
||
96 | |||
97 | /** |
||
98 | * Check if trait has method, with optional trait traverse. |
||
99 | * |
||
100 | * @param string $name |
||
101 | * @param bool $traverse |
||
102 | * |
||
103 | * @return bool |
||
104 | */ |
||
105 | View Code Duplication | public function hasMethod($name, $traverse = true) |
|
129 | |||
130 | /** |
||
131 | * Check if trait has public method, with optional trait traverse. |
||
132 | * |
||
133 | * @param string $name |
||
134 | * @param bool $traverse |
||
135 | * |
||
136 | * @return bool |
||
137 | */ |
||
138 | View Code Duplication | public function hasPublicMethod($name, $traverse = true) |
|
162 | |||
163 | /** |
||
164 | * Get public method for trait, with optional trait traverse. |
||
165 | * |
||
166 | * @param string $name |
||
167 | * @param bool $traverse |
||
168 | * |
||
169 | * @return MethodMetadata |
||
170 | * |
||
171 | * @throws \RunOpenCode\AbstractBuilder\Exception\RuntimeException |
||
172 | */ |
||
173 | public function getPublicMethod($name, $traverse = true) |
||
202 | |||
203 | /** |
||
204 | * @return Trait_ |
||
205 | */ |
||
206 | public function getAst() |
||
210 | |||
211 | /** |
||
212 | * {@inheritdoc} |
||
213 | */ |
||
214 | public function __toString() |
||
218 | } |
||
219 |
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.