Passed
Pull Request — PHP-7.x (#489)
by
unknown
03:52
created

ChokidarWatchTrait::addChokidarWatchProcess()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 38
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 24
c 1
b 0
f 0
nc 5
nop 3
dl 0
loc 38
rs 8.9137
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
use Symfony\Component\Process\ExecutableFinder;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Process\ExecutableFinder was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
10
trait ChokidarWatchTrait
0 ignored issues
show
Coding Style introduced by
Missing doc comment for trait ChokidarWatchTrait
Loading history...
11
{
12
    public function addChokidarWatchProcess(Server $swoole, array $config, array $laravelConf)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function addChokidarWatchProcess()
Loading history...
13
    {
14
        if (empty($config['enable'])) {
15
            return false;
16
        }
17
18
        $watchPathFiles = isset($config['watch_path_files']) ? (array)$config['watch_path_files'] : [];
19
        if (empty($watchPathFiles)) {
20
            $this->warning('No file to watch by chokidar');
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

20
            $this->/** @scrutinizer ignore-call */ 
21
                   warning('No file to watch by chokidar');
Loading history...
21
            return false;
22
        }
23
24
        $callback = function (Process $worker) use ($config, $laravelConf, $watchPathFiles) {
25
            $log = !empty($config['log']);
26
            $watch_base_path = $config['watch_base_path'];
27
            $this->setProcessTitle(sprintf('%s laravels: chokidar 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

27
            $this->/** @scrutinizer ignore-call */ 
28
                   setProcessTitle(sprintf('%s laravels: chokidar process', $config['process_prefix']));
Loading history...
28
            $nodeExecutable = (new ExecutableFinder)->find('node');
29
            $nodeScriptPath = realpath(__DIR__.'/../../bin/file-watcher.cjs');
30
            $this->info(sprintf('$nodeScriptPath: %s', $nodeScriptPath));
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

30
            $this->/** @scrutinizer ignore-call */ 
31
                   info(sprintf('$nodeScriptPath: %s', $nodeScriptPath));
Loading history...
31
32
            $watchOptions = isset($config['watch_options']) ? (array)$config['watch_options'] : ['ignoreInitial' => true];
33
            $worker->exec($nodeExecutable, [$nodeScriptPath,
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...
34
                json_encode(collect($watchPathFiles)->map(fn ($path) => $watch_base_path.'/'.$path)),
0 ignored issues
show
Bug introduced by
The function collect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

34
                json_encode(/** @scrutinizer ignore-call */ collect($watchPathFiles)->map(fn ($path) => $watch_base_path.'/'.$path)),
Loading history...
35
                json_encode($watchOptions),
36
            ]);
0 ignored issues
show
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...
37
38
            // 获取 Node.js 脚本的输出
39
            tap($worker->read(), function ($output) use ($log, $laravelConf) {
0 ignored issues
show
Bug introduced by
The function tap was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

39
            /** @scrutinizer ignore-call */ 
40
            tap($worker->read(), function ($output) use ($log, $laravelConf) {
Loading history...
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
40
                Portal::runLaravelSCommand($laravelConf['root_path'], 'reload');
41
                if ($log) {
42
                    $this->info(sprintf('reloaded by chokidar, reason: %s', $output));
43
                }
44
            });
0 ignored issues
show
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...
45
        };
46
47
        $process = new Process($callback, false, 0);
48
        $swoole->addProcess($process);
49
        return $process;
50
    }
51
}
52