Conditions | 3 |
Paths | 4 |
Total Lines | 51 |
Code Lines | 38 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
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 |
||
26 | function answer($command) { |
||
27 | $answer = ''; |
||
28 | JarvisPHP::getLogger()->debug('Answering to command: "'.$command.'"'); |
||
29 | |||
30 | $_IFTTT_MAKER_KEY = ''; |
||
31 | $_IFTTT_MAKER_EVENT = ''; |
||
32 | |||
33 | //Load API key from json config |
||
34 | if(file_exists('Plugins/Wemo_plugin/api-key.json')) { |
||
35 | //Create your own api key and put it in api-key.json |
||
36 | $json_config = json_decode(file_get_contents('Plugins/Wemo_plugin/api-key.json')); |
||
37 | $_IFTTT_MAKER_KEY = $json_config->ifttt_key; |
||
38 | $_IFTTT_MAKER_EVENT = $json_config->ifttt_event; |
||
39 | } |
||
40 | |||
41 | //Toggle switch via curl |
||
42 | $ch = curl_init(); |
||
43 | curl_setopt($ch, CURLOPT_URL, 'https://maker.ifttt.com/trigger/'.$_IFTTT_MAKER_EVENT.'/with/key/'.$_IFTTT_MAKER_KEY); |
||
44 | curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:42.0) Gecko/20100101 Firefox/42.0'); |
||
45 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
||
46 | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
||
47 | curl_setopt($ch, CURLOPT_FAILONERROR, true); |
||
48 | curl_setopt($ch, CURLOPT_VERBOSE, TRUE); |
||
49 | curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS); |
||
50 | curl_setopt($ch, CURLOPT_POST, 1); |
||
51 | $headers = array(); |
||
52 | $headers[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'; |
||
53 | $headers[] = 'Accept-Encoding: gzip, deflate'; |
||
54 | $headers[] = 'Accept-Language: it-IT,it;q=0.8,en-US;q=0.5,en;q=0.3'; |
||
55 | $headers[] = 'Cache-Control: no-cache'; |
||
56 | $headers[] = 'Host: maker.ifttt.com'; |
||
57 | curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); |
||
58 | curl_setopt($ch, CURLOPT_POSTFIELDS, array()); |
||
59 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); |
||
60 | curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); |
||
61 | $result = curl_exec($ch); |
||
62 | |||
63 | if(!$result) { |
||
64 | JarvisPHP::getLogger()->error('Curl error: '.curl_error($ch)); |
||
65 | $answer = JarvisLanguage::translate('command_not_sent',get_called_class()); |
||
66 | } |
||
67 | else { |
||
68 | $answer = JarvisLanguage::translate('command_sent_to_light_switch',get_called_class()); |
||
69 | } |
||
70 | |||
71 | curl_close($ch); |
||
72 | |||
73 | JarvisTTS::speak($answer); |
||
74 | $response = new \JarvisPHP\Core\JarvisResponse($answer, JarvisPHP::getRealClassName(get_called_class()), true); |
||
75 | $response->send(); |
||
76 | } |
||
77 | |||
103 |
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.