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 — develop ( e9751c...18a90f )
by Baptiste
03:20 queued 11s
created

Type::of()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 36
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
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
rs 8.8333
ccs 16
cts 16
cp 1
crap 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 466
     * @return SpecificationInterface
23
     */
24 466
    public static function of(string $type): SpecificationInterface
25 430
    {
26
        if ($type === '?null') {
27
            throw new \ParseError('\'null\' type is already nullable');
28 204
        }
29 4
30
        if ($type === '?mixed') {
31
            throw new \ParseError('\'mixed\' type already accepts \'null\' values');
32 202
        }
33 2
34
        $type = Str::of($type);
35
36 202
        if ($type->contains('|') && $type->contains('?')) {
37
            throw new \ParseError('Nullable expression is not allowed in a union type');
38
        }
39
40
        if ($type->contains('|')) {
41
            return new UnionType(
1 ignored issue
show
Bug introduced by
The call to Innmind\Immutable\Specif...nionType::__construct() has too few arguments starting with second. ( Ignorable by Annotation )

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

41
            return /** @scrutinizer ignore-call */ new UnionType(

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
42
                ...$type->split('|')->reduce(
43
                    [],
44
                    static function(array $types, Str $type): array {
45
                        $types[] = self::of((string) $type);
46 16
47
                        return $types;
48 16
                    }
49
                )
50 8
            );
51 16
        }
52 2
53
        if ($type->startsWith('?')) {
54 16
            return new NullableType(
55 16
                self::ofPrimitive((string) $type->drop(1))
56
            );
57 2
        }
58 2
59
        return self::ofPrimitive((string) $type);
60 2
    }
61 2
62
    /**
63 2
     * Return the type of the given value
64 2
     *
65
     * @param mixed $value
66
     *
67 2
     * @return string
68
     */
69
    public static function determine($value): string
70
    {
71
        $type = \gettype($value);
72
73
        switch ($type) {
74
            case 'object':
75
                return \get_class($value);
76
77
            case 'integer':
78
                return 'int';
79
80
            case 'boolean':
81
                return 'bool';
82
83
            case 'NULL':
84
                return 'null';
85
86
            case 'double':
87
                return 'float';
88
89
            default:
90
                return $type;
91
        }
92
    }
93
94
    private static function ofPrimitive(string $type): SpecificationInterface
95
    {
96
        if (\function_exists('is_'.$type)) {
97
            return new PrimitiveType($type);
98
        }
99
100
        if ($type === 'variable') {
101
            return new VariableType;
102
        }
103
104
        if ($type === 'mixed') {
105
            return new MixedType;
106
        }
107
108
        return new ClassType($type);
109
    }
110
}
111