Conditions | 13 |
Paths | 8 |
Total Lines | 63 |
Code Lines | 46 |
Lines | 0 |
Ratio | 0 % |
Changes | 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; |
||
15 | } |
||
16 | |||
17 | if (!extension_loaded('inotify')) { |
||
18 | $this->warning('Require extension inotify'); |
||
19 | return; |
||
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; |
||
26 | } |
||
27 | |||
28 | $autoReload = 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 | $reloadCmd = trim(sprintf('%s -c "%s" %s/bin/laravels reload', PHP_BINARY, php_ini_loaded_file(), $laravelConf['root_path'])); |
||
34 | Portal::runCommand($reloadCmd); |
||
35 | if ($log) { |
||
36 | $action = 'file:'; |
||
37 | switch ($event['mask']) { |
||
38 | case IN_CREATE: |
||
39 | $action = 'create'; |
||
40 | break; |
||
41 | case IN_DELETE: |
||
42 | $action = 'delete'; |
||
43 | break; |
||
44 | case IN_MODIFY: |
||
45 | $action = 'modify'; |
||
46 | break; |
||
47 | case IN_MOVE: |
||
48 | $action = 'move'; |
||
49 | break; |
||
50 | } |
||
51 | $this->info(sprintf('reloaded by inotify, reason: %s %s', $action, $event['name'])); |
||
52 | } |
||
53 | }); |
||
54 | $inotify->addFileTypes($config['file_types']); |
||
55 | if (empty($config['excluded_dirs'])) { |
||
56 | $config['excluded_dirs'] = []; |
||
57 | } |
||
58 | $inotify->addExcludedDirs($config['excluded_dirs']); |
||
59 | $inotify->watch(); |
||
60 | if ($log) { |
||
61 | $this->info(sprintf('[Inotify] watched files: %d; file types: %s; excluded directories: %s', |
||
62 | $inotify->getWatchedFileCount(), |
||
63 | implode(',', $config['file_types']), |
||
64 | implode(',', $config['excluded_dirs']) |
||
65 | ) |
||
66 | ); |
||
67 | } |
||
68 | $inotify->start(); |
||
69 | }; |
||
70 | |||
71 | $process = new Process($autoReload, false, false); |
||
72 | if ($swoole->addProcess($process)) { |
||
73 | return $process; |
||
74 | } |
||
76 | } |
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: