GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( c177fb...495f29 )
by Brent
05:03
created

ParentRuntime::getId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\Async\Runtime;
4
5
use Spatie\Async\Task;
6
use Spatie\Async\ParallelProcess;
7
use function Opis\Closure\serialize;
8
use Opis\Closure\SerializableClosure;
9
use function Opis\Closure\unserialize;
10
use Symfony\Component\Process\Process;
11
12
class ParentRuntime
13
{
14
    /** @var bool */
15
    protected static $isInitialised = false;
16
17
    /** @var string */
18
    protected static $autoloader;
19
20
    /** @var string */
21
    protected static $childProcessScript;
22
23
    protected static $currentId = 0;
24
25
    protected static $myPid = null;
26
27
    public static function init(string $autoloader = null)
28
    {
29
        if (! $autoloader) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $autoloader of type null|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
30
            $existingAutoloaderFiles = array_filter([
31
                __DIR__.'/../../../../autoload.php',
32
                __DIR__.'/../../../autoload.php',
33
                __DIR__.'/../../vendor/autoload.php',
34
                __DIR__.'/../../../vendor/autoload.php',
35
            ], function (string $path) {
36
                return file_exists($path);
37
            });
38
39
            $autoloader = reset($existingAutoloaderFiles);
40
        }
41
42
        self::$autoloader = $autoloader;
43
        self::$childProcessScript = __DIR__.'/ChildRuntime.php';
44
45
        self::$isInitialised = true;
46
    }
47
48
    /**
49
     * @param \Spatie\Async\Task|callable $task
50
     *
51
     * @return \Spatie\Async\ParallelProcess
52
     */
53
    public static function createChildProcess($task): ParallelProcess
54
    {
55
        if (! self::$isInitialised) {
56
            self::init();
57
        }
58
59
        $process = new Process(implode(' ', [
60
            'exec php',
61
            self::$childProcessScript,
62
            self::$autoloader,
63
            self::encodeTask($task),
64
        ]));
65
66
        return ParallelProcess::create($process, self::getId());
67
    }
68
69
    /**
70
     * @param \Spatie\Async\Task|callable $task
71
     *
72
     * @return string
73
     */
74
    public static function encodeTask($task): string
75
    {
76
        if (! $task instanceof Task) {
77
            $task = new SerializableClosure($task);
0 ignored issues
show
Documentation introduced by
$task is of type callable, but the function expects a object<Closure>.

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...
78
        }
79
80
        return base64_encode(serialize($task));
81
    }
82
83
    public static function decodeTask(string $task)
84
    {
85
        return unserialize(base64_decode($task));
86
    }
87
88
    protected static function getId(): string
89
    {
90
        if (self::$myPid === null) {
91
            self::$myPid = getmypid();
92
        }
93
94
        self::$currentId += 1;
95
96
        return (string) self::$currentId . (string) self::$myPid;
97
    }
98
}
99