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:
Complex classes like TraitUseTrait often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use TraitUseTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | trait TraitUseTrait |
||
10 | { |
||
11 | protected $traits = []; |
||
12 | protected $traitsMethodAliases = []; |
||
13 | protected $traitsMethodPrecedences = []; |
||
14 | |||
15 | /** |
||
16 | * Must be implemented by classes using this trait |
||
17 | */ |
||
18 | abstract public function getIndex(); |
||
19 | |||
20 | /** |
||
21 | * Returns an array of traits used by this class, without traits inherited from parents. |
||
22 | * |
||
23 | * Note that getTraits will NOT return any traits inherited from a parent. |
||
24 | * This is currently viewed as the desired behavior. |
||
25 | * |
||
26 | * @see http://php.net/manual/en/reflectionclass.gettraits.php#116513 |
||
27 | * @see self::getAllTraits() |
||
28 | * |
||
29 | * @return ReflectionTrait[] |
||
30 | */ |
||
31 | 579 | public function getTraits() |
|
43 | |||
44 | /** |
||
45 | * Returns an array of names of traits used by this class, without traits inherited from parents. |
||
46 | * |
||
47 | * This return only the trait names from the current class |
||
48 | * |
||
49 | * @see http://php.net/manual/en/reflectionclass.gettraitnames.php#113785 |
||
50 | * @see self::getAllTraitNames() |
||
51 | * |
||
52 | * @return string[] |
||
53 | */ |
||
54 | public function getTraitNames() |
||
58 | |||
59 | /** |
||
60 | * Returns an array of traits used by this class, with traits inherited from parents. |
||
61 | * |
||
62 | * @return ReflectionTrait[] |
||
63 | */ |
||
64 | 579 | public function getAllTraits() |
|
78 | |||
79 | /** |
||
80 | * Returns an array of names of traits used by this class, with traits inherited from parents. |
||
81 | * |
||
82 | * @return string[] |
||
83 | */ |
||
84 | public function getAllTraitNames() |
||
94 | |||
95 | /** |
||
96 | * Gets the traits, without inherited ones. |
||
97 | * |
||
98 | * @return ReflectionTrait[] |
||
99 | */ |
||
100 | 579 | View Code Duplication | public function getSelfTraits() |
118 | |||
119 | /** |
||
120 | * Gets an array of properties defined in class traits, with inherited ones. |
||
121 | * |
||
122 | * @return \Benoth\StaticReflection\Reflection\ReflectionProperty[] |
||
123 | */ |
||
124 | 195 | public function getTraitsProperties() |
|
141 | |||
142 | /** |
||
143 | * Gets an array of methods defined in class traits, including inherited ones. |
||
144 | * |
||
145 | * @return Benoth\StaticReflection\Reflection\ReflectionMethod[] |
||
146 | */ |
||
147 | 387 | public function getTraitsMethods() |
|
167 | |||
168 | /** |
||
169 | * Returns an array of trait methods aliases. |
||
170 | * |
||
171 | * @return string[] New method names in keys and original names (in the format "TraitName::original") in values |
||
172 | */ |
||
173 | public function getTraitAliases() |
||
183 | |||
184 | /** |
||
185 | * Returns an array of trait methods aliases. |
||
186 | * |
||
187 | * @return array[] Numeric keys with 3 values associative array : trait (name of the trait), oldName (old method name), newName (new method name) |
||
188 | */ |
||
189 | 117 | public function getTraitMethodAlias() |
|
193 | |||
194 | /** |
||
195 | * Returns an array of trait methods precedences. |
||
196 | * |
||
197 | * @return array[] Numeric keys with 3 values associative array : trait (name of the trait used), insteadof (name of the trait replaced), method (method name) |
||
198 | */ |
||
199 | 117 | public function getTraitMethodPrecedences() |
|
203 | |||
204 | /** |
||
205 | * Gets a method aliased name. |
||
206 | * |
||
207 | * @param string $traitName Fully Qualified Name of the trait |
||
208 | * @param string $methodName Name of the method |
||
209 | * |
||
210 | * @return string Aliased name or the method original name il no alias found |
||
211 | */ |
||
212 | 117 | public function getAliasedMethodName($traitName, $methodName) |
|
222 | |||
223 | /** |
||
224 | * Check if a method from a trait is replaced by precedence. |
||
225 | * |
||
226 | * @param string $traitName Fully Qualified Name of the trait |
||
227 | * @param string $methodName |
||
228 | * |
||
229 | * @return bool |
||
230 | */ |
||
231 | 117 | public function isReplacedByPrecedence($traitName, $methodName) |
|
241 | |||
242 | /** |
||
243 | * Add a trait use on the current entity. |
||
244 | * |
||
245 | * @param string $trait Fully Qualified Trait Name |
||
246 | * |
||
247 | * @return self |
||
248 | */ |
||
249 | 258 | public function addTrait($trait) |
|
255 | |||
256 | /** |
||
257 | * Add a trait method alias. |
||
258 | * |
||
259 | * @param string $trait Fully Qualified Name of the trait |
||
260 | * @param string $oldName Old method name |
||
261 | * @param string $newName New method name |
||
262 | * |
||
263 | * @return self |
||
264 | */ |
||
265 | 258 | public function addTraitMethodAlias($trait, $oldName, $newName) |
|
275 | |||
276 | /** |
||
277 | * Add a trait method precedence. |
||
278 | * |
||
279 | * @param string $trait Fully Qualified Name of the trait to use |
||
280 | * @param string $insteadof Fully Qualified Name of the replaced trait |
||
281 | * @param string $method Method name |
||
282 | */ |
||
283 | 258 | public function addTraitMethodPrecedences($trait, $insteadof, $method) |
|
293 | } |
||
294 |
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.