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 HourForecastController |
||
14 | { |
||
15 | /** |
||
16 | * @var \Netgen\Bundle\OpenWeatherMapBundle\API\OpenWeatherMap\Weather\HourForecastInterface |
||
17 | */ |
||
18 | protected $hourForecast; |
||
19 | |||
20 | /** |
||
21 | * HourForecastController constructor. |
||
22 | * |
||
23 | * @param \Netgen\Bundle\OpenWeatherMapBundle\API\OpenWeatherMap\Weather\HourForecastInterface $hourForecast |
||
24 | */ |
||
25 | public function __construct(HourForecastInterface $hourForecast) |
||
29 | |||
30 | /** |
||
31 | * Returns hour forecast by city name. |
||
32 | * |
||
33 | * @param string $cityName |
||
34 | * @param string $countryCode |
||
35 | * |
||
36 | * @return \Symfony\Component\HttpFoundation\Response |
||
37 | */ |
||
38 | View Code Duplication | public function getForecastByCityName($cityName, $countryCode = '') |
|
55 | |||
56 | /** |
||
57 | * Returns hour forecast by city id. |
||
58 | * |
||
59 | * @param int $cityId |
||
60 | * |
||
61 | * @return \Symfony\Component\HttpFoundation\Response |
||
62 | */ |
||
63 | View Code Duplication | public function getForecastByCityId($cityId) |
|
80 | |||
81 | /** |
||
82 | * Returns hour forecast by geographic coordinates. |
||
83 | * |
||
84 | * @param float $latitude |
||
85 | * @param float $longitude |
||
86 | * |
||
87 | * @return \Symfony\Component\HttpFoundation\Response |
||
88 | */ |
||
89 | View Code Duplication | public function getForecastByCityGeographicCoordinates($latitude, $longitude) |
|
106 | } |
||
107 |
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.