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 |
||
21 | final class ReverseQuery |
||
22 | { |
||
23 | /** |
||
24 | * @var Coordinates |
||
25 | */ |
||
26 | private $coordinates; |
||
27 | |||
28 | /** |
||
29 | * @var string|null |
||
30 | */ |
||
31 | private $locale; |
||
32 | |||
33 | /** |
||
34 | * @var int |
||
35 | */ |
||
36 | private $limit = Geocoder::DEFAULT_RESULT_LIMIT; |
||
37 | |||
38 | /** |
||
39 | * @var array |
||
40 | */ |
||
41 | private $data = []; |
||
42 | |||
43 | /** |
||
44 | * @param Coordinates $coordinates |
||
45 | */ |
||
46 | 4 | private function __construct(Coordinates $coordinates) |
|
50 | |||
51 | /** |
||
52 | * @param Coordinates $coordinates |
||
53 | * |
||
54 | * @return ReverseQuery |
||
55 | */ |
||
56 | 1 | public static function create(Coordinates $coordinates) |
|
57 | { |
||
58 | 1 | return new self($coordinates); |
|
59 | } |
||
60 | |||
61 | /** |
||
62 | * @param float $latitude |
||
63 | * @param float $longitude |
||
64 | * |
||
65 | * @return ReverseQuery |
||
66 | */ |
||
67 | 3 | public static function fromCoordinates($latitude, $longitude): ReverseQuery |
|
71 | |||
72 | /** |
||
73 | * @param Coordinates $coordinates |
||
74 | * |
||
75 | * @return ReverseQuery |
||
76 | */ |
||
77 | public function withCoordinates(Coordinates $coordinates): ReverseQuery |
||
84 | |||
85 | /** |
||
86 | * @param int $limit |
||
87 | * |
||
88 | * @return ReverseQuery |
||
89 | */ |
||
90 | 2 | public function withLimit(int $limit): ReverseQuery |
|
97 | |||
98 | /** |
||
99 | * @param string $locale |
||
100 | * |
||
101 | * @return ReverseQuery |
||
102 | */ |
||
103 | 1 | public function withLocale(string $locale): ReverseQuery |
|
110 | |||
111 | /** |
||
112 | * @param string $name |
||
113 | * @param mixed $value |
||
114 | * |
||
115 | * @return ReverseQuery |
||
116 | */ |
||
117 | 1 | public function withData(string $name, $value): ReverseQuery |
|
124 | |||
125 | /** |
||
126 | * @return Coordinates |
||
127 | */ |
||
128 | 1 | public function getCoordinates(): Coordinates |
|
132 | |||
133 | /** |
||
134 | * @return int |
||
135 | */ |
||
136 | 2 | public function getLimit(): int |
|
140 | |||
141 | /** |
||
142 | * @return string |
||
143 | */ |
||
144 | 1 | public function getLocale() |
|
148 | |||
149 | /** |
||
150 | * @param string $name |
||
151 | * @param mixed|null $default |
||
152 | * |
||
153 | * @return mixed |
||
154 | */ |
||
155 | View Code Duplication | public function getData(string $name, $default = null) |
|
163 | |||
164 | /** |
||
165 | * @return array |
||
166 | */ |
||
167 | 1 | public function getAllData(): array |
|
171 | |||
172 | /** |
||
173 | * String for logging. This is also a unique key for the query |
||
174 | * |
||
175 | * @return string |
||
176 | */ |
||
177 | 1 | public function __toString() |
|
187 | } |
||
188 |
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.