Conditions | 12 |
Paths | 6 |
Total Lines | 62 |
Code Lines | 45 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 1 | 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 |
||
11 | public function addInotifyProcess(Server $swoole, array $config, array $laravelConf) |
||
12 | { |
||
13 | if (empty($config['enable'])) { |
||
14 | return false; |
||
15 | } |
||
16 | |||
17 | if (!extension_loaded('inotify')) { |
||
18 | $this->warning('Require extension inotify'); |
||
19 | return false; |
||
20 | } |
||
21 | |||
22 | $fileTypes = isset($config['file_types']) ? (array)$config['file_types'] : []; |
||
23 | if (empty($fileTypes)) { |
||
24 | $this->warning('No file types to watch by inotify'); |
||
25 | return false; |
||
26 | } |
||
27 | |||
28 | $callback = function () use ($config, $laravelConf) { |
||
29 | $log = !empty($config['log']); |
||
30 | $this->setProcessTitle(sprintf('%s laravels: inotify process', $config['process_prefix'])); |
||
31 | $inotify = new Inotify($config['watch_path'], IN_CREATE | IN_DELETE | IN_MODIFY | IN_MOVE, |
||
32 | function ($event) use ($log, $laravelConf) { |
||
33 | Portal::runLaravelSCommand($laravelConf['root_path'], 'reload'); |
||
34 | if ($log) { |
||
35 | $action = 'file:'; |
||
36 | switch ($event['mask']) { |
||
37 | case IN_CREATE: |
||
38 | $action = 'create'; |
||
39 | break; |
||
40 | case IN_DELETE: |
||
41 | $action = 'delete'; |
||
42 | break; |
||
43 | case IN_MODIFY: |
||
44 | $action = 'modify'; |
||
45 | break; |
||
46 | case IN_MOVE: |
||
47 | $action = 'move'; |
||
48 | break; |
||
49 | } |
||
50 | $this->info(sprintf('reloaded by inotify, reason: %s %s', $action, $event['name'])); |
||
51 | } |
||
52 | }); |
||
53 | $inotify->addFileTypes($config['file_types']); |
||
54 | if (empty($config['excluded_dirs'])) { |
||
55 | $config['excluded_dirs'] = []; |
||
56 | } |
||
57 | $inotify->addExcludedDirs($config['excluded_dirs']); |
||
58 | $inotify->watch(); |
||
59 | if ($log) { |
||
60 | $this->info(sprintf('[Inotify] watched files: %d; file types: %s; excluded directories: %s', |
||
61 | $inotify->getWatchedFileCount(), |
||
62 | implode(',', $config['file_types']), |
||
63 | implode(',', $config['excluded_dirs']) |
||
64 | ) |
||
65 | ); |
||
66 | } |
||
67 | $inotify->start(); |
||
68 | }; |
||
69 | |||
70 | $process = new Process($callback, false, 0); |
||
71 | $swoole->addProcess($process); |
||
72 | return $process; |
||
73 | } |
||
74 | } |