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
Pull Request — master (#111)
by Alexander
01:48 queued 34s
created

ParentRuntime::createProcessExecutable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 3
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\Async\Runtime;
4
5
use Closure;
6
use Opis\Closure\SerializableClosure;
7
use function Opis\Closure\serialize;
8
use function Opis\Closure\unserialize;
9
use Spatie\Async\Pool;
10
use Spatie\Async\Process\ParallelProcess;
11
use Spatie\Async\Process\Runnable;
12
use Spatie\Async\Process\SynchronousProcess;
13
use Symfony\Component\Process\Process;
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
     * @param int|null $outputLength
54
     *
55
     * @param string $binary
56
     * @return \Spatie\Async\Process\Runnable
57
     */
58
    public static function createProcess($task, ?int $outputLength = null, string $binary = Pool::DEFAULT_PHP_BINARY): Runnable
59
    {
60
        if (! self::$isInitialised) {
61
            self::init();
62
        }
63
64
        if (! Pool::isSupported()) {
65
            return SynchronousProcess::create($task, self::getId());
66
        }
67
68
        $process = self::createProcessExecutable($task, $outputLength, $binary);
69
70
        return ParallelProcess::create($task, $process, self::getId(), $outputLength);
71
    }
72
73
    public static function createProcessExecutable($task, ?int $outputLength = null, string $binary = Pool::DEFAULT_PHP_BINARY): Process
74
    {
75
        return new Process([
76
            $binary,
77
            self::$childProcessScript,
78
            self::$autoloader,
79
            self::encodeTask($task),
80
            $outputLength,
81
        ]);
82
    }
83
84
    /**
85
     * @param \Spatie\Async\Task|callable $task
86
     *
87
     * @return string
88
     */
89
    public static function encodeTask($task): string
90
    {
91
        if ($task instanceof Closure) {
92
            $task = new SerializableClosure($task);
93
        }
94
95
        return base64_encode(serialize($task));
96
    }
97
98
    public static function decodeTask(string $task)
99
    {
100
        return unserialize(base64_decode($task));
101
    }
102
103
    protected static function getId(): string
104
    {
105
        if (self::$myPid === null) {
106
            self::$myPid = getmypid();
107
        }
108
109
        self::$currentId += 1;
110
111
        return (string) self::$currentId.(string) self::$myPid;
112
    }
113
}
114