Completed
Pull Request — master (#19)
by
unknown
36:44
created

Process.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace yiicod\socketio;
4
5
use Yii;
6
use yii\helpers\HtmlPurifier;
7
8
/**
9
 * Class Process
10
 *
11
 * @package SfCod\SocketIoBundle
12
 */
13
14
/**
15
 * Class Process
16
 *
17
 * @package yiicod\socketio
18
 */
19
class Process
20
{
21
    /**
22
     * @var array
23
     */
24
    private static $_inWork = [];
25
26
    /**
27
     * @var
28
     */
29
    public $yiiAlias;
30
31
    /**
32
     * @return int
33
     */
34
    public function getParallelEnv(): int
35
    {
36
        return getenv('SOCKET_IO.PARALLEL') ? getenv('SOCKET_IO.PARALLEL') : 10;
37
    }
38
39
    /**
40
     * Run process. If more then limit then wait and try run process on more time.
41
     *
42
     * @param string $handle
43
     * @param array $data
44
     *
45
     * @return \Symfony\Component\Process\Process
46
     */
47
    public function run(string $handle, array $data)
48
    {
49
        $this->inWork();
50
51
        while (count(self::$_inWork) >= $this->getParallelEnv()) {
52
            usleep(100);
53
54
            $this->inWork();
55
        }
56
57
        return $this->push($handle, $data);
58
    }
59
60
    /**
61
     * In work processes
62
     */
63
    private function inWork()
64
    {
65
        foreach (self::$_inWork as $i => $proccess) {
66
            if (false === $proccess->isRunning()) {
67
                unset(self::$_inWork[$i]);
68
            }
69
        }
70
    }
71
72
    /**
73
     * Create cmd process and push to queue.
74
     *
75
     * @param string $handle
76
     * @param array $data
77
     *
78
     * @return \Symfony\Component\Process\Process
79
     */
80
    private function push(string $handle, array $data): \Symfony\Component\Process\Process
81
    {
82
        $cmd = HtmlPurifier::process(sprintf('php yii socketio/process %s %s', escapeshellarg($handle), escapeshellarg(json_encode($data))));
83
84
        if (is_null($this->yiiAlias)) {
85
            if (file_exists(Yii::getAlias('@app/yii'))) {
86
                $this->yiiAlias = '@app';
87
            } elseif (file_exists(Yii::getAlias('@app/../yii'))) {
88
                $this->yiiAlias = '@app/../';
89
            }
90
        }
91
92
        $process = new \Symfony\Component\Process\Process($cmd, Yii::getAlias($this->yiiAlias));
0 ignored issues
show
It seems like \Yii::getAlias($this->yiiAlias) targeting yii\BaseYii::getAlias() can also be of type boolean; however, Symfony\Component\Process\Process::__construct() does only seem to accept null|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
$cmd is of type string|false|null, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
93
        $process->setTimeout(10);
94
        $process->start();
95
96
        self::$_inWork[] = $process;
97
98
        return $process;
99
    }
100
}
101