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 ( ae159d...52ca8a )
by Brent
01:53
created

ProcessCallbacks::isAllowedThrowableType()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 5
nop 2
dl 0
loc 28
rs 9.1608
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\Async\Process;
4
5
use ReflectionFunction;
6
use Throwable;
7
8
trait ProcessCallbacks
9
{
10
    protected $successCallbacks = [];
11
    protected $errorCallbacks = [];
12
    protected $timeoutCallbacks = [];
13
14
    public function then(callable $callback): self
15
    {
16
        $this->successCallbacks[] = $callback;
17
18
        return $this;
19
    }
20
21
    public function catch(callable $callback): self
22
    {
23
        $this->errorCallbacks[] = $callback;
24
25
        return $this;
26
    }
27
28
    public function timeout(callable $callback): self
29
    {
30
        $this->timeoutCallbacks[] = $callback;
31
32
        return $this;
33
    }
34
35
    public function triggerSuccess()
36
    {
37
        if ($this->getErrorOutput()) {
0 ignored issues
show
Bug introduced by
It seems like getErrorOutput() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
38
            $this->triggerError();
39
40
            return;
41
        }
42
43
        $output = $this->getOutput();
0 ignored issues
show
Bug introduced by
It seems like getOutput() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
44
45
        foreach ($this->successCallbacks as $callback) {
46
            call_user_func_array($callback, [$output]);
47
        }
48
49
        return $output;
50
    }
51
52
    public function triggerError()
53
    {
54
        $exception = $this->resolveErrorOutput();
0 ignored issues
show
Bug introduced by
It seems like resolveErrorOutput() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
55
56
        if (! $this->errorCallbacks) {
57
            throw $exception;
58
        }
59
60
        foreach ($this->errorCallbacks as $callback) {
61
            if (!$this->isAllowedThrowableType($exception, $callback)) {
62
                continue;
63
            }
64
65
            call_user_func_array($callback, [$exception]);
66
67
            break;
68
        }
69
    }
70
71
    public function triggerTimeout()
72
    {
73
        foreach ($this->timeoutCallbacks as $callback) {
74
            call_user_func_array($callback, []);
75
        }
76
    }
77
78
    protected function isAllowedThrowableType(Throwable $throwable, callable $callable): bool
79
    {
80
        $reflection = new ReflectionFunction($callable);
81
82
        $parameters = $reflection->getParameters();
83
84
        if (! isset($parameters[0])) {
85
            return true;
86
        }
87
88
        $firstParameter = $parameters[0];
89
90
        if (! $firstParameter) {
91
            return true;
92
        }
93
94
        $type = $firstParameter->getType();
95
96
        if (! $type) {
97
            return true;
98
        }
99
100
        if (is_a($throwable, $type->getName())) {
101
            return true;
102
        }
103
104
        return false;
105
    }
106
}
107