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.

Manifest::findRecent()   B
last analyzed

Complexity

Conditions 9
Paths 14

Size

Total Lines 26
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 0
Metric Value
cc 9
eloc 13
c 0
b 0
f 0
nc 14
nop 3
dl 0
loc 26
rs 8.0555
ccs 0
cts 19
cp 0
crap 90
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Deployer\Component\PharUpdate;
6
7
use Deployer\Component\PharUpdate\Version\Comparator;
8
use Deployer\Component\PharUpdate\Version\Parser;
9
use Deployer\Component\PharUpdate\Version\Version;
10
11
/**
12
 * Manages the contents of an updates manifest file.
13
 *
14
 * @author Kevin Herrera <[email protected]>
15
 */
16
class Manifest
17
{
18
    /**
19
     * The list of updates in the manifest.
20
     *
21
     * @var Update[]
22
     */
23
    private $updates;
24
25
    /**
26
     * Sets the list of updates from the manifest.
27
     *
28
     * @param Update[] $updates The updates.
29
     */
30
    public function __construct(array $updates = [])
31
    {
32
        $this->updates = $updates;
33
    }
34
35
    /**
36
     * Finds the most recent update and returns it.
37
     *
38
     * @param Version $version The current version.
39
     * @param boolean $major   Lock to major version?
40
     * @param boolean $pre     Allow pre-releases?
41
     */
42
    public function findRecent(Version $version, bool $major = false, bool $pre = false): ?Update
43
    {
44
        /** @var Update|null */
45
        $current = null;
46
        $major = $major ? $version->getMajor() : null;
47
48
        foreach ($this->updates as $update) {
49
            if ($major && ($major !== $update->getVersion()->getMajor())) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $major of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
50
                continue;
51
            }
52
53
            if ((false === $pre)
54
                && !$update->getVersion()->isStable()) {
55
                continue;
56
            }
57
58
            $test = $current ? $current->getVersion() : $version;
59
60
            if (false === $update->isNewer($test)) {
61
                continue;
62
            }
63
64
            $current = $update;
65
        }
66
67
        return $current;
68
    }
69
70
    /**
71
     * Returns the list of updates in the manifest.
72
     *
73
     * @return Update[] The updates.
74
     */
75
    public function getUpdates(): array
76
    {
77
        return $this->updates;
78
    }
79
80
    /**
81
     * Loads the manifest from a JSON encoded string.
82
     *
83
     * @param string $json The JSON encoded string.
84
     */
85
    public static function load(string $json): self
86
    {
87
        return self::create(json_decode($json));
88
    }
89
90
    /**
91
     * Loads the manifest from a JSON encoded file.
92
     *
93
     * @param string $file The JSON encoded file.
94
     */
95
    public static function loadFile(string $file): self
96
    {
97
        return self::create(json_decode(file_get_contents($file)));
98
    }
99
100
    /**
101
     * Validates the data, processes it, and returns a new instance of Manifest.
102
     *
103
     * @param array $decoded The decoded JSON data.
104
     *
105
     * @return static The new instance.
106
     */
107
    private static function create(array $decoded): self
108
    {
109
        $updates = [];
110
111
        foreach ($decoded as $update) {
112
            $updates[] = new Update(
113
                $update->name,
114
                $update->sha1,
115
                $update->url,
116
                Parser::toVersion($update->version),
117
                $update->publicKey ?? null,
118
            );
119
        }
120
121
        usort(
122
            $updates,
123
            function (Update $a, Update $b) {
124
                return Comparator::isGreaterThan(
125
                    $a->getVersion(),
126
                    $b->getVersion(),
127
                ) ? 1 : 0;
128
            },
129
        );
130
131
        return new static($updates);
132
    }
133
}
134