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 |
||
14 | class WeatherController |
||
15 | { |
||
16 | /** |
||
17 | * @var \Netgen\Bundle\OpenWeatherMapBundle\API\OpenWeatherMap\Weather\WeatherInterface |
||
18 | */ |
||
19 | protected $weather; |
||
20 | |||
21 | /** |
||
22 | * WeatherController constructor. |
||
23 | * |
||
24 | * @param \Netgen\Bundle\OpenWeatherMapBundle\API\OpenWeatherMap\Weather\WeatherInterface $weather |
||
25 | */ |
||
26 | public function __construct(WeatherInterface $weather) |
||
30 | |||
31 | /** |
||
32 | * Returns weather data by geographic coordinates. |
||
33 | * |
||
34 | * @param float $latitude |
||
35 | * @param float $longitude |
||
36 | * |
||
37 | * @return \Symfony\Component\HttpFoundation\Response |
||
38 | */ |
||
39 | View Code Duplication | public function byGeographicCoordinates($latitude, $longitude) |
|
56 | |||
57 | /** |
||
58 | * Returns weather data by city name. |
||
59 | * |
||
60 | * @param string $cityName |
||
61 | * @param string $countryCode |
||
62 | * |
||
63 | * @return \Symfony\Component\HttpFoundation\Response |
||
64 | */ |
||
65 | View Code Duplication | public function byCityName($cityName, $countryCode = '') |
|
82 | |||
83 | /** |
||
84 | * Returns weather data by city id. |
||
85 | * |
||
86 | * @param int $cityId |
||
87 | * |
||
88 | * @return \Symfony\Component\HttpFoundation\Response |
||
89 | */ |
||
90 | View Code Duplication | public function byCityId($cityId) |
|
107 | |||
108 | /** |
||
109 | * Returns weather data by zip code. |
||
110 | * |
||
111 | * @param int $zipCode |
||
112 | * @param string $countryCode |
||
113 | * |
||
114 | * @return \Symfony\Component\HttpFoundation\Response |
||
115 | */ |
||
116 | View Code Duplication | public function byZipCode($zipCode, $countryCode = '') |
|
133 | |||
134 | /** |
||
135 | * Returns weather data for cities in rectangle zone. |
||
136 | * |
||
137 | * @param float $longitudeLeft |
||
138 | * @param float $latitudeBottom |
||
139 | * @param float $longitudeRight |
||
140 | * @param float $latitudeTop |
||
141 | * @param int $mapZoom |
||
142 | * @param string $cluster |
||
143 | * |
||
144 | * @return \Symfony\Component\HttpFoundation\Response |
||
145 | */ |
||
146 | View Code Duplication | public function byRectangleZone($longitudeLeft, $latitudeBottom, $longitudeRight, $latitudeTop, $mapZoom = 10, $cluster = 'yes') |
|
165 | |||
166 | /** |
||
167 | * Returns weather data for cities in circle. |
||
168 | * |
||
169 | * @param float $latitude |
||
170 | * @param float $longitude |
||
171 | * @param string $cluster |
||
172 | * @param int $numberOfCities |
||
173 | * |
||
174 | * @return \Symfony\Component\HttpFoundation\Response |
||
175 | */ |
||
176 | View Code Duplication | public function byCircle($latitude, $longitude, $cluster = 'yes', $numberOfCities = 10) |
|
193 | |||
194 | /** |
||
195 | * Returns weather data by several city ids. |
||
196 | * |
||
197 | * @param \Symfony\Component\HttpFoundation\Request $request |
||
198 | * |
||
199 | * @return \Symfony\Component\HttpFoundation\Response |
||
200 | */ |
||
201 | public function byCityIds(Request $request) |
||
224 | } |
||
225 |
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.