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 |
||
9 | class VdsTariffForm extends AbstractTariffForm |
||
10 | { |
||
11 | public $note; |
||
12 | public $label; |
||
13 | |||
14 | public function load($data, $formName = null) |
||
21 | |||
22 | public function rules() |
||
29 | |||
30 | /** |
||
31 | * @return \hipanel\modules\finance\models\ServerResource[] |
||
32 | */ |
||
33 | View Code Duplication | public function getHardwareResources() |
|
44 | |||
45 | /** |
||
46 | * @param ServerResource[] $resources |
||
47 | * @param array $order array of ordered values. $resources array will be re-ordered according this order |
||
48 | * @param string $key the key that will be used to re-order |
||
49 | * @return array |
||
50 | */ |
||
51 | private function sortResourcesByDefinedOrder($resources, $order, $key) |
||
64 | |||
65 | /** |
||
66 | * @return \hipanel\modules\finance\models\ServerResource[] |
||
67 | */ |
||
68 | View Code Duplication | public function getOveruseResources() |
|
80 | |||
81 | public function getParentOveruseResource($type_id) |
||
88 | |||
89 | /** |
||
90 | * @return \hipanel\modules\finance\models\ServerResource[] |
||
91 | */ |
||
92 | public function getParentHardwareResource($object_id) |
||
99 | |||
100 | /** @inheritdoc */ |
||
101 | View Code Duplication | public function setResources($resources) |
|
123 | } |
||
124 |
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.