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 ( f7060e...00f73a )
by Brent
10s
created

ParentRuntime::createProcess()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 4
nop 1
dl 0
loc 19
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\Async\Runtime;
4
5
use Closure;
6
use Spatie\Async\Pool;
7
use Spatie\Async\Process\Runnable;
8
use function Opis\Closure\serialize;
9
use Opis\Closure\SerializableClosure;
10
use function Opis\Closure\unserialize;
11
use Symfony\Component\Process\Process;
12
use Spatie\Async\Process\ParallelProcess;
13
use Spatie\Async\Process\SynchronousProcess;
14
15
class ParentRuntime
16
{
17
    /** @var bool */
18
    protected static $isInitialised = false;
19
20
    /** @var string */
21
    protected static $autoloader;
22
23
    /** @var string */
24
    protected static $childProcessScript;
25
26
    protected static $currentId = 0;
27
28
    protected static $myPid = null;
29
30
    public static function init(string $autoloader = null)
31
    {
32
        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...
33
            $existingAutoloaderFiles = array_filter([
34
                __DIR__.'/../../../../autoload.php',
35
                __DIR__.'/../../../autoload.php',
36
                __DIR__.'/../../vendor/autoload.php',
37
                __DIR__.'/../../../vendor/autoload.php',
38
            ], function (string $path) {
39
                return file_exists($path);
40
            });
41
42
            $autoloader = reset($existingAutoloaderFiles);
43
        }
44
45
        self::$autoloader = $autoloader;
46
        self::$childProcessScript = __DIR__.'/ChildRuntime.php';
47
48
        self::$isInitialised = true;
49
    }
50
51
    /**
52
     * @param \Spatie\Async\Task|callable $task
53
     *
54
     * @return \Spatie\Async\Process\Runnable
55
     */
56
    public static function createProcess($task): Runnable
57
    {
58
        if (! self::$isInitialised) {
59
            self::init();
60
        }
61
62
        if (! Pool::isSupported()) {
63
            return SynchronousProcess::create($task, self::getId());
64
        }
65
66
        $process = new Process(implode(' ', [
67
            'exec php',
68
            self::$childProcessScript,
69
            self::$autoloader,
70
            self::encodeTask($task),
71
        ]));
72
73
        return ParallelProcess::create($process, self::getId());
74
    }
75
76
    /**
77
     * @param \Spatie\Async\Task|callable $task
78
     *
79
     * @return string
80
     */
81
    public static function encodeTask($task): string
82
    {
83
        if ($task instanceof Closure) {
84
            $task = new SerializableClosure($task);
85
        }
86
87
        return base64_encode(serialize($task));
88
    }
89
90
    public static function decodeTask(string $task)
91
    {
92
        return unserialize(base64_decode($task));
93
    }
94
95
    protected static function getId(): string
96
    {
97
        if (self::$myPid === null) {
98
            self::$myPid = getmypid();
99
        }
100
101
        self::$currentId += 1;
102
103
        return (string) self::$currentId.(string) self::$myPid;
104
    }
105
}
106