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 |
||
16 | class Dictionary implements DictionaryInterface |
||
17 | { |
||
18 | /** |
||
19 | * Mảng tập hợp hàng đơn vị. |
||
20 | * |
||
21 | * @var array |
||
22 | */ |
||
23 | protected static $tripletUnits = [ |
||
24 | 'không', |
||
25 | 'một', |
||
26 | 'hai', |
||
27 | 'ba', |
||
28 | 'bốn', |
||
29 | 'năm', |
||
30 | 'sáu', |
||
31 | 'bảy', |
||
32 | 'tám', |
||
33 | 'chín', |
||
34 | ]; |
||
35 | |||
36 | /** |
||
37 | * Mảng tập hợp hàng chục. |
||
38 | * |
||
39 | * @var array |
||
40 | */ |
||
41 | protected static $tripletTens = [ |
||
42 | '', |
||
43 | 'mười', |
||
44 | 'hai mươi', |
||
45 | 'ba mươi', |
||
46 | 'bốn mươi', |
||
47 | 'năm mươi', |
||
48 | 'sáu mươi', |
||
49 | 'bảy mươi', |
||
50 | 'tám mươi', |
||
51 | 'chín mươi', |
||
52 | ]; |
||
53 | |||
54 | /** |
||
55 | * Từ ngữ mô tả hàng trăm. |
||
56 | * |
||
57 | * @var string |
||
58 | */ |
||
59 | protected static $hundred = 'trăm'; |
||
60 | |||
61 | /** |
||
62 | * Mảng tập hợp đơn vị tính dựa trên số mũ 3. |
||
63 | * |
||
64 | * @var array |
||
65 | */ |
||
66 | protected static $exponents = [ |
||
67 | '', |
||
68 | 'nghìn', |
||
69 | 'triệu', |
||
70 | 'tỷ', |
||
71 | 'nghìn tỷ', |
||
72 | 'triệu tỷ', |
||
73 | ]; |
||
74 | |||
75 | /** |
||
76 | * {@inheritdoc} |
||
77 | */ |
||
78 | public function zero(): string |
||
82 | |||
83 | /** |
||
84 | * {@inheritdoc} |
||
85 | */ |
||
86 | public function minus(): string |
||
90 | |||
91 | /** |
||
92 | * {@inheritdoc} |
||
93 | */ |
||
94 | public function separator(): string |
||
98 | |||
99 | /** |
||
100 | * {@inheritdoc} |
||
101 | */ |
||
102 | public function tripletTenSeparator(): string |
||
106 | |||
107 | /** |
||
108 | * {@inheritdoc} |
||
109 | */ |
||
110 | public function specialTripletUnitOne(): string |
||
114 | |||
115 | /** |
||
116 | * {@inheritdoc} |
||
117 | */ |
||
118 | public function specialTripletUnitFour(): string |
||
122 | |||
123 | /** |
||
124 | * {@inheritdoc} |
||
125 | */ |
||
126 | public function specialTripletUnitFive(): string |
||
130 | |||
131 | /** |
||
132 | * {@inheritdoc} |
||
133 | */ |
||
134 | public function fraction(): string |
||
138 | |||
139 | /** |
||
140 | * {@inheritdoc} |
||
141 | */ |
||
142 | View Code Duplication | public function getTripletUnit(int $unit): string |
|
150 | |||
151 | /** |
||
152 | * {@inheritdoc} |
||
153 | */ |
||
154 | public function getTripletTen(int $ten): string |
||
162 | |||
163 | /** |
||
164 | * {@inheritdoc} |
||
165 | */ |
||
166 | View Code Duplication | public function getTripletHundred(int $hundred): string |
|
174 | |||
175 | /** |
||
176 | * {@inheritdoc} |
||
177 | */ |
||
178 | public function getExponent(int $power): string |
||
186 | } |
||
187 |
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.