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( |
|
1 ignored issue
–
show
|
|||
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 |
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.