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 |
||
10 | class DomainTariffForm extends AbstractTariffForm |
||
11 | { |
||
12 | /** |
||
13 | * @var array Domain zones |
||
14 | * Key - zone name (com, net, ...) |
||
15 | * Value - zone id |
||
16 | * @see getZones |
||
17 | */ |
||
18 | protected $zones; |
||
19 | |||
20 | public function load($data, $formName = null) |
||
29 | |||
30 | /** |
||
31 | * @return DomainService[] |
||
32 | */ |
||
33 | public function getServices() |
||
37 | |||
38 | /** |
||
39 | * @return DomainService[] |
||
40 | */ |
||
41 | public function getParentServices() |
||
42 | { |
||
43 | return $this->createServices($this->parentTariff->resources); |
||
44 | } |
||
45 | |||
46 | /** |
||
47 | * @param $resources |
||
48 | * @return DomainService[] |
||
49 | * |
||
50 | */ |
||
51 | protected function createServices($resources) |
||
72 | |||
73 | View Code Duplication | public function setResources($resources) |
|
95 | |||
96 | View Code Duplication | public function getZoneResources($zone) |
|
113 | |||
114 | View Code Duplication | public function getZoneParentResources($zone) |
|
115 | { |
||
116 | $id = $this->zones[$zone]; |
||
117 | |||
118 | $result = []; |
||
119 | |||
120 | foreach ($this->parentTariff->resources as $resource) { |
||
121 | if ($resource->object_id == $id && $resource->isTypeCorrect()) { |
||
122 | $result[$resource->type] = $resource; |
||
123 | } |
||
124 | } |
||
125 | |||
126 | // sorts $result by order of $resource->getTypes() |
||
127 | $result = array_merge($resource->getTypes(), $result); |
||
128 | |||
129 | return $result; |
||
130 | } |
||
131 | |||
132 | public function getZones() |
||
136 | |||
137 | public function setZones(array $zones) |
||
141 | } |
||
142 |
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.