1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dgame\Annotation; |
4
|
|
|
|
5
|
|
|
use Dgame\Type\Type; |
6
|
|
|
use Dgame\Type\TypeFactory; |
7
|
|
|
use Exception; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class AnnotationType |
11
|
|
|
* @package Dgame\Annotation |
12
|
|
|
*/ |
13
|
|
|
final class AnnotationType |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var Type |
17
|
|
|
*/ |
18
|
|
|
private $type; |
19
|
|
|
/** |
20
|
|
|
* @var AnnotationType|null |
21
|
|
|
*/ |
22
|
|
|
private $next; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* AnnotationType constructor. |
26
|
|
|
* |
27
|
|
|
* @param string $type |
28
|
|
|
* @param AnnotationType|null $next |
|
|
|
|
29
|
|
|
* |
30
|
|
|
* @throws Exception |
31
|
|
|
*/ |
32
|
|
|
private function __construct(string $type, self $next = null) |
33
|
|
|
{ |
34
|
|
|
$this->type = Type::import($type); |
|
|
|
|
35
|
|
|
$this->next = $next; |
|
|
|
|
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param string $type |
40
|
|
|
* |
41
|
|
|
* @return AnnotationType |
|
|
|
|
42
|
|
|
* @throws Exception |
43
|
|
|
*/ |
44
|
|
|
public static function parse(string $type): self |
45
|
|
|
{ |
46
|
|
|
if (preg_match_all('/(\[\s*\])/S', $type, $brackets)) { |
47
|
|
|
$base = str_replace(['[', ']'], '', $type); |
48
|
|
|
$type = new self('array', new self($base)); |
|
|
|
|
49
|
|
|
for ($i = 1, $c = count($brackets[1]); $i < $c; $i++) { |
50
|
|
|
$type = new self('array', $type); |
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return $type; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return new self($type); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return Type |
61
|
|
|
*/ |
62
|
|
|
public function getType(): Type |
63
|
|
|
{ |
64
|
|
|
return $this->type; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return bool |
69
|
|
|
*/ |
70
|
|
|
public function hasNext(): bool |
71
|
|
|
{ |
72
|
|
|
return $this->next !== null; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return AnnotationType|null |
77
|
|
|
*/ |
78
|
|
|
public function next(): ?self |
79
|
|
|
{ |
80
|
|
|
return $this->next; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param mixed $expression |
85
|
|
|
* |
86
|
|
|
* @return bool |
87
|
|
|
* @throws Exception |
88
|
|
|
*/ |
89
|
|
|
public function isImplicit($expression): bool |
90
|
|
|
{ |
91
|
|
|
$type = TypeFactory::expression($expression); |
92
|
|
|
if ($type->isArray() && $this->hasNext()) { |
93
|
|
|
return $this->compare($expression, function (self $type, $expr): bool { |
94
|
|
|
return $type->isImplicit($expr); |
95
|
|
|
}); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $type->isImplicitSame($this->type); |
|
|
|
|
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param mixed $expression |
103
|
|
|
* |
104
|
|
|
* @return bool |
105
|
|
|
* @throws Exception |
106
|
|
|
*/ |
107
|
|
|
public function isSame($expression): bool |
108
|
|
|
{ |
109
|
|
|
$type = TypeFactory::expression($expression); |
110
|
|
|
if ($type->isArray() && $this->hasNext()) { |
111
|
|
|
return $this->compare($expression, function (self $type, $expr): bool { |
112
|
|
|
return $type->isSame($expr); |
113
|
|
|
}); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $type->isSame($this->type); |
|
|
|
|
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param array $expression |
121
|
|
|
* @param callable $callback |
122
|
|
|
* |
123
|
|
|
* @return bool |
124
|
|
|
*/ |
125
|
|
|
private function compare(array $expression, callable $callback): bool |
126
|
|
|
{ |
127
|
|
|
foreach ($expression as $expr) { |
128
|
|
|
if (!$callback($this->next(), $expr)) { |
129
|
|
|
return false; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return true; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @return int |
138
|
|
|
*/ |
139
|
|
|
public function getDimension(): int |
140
|
|
|
{ |
141
|
|
|
$this->iterate($dimension); |
142
|
|
|
|
143
|
|
|
return $dimension; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @return Type|null |
148
|
|
|
*/ |
149
|
|
|
public function getBaseType(): ?Type |
150
|
|
|
{ |
151
|
|
|
$type = $this->iterate(); |
152
|
|
|
|
153
|
|
|
return $type === null ? null : $type->getType(); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param int|null $dimension |
158
|
|
|
* |
159
|
|
|
* @return AnnotationType|null |
160
|
|
|
*/ |
161
|
|
|
private function iterate(int &$dimension = null): ?self |
162
|
|
|
{ |
163
|
|
|
$dimension = 0; |
164
|
|
|
$type = $this; |
165
|
|
|
while ($type !== null && $type->hasNext()) { |
166
|
|
|
$type = $type->next(); |
167
|
|
|
$dimension++; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
return $type; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @return string|null |
175
|
|
|
*/ |
176
|
|
|
public function export(): ?string |
177
|
|
|
{ |
178
|
|
|
$type = $this->getBaseType(); |
179
|
|
|
|
180
|
|
|
return $type === null ? null : $type->export() . str_repeat('[]', $this->getDimension()); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.