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 | View Code Duplication | public function getResourceAttributes(array $fields = []) |
|
| 50 | |||
| 51 | /** |
||
| 52 | * @return array |
||
| 53 | */ |
||
| 54 | View Code Duplication | public function getResourceRelationships() |
|
| 63 | |||
| 64 | /** |
||
| 65 | * @param string $name the case sensitive name of the relationship. |
||
| 66 | * @param $relationship |
||
| 67 | */ |
||
| 68 | public function setResourceRelationship($name, $relationship) |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @return array |
||
| 82 | */ |
||
| 83 | public function extraFields() |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @param array $fields |
||
| 90 | * @param array $fieldSet |
||
| 91 | * @return array |
||
| 92 | */ |
||
| 93 | protected function resolveFields(array $fields, array $fieldSet = []) |
||
| 108 | } |
||
| 109 |
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.