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 ( 2809db...84b5a3 )
by Brent
01:29
created

Runtime::maximumExecutionTime()   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 Exception;
6
use Spatie\Async\Output\ErrorProcessOutput;
7
use Spatie\Async\Output\ProcessOutput;
8
use Throwable;
9
10
class Runtime
11
{
12
    protected $maximumExecutionTime = 300;
13
14
    public function maximumExecutionTime(int $maximumExecutionTime): self
15
    {
16
        $this->maximumExecutionTime = $maximumExecutionTime;
17
18
        return $this;
19
    }
20
21
    public function start(Process $process): Process
22
    {
23
        socket_create_pair(AF_UNIX, SOCK_STREAM, 0, $sockets);
24
25
        [$parentSocket, $childSocket] = $sockets;
0 ignored issues
show
Bug introduced by
The variable $parentSocket does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $childSocket does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
26
27
        if (($pid = pcntl_fork()) == 0) {
28
            socket_close($childSocket);
29
30
            $this->executeChildProcess($parentSocket, $process);
31
32
            exit;
33
        }
34
35
        socket_close($parentSocket);
36
37
        return $process
38
            ->setPid($pid)
39
            ->setSocket($childSocket)
40
            ->setStartTime(time());
41
    }
42
43
    public function handleFinishedProcess(Process $process): bool
44
    {
45
        $output = $this->readProcessOutputFromSocket($process);
46
47
        if (!$output->isSuccess()) {
48
            $process->triggerError($output->payload());
49
50
            return false;
51
        }
52
53
        $process->triggerSuccess($output->payload());
54
55
        return true;
56
    }
57
58
    public function handleRunningProcess(Process $process, $status): bool
59
    {
60
        if ($process->startTime() + $this->maximumExecutionTime < time() || pcntl_wifstopped($status)) {
61
            if (! posix_kill($process->pid(), SIGKILL)) {
62
                throw new Exception("Failed to kill {$process->pid()}: ".posix_strerror(posix_get_last_error()));
63
            }
64
65
            $process->triggerTimeout();
66
67
            return false;
68
        }
69
70
        return true;
71
    }
72
73
    protected function executeChildProcess($parentSocket, Process $process): void
74
    {
75
        try {
76
            $output = ProcessOutput::create($process->execute())->setSuccess();
77
        } catch (Throwable $e) {
78
            $output = ErrorProcessOutput::create($e);
79
        }
80
81
        socket_write($parentSocket, $output->serialize());
82
83
        socket_close($parentSocket);
84
    }
85
86
    protected function readProcessOutputFromSocket(Process $process): ProcessOutput
87
    {
88
        $rawOutput = '';
89
90
        while ($buffer = socket_read($process->socket(), 1024)) {
91
            $rawOutput .= $buffer;
92
        }
93
94
        $output = unserialize($rawOutput);
95
96
        socket_close($process->socket());
97
98
        return $output;
99
    }
100
}
101