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 ( 495f29...5d138d )
by Brent
01:45
created

ParallelProcess::resolveErrorOutput()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 4
nop 0
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\Async;
4
5
use Throwable;
6
use Symfony\Component\Process\Process;
7
use Spatie\Async\Output\SerializableException;
8
9
class ParallelProcess
10
{
11
    protected $process;
12
    protected $id;
13
    protected $pid;
14
15
    protected $successCallbacks = [];
16
    protected $errorCallbacks = [];
17
    protected $timeoutCallbacks = [];
18
19
    protected $output;
20
    protected $errorOutput;
21
22
    protected $startTime;
23
24
    public function __construct(Process $process, string $id)
25
    {
26
        $this->process = $process;
27
        $this->id = $id;
28
    }
29
30
    public static function create(Process $process, string $id): self
31
    {
32
        return new self($process, $id);
33
    }
34
35
    public function then(callable $callback): self
36
    {
37
        $this->successCallbacks[] = $callback;
38
39
        return $this;
40
    }
41
42
    public function catch(callable $callback): self
0 ignored issues
show
Coding Style introduced by
Possible parse error: non-abstract method defined as abstract
Loading history...
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
43
    {
44
        $this->errorCallbacks[] = $callback;
45
46
        return $this;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $this.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
47
    }
48
49
    public function timeout(callable $callback): self
50
    {
51
        $this->timeoutCallbacks[] = $callback;
52
53
        return $this;
54
    }
55
56
    public function start(): self
57
    {
58
        $this->startTime = microtime(true);
59
60
        $this->process->start();
61
62
        $this->pid = $this->process->getPid();
63
64
        return $this;
65
    }
66
67
    public function stop(): self
68
    {
69
        $this->process->stop(10, SIGKILL);
70
71
        return $this;
72
    }
73
74
    public function isRunning(): bool
75
    {
76
        return $this->process->isRunning();
77
    }
78
79
    public function isSuccessful(): bool
80
    {
81
        return $this->process->isSuccessful();
82
    }
83
84
    public function isTerminated(): bool
85
    {
86
        return $this->process->isTerminated();
87
    }
88
89
    public function getOutput()
90
    {
91
        if (! $this->output) {
92
            $processOutput = $this->process->getOutput();
93
94
            $this->output = @unserialize(base64_decode($processOutput));
95
96
            if (! $this->output) {
97
                $this->errorOutput = $processOutput;
98
            }
99
        }
100
101
        return $this->output;
102
    }
103
104
    public function getErrorOutput()
105
    {
106
        if (! $this->errorOutput) {
107
            $processOutput = $this->process->getErrorOutput();
108
109
            $this->errorOutput = @unserialize(base64_decode($processOutput));
110
111
            if (! $this->errorOutput) {
112
                $this->errorOutput = $processOutput;
113
            }
114
        }
115
116
        return $this->errorOutput;
117
    }
118
119
    public function getProcess(): Process
120
    {
121
        return $this->process;
122
    }
123
124
    public function getId(): string
125
    {
126
        return $this->id;
127
    }
128
129
    public function getPid(): ?string
130
    {
131
        return $this->pid;
132
    }
133
134
    public function getCurrentExecutionTime(): float
135
    {
136
        return microtime(true) - $this->startTime;
137
    }
138
139
    public function triggerSuccess()
140
    {
141
        if ($this->getErrorOutput()) {
142
            $this->triggerError();
143
144
            return;
145
        }
146
147
        $output = $this->getOutput();
148
149
        foreach ($this->successCallbacks as $callback) {
150
            call_user_func_array($callback, [$output]);
151
        }
152
153
        return $output;
154
    }
155
156
    public function triggerError()
157
    {
158
        $exception = $this->resolveErrorOutput();
159
160
        foreach ($this->errorCallbacks as $callback) {
161
            call_user_func_array($callback, [$exception]);
162
        }
163
164
        if (! $this->errorCallbacks) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->errorCallbacks of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
165
            throw $exception;
166
        }
167
    }
168
169
    public function triggerTimeout()
170
    {
171
        foreach ($this->timeoutCallbacks as $callback) {
172
            call_user_func_array($callback, []);
173
        }
174
    }
175
176
    protected function resolveErrorOutput(): Throwable
177
    {
178
        $exception = $this->getErrorOutput();
179
180
        if ($exception instanceof SerializableException) {
181
            $exception = $exception->asThrowable();
182
        }
183
184
        if (! $exception instanceof Throwable) {
185
            $exception = ParallelError::fromException($exception);
186
        }
187
188
        return $exception;
189
    }
190
}
191