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 | class PopoloObject |
||
9 | { |
||
10 | use ArrayGetterTrait; |
||
11 | |||
12 | protected $properties = []; |
||
13 | protected $baseProperties = [ |
||
14 | 'keyForHash', |
||
15 | ]; |
||
16 | protected $data; |
||
17 | protected $allPopolo; |
||
18 | |||
19 | /** |
||
20 | * Creates a new instance |
||
21 | */ |
||
22 | 255 | public function __construct($data, $allPopolo) |
|
27 | |||
28 | /** |
||
29 | * Getter for public attributes |
||
30 | * |
||
31 | * @param string $prop the attribute to get |
||
32 | * |
||
33 | * @return mixed |
||
34 | */ |
||
35 | 255 | View Code Duplication | public function __get($prop) |
44 | |||
45 | 222 | protected function getKeyForHash() |
|
49 | |||
50 | protected function getId() |
||
54 | |||
55 | 27 | private function getRelatedObjects($popoloArray) |
|
59 | |||
60 | 33 | protected function getDate($attr, $default = null) |
|
68 | |||
69 | 28 | private function getRelatedValues($popoloArray, $infoTypeKey, $infoType, $infoValueKey) |
|
107 | |||
108 | 12 | public function identifierValues($scheme) |
|
112 | |||
113 | 12 | public function identifierValue($scheme) |
|
118 | |||
119 | 12 | public function linkValues($note) |
|
123 | |||
124 | 9 | public function linkValue($note) |
|
129 | |||
130 | 12 | public function contactDetailValues($contactType) |
|
134 | |||
135 | 12 | public function contactDetailValue($contactType) |
|
140 | |||
141 | 42 | View Code Duplication | public function equals($other) |
149 | |||
150 | 51 | public function getRelatedObjectArr($popoloArray) |
|
154 | } |
||
155 |
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.