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 |
||
4 | class Currency extends BaseModel |
||
5 | { |
||
6 | /** |
||
7 | * @inheritdoc |
||
8 | */ |
||
9 | public static $tag = 'currency'; |
||
10 | |||
11 | /** |
||
12 | * @inheritdoc |
||
13 | */ |
||
14 | public static $tagProperties = [ |
||
15 | 'id', |
||
16 | 'rate', |
||
17 | 'plus', |
||
18 | ]; |
||
19 | |||
20 | public $id; |
||
21 | public $rate; |
||
22 | public $plus; |
||
23 | |||
24 | /** |
||
25 | * @inheritdoc |
||
26 | */ |
||
27 | 5 | public function rules() |
|
28 | { |
||
29 | return [ |
||
30 | [ |
||
31 | [ |
||
32 | 5 | 'id', |
|
33 | 5 | 'rate', |
|
34 | 5 | ], |
|
35 | 5 | 'required', |
|
36 | 5 | ], |
|
37 | [ |
||
38 | [ |
||
39 | 5 | 'rate', |
|
40 | 5 | ], |
|
41 | 5 | 'string', |
|
42 | 5 | 'max' => 5, |
|
43 | 5 | ], |
|
44 | [ |
||
45 | [ |
||
46 | 5 | 'id', |
|
47 | 5 | ], |
|
48 | 5 | 'string', |
|
49 | 5 | 'max' => 3, |
|
50 | 5 | ], |
|
51 | [ |
||
52 | [ |
||
53 | 5 | 'plus', |
|
54 | 5 | ], |
|
55 | 5 | 'integer', |
|
56 | 5 | ], |
|
57 | [ |
||
58 | [ |
||
59 | 5 | 'id', |
|
60 | 5 | ], |
|
61 | 5 | 'in', |
|
62 | 'range' => [ |
||
63 | 5 | 'RUR', |
|
64 | 5 | 'RUB', |
|
65 | 5 | 'UAH', |
|
66 | 5 | 'BYR', |
|
67 | 5 | 'KZT', |
|
68 | 5 | 'USD', |
|
69 | 'EUR' |
||
70 | 5 | ], |
|
71 | 5 | ], |
|
72 | 5 | ]; |
|
73 | } |
||
74 | |||
75 | /** |
||
76 | * @inheritdoc |
||
77 | */ |
||
78 | 5 | protected function getYmlBody() |
|
82 | |||
83 | /** |
||
84 | * @return string |
||
85 | */ |
||
86 | 5 | protected function getYmlEndTag() |
|
90 | |||
91 | /** |
||
92 | * @return string |
||
93 | */ |
||
94 | 5 | View Code Duplication | protected function getYmlStartTag() |
103 | } |
||
104 |
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.