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 ( 86a0a5...f6adab )
by Brent
13s
created

Pool::timeout()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\Async;
4
5
use ArrayAccess;
6
use Spatie\Async\Runtime\ParentRuntime;
7
8
class Pool implements ArrayAccess
9
{
10
    protected $concurrency = 20;
11
    protected $tasksPerProcess = 1;
12
    protected $timeout = 300;
13
    protected $sleepTime = 50000;
14
15
    /** @var \Spatie\Async\ParallelProcess[] */
16
    protected $queue = [];
17
18
    /** @var \Spatie\Async\ParallelProcess[] */
19
    protected $inProgress = [];
20
21
    /** @var \Spatie\Async\ParallelProcess[] */
22
    protected $finished = [];
23
24
    /** @var \Spatie\Async\ParallelProcess[] */
25
    protected $failed = [];
26
27
    protected $results = [];
28
29
    public function __construct()
30
    {
31
        $this->registerListener();
32
    }
33
34
    /**
35
     * @return static
36
     */
37
    public static function create()
38
    {
39
        return new static();
40
    }
41
42
    public function concurrency(int $concurrency): self
43
    {
44
        $this->concurrency = $concurrency;
45
46
        return $this;
47
    }
48
49
    public function timeout(int $timeout): self
50
    {
51
        $this->timeout = $timeout;
52
53
        return $this;
54
    }
55
56
    public function autoload(string $autoloader): self
57
    {
58
        ParentRuntime::init($autoloader);
59
60
        return $this;
61
    }
62
63
    public function sleepTime(int $sleepTime): self
64
    {
65
        $this->sleepTime = $sleepTime;
66
67
        return $this;
68
    }
69
70
    public function notify()
71
    {
72
        if (count($this->inProgress) >= $this->concurrency) {
73
            return;
74
        }
75
76
        $process = array_shift($this->queue);
77
78
        if (! $process) {
79
            return;
80
        }
81
82
        $this->putInProgress($process);
83
    }
84
85
    /**
86
     * @param \Spatie\Async\ParallelProcess|callable $process
87
     *
88
     * @return \Spatie\Async\ParallelProcess
89
     */
90
    public function add($process): ParallelProcess
91
    {
92
        if (! $process instanceof ParallelProcess) {
93
            $process = ParentRuntime::createChildProcess($process);
94
        }
95
96
        $this->putInQueue($process);
97
98
        return $process;
99
    }
100
101
    public function wait(): array
102
    {
103
        while ($this->inProgress) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->inProgress of type Spatie\Async\ParallelProcess[] 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...
104
            foreach ($this->inProgress as $process) {
105
                if ($process->getCurrentExecutionTime() > $this->timeout) {
106
                    $this->markAsTimedOut($process);
107
                }
108
            }
109
110
            if (! $this->inProgress) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->inProgress of type Spatie\Async\ParallelProcess[] 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...
111
                break;
112
            }
113
114
            usleep($this->sleepTime);
115
        }
116
117
        return $this->results;
118
    }
119
120
    public function putInQueue(ParallelProcess $process)
121
    {
122
        $this->queue[$process->getId()] = $process;
123
124
        $this->notify();
125
    }
126
127
    public function putInProgress(ParallelProcess $process)
128
    {
129
        $process->getProcess()->setTimeout($this->timeout);
130
131
        $process->start();
132
133
        unset($this->queue[$process->getId()]);
134
135
        $this->inProgress[$process->getPid()] = $process;
136
    }
137
138
    public function markAsFinished(ParallelProcess $process)
139
    {
140
        $this->results[] = $process->triggerSuccess();
141
142
        unset($this->inProgress[$process->getPid()]);
143
144
        $this->finished[$process->getPid()] = $process;
145
146
        $this->notify();
147
    }
148
149 View Code Duplication
    public function markAsTimedOut(ParallelProcess $process)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
150
    {
151
        $process->triggerTimeout();
152
153
        unset($this->inProgress[$process->getPid()]);
154
155
        $this->failed[$process->getPid()] = $process;
156
157
        $this->notify();
158
    }
159
160 View Code Duplication
    public function markAsFailed(ParallelProcess $process)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
161
    {
162
        $process->triggerError();
163
164
        unset($this->inProgress[$process->getPid()]);
165
166
        $this->failed[$process->getPid()] = $process;
167
168
        $this->notify();
169
    }
170
171
    public function offsetExists($offset)
172
    {
173
        // TODO
174
175
        return false;
176
    }
177
178
    public function offsetGet($offset)
179
    {
180
        // TODO
181
    }
182
183
    public function offsetSet($offset, $value)
184
    {
185
        $this->add($value);
0 ignored issues
show
Documentation introduced by
$value is of type *, but the function expects a callable.

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...
186
    }
187
188
    public function offsetUnset($offset)
189
    {
190
        // TODO
191
    }
192
193
    /**
194
     * @return \Spatie\Async\ParallelProcess[]
195
     */
196
    public function getFinished(): array
197
    {
198
        return $this->finished;
199
    }
200
201
    /**
202
     * @return \Spatie\Async\ParallelProcess[]
203
     */
204
    public function getFailed(): array
205
    {
206
        return $this->failed;
207
    }
208
209
    protected function registerListener()
210
    {
211
        pcntl_async_signals(true);
212
213
        pcntl_signal(SIGCHLD, function ($signo, $status) {
214
            while (true) {
215
                $pid = pcntl_waitpid(-1, $processState, WNOHANG | WUNTRACED);
216
217
                if ($pid <= 0) {
218
                    break;
219
                }
220
221
                $process = $this->inProgress[$pid] ?? null;
222
223
                if (! $process) {
224
                    continue;
225
                }
226
227
                if ($status['status'] === 0) {
228
                    $this->markAsFinished($process);
229
230
                    continue;
231
                }
232
233
                $this->markAsFailed($process);
234
            }
235
        });
236
    }
237
}
238