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 DeliveryOption extends BaseModel |
||
11 | { |
||
12 | /** |
||
13 | * @inheritdoc |
||
14 | */ |
||
15 | public static $tag = 'option'; |
||
16 | |||
17 | /** |
||
18 | * @inheritdoc |
||
19 | */ |
||
20 | public static $tagProperties = [ |
||
21 | 'cost', |
||
22 | 'days', |
||
23 | 'orderBefore' => 'order-before' |
||
24 | ]; |
||
25 | |||
26 | /** @var int Стоимость доставки в рублях. */ |
||
27 | public $cost; |
||
28 | |||
29 | /** @var string Срок доставки в рабочих днях. */ |
||
30 | public $days; |
||
31 | |||
32 | /** @var int Время, до которого нужно сделать заказ, чтобы получить его в этот срок. */ |
||
33 | public $orderBefore; |
||
34 | 4 | ||
35 | /** |
||
36 | * @inheritdoc |
||
37 | */ |
||
38 | 4 | View Code Duplication | public function rules() |
|
|||
39 | 4 | { |
|
40 | 4 | return [ |
|
41 | [ |
||
42 | 4 | ['cost', 'days'], |
|
43 | 4 | 'required', |
|
44 | 4 | ], |
|
45 | [ |
||
46 | 4 | ['cost', 'orderBefore'], |
|
47 | 4 | 'integer', |
|
48 | 4 | ], |
|
49 | 4 | [ |
|
50 | 4 | ['days'], |
|
51 | 'string', |
||
52 | 'max' => 5, |
||
53 | ], |
||
54 | ]; |
||
55 | } |
||
56 | 4 | ||
57 | /** |
||
58 | 4 | * @inheritdoc |
|
59 | */ |
||
60 | protected function getYmlBody() |
||
64 | 4 | ||
65 | /** |
||
66 | 4 | * @return string |
|
67 | */ |
||
68 | protected function getYmlStartTag() |
||
72 | 4 | ||
73 | /** |
||
74 | 4 | * @return string |
|
75 | */ |
||
76 | protected function getYmlEndTag() |
||
80 | } |
||
81 |
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.