Completed
Push — master ( 002bf2...7cfd63 )
by Biao
03:30
created

CustomProcessTrait::addCustomProcesses()   C

Complexity

Conditions 12
Paths 12

Size

Total Lines 75
Code Lines 52

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 52
nc 12
nop 4
dl 0
loc 75
rs 6.9666
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\Traits;
4
5
use Hhxsv5\LaravelS\Illuminate\Laravel;
6
use Hhxsv5\LaravelS\Swoole\Process\CustomProcessInterface;
7
use Swoole\Http\Server;
8
use Swoole\Process;
9
10
trait CustomProcessTrait
11
{
12
    use ProcessTitleTrait;
13
    use LogTrait;
14
15
    public function addCustomProcesses(Server $swoole, $processPrefix, array $processes, array $laravelConfig)
16
    {
17
        /**@var []CustomProcessInterface $processList */
18
        $processList = [];
19
        foreach ($processes as $item) {
20
            if (is_string($item)) {
21
                // Backwards compatible
22
                Laravel::autoload($laravelConfig['root_path']);
23
                $process = $item;
24
                $redirect = $process::isRedirectStdinStdout();
25
                $pipe = $process::getPipeType();
26
            } else {
27
                if (empty($item['class'])) {
28
                    throw new \InvalidArgumentException(sprintf(
29
                            'process class name must be specified'
30
                        )
31
                    );
32
                }
33
                $process = $item['class'];
34
                $redirect = isset($item['redirect']) ? $item['redirect'] : false;
35
                $pipe = isset($item['pipe']) ? $item['pipe'] : 0;
36
            }
37
38
            $processHandler = function (Process $worker) use ($swoole, $processPrefix, $process, $laravelConfig) {
39
                if (!isset(class_implements($process)[CustomProcessInterface::class])) {
40
                    throw new \InvalidArgumentException(
41
                        sprintf(
42
                            '%s must implement the interface %s',
43
                            $process,
44
                            CustomProcessInterface::class
45
                        )
46
                    );
47
                }
48
                $name = $process::getName() ?: 'custom';
49
                $this->setProcessTitle(sprintf('%s laravels: %s process', $processPrefix, $name));
50
                $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

50
                $this->/** @scrutinizer ignore-call */ 
51
                       initLaravel($laravelConfig, $swoole);
Loading history...
51
52
                Process::signal(SIGUSR1, function ($signo) use ($name, $process, $worker, $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

52
                Process::signal(SIGUSR1, function (/** @scrutinizer ignore-unused */ $signo) use ($name, $process, $worker, $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...
53
                    $this->info(sprintf('Reloading the process %s [pid=%d].', $name, $worker->pid));
54
                    $process::onReload($swoole, $worker);
55
                });
56
57
                $enableCoroutine = class_exists('Swoole\Coroutine');
58
                $runProcess = function () use ($name, $process, $swoole, $worker, $enableCoroutine) {
59
                    $maxTry = 10;
60
                    $i = 0;
61
                    do {
62
                        $this->callWithCatchException([$process, 'callback'], [$swoole, $worker]);
63
                        ++$i;
64
                        if ($enableCoroutine) {
65
                            \Swoole\Coroutine::sleep(1);
66
                        } else {
67
                            sleep(1);
68
                        }
69
                    } while ($i < $maxTry);
70
                    $this->error(
71
                        sprintf(
72
                            'The custom process "%s" reaches the maximum number of retries %d times, and will be restarted by the manager process.',
73
                            $name,
74
                            $maxTry
75
                        )
76
                    );
77
                };
78
                $enableCoroutine ? go($runProcess) : $runProcess();;
79
            };
80
            $customProcess = new Process(
81
                $processHandler,
82
                $redirect,
83
                $pipe
84
            );
85
            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...
86
                $processList[] = $customProcess;
87
            }
88
        }
89
        return $processList;
90
    }
91
92
}
93