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   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 39
c 1
b 0
f 0
dl 0
loc 94
ccs 41
cts 41
cp 1
rs 10
wmc 17

3 Methods

Rating   Name   Duplication   Size   Complexity  
A determine() 0 22 6
A ofPrimitive() 0 15 4
B of() 0 36 7
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