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.
Test Failed
Push — master ( 292c14...96865b )
by Jamie
07:21
created

HasWaiterTrait::waitUntil()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 14
rs 9.2
cc 4
eloc 7
nc 3
nop 3
1
<?php
2
3
namespace OpenStack\Common\Resource;
4
use OpenStack\Common\Error\BadResponseError;
5
6
/**
7
 * Contains reusable functionality for resources that have long operations which require waiting in
8
 * order to reach a particular state.
9
 *
10
 * @codeCoverageIgnore
11
 *
12
 * @package OpenStack\Common\Resource
13
 */
14
trait HasWaiterTrait
15
{
16
    /**
17
     * Provides a blocking operation until the resource has reached a particular state. The method
18
     * will enter a loop, requesting feedback from the remote API until it sends back an appropriate
19
     * status.
20
     *
21
     * @param string $status      The state to be reached
22
     * @param int    $timeout     The maximum timeout. If the total time taken by the waiter has reached
23
     *                            or exceed this timeout, the blocking operation will immediately cease.
24
     * @param int    $sleepPeriod The amount of time to pause between each HTTP request.
25
     */
26
    public function waitUntil($status, $timeout = 60, $sleepPeriod = 1)
27
    {
28
        $startTime = time();
29
30
        while (true) {
31
            $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...
32
33
            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...
34
                break;
35
            }
36
37
            sleep($sleepPeriod);
38
        }
39
    }
40
41
    /**
42
     * Provides a blocking operation until the resource has reached a particular state. The method
43
     * will enter a loop, executing the callback until TRUE is returned. This provides great
44
     * flexibility.
45
     *
46
     * @param callable $fn          An anonymous function that will be executed on every iteration. You can
47
     *                              encapsulate your own logic to determine whether the resource has
48
     *                              successfully transitioned. When TRUE is returned by the callback,
49
     *                              the loop will end.
50
     * @param int|bool $timeout     The maximum timeout in seconds. If the total time taken by the waiter has reached
51
     *                              or exceed this timeout, the blocking operation will immediately cease. If FALSE
52
     *                              is provided, the timeout will never be considered.
53
     * @param int      $sleepPeriod The amount of time to pause between each HTTP request.
54
     */
55
    public function waitWithCallback(callable $fn, $timeout = 60, $sleepPeriod = 1)
56
    {
57
        $startTime = time();
58
59
        while (true) {
60
            $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...
61
62
            $response = call_user_func_array($fn, [$this]);
63
64
            if ($response === true || $this->shouldHalt($timeout, $startTime)) {
65
                break;
66
            }
67
68
            sleep($sleepPeriod);
69
        }
70
    }
71
72
    /**
73
     * Internal method used to identify whether a timeout has been exceeded.
74
     *
75
     * @param bool|int $timeout
76
     * @param int      $startTime
77
     *
78
     * @return bool
79
     */
80
    private function shouldHalt($timeout, $startTime)
81
    {
82
        if ($timeout === false) {
83
            return false;
84
        }
85
86
        return time() - $startTime >= $timeout;
87
    }
88
89
    /**
90
     * Convenience method providing a blocking operation until the resource transitions to an
91
     * ``ACTIVE`` status.
92
     *
93
     * @param int|bool $timeout The maximum timeout in seconds. If the total time taken by the waiter has reached
94
     *                          or exceed this timeout, the blocking operation will immediately cease. If FALSE
95
     *                          is provided, the timeout will never be considered.
96
     */
97
    public function waitUntilActive($timeout = 60)
0 ignored issues
show
Unused Code introduced by
The parameter $timeout is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
98
    {
99
        $this->waitUntil('ACTIVE');
100
    }
101
102
    public function waitUntilDeleted($timeout = 60, $sleepPeriod = 1)
103
    {
104
        $startTime = time();
105
106
        while (true) {
107
            try {
108
                $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...
109
            } catch (BadResponseError $e) {
110
                if ($e->getResponse()->getStatusCode() === 404) {
111
                    break;
112
                }
113
                throw $e;
114
            }
115
116
            if ($this->shouldHalt($timeout, $startTime)) {
117
                break;
118
            }
119
120
            sleep($sleepPeriod);
121
        }
122
    }
123
}
124