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 |
||
8 | trait InterfaceTrait |
||
9 | { |
||
10 | /** |
||
11 | * @type string[] |
||
12 | */ |
||
13 | protected $interfaces = []; |
||
14 | |||
15 | /** |
||
16 | * Must be implemented by classes using this trait |
||
17 | */ |
||
18 | abstract public function getIndex(); |
||
19 | |||
20 | /** |
||
21 | * Gets the interfaces, including inherited ones. |
||
22 | * |
||
23 | * @return ReflectionInterface[] |
||
24 | */ |
||
25 | 417 | public function getInterfaces() |
|
37 | |||
38 | /** |
||
39 | * Gets the interfaces, without inherited ones. |
||
40 | * |
||
41 | * @return ReflectionInterface[] |
||
42 | */ |
||
43 | 453 | View Code Duplication | public function getSelfInterfaces() |
61 | |||
62 | /** |
||
63 | * Gets the interfaces names, including inherited ones. |
||
64 | * |
||
65 | * @return string[] A numerical array with interface names as the values |
||
66 | */ |
||
67 | 30 | public function getInterfaceNames() |
|
79 | |||
80 | /** |
||
81 | * Check whether current entity implements or extends an interface |
||
82 | * |
||
83 | * @param string $interfaceName The interface fully qualified name |
||
84 | * |
||
85 | * @return bool |
||
86 | */ |
||
87 | public function hasInterface($interfaceName) |
||
98 | |||
99 | /** |
||
100 | * Add an interface. |
||
101 | * |
||
102 | * @param string $interface Fully Qualified Interface Name |
||
103 | * |
||
104 | * @return self |
||
105 | */ |
||
106 | 708 | public function addInterface($interface) |
|
112 | } |
||
113 |
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.