Conditions | 10 |
Paths | 24 |
Total Lines | 54 |
Code Lines | 35 |
Lines | 12 |
Ratio | 22.22 % |
Changes | 3 | ||
Bugs | 0 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
29 | function answer($command) { |
||
30 | $answer = ''; |
||
31 | JarvisPHP::getLogger()->debug('Answering to command: "'.$command.'"'); |
||
32 | |||
33 | $owm = new OpenWeatherMap(); |
||
34 | |||
35 | $tomorrow = new \DateTime(); |
||
36 | $tomorrow->modify('+1 day'); |
||
37 | |||
38 | $_OPENWEATHERMAP_API_KEY = ''; |
||
39 | |||
40 | //Load API key from json config |
||
41 | if(file_exists('Plugins/Weather_plugin/api-key.json')) { |
||
42 | //Create your own api key and put it in api-key.json |
||
43 | $json_config = json_decode(file_get_contents('Plugins/Weather_plugin/api-key.json')); |
||
44 | $_OPENWEATHERMAP_API_KEY = $json_config->openweathermap_key; |
||
45 | } |
||
46 | |||
47 | try { |
||
48 | $forecast = $owm->getWeatherForecast('Rimini', 'metric', _LANGUAGE, $_OPENWEATHERMAP_API_KEY,2); |
||
49 | } catch(OWMException $e) { |
||
50 | JarvisPHP::getLogger()->error('OpenWeatherMap exception: ' . $e->getMessage() . ' (Code ' . $e->getCode() . ').'); |
||
51 | $answer = JarvisLanguage::translate('weather_error',get_called_class()); |
||
52 | } catch(\Exception $e) { |
||
53 | JarvisPHP::getLogger()->error('General exception: ' . $e->getMessage() . ' (Code ' . $e->getCode() . ').'); |
||
54 | $answer = JarvisLanguage::translate('weather_error',get_called_class()); |
||
55 | } |
||
56 | |||
57 | if(preg_match(JarvisLanguage::translate('preg_match_today',get_called_class()), $command)) { |
||
58 | $answer = JarvisLanguage::translate('forecast_for_today',get_called_class()); |
||
59 | View Code Duplication | foreach ($forecast as $weather) { |
|
60 | if($weather->time->day->format('d.m.Y')==date('d.m.Y')) { |
||
61 | $answer.=JarvisLanguage::translate('from',get_called_class()) . " " .$weather->time->from->format('H:i') . " ". JarvisLanguage::translate('to',get_called_class()) . " " . $weather->time->to->format('H:i').": "; |
||
62 | $answer.=$weather->weather->description." ".sprintf(JarvisLanguage::translate('temperature',get_called_class()),trim(str_replace('°C','',$weather->temperature))); |
||
63 | } |
||
64 | } |
||
65 | } else if(preg_match(JarvisLanguage::translate('preg_match_tomorrow',get_called_class()), $command)) { |
||
66 | $answer = JarvisLanguage::translate('forecast_for_tomorrow',get_called_class()); |
||
67 | View Code Duplication | foreach ($forecast as $weather) { |
|
68 | if($weather->time->day->format('d.m.Y')==$tomorrow->format('d.m.Y')) { |
||
69 | $answer.=JarvisLanguage::translate('from',get_called_class()) . " " .$weather->time->from->format('H:i') . " ". JarvisLanguage::translate('to',get_called_class()) . " " . $weather->time->to->format('H:i').": "; |
||
70 | $answer.=$weather->weather->description." ".sprintf(JarvisLanguage::translate('temperature',get_called_class()),trim(str_replace('°C','',$weather->temperature))); |
||
71 | } |
||
72 | } |
||
73 | } else { |
||
74 | $answer = JarvisLanguage::translate('not_understand_weather_day',get_called_class()); |
||
75 | } |
||
76 | |||
77 | |||
78 | |||
79 | JarvisTTS::speak($answer); |
||
80 | $response = new \JarvisPHP\Core\JarvisResponse($answer, JarvisPHP::getRealClassName(get_called_class()), true); |
||
81 | $response->send(); |
||
82 | } |
||
83 | |||
109 |
The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using
the property is implicitly global.
To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.