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 ( 89c4ce...795e68 )
by Tomasz
10:48
created

EnumTrait::hasOneOfMembers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
namespace Thunder\Platenum\Enum;
4
5
use Thunder\Platenum\Exception\PlatenumException;
6
7
/**
8
 * @author Tomasz Kowalczyk <[email protected]>
9
 */
10
trait EnumTrait
11
{
12
    /** @var string */
13
    private $member;
14
    /** @var int|string */
15
    private $value;
16
17
    /** @var non-empty-array<string,non-empty-array<string,int|string>> */
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-array<string,n...ray<string,int|string>> at position 0 could not be parsed: Unknown type name 'non-empty-array' at position 0 in non-empty-array<string,non-empty-array<string,int|string>>.
Loading history...
18
    protected static $members = [];
19
    /** @var array<string,array<string,static>> */
20
    protected static $instances = [];
21
22
    /** @param int|string $value */
23 26
    /* final */ private function __construct(string $member, $value)
24
    {
25 26
        $this->member = $member;
26 26
        $this->value = $value;
27 26
    }
28
29
    /* --- CREATE --- */
30
31 38
    final public static function __callStatic(string $member, array $arguments)
32
    {
33 38
        $class = static::class;
34 38
        if($arguments) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $arguments of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
35 1
            throw PlatenumException::fromConstantArguments($class);
36
        }
37
38 37
        return static::fromMember($member);
39
    }
40
41 45
    final public static function fromMember(string $member): self
42
    {
43 45
        $class = static::class;
44 45
        if(isset(static::$instances[$class][$member])) {
45 9
            return static::$instances[$class][$member];
46
        }
47
48 44
        static::resolveMembers();
49 36
        if(false === array_key_exists($member, static::$members[$class])) {
50 10
            static::throwInvalidMemberException($member);
51 6
            static::throwDefaultInvalidMemberException($member);
52
        }
53
54
        /** @psalm-suppress UnsafeInstantiation */
55 26
        return static::$instances[$class][$member] = new static($member, static::$members[$class][$member]);
56
    }
57
58
    /**
59
     * @param mixed $value
60
     * @return static
61
     */
62 14
    final public static function fromValue($value): self
63
    {
64 14
        $class = static::class;
65 14
        if(false === is_scalar($value)) {
66 1
            throw PlatenumException::fromIllegalValue($class, $value);
67
        }
68
69 13
        static::resolveMembers();
70 11
        $member = array_search($value, static::$members[$class], true);
71 11
        if(false === $member) {
72 5
            static::throwInvalidValueException($value);
73 3
            static::throwDefaultInvalidValueException($value);
74
        }
75
76
        /** @var string $member */
77 6
        return static::fromMember($member);
78
    }
79
80
    /**
81
     * @param static $enum
82
     * @return static
83
     */
84 4
    final public static function fromEnum($enum): self
85
    {
86 4
        if(false === $enum instanceof static) {
87 2
            throw PlatenumException::fromMismatchedClass(static::class, \get_class($enum));
88
        }
89
90 2
        return static::fromValue($enum->value);
91
    }
92
93
    /**
94
     * @param static $enum
95
     * @param-out AbstractConstantsEnum|AbstractDocblockEnum|AbstractStaticEnum|AbstractCallbackEnum|AbstractAttributeEnum $enum
96
     */
97 2
    final public function fromInstance(&$enum): void
98
    {
99 2
        $enum = static::fromEnum($enum);
100 1
    }
101
102
    /**
103
     * @psalm-suppress UnusedForeachValue
104 3
     * @return list<static>
0 ignored issues
show
Bug introduced by
The type Thunder\Platenum\Enum\list was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
105
     */
106 3
    final public static function getInstances(): array
107
    {
108 9
        static::resolveMembers();
109
        foreach(static::$members[static::class] as $member => $value) {
110 9
            static::fromMember($member);
111
        }
112
113
        return array_values(static::$instances[static::class]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return array_values(stat...stances[static::class]) returns the type array which is incompatible with the documented return type Thunder\Platenum\Enum\list.
Loading history...
114 2
    }
115
116 2
    /* --- EXCEPTIONS --- */
117
118
    /** @psalm-suppress UnusedParam */
119 6
    private static function throwInvalidMemberException(string $member): void
0 ignored issues
show
Unused Code introduced by
The parameter $member is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

119
    private static function throwInvalidMemberException(/** @scrutinizer ignore-unused */ string $member): void

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

Loading history...
120
    {
121 6
    }
122
123
    private static function throwDefaultInvalidMemberException(string $member): void
124
    {
125
        throw PlatenumException::fromInvalidMember(static::class, $member, static::$members[static::class]);
126
    }
127 3
128
    /**
129 3
     * @param mixed $value
130
     * @psalm-suppress UnusedParam
131
     */
132
    private static function throwInvalidValueException($value): void
0 ignored issues
show
Unused Code introduced by
The parameter $value is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

132
    private static function throwInvalidValueException(/** @scrutinizer ignore-unused */ $value): void

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

Loading history...
133
    {
134 6
    }
135
136 6
    /** @param mixed $value */
137
    private static function throwDefaultInvalidValueException($value): void
138
    {
139
        throw PlatenumException::fromInvalidValue(static::class, $value);
140 9
    }
141
142 9
    /* --- COMPARE --- */
143
144
    /** @param static $other */
145 1
    final public function equals($other): bool
146
    {
147 1
        return $other instanceof $this && $this->value === $other->value;
148
    }
149
150 1
    /* --- TRANSFORM --- */
151
152 1
    final public function getMember(): string
153
    {
154
        return $this->member;
155
    }
156
157 8
    /** @return int|string */
158
    final public function getValue()
159 8
    {
160
        return $this->value;
161 7
    }
162
163
    #[\ReturnTypeWillChange]
164
    final public function jsonSerialize()
165 8
    {
166
        return $this->getValue();
167 8
    }
168
169 8
    final public function __toString(): string
170
    {
171
        return (string)$this->getValue();
172 1
    }
173
174 1
    /* --- CHECK --- */
175
176
    final public static function memberExists(string $member): bool
177
    {
178 1
        static::resolveMembers();
179
180 1
        return array_key_exists($member, static::$members[static::class]);
181
    }
182
183
    /** @param int|string $value */
184
    final public static function valueExists($value): bool
185
    {
186 6
        static::resolveMembers();
187
188 6
        return \in_array($value, static::$members[static::class], true);
189 5
    }
190 3
191
    /** @param list<string> $members */
192
    final public static function oneOfMembersExists(array $members): bool
193 1
    {
194
        static::resolveMembers();
195
196
        return [] !== array_intersect(array_keys(static::$members[static::class]), $members);
197 7
    }
198
199 7
    /** @param list<int|string> $values */
200 5
    final public static function oneOfValuesExists(array $values): bool
201 3
    {
202
        static::resolveMembers();
203
204 2
        return [] !== array_intersect(static::$members[static::class], $values);
205
    }
206
207 1
    final public function hasMember(string $members): bool
208
    {
209 1
        return $members === $this->member;
210
    }
211 1
212
    /** @param int|string $value */
213
    final public function hasValue($value): bool
214 1
    {
215
        return $value === $this->value;
216 1
    }
217
218 1
    /** @param list<string> $members */
219
    final public function hasOneOfMembers(array $members): bool
220
    {
221 5
        return in_array($this->member, $members, true);
222
    }
223 5
224
    /** @param list<int|string> $values */
225 5
    final public function hasOneOfValues(array $values): bool
226
    {
227
        return in_array($this->value, $values, true);
228
    }
229
230 74
    /** @param list<static> $enums */
231
    final public function isOneOfInstances(array $enums): bool
232 74
    {
233 74
        return in_array($this, $enums, true);
234 15
    }
235
236
    /* --- INFO --- */
237 73
238 1
    /** @return int|string */
239 73
    final public static function memberToValue(string $member)
240
    {
241
        if(false === static::memberExists($member)) {
242 73
            static::throwInvalidMemberException($member);
243
            static::throwDefaultInvalidMemberException($member);
244 73
        }
245 66
246 1
        return static::$members[static::class][$member];
247
    }
248 65
249 1
    /** @param int|string $value */
250
    final public static function valueToMember($value): string
251
    {
252 64
        if(false === static::valueExists($value)) {
253 1
            static::throwInvalidValueException($value);
254
            static::throwDefaultInvalidValueException($value);
255 63
        }
256 1
257
        return (string)array_search($value, static::$members[static::class], true);
258
    }
259 62
260 62
    final public static function getMembers(): array
261
    {
262
        static::resolveMembers();
263
264 1
        return array_keys(static::$members[static::class]);
265
    }
266 1
267
    final public static function getValues(): array
268
    {
269 1
        static::resolveMembers();
270
271 1
        return array_values(static::$members[static::class]);
272
    }
273
274 1
    final public static function getMembersAndValues(): array
275
    {
276 1
        static::resolveMembers();
277
278
        return static::$members[static::class];
279
    }
280 1
281
    /* --- SOURCE --- */
282 1
283
    private static function resolveMembers(): void
284
    {
285
        $class = static::class;
286 1
        if(isset(static::$members[$class])) {
287
            return;
288 1
        }
289
290
        $throwMissingResolve = function(string $class): void {
291
            throw PlatenumException::fromMissingResolve($class);
292 1
        };
293
        // reflection instead of method_exists because of PHP 7.4 bug #78632
294 1
        // @see https://bugs.php.net/bug.php?id=78632
295
        $hasResolve = (new \ReflectionClass($class))->hasMethod('resolve');
296
        /** @var array<string,int|string> $members */
297 1
        $members = $hasResolve ? static::resolve() : $throwMissingResolve($class);
0 ignored issues
show
Bug introduced by
The method resolve() does not exist on Thunder\Platenum\Enum\EnumTrait. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

297
        $members = $hasResolve ? static::/** @scrutinizer ignore-call */ resolve() : $throwMissingResolve($class);
Loading history...
298
        if(empty($members)) {
299 1
            throw PlatenumException::fromEmptyMembers($class);
300
        }
301
        if(\count($members) !== \count(\array_unique($members))) {
302
            throw PlatenumException::fromNonUniqueMembers($class);
303
        }
304
        if(['string'] !== \array_unique(\array_map('gettype', array_keys($members)))) {
305
            throw PlatenumException::fromNonStringMembers($class);
306
        }
307
        if(1 !== \count(\array_unique(\array_map('gettype', $members)))) {
308
            throw PlatenumException::fromNonUniformMemberValues($class, $members);
309
        }
310
311
        static::$members[$class] = $members;
312
    }
313
314
    /* --- MAGIC --- */
315
316
    final public function __clone()
317
    {
318
        throw PlatenumException::fromMagicMethod(static::class, __FUNCTION__);
319
    }
320
321
    final public function __call(string $name, array $arguments)
322
    {
323
        throw PlatenumException::fromMagicMethod(static::class, __FUNCTION__);
324
    }
325
326
    final public function __invoke()
327
    {
328
        throw PlatenumException::fromMagicMethod(static::class, __FUNCTION__);
329
    }
330
331
    /** @param mixed $name */
332
    final public function __isset($name)
333
    {
334
        throw PlatenumException::fromMagicMethod(static::class, __FUNCTION__);
335
    }
336
337
    /** @param mixed $name */
338
    final public function __unset($name)
339
    {
340
        throw PlatenumException::fromMagicMethod(static::class, __FUNCTION__);
341
    }
342
343
    /** @param mixed $value */
344
    final public function __set(string $name, $value)
345
    {
346
        throw PlatenumException::fromMagicMethod(static::class, __FUNCTION__);
347
    }
348
349
    final public function __get(string $name)
350
    {
351
        throw PlatenumException::fromMagicMethod(static::class, __FUNCTION__);
352
    }
353
}
354