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 |
||
14 | trait ResourceTrait |
||
15 | { |
||
16 | /** |
||
17 | * @return null|string |
||
18 | */ |
||
19 | public function getId() |
||
26 | |||
27 | /** |
||
28 | * @return string |
||
29 | */ |
||
30 | public function getType() |
||
36 | |||
37 | /** |
||
38 | * @param array $fields |
||
39 | * @return array |
||
40 | */ |
||
41 | public function getResourceAttributes(array $fields = []) |
||
56 | |||
57 | /** |
||
58 | * @return array |
||
59 | */ |
||
60 | public function getResourceRelationships() |
||
73 | |||
74 | /** |
||
75 | * @param string $name the case sensitive name of the relationship. |
||
76 | * @param $relationship |
||
77 | */ |
||
78 | public function setResourceRelationship($name, $relationship) |
||
89 | |||
90 | /** |
||
91 | * @param string $name the case sensitive name of the relationship. |
||
92 | * @return array |
||
93 | */ |
||
94 | public function getRelationshipLinks($name) |
||
112 | |||
113 | /** |
||
114 | * @param array $fields |
||
115 | * @param array $fieldSet |
||
116 | * @return array |
||
117 | */ |
||
118 | protected function resolveFields(array $fields, array $fieldSet = []) |
||
133 | } |
||
134 |
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.