|
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( |
|
|
|
|
|
|
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
|
|
|
|
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.