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.
Passed
Push — master ( 2cb105...292c14 )
by Jamie
12:09
created

HasWaiterTrait   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 11
c 1
b 0
f 1
lcom 1
cbo 0
dl 0
loc 88
rs 10

4 Methods

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