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

CustomProcessTrait::addCustomProcesses()   C

Complexity

Conditions 15
Paths 26

Size

Total Lines 84
Code Lines 58

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 15
eloc 58
nc 26
nop 4
dl 0
loc 84
rs 5.9166
c 0
b 0
f 0

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
3
namespace Hhxsv5\LaravelS\Swoole\Process;
4
5
use Hhxsv5\LaravelS\Illuminate\Laravel;
6
use Swoole\Http\Server;
7
use Swoole\Process;
8
9
trait CustomProcessTrait
10
{
11
    public function addCustomProcesses(Server $swoole, $processPrefix, array $processes, array $laravelConfig)
12
    {
13
        $pidfile = dirname($swoole->setting['pid_file']) . '/laravels-custom-processes.pid';
14
        if (file_exists($pidfile)) {
15
            unlink($pidfile);
16
        }
17
18
        /**@var []CustomProcessInterface $processList */
19
        $processList = [];
20
        foreach ($processes as $item) {
21
            if (is_string($item)) {
22
                // Backwards compatible
23
                Laravel::autoload($laravelConfig['root_path']);
24
                $process = $item;
25
                $redirect = $process::isRedirectStdinStdout();
26
                $pipe = $process::getPipeType();
27
            } else {
28
                if (empty($item['class'])) {
29
                    throw new \InvalidArgumentException(sprintf(
30
                            'process class name must be specified'
31
                        )
32
                    );
33
                }
34
                if (isset($item['enable']) && !$item['enable']) {
35
                    continue;
36
                }
37
                $process = $item['class'];
38
                $redirect = isset($item['redirect']) ? $item['redirect'] : false;
39
                $pipe = isset($item['pipe']) ? $item['pipe'] : 0;
40
            }
41
42
            $processHandler = function (Process $worker) use ($pidfile, $swoole, $processPrefix, $process, $laravelConfig) {
43
                file_put_contents($pidfile, $worker->pid . "\n", FILE_APPEND | LOCK_EX);
44
                $this->initLaravel($laravelConfig, $swoole);
0 ignored issues
show
Bug introduced by
It seems like initLaravel() 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

44
                $this->/** @scrutinizer ignore-call */ 
45
                       initLaravel($laravelConfig, $swoole);
Loading history...
45
                if (!isset(class_implements($process)[CustomProcessInterface::class])) {
46
                    throw new \InvalidArgumentException(
47
                        sprintf(
48
                            '%s must implement the interface %s',
49
                            $process,
50
                            CustomProcessInterface::class
51
                        )
52
                    );
53
                }
54
                $name = $process::getName() ?: 'custom';
55
                $this->setProcessTitle(sprintf('%s laravels: %s process', $processPrefix, $name));
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

55
                $this->/** @scrutinizer ignore-call */ 
56
                       setProcessTitle(sprintf('%s laravels: %s process', $processPrefix, $name));
Loading history...
56
57
                Process::signal(SIGUSR1, function ($signo) use ($name, $process, $worker, $pidfile, $swoole) {
0 ignored issues
show
Unused Code introduced by
The parameter $signo is not used and could be removed. ( Ignorable by Annotation )

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

57
                Process::signal(SIGUSR1, function (/** @scrutinizer ignore-unused */ $signo) use ($name, $process, $worker, $pidfile, $swoole) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The import $pidfile is not used and could be removed.

This check looks for imports that have been defined, but are not used in the scope.

Loading history...
58
                    $this->info(sprintf('Reloading the process %s [pid=%d].', $name, $worker->pid));
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

58
                    $this->/** @scrutinizer ignore-call */ 
59
                           info(sprintf('Reloading the process %s [pid=%d].', $name, $worker->pid));
Loading history...
59
                    $process::onReload($swoole, $worker);
60
                });
61
62
                $enableCoroutine = class_exists('Swoole\Coroutine');
63
                $runProcess = function () use ($name, $process, $swoole, $worker, $enableCoroutine) {
64
                    $maxTry = 10;
65
                    $i = 0;
66
                    do {
67
                        $this->callWithCatchException([$process, 'callback'], [$swoole, $worker]);
0 ignored issues
show
Bug introduced by
It seems like callWithCatchException() 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

67
                        $this->/** @scrutinizer ignore-call */ 
68
                               callWithCatchException([$process, 'callback'], [$swoole, $worker]);
Loading history...
68
                        ++$i;
69
                        if ($enableCoroutine) {
70
                            \Swoole\Coroutine::sleep(1);
71
                        } else {
72
                            sleep(1);
73
                        }
74
                    } while ($i < $maxTry);
75
                    $this->error(
0 ignored issues
show
Bug introduced by
It seems like error() 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

75
                    $this->/** @scrutinizer ignore-call */ 
76
                           error(
Loading history...
76
                        sprintf(
77
                            'The custom process "%s" reaches the maximum number of retries %d times, and will be restarted by the manager process.',
78
                            $name,
79
                            $maxTry
80
                        )
81
                    );
82
                };
83
                $enableCoroutine ? go($runProcess) : $runProcess();
84
            };
85
            $customProcess = new Process(
86
                $processHandler,
87
                $redirect,
88
                $pipe
89
            );
90
            if ($swoole->addProcess($customProcess)) {
0 ignored issues
show
Bug introduced by
Are you sure the usage of $swoole->addProcess($customProcess) 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...
91
                $processList[] = $customProcess;
92
            }
93
        }
94
        return $processList;
95
    }
96
}
97