InotifyTrait::addInotifyProcess()   C
last analyzed

Complexity

Conditions 12
Paths 6

Size

Total Lines 62
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 12
eloc 45
c 1
b 1
f 0
nc 6
nop 3
dl 0
loc 62
rs 6.9666

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
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
0 ignored issues
show
Coding Style introduced by
Missing doc comment for trait InotifyTrait
Loading history...
10
{
11
    public function addInotifyProcess(Server $swoole, array $config, array $laravelConf)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function addInotifyProcess()
Loading history...
12
    {
13
        if (empty($config['enable'])) {
14
            return false;
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 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']));
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,
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
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:
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 24 spaces, found 28
Loading history...
38
                                $action = 'create';
39
                                break;
40
                            case IN_DELETE:
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 24 spaces, found 28
Loading history...
41
                                $action = 'delete';
42
                                break;
43
                            case IN_MODIFY:
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 24 spaces, found 28
Loading history...
44
                                $action = 'modify';
45
                                break;
46
                            case IN_MOVE:
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 24 spaces, found 28
Loading history...
47
                                $action = 'move';
48
                                break;
49
                        }
50
                        $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

50
                        $this->/** @scrutinizer ignore-call */ 
51
                               info(sprintf('reloaded by inotify, reason: %s %s', $action, $event['name']));
Loading history...
51
                    }
52
                });
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 12 spaces, but found 16.
Loading history...
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
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',
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
61
                        $inotify->getWatchedFileCount(),
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 20 spaces, but found 24.
Loading history...
62
                        implode(',', $config['file_types']),
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 20 spaces, but found 24.
Loading history...
63
                        implode(',', $config['excluded_dirs'])
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 20 spaces, but found 24.
Loading history...
64
                    )
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 16 spaces, but found 20.
Loading history...
65
                );
66
            }
67
            $inotify->start();
68
        };
69
70
        $process = new Process($callback, false, 0);
71
        $swoole->addProcess($process);
72
        return $process;
73
    }
74
}