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 WeatherStationsController |
||
14 | { |
||
15 | /** |
||
16 | * @var \Netgen\Bundle\OpenWeatherMapBundle\API\OpenWeatherMap\Weather\WeatherStationsInterface |
||
17 | */ |
||
18 | protected $weatherStations; |
||
19 | |||
20 | /** |
||
21 | * WeatherStationsController constructor. |
||
22 | * |
||
23 | * @param \Netgen\Bundle\OpenWeatherMapBundle\API\OpenWeatherMap\Weather\WeatherStationsInterface $weatherStations |
||
24 | */ |
||
25 | public function __construct(WeatherStationsInterface $weatherStations) |
||
29 | |||
30 | /** |
||
31 | * Returns data from one stations by station id. |
||
32 | * |
||
33 | * @param int $stationId |
||
34 | * |
||
35 | * @return \Symfony\Component\HttpFoundation\Response |
||
36 | */ |
||
37 | View Code Duplication | public function getFromOnStationById($stationId) |
|
54 | |||
55 | /** |
||
56 | * Returns data from several stations by rectangle zone. |
||
57 | * |
||
58 | * @param float $longitudeTopLeft |
||
59 | * @param float $latitudeTopLeft |
||
60 | * @param float $longitudeBottomRight |
||
61 | * @param float $latitudeBottomRight |
||
62 | * @param int $mapZoom |
||
63 | * @param string $cluster |
||
64 | * @param int $numberOfStations |
||
65 | * |
||
66 | * @return \Symfony\Component\HttpFoundation\Response |
||
67 | */ |
||
68 | View Code Duplication | public function getFromSeveralByRectangleZone($longitudeTopLeft, $latitudeTopLeft, $longitudeBottomRight, $latitudeBottomRight, $mapZoom, $cluster = 'yes', $numberOfStations = 10) |
|
89 | |||
90 | /** |
||
91 | * Returns data from several stations by geo point. |
||
92 | * |
||
93 | * @param float $latitude |
||
94 | * @param float $longitude |
||
95 | * @param int $numberOfStations |
||
96 | * |
||
97 | * @return \Symfony\Component\HttpFoundation\Response |
||
98 | */ |
||
99 | View Code Duplication | public function getFromSeveralByGeoPoint($latitude, $longitude, $numberOfStations = 10) |
|
116 | } |
||
117 |
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.