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 ( 4deae4...f04772 )
by Brent
15s queued 11s
created

Pool::withBinary()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\Async;
4
5
use ArrayAccess;
6
use InvalidArgumentException;
7
use Spatie\Async\Process\ParallelProcess;
8
use Spatie\Async\Process\Runnable;
9
use Spatie\Async\Process\SynchronousProcess;
10
use Spatie\Async\Runtime\ParentRuntime;
11
12
class Pool implements ArrayAccess
13
{
14
    public static $forceSynchronous = false;
15
16
    protected $concurrency = 20;
17
    protected $tasksPerProcess = 1;
18
    protected $timeout = 300;
19
    protected $sleepTime = 50000;
20
21
    /** @var \Spatie\Async\Process\Runnable[] */
22
    protected $queue = [];
23
24
    /** @var \Spatie\Async\Process\Runnable[] */
25
    protected $inProgress = [];
26
27
    /** @var \Spatie\Async\Process\Runnable[] */
28
    protected $finished = [];
29
30
    /** @var \Spatie\Async\Process\Runnable[] */
31
    protected $failed = [];
32
33
    /** @var \Spatie\Async\Process\Runnable[] */
34
    protected $timeouts = [];
35
36
    protected $results = [];
37
38
    protected $status;
39
40
    protected $stopped = false;
41
42
    protected $binary = 'php';
43
44
    public function __construct()
45
    {
46
        if (static::isSupported()) {
47
            $this->registerListener();
48
        }
49
50
        $this->status = new PoolStatus($this);
51
    }
52
53
    /**
54
     * @return static
55
     */
56
    public static function create()
57
    {
58
        return new static();
59
    }
60
61
    public static function isSupported(): bool
62
    {
63
        return
64
            function_exists('pcntl_async_signals')
65
            && function_exists('posix_kill')
66
            && ! self::$forceSynchronous;
67
    }
68
69
    public function concurrency(int $concurrency): self
70
    {
71
        $this->concurrency = $concurrency;
72
73
        return $this;
74
    }
75
76
    public function timeout(float $timeout): self
77
    {
78
        $this->timeout = $timeout;
0 ignored issues
show
Documentation Bug introduced by
The property $timeout was declared of type integer, but $timeout is of type double. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
79
80
        return $this;
81
    }
82
83
    public function autoload(string $autoloader): self
84
    {
85
        ParentRuntime::init($autoloader);
86
87
        return $this;
88
    }
89
90
    public function sleepTime(int $sleepTime): self
91
    {
92
        $this->sleepTime = $sleepTime;
93
94
        return $this;
95
    }
96
97
    public function withBinary(string $binary): self
98
    {
99
        $this->binary = $binary;
100
101
        return $this;
102
    }
103
104
    public function notify()
105
    {
106
        if (count($this->inProgress) >= $this->concurrency) {
107
            return;
108
        }
109
110
        $process = array_shift($this->queue);
111
112
        if (! $process) {
113
            return;
114
        }
115
116
        $this->putInProgress($process);
117
    }
118
119
    /**
120
     * @param \Spatie\Async\Process\Runnable|callable $process
121
     * @param int|null $outputLength
122
     *
123
     * @return \Spatie\Async\Process\Runnable
124
     */
125
    public function add($process, ?int $outputLength = null): Runnable
126
    {
127
        if (! is_callable($process) && ! $process instanceof Runnable) {
128
            throw new InvalidArgumentException('The process passed to Pool::add should be callable.');
129
        }
130
131
        if (! $process instanceof Runnable) {
132
            $process = ParentRuntime::createProcess(
133
                $process,
134
                $outputLength,
135
                $this->binary
136
            );
137
        }
138
139
        $this->putInQueue($process);
140
141
        return $process;
142
    }
143
144
    public function wait(?callable $intermediateCallback = null): array
145
    {
146
        while ($this->inProgress) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->inProgress of type Spatie\Async\Process\Runnable[] 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...
147
            foreach ($this->inProgress as $process) {
148
                if ($process->getCurrentExecutionTime() > $this->timeout) {
149
                    $this->markAsTimedOut($process);
150
                }
151
152
                if ($process instanceof SynchronousProcess) {
153
                    $this->markAsFinished($process);
154
                }
155
            }
156
157
            if (! $this->inProgress) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->inProgress of type Spatie\Async\Process\Runnable[] 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...
158
                break;
159
            }
160
161
            if ($intermediateCallback) {
162
                call_user_func_array($intermediateCallback, [$this]);
163
            }
164
165
            usleep($this->sleepTime);
166
        }
167
168
        return $this->results;
169
    }
170
171
    public function putInQueue(Runnable $process)
172
    {
173
        $this->queue[$process->getId()] = $process;
174
175
        $this->notify();
176
    }
177
178
    public function putInProgress(Runnable $process)
179
    {
180
        if ($this->stopped) {
181
            return;
182
        }
183
184
        if ($process instanceof ParallelProcess) {
185
            $process->getProcess()->setTimeout($this->timeout);
186
        }
187
188
        $process->start();
189
190
        unset($this->queue[$process->getId()]);
191
192
        $this->inProgress[$process->getPid()] = $process;
193
    }
194
195
    public function markAsFinished(Runnable $process)
196
    {
197
        unset($this->inProgress[$process->getPid()]);
198
199
        $this->notify();
200
201
        $this->results[] = $process->triggerSuccess();
202
203
        $this->finished[$process->getPid()] = $process;
204
    }
205
206
    public function markAsTimedOut(Runnable $process)
207
    {
208
        unset($this->inProgress[$process->getPid()]);
209
210
        $this->notify();
211
212
        $process->triggerTimeout();
213
214
        $this->timeouts[$process->getPid()] = $process;
215
    }
216
217
    public function markAsFailed(Runnable $process)
218
    {
219
        unset($this->inProgress[$process->getPid()]);
220
221
        $this->notify();
222
223
        $process->triggerError();
224
225
        $this->failed[$process->getPid()] = $process;
226
    }
227
228
    public function offsetExists($offset)
229
    {
230
        // TODO
231
232
        return false;
233
    }
234
235
    public function offsetGet($offset)
236
    {
237
        // TODO
238
    }
239
240
    public function offsetSet($offset, $value)
241
    {
242
        $this->add($value);
243
    }
244
245
    public function offsetUnset($offset)
246
    {
247
        // TODO
248
    }
249
250
    /**
251
     * @return \Spatie\Async\Process\Runnable[]
252
     */
253
    public function getQueue(): array
254
    {
255
        return $this->queue;
256
    }
257
258
    /**
259
     * @return \Spatie\Async\Process\Runnable[]
260
     */
261
    public function getInProgress(): array
262
    {
263
        return $this->inProgress;
264
    }
265
266
    /**
267
     * @return \Spatie\Async\Process\Runnable[]
268
     */
269
    public function getFinished(): array
270
    {
271
        return $this->finished;
272
    }
273
274
    /**
275
     * @return \Spatie\Async\Process\Runnable[]
276
     */
277
    public function getFailed(): array
278
    {
279
        return $this->failed;
280
    }
281
282
    /**
283
     * @return \Spatie\Async\Process\Runnable[]
284
     */
285
    public function getTimeouts(): array
286
    {
287
        return $this->timeouts;
288
    }
289
290
    public function status(): PoolStatus
291
    {
292
        return $this->status;
293
    }
294
295
    protected function registerListener()
296
    {
297
        pcntl_async_signals(true);
298
299
        pcntl_signal(SIGCHLD, function ($signo, $status) {
300
            while (true) {
301
                $pid = pcntl_waitpid(-1, $processState, WNOHANG | WUNTRACED);
302
303
                if ($pid <= 0) {
304
                    break;
305
                }
306
307
                $process = $this->inProgress[$pid] ?? null;
308
309
                if (! $process) {
310
                    continue;
311
                }
312
313
                if ($status['status'] === 0) {
314
                    $this->markAsFinished($process);
315
316
                    continue;
317
                }
318
319
                $this->markAsFailed($process);
320
            }
321
        });
322
    }
323
324
    public function stop()
325
    {
326
        $this->stopped = true;
327
    }
328
}
329