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