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 ( a700a2...5ecdcc )
by SignpostMarv
04:49
created

DefinitionAssistant::ObtainExpectedProperties()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 40
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 40
ccs 19
cts 19
cp 1
rs 9.52
c 0
b 0
f 0
cc 4
nc 4
nop 1
crap 4
1
<?php
2
/**
3
* @author SignpostMarv
4
*/
5
declare(strict_types=1);
6
7
namespace SignpostMarv\DaftObject;
8
9
use InvalidArgumentException;
10
11
class DefinitionAssistant
12
{
13
    /**
14
    * @var array<string, array<int, string>>
15
    */
16
    protected static $types = [];
17
18 50
    public static function IsTypeUnregistered(string $type) : bool
19
    {
20 50
        if ( ! is_a($type, DaftObject::class, true)) {
21 2
            throw new InvalidArgumentException(
22
                'Argument 1 passed to ' .
23
                __METHOD__ .
24
                '() must be an instance of ' .
25
                DaftObject::class .
26
                ', ' .
27 2
                $type .
28 2
                ' given!'
29
            );
30
        }
31
32 48
        return ! isset(self::$types[$type]);
33
    }
34
35 10
    public static function RegisterType(string $type, array $matrix) : void
36
    {
37 10
        if ( ! static::IsTypeUnregistered($type)) {
38 2
            throw new InvalidArgumentException(
39
                'Argument 1 passed to ' .
40
                __METHOD__ .
41 2
                '() has already been registered!'
42
            );
43
        }
44
45 10
        $initialCount = count($matrix);
46
47 10
        if ($initialCount < 1) {
48 2
            throw new InvalidArgumentException(
49
                'Argument 2 passed to ' .
50
                __METHOD__ .
51 2
                '() must be a non-empty array!'
52
            );
53
        }
54
55
        /**
56
        * @var array<int, mixed>
57
        */
58 8
        $matrix = array_filter($matrix, 'is_int', ARRAY_FILTER_USE_KEY);
59
60 8
        if (count($matrix) !== $initialCount) {
61 2
            throw new InvalidArgumentException(
62
                'Argument 2 passed to ' .
63
                __METHOD__ .
64 2
                '() must be an array with only integer keys!'
65
            );
66
        }
67
68
        /**
69
        * @var array<int, string>
70
        */
71 6
        $matrix = array_filter($matrix, 'is_string');
72
73 6
        if (count($matrix) !== $initialCount) {
74 2
            throw new InvalidArgumentException(
75
                'Argument 2 passed to ' .
76
                __METHOD__ .
77 2
                '() must be an array of shape array<int, string>!'
78
            );
79
        }
80
81 4
        self::$types[$type] = $matrix;
82 4
    }
83
84 40
    public static function RegisterAbstractDaftObjectType(string $maybe) : void
85
    {
86 40
        if ( ! is_a($maybe, AbstractDaftObject::class, true)) {
87 2
            throw new InvalidArgumentException(
88
                'Argument 1 passed to ' .
89
                __METHOD__ .
90
                '() must be an implementation of ' .
91
                AbstractDaftObject::class .
92
                ', ' .
93 2
                $maybe .
94 2
                ' given!'
95
            );
96 38
        } elseif ( ! static::IsTypeUnregistered($maybe)) {
97 2
            throw new InvalidArgumentException(
98
                'Argument 1 passed to ' .
99
                __METHOD__ .
100 2
                '() has already been registered!'
101
            );
102
        }
103
104 38
        self::$types[$maybe] = TypeCertainty::EnsureArgumentIsArray($maybe::PROPERTIES);
105 38
    }
106
107
    /**
108
    * @param scalar|object $maybe
109
    *
110
    * @return array<int, string>
111
    */
112 42
    public static function ObtainExpectedProperties($maybe) : array
113
    {
114 42
        if (is_object($maybe)) {
115 2
            if ( ! ($maybe instanceof DaftObject)) {
116 2
                throw new InvalidArgumentException(
117
                    'Argument 1 passed to ' .
118
                    __METHOD__ .
119
                    '() must be either a string or an instance of ' .
120
                    DaftObject::class .
121
                    ', ' .
122 2
                    get_class($maybe) .
123 2
                    ' given!'
124
                );
125
            }
126 40
        } elseif ( ! is_string($maybe)) {
127 2
            throw new InvalidArgumentException(
128
                'Argument 1 passed to ' .
129
                __METHOD__ .
130
                '() must be either a string or an object, ' .
131 2
                gettype($maybe) .
132 2
                ' given!'
133
            );
134
        }
135
136
        /**
137
        * @var array<int, string>
138
        */
139 38
        $out = array_values(array_unique(array_reduce(
140 38
            array_filter(
141 38
                self::$types,
142
                function (string $type) use ($maybe) : bool {
143 38
                    return is_a($maybe, $type, is_string($maybe));
144 38
                },
145 38
                ARRAY_FILTER_USE_KEY
146
            ),
147 38
            'array_merge',
148 38
            []
149
        )));
150
151 38
        return $out;
152
    }
153
154
    /**
155
    * @return array<int, string>
156
    */
157 36
    public static function ObtainExpectedOrDefaultPropertiesWithAutoRegister(string $class) : array
158
    {
159
        if (
160 36
            static::IsTypeUnregistered($class) &&
161 36
            TypeParanoia::IsThingStrings($class, AbstractDaftObject::class)
162
        ) {
163 34
            static::RegisterAbstractDaftObjectType($class);
164
        }
165
166
        /**
167
        * @var array<int, string>
168
        */
169
        $out =
170 36
            ! static::IsTypeUnregistered($class)
171 36
                ? static::ObtainExpectedProperties($class)
172 36
                : $class::DaftObjectProperties();
173
174 36
        return $out;
175
    }
176
}
177