Completed
Push — master ( 1915ce...cbef56 )
by Biao
10:02
created

InotifyTrait   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 47
dl 0
loc 65
rs 10
c 0
b 0
f 0
wmc 13

1 Method

Rating   Name   Duplication   Size   Complexity  
C addInotifyProcess() 0 63 13
1
<?php
2
3
namespace Hhxsv5\LaravelS\Swoole;
4
5
use Hhxsv5\LaravelS\Console\Portal;
6
use Swoole\Http\Server;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Hhxsv5\LaravelS\Swoole\Server. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/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 before OtherDir/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:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
7
use Swoole\Process;
8
9
trait InotifyTrait
10
{
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');
0 ignored issues
show
Bug introduced by
It seems like warning() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

18
            $this->/** @scrutinizer ignore-call */ 
19
                   warning('Require extension inotify');
Loading history...
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']));
0 ignored issues
show
Bug introduced by
It seems like setProcessTitle() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

30
            $this->/** @scrutinizer ignore-call */ 
31
                   setProcessTitle(sprintf('%s laravels: inotify process', $config['process_prefix']));
Loading history...
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']));
0 ignored issues
show
Bug introduced by
It seems like info() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

51
                        $this->/** @scrutinizer ignore-call */ 
52
                               info(sprintf('reloaded by inotify, reason: %s %s', $action, $event['name']));
Loading history...
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)) {
0 ignored issues
show
Bug introduced by
Are you sure the usage of $swoole->addProcess($process) targeting Swoole\Server::addProcess() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
73
            return $process;
74
        }
75
    }
76
}