Conditions | 8 |
Paths | 72 |
Total Lines | 66 |
Code Lines | 45 |
Lines | 40 |
Ratio | 60.61 % |
Changes | 1 | ||
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 | |||
28 | $start_vlc = false; |
||
29 | $stream_url=''; |
||
30 | $answer='nothing done'; |
||
31 | |||
32 | $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); |
||
33 | |||
34 | View Code Duplication | if(preg_match(JarvisLanguage::translate('preg_match_relax_music',get_called_class()), $command)) { |
|
35 | $stream_url = 'http://pub4.radiotunes.com:80/radiotunes_relaxation_aacplus.flv'; |
||
36 | JarvisPHP::getLogger()->debug('Playing relaxation'); |
||
37 | $start_vlc = true; |
||
38 | } |
||
39 | View Code Duplication | if(preg_match(JarvisLanguage::translate('preg_match_lounge_music',get_called_class()), $command)) { |
|
40 | $stream_url = 'http://pub4.radiotunes.com:80/radiotunes_smoothlounge_aacplus.flv'; |
||
41 | JarvisPHP::getLogger()->debug('Playing lounge'); |
||
42 | $start_vlc = true; |
||
43 | } |
||
44 | View Code Duplication | if(preg_match(JarvisLanguage::translate('preg_match_jazz_music',get_called_class()), $command)) { |
|
45 | $stream_url = 'http://pub4.radiotunes.com:80/radiotunes_smoothjazz_aacplus.flv'; |
||
46 | JarvisPHP::getLogger()->debug('Playing jazz'); |
||
47 | $start_vlc = true; |
||
48 | } |
||
49 | if(preg_match(JarvisLanguage::translate('preg_match_stop',get_called_class()), $command)) { |
||
50 | $result = @socket_connect($socket, 'localhost', 9999); |
||
51 | View Code Duplication | if ($result === false) { |
|
52 | //Error handling |
||
53 | JarvisPHP::getLogger()->warn('Failed to connect!'); |
||
54 | $answer = 'Failed to connect!'; |
||
55 | } |
||
56 | else { |
||
57 | $vlc_remote_command = 'stop'.chr(13); |
||
58 | $vlc_remote_command = 'quit'.chr(13); |
||
59 | socket_write($socket, $vlc_remote_command, strlen($vlc_remote_command)); |
||
60 | socket_close($socket); |
||
61 | JarvisPHP::getLogger()->debug('Radio stopped'); |
||
62 | $answer = 'Radio stopped'; |
||
63 | } |
||
64 | $start_vlc = false; |
||
65 | } |
||
66 | if($start_vlc) { |
||
67 | //Start Vlc on port 9999 |
||
68 | //exec('vlc -I rc --rc-host localhost:9999'); |
||
69 | @exec('/usr/bin/nohup /usr/bin/vlc -I rc --rc-host localhost:9999 &'); |
||
70 | |||
71 | sleep(1); |
||
72 | |||
73 | $result = @socket_connect($socket, 'localhost', 9999); |
||
74 | View Code Duplication | if ($result === false) { |
|
75 | //Error handling |
||
76 | JarvisPHP::getLogger()->warn('Failed to connect!'); |
||
77 | $answer = 'Failed to connect!'; |
||
78 | } |
||
79 | else { |
||
80 | $vlc_remote_command = 'add '.$stream_url.chr(13); |
||
81 | socket_write($socket, $vlc_remote_command, strlen($vlc_remote_command)); |
||
82 | socket_close($socket); |
||
83 | JarvisPHP::getLogger()->debug('Radio started'); |
||
84 | $answer = 'Radio started'; |
||
85 | } |
||
86 | } |
||
87 | |||
88 | $response = new \JarvisPHP\Core\JarvisResponse($answer, JarvisPHP::getRealClassName(get_called_class()), true); |
||
89 | $response->send(); |
||
90 | |||
91 | } |
||
92 | |||
118 |
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.