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 ( 8b879e...a700a2 )
by SignpostMarv
03:48
created

DefinitionAssistant::RegisterType()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 47
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 5

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 47
ccs 17
cts 17
cp 1
rs 9.2248
c 0
b 0
f 0
cc 5
nc 5
nop 2
crap 5
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_string($maybe) && ! is_object($maybe)) {
115 2
            throw new InvalidArgumentException(
116
                'Argument 1 passed to ' .
117
                __METHOD__ .
118
                '() must be either a string or an object, ' .
119 2
                gettype($maybe) .
120 2
                ' given!'
121
            );
122 40
        } elseif (is_object($maybe) && ! ($maybe instanceof DaftObject)) {
123 2
            throw new InvalidArgumentException(
124
                'Argument 1 passed to ' .
125
                __METHOD__ .
126
                '() must be either a string or an instance of ' .
127
                DaftObject::class .
128
                ', ' .
129 2
                get_class($maybe) .
130 2
                ' given!'
131
            );
132
        }
133
134
        /**
135
        * @var array<int, string>
136
        */
137 38
        $out = [];
138
139 38
        foreach (self::$types as $type => $properties) {
140 38
            if (is_a($maybe, $type, is_string($maybe))) {
141 38
                $out = array_merge($out, $properties);
142
            }
143
        }
144
145 38
        return array_values(array_unique($out));
146
    }
147
}
148