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:
Complex classes like OpenWeatherMap often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use OpenWeatherMap, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
36 | class OpenWeatherMap |
||
37 | { |
||
38 | /** |
||
39 | * The copyright notice. This is no official text, it was created by |
||
40 | * following the guidelines at http://openweathermap.org/copyright. |
||
41 | * |
||
42 | * @var string $copyright |
||
43 | */ |
||
44 | const COPYRIGHT = "Weather data from <a href=\"http://openweathermap.org\">OpenWeatherMap.org</a>"; |
||
45 | |||
46 | /** |
||
47 | * @var string The basic api url to fetch weather data from. |
||
48 | */ |
||
49 | private $weatherUrl = 'http://api.openweathermap.org/data/2.5/weather?'; |
||
50 | |||
51 | /** |
||
52 | * @var string The basic api url to fetch weather group data from. |
||
53 | */ |
||
54 | private $weatherGroupUrl = 'http://api.openweathermap.org/data/2.5/group?'; |
||
55 | |||
56 | /** |
||
57 | * @var string The basic api url to fetch weekly forecast data from. |
||
58 | */ |
||
59 | private $weatherHourlyForecastUrl = 'http://api.openweathermap.org/data/2.5/forecast?'; |
||
60 | |||
61 | /** |
||
62 | * @var string The basic api url to fetch daily forecast data from. |
||
63 | */ |
||
64 | private $weatherDailyForecastUrl = 'http://api.openweathermap.org/data/2.5/forecast/daily?'; |
||
65 | |||
66 | /** |
||
67 | * @var string The basic api url to fetch history weather data from. |
||
68 | */ |
||
69 | private $weatherHistoryUrl = 'http://history.openweathermap.org/data/2.5/history/city?'; |
||
70 | |||
71 | /** |
||
72 | * @var string The basic api url to fetch current uv data from. |
||
73 | */ |
||
74 | private $uviUrl = 'http://api.openweathermap.org/v3/uvi/%s,%s/current.json?'; |
||
75 | |||
76 | /** |
||
77 | * @var string The basic api url to fetch current uv data from. |
||
78 | */ |
||
79 | private $uviHistoryUrl = 'http://api.openweathermap.org/v3/uvi/%s,%s/%s.json?'; |
||
80 | |||
81 | /** |
||
82 | * @var AbstractCache|bool $cache The cache to use. |
||
83 | */ |
||
84 | private $cache = false; |
||
85 | |||
86 | /** |
||
87 | * @var int |
||
88 | */ |
||
89 | private $seconds; |
||
90 | |||
91 | /** |
||
92 | * @var bool |
||
93 | */ |
||
94 | private $wasCached = false; |
||
95 | |||
96 | /** |
||
97 | * @var FetcherInterface The url fetcher. |
||
98 | */ |
||
99 | private $fetcher; |
||
100 | |||
101 | /** |
||
102 | * @var string |
||
103 | */ |
||
104 | private $apiKey = ''; |
||
105 | |||
106 | /** |
||
107 | * Constructs the OpenWeatherMap object. |
||
108 | * |
||
109 | * @param string $apiKey The OpenWeatherMap API key. Required and only optional for BC. |
||
110 | * @param null|FetcherInterface $fetcher The interface to fetch the data from OpenWeatherMap. Defaults to |
||
111 | * CurlFetcher() if cURL is available. Otherwise defaults to |
||
112 | * FileGetContentsFetcher() using 'file_get_contents()'. |
||
113 | * @param bool|string $cache If set to false, caching is disabled. Otherwise this must be a class |
||
114 | * extending AbstractCache. Defaults to false. |
||
115 | * @param int $seconds How long weather data shall be cached. Default 10 minutes. |
||
116 | * |
||
117 | * @throws \Exception If $cache is neither false nor a valid callable extending Cmfcmf\OpenWeatherMap\Util\Cache. |
||
118 | * |
||
119 | * @api |
||
120 | */ |
||
121 | public function __construct($apiKey = '', $fetcher = null, $cache = false, $seconds = 600) |
||
149 | |||
150 | /** |
||
151 | * Sets the API Key. |
||
152 | * |
||
153 | * @param string $apiKey API key for the OpenWeatherMap account. |
||
154 | * |
||
155 | * @api |
||
156 | */ |
||
157 | public function setApiKey($apiKey) |
||
161 | |||
162 | /** |
||
163 | * Returns the API Key. |
||
164 | * |
||
165 | * @return string |
||
166 | * |
||
167 | * @api |
||
168 | */ |
||
169 | public function getApiKey() |
||
173 | |||
174 | /** |
||
175 | * Returns the current weather at the place you specified. |
||
176 | * |
||
177 | * @param array|int|string $query The place to get weather information for. For possible values see below. |
||
178 | * @param string $units Can be either 'metric' or 'imperial' (default). This affects almost all units returned. |
||
179 | * @param string $lang The language to use for descriptions, default is 'en'. For possible values see http://openweathermap.org/current#multi. |
||
180 | * @param string $appid Your app id, default ''. See http://openweathermap.org/appid for more details. |
||
181 | * |
||
182 | * @throws OpenWeatherMap\Exception If OpenWeatherMap returns an error. |
||
183 | * @throws \InvalidArgumentException If an argument error occurs. |
||
184 | * |
||
185 | * @return CurrentWeather The weather object. |
||
186 | * |
||
187 | * There are three ways to specify the place to get weather information for: |
||
188 | * - Use the city name: $query must be a string containing the city name. |
||
189 | * - Use the city id: $query must be an integer containing the city id. |
||
190 | * - Use the coordinates: $query must be an associative array containing the 'lat' and 'lon' values. |
||
191 | * |
||
192 | * @api |
||
193 | */ |
||
194 | public function getWeather($query, $units = 'imperial', $lang = 'en', $appid = '') |
||
201 | |||
202 | /** |
||
203 | * Returns the current weather for a group of city ids. |
||
204 | * |
||
205 | * @param array $ids The city ids to get weather information for |
||
206 | * @param string $units Can be either 'metric' or 'imperial' (default). This affects almost all units returned. |
||
207 | * @param string $lang The language to use for descriptions, default is 'en'. For possible values see http://openweathermap.org/current#multi. |
||
208 | * @param string $appid Your app id, default ''. See http://openweathermap.org/appid for more details. |
||
209 | * |
||
210 | * @throws OpenWeatherMap\Exception If OpenWeatherMap returns an error. |
||
211 | * @throws \InvalidArgumentException If an argument error occurs. |
||
212 | * |
||
213 | * @return CurrentWeatherGroup |
||
214 | * |
||
215 | * @api |
||
216 | */ |
||
217 | public function getWeatherGroup($ids, $units = 'imperial', $lang = 'en', $appid = '') |
||
224 | |||
225 | /** |
||
226 | * Returns the forecast for the place you specified. DANGER: Might return |
||
227 | * fewer results than requested due to a bug in the OpenWeatherMap API! |
||
228 | * |
||
229 | * @param array|int|string $query The place to get weather information for. For possible values see ::getWeather. |
||
230 | * @param string $units Can be either 'metric' or 'imperial' (default). This affects almost all units returned. |
||
231 | * @param string $lang The language to use for descriptions, default is 'en'. For possible values see http://openweathermap.org/current#multi. |
||
232 | * @param string $appid Your app id, default ''. See http://openweathermap.org/appid for more details. |
||
233 | * @param int $days For how much days you want to get a forecast. Default 1, maximum: 16. |
||
234 | * |
||
235 | * @throws OpenWeatherMap\Exception If OpenWeatherMap returns an error. |
||
236 | * @throws \InvalidArgumentException If an argument error occurs. |
||
237 | * |
||
238 | * @return WeatherForecast |
||
239 | * |
||
240 | * @api |
||
241 | */ |
||
242 | public function getWeatherForecast($query, $units = 'imperial', $lang = 'en', $appid = '', $days = 1) |
||
255 | |||
256 | /** |
||
257 | * Returns the DAILY forecast for the place you specified. DANGER: Might return |
||
258 | * fewer results than requested due to a bug in the OpenWeatherMap API! |
||
259 | * |
||
260 | * @param array|int|string $query The place to get weather information for. For possible values see ::getWeather. |
||
261 | * @param string $units Can be either 'metric' or 'imperial' (default). This affects almost all units returned. |
||
262 | * @param string $lang The language to use for descriptions, default is 'en'. For possible values see http://openweathermap.org/current#multi. |
||
263 | * @param string $appid Your app id, default ''. See http://openweathermap.org/appid for more details. |
||
264 | * @param int $days For how much days you want to get a forecast. Default 1, maximum: 16. |
||
265 | * |
||
266 | * @throws OpenWeatherMap\Exception If OpenWeatherMap returns an error. |
||
267 | * @throws \InvalidArgumentException If an argument error occurs. |
||
268 | * |
||
269 | * @return WeatherForecast |
||
270 | * |
||
271 | * @api |
||
272 | */ |
||
273 | public function getDailyWeatherForecast($query, $units = 'imperial', $lang = 'en', $appid = '', $days = 1) |
||
283 | |||
284 | /** |
||
285 | * Returns the weather history for the place you specified. |
||
286 | * |
||
287 | * @param array|int|string $query The place to get weather information for. For possible values see ::getWeather. |
||
288 | * @param \DateTime $start |
||
289 | * @param int $endOrCount |
||
290 | * @param string $type Can either be 'tick', 'hour' or 'day'. |
||
291 | * @param string $units Can be either 'metric' or 'imperial' (default). This affects almost all units returned. |
||
292 | * @param string $lang The language to use for descriptions, default is 'en'. For possible values see http://openweathermap.org/current#multi. |
||
293 | * @param string $appid Your app id, default ''. See http://openweathermap.org/appid for more details. |
||
294 | * |
||
295 | * @throws OpenWeatherMap\Exception If OpenWeatherMap returns an error. |
||
296 | * @throws \InvalidArgumentException If an argument error occurs. |
||
297 | * |
||
298 | * @return WeatherHistory |
||
299 | * |
||
300 | * @api |
||
301 | */ |
||
302 | public function getWeatherHistory($query, \DateTime $start, $endOrCount = 1, $type = 'hour', $units = 'imperial', $lang = 'en', $appid = '') |
||
316 | |||
317 | /** |
||
318 | * Directly returns the xml/json/html string returned by OpenWeatherMap for the current weather. |
||
319 | * |
||
320 | * @param array|int|string $query The place to get weather information for. For possible values see ::getWeather. |
||
321 | * @param string $units Can be either 'metric' or 'imperial' (default). This affects almost all units returned. |
||
322 | * @param string $lang The language to use for descriptions, default is 'en'. For possible values see http://openweathermap.org/current#multi. |
||
323 | * @param string $appid Your app id, default ''. See http://openweathermap.org/appid for more details. |
||
324 | * @param string $mode The format of the data fetched. Possible values are 'json', 'html' and 'xml' (default). |
||
325 | * |
||
326 | * @return string Returns false on failure and the fetched data in the format you specified on success. |
||
327 | * |
||
328 | * Warning: If an error occurs, OpenWeatherMap ALWAYS returns json data. |
||
329 | * |
||
330 | * @api |
||
331 | */ |
||
332 | public function getRawWeatherData($query, $units = 'imperial', $lang = 'en', $appid = '', $mode = 'xml') |
||
338 | |||
339 | /** |
||
340 | * Directly returns the JSON string returned by OpenWeatherMap for the group of current weather. |
||
341 | * Only a JSON response format is supported for this webservice. |
||
342 | * |
||
343 | * @param array $ids The city ids to get weather information for |
||
344 | * @param string $units Can be either 'metric' or 'imperial' (default). This affects almost all units returned. |
||
345 | * @param string $lang The language to use for descriptions, default is 'en'. For possible values see http://openweathermap.org/current#multi. |
||
346 | * @param string $appid Your app id, default ''. See http://openweathermap.org/appid for more details. |
||
347 | * |
||
348 | * @return string Returns false on failure and the fetched data in the format you specified on success. |
||
349 | * |
||
350 | * @api |
||
351 | */ |
||
352 | public function getRawWeatherGroupData($ids, $units = 'imperial', $lang = 'en', $appid = '') |
||
358 | |||
359 | /** |
||
360 | * Directly returns the xml/json/html string returned by OpenWeatherMap for the hourly forecast. |
||
361 | * |
||
362 | * @param array|int|string $query The place to get weather information for. For possible values see ::getWeather. |
||
363 | * @param string $units Can be either 'metric' or 'imperial' (default). This affects almost all units returned. |
||
364 | * @param string $lang The language to use for descriptions, default is 'en'. For possible values see http://openweathermap.org/current#multi. |
||
365 | * @param string $appid Your app id, default ''. See http://openweathermap.org/appid for more details. |
||
366 | * @param string $mode The format of the data fetched. Possible values are 'json', 'html' and 'xml' (default). |
||
367 | * |
||
368 | * @return string Returns false on failure and the fetched data in the format you specified on success. |
||
369 | * |
||
370 | * Warning: If an error occurs, OpenWeatherMap ALWAYS returns json data. |
||
371 | * |
||
372 | * @api |
||
373 | */ |
||
374 | public function getRawHourlyForecastData($query, $units = 'imperial', $lang = 'en', $appid = '', $mode = 'xml') |
||
380 | |||
381 | /** |
||
382 | * Directly returns the xml/json/html string returned by OpenWeatherMap for the daily forecast. |
||
383 | * |
||
384 | * @param array|int|string $query The place to get weather information for. For possible values see ::getWeather. |
||
385 | * @param string $units Can be either 'metric' or 'imperial' (default). This affects almost all units returned. |
||
386 | * @param string $lang The language to use for descriptions, default is 'en'. For possible values see http://openweathermap.org/current#multi. |
||
387 | * @param string $appid Your app id, default ''. See http://openweathermap.org/appid for more details. |
||
388 | * @param string $mode The format of the data fetched. Possible values are 'json', 'html' and 'xml' (default) |
||
389 | * @param int $cnt How many days of forecast shall be returned? Maximum (and default): 16 |
||
390 | * |
||
391 | * @throws \InvalidArgumentException If $cnt is higher than 16. |
||
392 | * |
||
393 | * @return string Returns false on failure and the fetched data in the format you specified on success. |
||
394 | * |
||
395 | * Warning: If an error occurs, OpenWeatherMap ALWAYS returns json data. |
||
396 | * |
||
397 | * @api |
||
398 | */ |
||
399 | public function getRawDailyForecastData($query, $units = 'imperial', $lang = 'en', $appid = '', $mode = 'xml', $cnt = 16) |
||
408 | |||
409 | /** |
||
410 | * Directly returns the xml/json/html string returned by OpenWeatherMap for the weather history. |
||
411 | * |
||
412 | * @param array|int|string $query The place to get weather information for. For possible values see ::getWeather. |
||
413 | * @param \DateTime $start The \DateTime object of the date to get the first weather information from. |
||
414 | * @param \DateTime|int $endOrCount Can be either a \DateTime object representing the end of the period to |
||
415 | * receive weather history data for or an integer counting the number of |
||
416 | * reports requested. |
||
417 | * @param string $type The period of the weather history requested. Can be either be either "tick", |
||
418 | * "hour" or "day". |
||
419 | * @param string $units Can be either 'metric' or 'imperial' (default). This affects almost all units returned. |
||
420 | * @param string $lang The language to use for descriptions, default is 'en'. For possible values see http://openweathermap.org/current#multi. |
||
421 | * @param string $appid Your app id, default ''. See http://openweathermap.org/appid for more details. |
||
422 | * |
||
423 | * @throws \InvalidArgumentException |
||
424 | * |
||
425 | * @return string Returns false on failure and the fetched data in the format you specified on success. |
||
426 | * |
||
427 | * Warning If an error occurred, OpenWeatherMap ALWAYS returns data in json format. |
||
428 | * |
||
429 | * @api |
||
430 | */ |
||
431 | public function getRawWeatherHistory($query, \DateTime $start, $endOrCount = 1, $type = 'hour', $units = 'imperial', $lang = 'en', $appid = '') |
||
449 | |||
450 | /** |
||
451 | * Directly returns the json string returned by OpenWeatherMap for the UVI data. |
||
452 | * |
||
453 | * @param array $query The place to get information as follows: [latitude, longitude, date time]. For possible values see ::getWeather. |
||
454 | * @param string $appid Your app id, default ''. See http://openweathermap.org/appid for more details. |
||
455 | * |
||
456 | * @throws \InvalidArgumentException |
||
457 | * |
||
458 | * @return string Returns false on failure and the fetched data in the format you specified on success. |
||
459 | * |
||
460 | * Warning If an error occurred, OpenWeatherMap ALWAYS returns data in json format. |
||
461 | * |
||
462 | * @api |
||
463 | */ |
||
464 | View Code Duplication | public function getRawUviData($query, $appid = '') |
|
476 | |||
477 | /** |
||
478 | * Directly returns the json string returned by OpenWeatherMap for the UVI history data. |
||
479 | * |
||
480 | * @param array|int|string $query The place to get weather information for. For possible values see ::getWeather. |
||
481 | * @param string $appid Your app id, default ''. See http://openweathermap.org/appid for more details. |
||
482 | * |
||
483 | * @throws \InvalidArgumentException |
||
484 | * |
||
485 | * @return string Returns false on failure and the fetched data in the format you specified on success. |
||
486 | * |
||
487 | * Warning If an error occurred, OpenWeatherMap ALWAYS returns data in json format. |
||
488 | * |
||
489 | * @api |
||
490 | */ |
||
491 | View Code Duplication | public function getRawUviHistory($query, $appid = '') |
|
503 | |||
504 | /** |
||
505 | * Returns the current uvi at the location you specified. |
||
506 | * |
||
507 | * @param array|int|string $query The place to get weather information for. For possible values see below. |
||
508 | * @param string $appid Your app id, default ''. See http://openweathermap.org/appid for more details. |
||
509 | * |
||
510 | * @throws OpenWeatherMap\Exception If OpenWeatherMap returns an error. |
||
511 | * @throws \InvalidArgumentException If an argument error occurs. |
||
512 | * |
||
513 | * @return CurrentUvi The uvi object. |
||
514 | * |
||
515 | * There are three ways to specify the place to get weather information for: |
||
516 | * - Use the coordinates: $query must be an associative array containing the 'lat' and 'lon' values. |
||
517 | * |
||
518 | * @api |
||
519 | */ |
||
520 | View Code Duplication | public function getUvi($query, $appid = '') |
|
527 | |||
528 | /** |
||
529 | * Returns the history uvi at the location you specified. |
||
530 | * |
||
531 | * @param array|int|string $query The place to get weather information for. For possible values see below. |
||
532 | * @param string $appid Your app id, default ''. See http://openweathermap.org/appid for more details. |
||
533 | * @param string $dateTime Your date time, default ''. See http://openweathermap.org/api/uvi for more details about date format. |
||
534 | * |
||
535 | * @throws OpenWeatherMap\Exception If OpenWeatherMap returns an error. |
||
536 | * @throws \InvalidArgumentException If an argument error occurs. |
||
537 | * |
||
538 | * @return CurrentUvi The uvi object. |
||
539 | * |
||
540 | * There are three ways to specify the place to get weather information for: |
||
541 | * - Use the coordinates: $query must be an associative array containing the 'lat' and 'lon' values. |
||
542 | * |
||
543 | * @api |
||
544 | */ |
||
545 | View Code Duplication | public function getUviHistory($query, $appid = '') |
|
552 | |||
553 | /** |
||
554 | * Returns whether or not the last result was fetched from the cache. |
||
555 | * |
||
556 | * @return bool true if last result was fetched from cache, false otherwise. |
||
557 | */ |
||
558 | public function wasCached() |
||
562 | |||
563 | /** |
||
564 | * @deprecated Use {@link self::getRawWeatherData()} instead. |
||
565 | */ |
||
566 | public function getRawData($query, $units = 'imperial', $lang = 'en', $appid = '', $mode = 'xml') |
||
570 | |||
571 | /** |
||
572 | * Fetches the result or delivers a cached version of the result. |
||
573 | * |
||
574 | * @param string $url |
||
575 | * |
||
576 | * @return string |
||
577 | */ |
||
578 | private function cacheOrFetchResult($url) |
||
597 | |||
598 | /** |
||
599 | * Build the url to fetch weather data from. |
||
600 | * |
||
601 | * @param $query |
||
602 | * @param $units |
||
603 | * @param $lang |
||
604 | * @param $appid |
||
605 | * @param $mode |
||
606 | * @param string $url The url to prepend. |
||
607 | * |
||
608 | * @return bool|string The fetched url, false on failure. |
||
609 | */ |
||
610 | private function buildUrl($query, $units, $lang, $appid, $mode, $url) |
||
619 | |||
620 | /** |
||
621 | * Build the url to fetch UVI data from. |
||
622 | * |
||
623 | * @param $query |
||
624 | * @param $units |
||
625 | * @param $lang |
||
626 | * @param $appid |
||
627 | * @param $mode |
||
628 | * @param string $url The url to prepend. |
||
629 | * |
||
630 | * @return bool|string The fetched url, false on failure. |
||
631 | */ |
||
632 | private function buildUviUrl($query, $appid) |
||
647 | |||
648 | /** |
||
649 | * Builds the query string for the url. |
||
650 | * |
||
651 | * @param mixed $query |
||
652 | * |
||
653 | * @return string The built query string for the url. |
||
654 | * |
||
655 | * @throws \InvalidArgumentException If the query parameter is invalid. |
||
656 | */ |
||
657 | private function buildQueryUrlParameter($query) |
||
672 | |||
673 | /** |
||
674 | * @param string $answer The content returned by OpenWeatherMap. |
||
675 | * |
||
676 | * @return \SimpleXMLElement |
||
677 | * @throws OWMException If the content isn't valid XML. |
||
678 | */ |
||
679 | private function parseXML($answer) |
||
697 | |||
698 | /** |
||
699 | * @param string $answer The content returned by OpenWeatherMap. |
||
700 | * |
||
701 | * @return \stdClass |
||
702 | * @throws OWMException If the content isn't valid JSON. |
||
703 | */ |
||
704 | private function parseJson($answer) |
||
713 | |||
714 | private function json_last_error_msg() |
||
732 | } |
||
733 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.