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 |
||
13 | class DMS implements FormatterInterface |
||
14 | { |
||
15 | const UNITS_UTF8 = 'UTF-8'; |
||
16 | const UNITS_ASCII = 'ASCII'; |
||
17 | |||
18 | /** |
||
19 | * @var string Separator string between latitude and longitude |
||
20 | */ |
||
21 | protected $separator; |
||
22 | |||
23 | /** |
||
24 | * Use cardinal letters for N/S and W/E instead of minus sign |
||
25 | * |
||
26 | * @var bool |
||
27 | */ |
||
28 | protected $useCardinalLetters; |
||
29 | |||
30 | /** |
||
31 | * @var string |
||
32 | * |
||
33 | * @psalm-suppress PropertyNotSetInConstructor |
||
34 | */ |
||
35 | protected $unitType; |
||
36 | |||
37 | /** |
||
38 | * @var array |
||
39 | */ |
||
40 | protected $units = [ |
||
41 | 'UTF-8' => [ |
||
42 | 'deg' => '°', |
||
43 | 'min' => '′', |
||
44 | 'sec' => '″', |
||
45 | ], |
||
46 | 'ASCII' => [ |
||
47 | 'deg' => '°', |
||
48 | 'min' => '\'', |
||
49 | 'sec' => '"', |
||
50 | ], |
||
51 | ]; |
||
52 | |||
53 | /** |
||
54 | * @param string $separator |
||
55 | * |
||
56 | * @throws \InvalidArgumentException |
||
57 | */ |
||
58 | public function __construct(string $separator = ' ') |
||
64 | |||
65 | /** |
||
66 | * Sets the separator between latitude and longitude values |
||
67 | * |
||
68 | * @param string $separator |
||
69 | * |
||
70 | * @return DMS |
||
71 | */ |
||
72 | public function setSeparator(string $separator): DMS |
||
78 | |||
79 | /** |
||
80 | * @param bool $value |
||
81 | * |
||
82 | * @return DMS |
||
83 | */ |
||
84 | public function useCardinalLetters(bool $value): DMS |
||
90 | |||
91 | /** |
||
92 | * @param string $type |
||
93 | * |
||
94 | * @return DMS |
||
95 | * @throws \InvalidArgumentException |
||
96 | */ |
||
97 | View Code Duplication | public function setUnits(string $type): DMS |
|
107 | |||
108 | /** |
||
109 | * @param Coordinate $coordinate |
||
110 | * |
||
111 | * @return string |
||
112 | */ |
||
113 | public function format(Coordinate $coordinate): string |
||
155 | |||
156 | /** |
||
157 | * @param float $lat |
||
158 | * |
||
159 | * @return string |
||
160 | */ |
||
161 | protected function getLatPrefix(float $lat): string |
||
169 | |||
170 | /** |
||
171 | * @param float $lng |
||
172 | * |
||
173 | * @return string |
||
174 | */ |
||
175 | protected function getLngPrefix(float $lng): string |
||
183 | |||
184 | /** |
||
185 | * @param float $lat |
||
186 | * |
||
187 | * @return string |
||
188 | */ |
||
189 | protected function getLatSuffix(float $lat): string |
||
201 | |||
202 | /** |
||
203 | * @param float $lng |
||
204 | * |
||
205 | * @return string |
||
206 | */ |
||
207 | protected function getLngSuffix(float $lng): string |
||
219 | } |
||
220 |
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.