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 |
||
15 | class YahooWeatherAPI implements YahooWeatherAPIInterface |
||
16 | { |
||
17 | /** |
||
18 | * @var Client Goutte client |
||
19 | */ |
||
20 | protected $client; |
||
21 | |||
22 | /** |
||
23 | * @var array last response from API |
||
24 | */ |
||
25 | protected $lastResponse; |
||
26 | |||
27 | /** |
||
28 | * @var string last woeid explicitly called |
||
29 | */ |
||
30 | protected $woeid; |
||
31 | |||
32 | /** |
||
33 | * @var string last city name explicitly called |
||
34 | */ |
||
35 | protected $city; |
||
36 | |||
37 | /** |
||
38 | * @var string the last url called |
||
39 | */ |
||
40 | protected $yql; |
||
41 | |||
42 | public function __construct() |
||
46 | |||
47 | /** |
||
48 | * {@inheritdoc} |
||
49 | */ |
||
50 | View Code Duplication | public function callApiWoeid($woeid = null, $unit = 'c') |
|
64 | |||
65 | /** |
||
66 | * {@inheritdoc} |
||
67 | */ |
||
68 | View Code Duplication | public function callApiCityName($city = null, $unit = 'c') |
|
82 | |||
83 | /** |
||
84 | * {@inheritdoc} |
||
85 | */ |
||
86 | public function callApi($yql = null) |
||
113 | |||
114 | /** |
||
115 | * Get lastResponse. |
||
116 | * |
||
117 | * @param bool $toJson choose format for the return value (array or json) |
||
118 | * |
||
119 | * @return array|string |
||
120 | */ |
||
121 | public function getLastResponse($toJson = false) |
||
125 | |||
126 | /** |
||
127 | * Set lastResponse. |
||
128 | * |
||
129 | * @param array $data data from json_encode |
||
130 | */ |
||
131 | public function setLastResponse($data) |
||
135 | |||
136 | /** |
||
137 | * Get current temperature. |
||
138 | * |
||
139 | * @param bool $withUnit return or not the unit |
||
140 | * |
||
141 | * @return string |
||
142 | */ |
||
143 | public function getTemperature($withUnit = false) |
||
155 | |||
156 | /** |
||
157 | * Get Location. |
||
158 | * |
||
159 | * @return string |
||
160 | */ |
||
161 | View Code Duplication | public function getLocation() |
|
169 | |||
170 | /** |
||
171 | * get Forecast. |
||
172 | * |
||
173 | * @return array |
||
174 | */ |
||
175 | View Code Duplication | public function getForecast() |
|
183 | |||
184 | /** |
||
185 | * get Wind. |
||
186 | * |
||
187 | * @param bool $withUnit return or not the unit |
||
188 | * |
||
189 | * @return array |
||
190 | */ |
||
191 | public function getWind($withUnit = false) |
||
209 | |||
210 | /** |
||
211 | * @param GoutteClient $client |
||
212 | */ |
||
213 | public function setClient(Client $client) |
||
217 | |||
218 | /** |
||
219 | * @return string |
||
220 | */ |
||
221 | public function getWoeid() |
||
225 | |||
226 | /** |
||
227 | * @return string |
||
228 | */ |
||
229 | public function getCity() |
||
233 | |||
234 | /** |
||
235 | * @return string |
||
236 | */ |
||
237 | public function getYql() |
||
241 | } |
||
242 |
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.