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

TimerTrait::addTimerProcess()   C

Complexity

Conditions 13
Paths 3

Size

Total Lines 59
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 38
nc 3
nop 3
dl 0
loc 59
rs 6.6166
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\Timer;
4
5
use Swoole\Http\Server;
6
use Swoole\Process;
7
use Swoole\Timer;
8
9
trait TimerTrait
10
{
11
    public function addTimerProcess(Server $swoole, array $config, array $laravelConfig)
12
    {
13
        if (empty($config['enable']) || empty($config['jobs'])) {
14
            return;
15
        }
16
17
        $startTimer = function (Process $process) use ($swoole, $config, $laravelConfig) {
18
            $pidfile = dirname($swoole->setting['pid_file']) . '/laravels-timer-process.pid';
19
            file_put_contents($pidfile, $process->pid);
20
            $this->setProcessTitle(sprintf('%s laravels: timer 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

20
            $this->/** @scrutinizer ignore-call */ 
21
                   setProcessTitle(sprintf('%s laravels: timer process', $config['process_prefix']));
Loading history...
21
            $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

21
            $this->/** @scrutinizer ignore-call */ 
22
                   initLaravel($laravelConfig, $swoole);
Loading history...
22
            $timerIds = [];
23
            foreach ($config['jobs'] as $jobClass) {
24
                if (is_array($jobClass) && isset($jobClass[0])) {
25
                    $job = new $jobClass[0](isset($jobClass[1]) ? $jobClass[1] : []);
26
                } else {
27
                    $job = new $jobClass();
28
                }
29
                if (!($job instanceof CronJob)) {
30
                    throw new \InvalidArgumentException(sprintf(
31
                            '%s must extend the abstract class %s',
32
                            get_class($job),
33
                            CronJob::class
34
                        )
35
                    );
36
                }
37
                if (empty($job->interval())) {
38
                    throw new \InvalidArgumentException(sprintf('The interval of %s cannot be empty', get_class($job)));
39
                }
40
                $runProcess = function () use ($job) {
41
                    $runCallback = function () use ($job) {
42
                        $this->callWithCatchException(function () use ($job) {
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

42
                        $this->/** @scrutinizer ignore-call */ 
43
                               callWithCatchException(function () use ($job) {
Loading history...
43
                            $job->run();
44
                        });
45
                    };
46
                    class_exists('Swoole\Coroutine') ? go($runCallback) : $runCallback();
47
                };
48
49
                $timerId = Timer::tick($job->interval(), $runProcess);
50
                $timerIds[] = $timerId;
51
                $job->setTimerId($timerId);
52
                if ($job->isImmediate()) {
53
                    Timer::after(1, $runProcess);
54
                }
55
            }
56
57
            Process::signal(SIGUSR1, function ($signo) use ($config, $timerIds, $process) {
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 ($config, $timerIds, $process) {

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...
58
                foreach ($timerIds as $timerId) {
59
                    Timer::clear($timerId);
60
                }
61
                Timer::after($config['max_wait_time'] * 1000, function () use ($process) {
62
                    $process->exit(0);
63
                });
64
            });
65
        };
66
67
        $timerProcess = new Process($startTimer, false, false);
68
        if ($swoole->addProcess($timerProcess)) {
0 ignored issues
show
Bug introduced by
Are you sure the usage of $swoole->addProcess($timerProcess) 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...
69
            return $timerProcess;
70
        }
71
    }
72
}