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.
Completed
Branch develop (951dc0)
by Baptiste
01:47
created

Type::of()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 36
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 7

Importance

Changes 0
Metric Value
cc 7
eloc 17
c 0
b 0
f 0
nc 6
nop 1
dl 0
loc 36
ccs 19
cts 19
cp 1
crap 7
rs 8.8333
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Immutable;
5
6
use Innmind\Immutable\{
7
    Specification\PrimitiveType,
8
    Specification\ClassType,
9
    Specification\VariableType,
10
    Specification\MixedType,
11
    Specification\NullableType,
12
    Specification\UnionType
13
};
14
15
final class Type
16
{
17
    /**
18
     * Build the appropriate specification for the given type
19
     *
20
     * @param string $type
21
     *
22
     * @return SpecificationInterface
23
     */
24 472
    public static function of(string $type): SpecificationInterface
25
    {
26 472
        if ($type === '?null') {
27 2
            throw new \ParseError('\'null\' type is already nullable');
28
        }
29
30 470
        if ($type === '?mixed') {
31 2
            throw new \ParseError('\'mixed\' type already accepts \'null\' values');
32
        }
33
34 468
        $type = Str::of($type);
35
36 468
        if ($type->contains('|') && $type->contains('?')) {
37 2
            throw new \ParseError('Nullable expression is not allowed in a union type');
38
        }
39
40 466
        if ($type->contains('|')) {
41 2
            return new UnionType(
42 2
                ...$type->split('|')->reduce(
43 2
                    [],
44
                    static function(array $types, Str $type): array {
45 2
                        $types[] = self::of((string) $type);
46
47 2
                        return $types;
48 2
                    }
49
                )
50
            );
51
        }
52
53 466
        if ($type->startsWith('?')) {
54 2
            return new NullableType(
55 2
                self::ofPrimitive((string) $type->drop(1))
56
            );
57
        }
58
59 466
        return self::ofPrimitive((string) $type);
60
    }
61
62
    /**
63
     * Return the type of the given value
64
     *
65
     * @param mixed $value
66
     *
67
     * @return string
68
     */
69 16
    public static function determine($value): string
70
    {
71 16
        $type = \gettype($value);
72
73 8
        switch ($type) {
74 16
            case 'object':
75 2
                return \get_class($value);
76
77 16
            case 'integer':
78 16
                return 'int';
79
80 2
            case 'boolean':
81 2
                return 'bool';
82
83 2
            case 'NULL':
84 2
                return 'null';
85
86 2
            case 'double':
87 2
                return 'float';
88
89
            default:
90 2
                return $type;
91
        }
92
    }
93
94 466
    private static function ofPrimitive(string $type): SpecificationInterface
95
    {
96 466
        if (\function_exists('is_'.$type)) {
97 430
            return new PrimitiveType($type);
98
        }
99
100 204
        if ($type === 'variable') {
101 4
            return new VariableType;
102
        }
103
104 202
        if ($type === 'mixed') {
105 2
            return new MixedType;
106
        }
107
108 202
        return new ClassType($type);
109
    }
110
}
111