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.

HasWaiterTrait::shouldHalt()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 2
1
<?php declare(strict_types=1);
2
3
namespace OpenStack\Common\Resource;
4
5
use OpenStack\Common\Error\BadResponseError;
6
7
/**
8
 * Contains reusable functionality for resources that have long operations which require waiting in
9
 * order to reach a particular state.
10
 *
11
 * @codeCoverageIgnore
12
 *
13
 * @package OpenStack\Common\Resource
14
 */
15
trait HasWaiterTrait
16
{
17
    /**
18
     * Provides a blocking operation until the resource has reached a particular state. The method
19
     * will enter a loop, requesting feedback from the remote API until it sends back an appropriate
20
     * status.
21
     *
22
     * @param string $status      The state to be reached
23
     * @param int    $timeout     The maximum timeout. If the total time taken by the waiter has reached
24
     *                            or exceed this timeout, the blocking operation will immediately cease.
25
     * @param int    $sleepPeriod The amount of time to pause between each HTTP request.
26
     */
27
    public function waitUntil(string $status, $timeout = 60, int $sleepPeriod = 1)
28
    {
29
        $startTime = time();
30
31
        while (true) {
32
            $this->retrieve();
0 ignored issues
show
Bug introduced by
It seems like retrieve() 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...
33
34
            if ($this->status == $status || $this->shouldHalt($timeout, $startTime)) {
0 ignored issues
show
Bug introduced by
The property status does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
35
                break;
36
            }
37
38
            sleep($sleepPeriod);
39
        }
40
    }
41
42
    /**
43
     * Provides a blocking operation until the resource has reached a particular state. The method
44
     * will enter a loop, executing the callback until TRUE is returned. This provides great
45
     * flexibility.
46
     *
47
     * @param callable $fn          An anonymous function that will be executed on every iteration. You can
48
     *                              encapsulate your own logic to determine whether the resource has
49
     *                              successfully transitioned. When TRUE is returned by the callback,
50
     *                              the loop will end.
51
     * @param int|bool $timeout     The maximum timeout in seconds. If the total time taken by the waiter has reached
52
     *                              or exceed this timeout, the blocking operation will immediately cease. If FALSE
53
     *                              is provided, the timeout will never be considered.
54
     * @param int      $sleepPeriod The amount of time to pause between each HTTP request.
55
     */
56
    public function waitWithCallback(callable $fn, $timeout = 60, int $sleepPeriod = 1)
57
    {
58
        $startTime = time();
59
60
        while (true) {
61
            $this->retrieve();
0 ignored issues
show
Bug introduced by
It seems like retrieve() 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...
62
63
            $response = call_user_func_array($fn, [$this]);
64
65
            if ($response === true || $this->shouldHalt($timeout, $startTime)) {
66
                break;
67
            }
68
69
            sleep($sleepPeriod);
70
        }
71
    }
72
73
    /**
74
     * Internal method used to identify whether a timeout has been exceeded.
75
     *
76
     * @param bool|int $timeout
77
     * @param int      $startTime
78
     *
79
     * @return bool
80
     */
81
    private function shouldHalt($timeout, int $startTime)
82
    {
83
        if ($timeout === false) {
84
            return false;
85
        }
86
87
        return time() - $startTime >= $timeout;
88
    }
89
90
    /**
91
     * Convenience method providing a blocking operation until the resource transitions to an
92
     * ``ACTIVE`` status.
93
     *
94
     * @param int|bool $timeout The maximum timeout in seconds. If the total time taken by the waiter has reached
95
     *                          or exceed this timeout, the blocking operation will immediately cease. If FALSE
96
     *                          is provided, the timeout will never be considered.
97
     */
98
    public function waitUntilActive($timeout = false)
99
    {
100
        $this->waitUntil('ACTIVE', $timeout);
0 ignored issues
show
Bug introduced by
It seems like $timeout defined by parameter $timeout on line 98 can also be of type boolean; however, OpenStack\Common\Resourc...aiterTrait::waitUntil() does only seem to accept integer, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
101
    }
102
103
    public function waitUntilDeleted($timeout = 60, int $sleepPeriod = 1)
104
    {
105
        $startTime = time();
106
107
        while (true) {
108
            try {
109
                $this->retrieve();
0 ignored issues
show
Bug introduced by
It seems like retrieve() 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...
110
            } catch (BadResponseError $e) {
111
                if ($e->getResponse()->getStatusCode() === 404) {
112
                    break;
113
                }
114
                throw $e;
115
            }
116
117
            if ($this->shouldHalt($timeout, $startTime)) {
118
                break;
119
            }
120
121
            sleep($sleepPeriod);
122
        }
123
    }
124
}
125