Completed
Pull Request — master (#18)
by
unknown
39:59
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 Symfony\Component\Process\Process as SymfonyProcess;
6
use Yii;
7
use yii\helpers\HtmlPurifier;
8
9
/**
10
 * Class Process
11
 *
12
 * @package SfCod\SocketIoBundle
13
 */
14
15
/**
16
 * Class Process
17
 *
18
 * @package yiicod\socketio
19
 */
20
class Process
21
{
22
    /**
23
     * @var array
24
     */
25
    private static $_inWork = [];
26
27
    /**
28
     * @var
29
     */
30
    public $yiiAlias;
31
32
    /**
33
     * @return int
34
     */
35
    public function getParallelEnv(): int
36
    {
37
        return getenv('SOCKET_IO.PARALLEL') ? getenv('SOCKET_IO.PARALLEL') : 10;
38
    }
39
40
	/**
41
	 * Run process. If more then limit then wait and try run process on more time.
42
	 *
43
	 * @param string $handle
44
	 * @param array $data
45
	 * @param string $id
46
	 *
47
	 * @return SymfonyProcess
48
	 */
49
    public function run(string $handle, array $data, string $id)
50
    {
51
        $this->inWork();
52
53
        while (count(self::$_inWork) >= $this->getParallelEnv()) {
54
            usleep(100);
55
56
            $this->inWork();
57
        }
58
59
        return $this->push($handle, $data, $id);
60
    }
61
62
    /**
63
     * In work processes
64
     */
65
    private function inWork()
66
    {
67
        foreach (self::$_inWork as $i => $proccess) {
68
            if (false === $proccess->isRunning()) {
69
                unset(self::$_inWork[$i]);
70
            }
71
        }
72
    }
73
74
	/**
75
	 * Create cmd process and push to queue.
76
	 *
77
	 * @param string $handle
78
	 * @param array $data
79
	 * @param string $id
80
	 *
81
	 * @return SymfonyProcess
82
	 */
83
    private function push(string $handle, array $data, string $id): SymfonyProcess
84
    {
85
        $cmd = HtmlPurifier::process(sprintf('php yii socketio/process %s %s %s', escapeshellarg($handle), escapeshellarg(json_encode($data)), escapeshellarg($id)));
86
87
        if (is_null($this->yiiAlias)) {
88
            if (file_exists(Yii::getAlias('@app/yii'))) {
89
                $this->yiiAlias = '@app';
90
            } elseif (file_exists(Yii::getAlias('@app/../yii'))) {
91
                $this->yiiAlias = '@app/../';
92
            }
93
        }
94
95
        $process = new SymfonyProcess($cmd, Yii::getAlias($this->yiiAlias));
0 ignored issues
show
$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...
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...
96
        $process->setTimeout(10);
97
        $process->start();
98
99
        self::$_inWork[] = $process;
100
101
        return $process;
102
    }
103
}
104