-
-
Notifications
You must be signed in to change notification settings - Fork 152
Expand file tree
/
Copy pathType.php
More file actions
314 lines (256 loc) · 7.84 KB
/
Type.php
File metadata and controls
314 lines (256 loc) · 7.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
<?php declare(strict_types=1);
/**
* This file is part of the Nette Framework (https://nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/
namespace Nette\Utils;
use Nette;
use function array_map, array_search, array_splice, array_values, count, explode, implode, is_a, is_resource, is_string, strcasecmp, strtolower, substr, trim;
/**
* PHP type reflection.
*/
final readonly class Type
{
/** @var list<string|self> */
private array $types;
private ?string $singleName;
private string $kind; // | &
/**
* Creates a Type object based on reflection. Resolves self, static and parent to the actual class name.
* If the subject has no type, it returns null.
*/
public static function fromReflection(
\ReflectionFunctionAbstract|\ReflectionParameter|\ReflectionProperty $reflection,
): ?self
{
$type = $reflection instanceof \ReflectionFunctionAbstract
? $reflection->getReturnType() ?? ($reflection instanceof \ReflectionMethod ? $reflection->getTentativeReturnType() : null)
: $reflection->getType();
return $type ? self::fromReflectionType($type, $reflection, asObject: true) : null;
}
/** @return ($asObject is true ? self : self|string) */
private static function fromReflectionType(
\ReflectionType $type,
\ReflectionFunctionAbstract|\ReflectionParameter|\ReflectionProperty $of,
bool $asObject,
): self|string
{
if ($type instanceof \ReflectionNamedType) {
$name = self::resolve($type->getName(), $of);
return $asObject
? new self($type->allowsNull() && $name !== 'mixed' ? [$name, 'null'] : [$name])
: $name;
} elseif ($type instanceof \ReflectionUnionType || $type instanceof \ReflectionIntersectionType) {
return new self(
array_map(fn($t) => self::fromReflectionType($t, $of, asObject: false), $type->getTypes()),
$type instanceof \ReflectionUnionType ? '|' : '&',
);
} else {
throw new Nette\InvalidStateException('Unexpected type of ' . Reflection::toString($of));
}
}
/**
* Creates the Type object according to the text notation.
*/
public static function fromString(string $type): self
{
if (!Validators::isTypeDeclaration($type)) {
throw new Nette\InvalidArgumentException("Invalid type '$type'.");
}
if ($type[0] === '?') {
return new self([substr($type, 1), 'null']);
}
$unions = [];
foreach (explode('|', $type) as $part) {
$part = explode('&', trim($part, '()'));
$unions[] = count($part) === 1 ? $part[0] : new self($part, '&');
}
return count($unions) === 1 && $unions[0] instanceof self
? $unions[0]
: new self($unions);
}
/**
* Creates a Type object based on the actual type of value.
*/
public static function fromValue(mixed $value): self
{
$type = get_debug_type($value);
if (is_resource($value)) {
$type = 'mixed';
} elseif (str_ends_with($type, '@anonymous')) {
$parent = substr($type, 0, -10);
$type = $parent === 'class' ? 'object' : $parent;
}
return new self([$type]);
}
/**
* Resolves 'self', 'static' and 'parent' to the actual class name.
*/
public static function resolve(
string $type,
\ReflectionFunction|\ReflectionMethod|\ReflectionParameter|\ReflectionProperty $of,
): string
{
$lower = strtolower($type);
if ($of instanceof \ReflectionFunction) {
return $type;
}
$class = $of->getDeclaringClass();
if ($class === null) {
return $type;
} elseif ($lower === 'self') {
return $class->name;
} elseif ($lower === 'static') {
return ($of instanceof ReflectionMethod ? $of->getOriginalClass() : $class)->name;
} elseif ($lower === 'parent' && $class->getParentClass()) {
return $class->getParentClass()->name;
} else {
return $type;
}
}
/** @param array<string|self> $types */
private function __construct(array $types, string $kind = '|')
{
$o = array_search('null', $types, strict: true);
if ($o !== false) { // null as last
array_splice($types, (int) $o, 1);
$types[] = 'null';
}
$this->types = array_values($types);
$this->singleName = is_string($types[0]) && ($types[1] ?? 'null') === 'null' ? $types[0] : null;
$this->kind = count($types) > 1 ? $kind : '';
}
public function __toString(): string
{
$multi = count($this->types) > 1;
if ($this->singleName !== null) {
return ($multi ? '?' : '') . $this->singleName;
}
$res = [];
foreach ($this->types as $type) {
$res[] = $type instanceof self && $multi ? "($type)" : $type;
}
return implode($this->kind, $res);
}
/**
* Returns a union type that accepts both the current type and the given type.
*/
public function with(string|self $type): self
{
$type = is_string($type) ? self::fromString($type) : $type;
return match (true) {
$this->allows($type) => $this,
$type->allows($this) => $type,
default => new self(array_unique(
array_merge($this->isIntersection() ? [$this] : $this->types, $type->isIntersection() ? [$type] : $type->types),
SORT_REGULAR,
), '|'),
};
}
/**
* Returns the array of subtypes that make up the compound type as strings.
* @return list<string|array<string|array<mixed>>>
*/
public function getNames(): array
{
return array_map(fn($t) => $t instanceof self ? $t->getNames() : $t, $this->types);
}
/**
* Returns the array of subtypes that make up the compound type as Type objects.
* @return list<self>
*/
public function getTypes(): array
{
return array_map(fn($t) => $t instanceof self ? $t : new self([$t]), $this->types);
}
/**
* Returns the type name for simple types, otherwise null.
*/
public function getSingleName(): ?string
{
return $this->singleName;
}
/**
* Returns true whether it is a union type.
*/
public function isUnion(): bool
{
return $this->kind === '|';
}
/**
* Returns true whether it is an intersection type.
*/
public function isIntersection(): bool
{
return $this->kind === '&';
}
/**
* Checks whether it is a simple (non-compound) type. Single nullable types such as ?int are also considered simple.
*/
public function isSimple(): bool
{
return $this->singleName !== null;
}
#[\Deprecated('use isSimple()')]
public function isSingle(): bool
{
return $this->singleName !== null;
}
/**
* Checks whether it is a simple PHP built-in type (int, string, bool, etc.).
*/
public function isBuiltin(): bool
{
return $this->singleName !== null && Validators::isBuiltinType($this->singleName);
}
/**
* Checks whether it is a simple class or interface name (not a built-in type).
*/
public function isClass(): bool
{
return $this->singleName !== null && !Validators::isBuiltinType($this->singleName);
}
/**
* Determines if type is special class name self/parent/static.
*/
public function isClassKeyword(): bool
{
return $this->singleName !== null && Validators::isClassKeyword($this->singleName);
}
/**
* Checks whether a value of the given type could be assigned to this type.
*/
public function allows(string|self $type): bool
{
if ($this->types === ['mixed']) {
return true;
}
$type = is_string($type) ? self::fromString($type) : $type;
return $type->isUnion()
? Arrays::every($type->types, fn($t) => $this->allowsAny($t instanceof self ? $t->types : [$t]))
: $this->allowsAny($type->types);
}
/** @param array<string> $givenTypes */
private function allowsAny(array $givenTypes): bool
{
return $this->isUnion()
? Arrays::some($this->types, fn($t) => $this->allowsAll($t instanceof self ? $t->types : [$t], $givenTypes))
: $this->allowsAll($this->types, $givenTypes);
}
/**
* @param array<string> $ourTypes
* @param array<string> $givenTypes
*/
private function allowsAll(array $ourTypes, array $givenTypes): bool
{
return Arrays::every(
$ourTypes,
fn(string $ourType) => Arrays::some(
$givenTypes,
fn(string $givenType) => Validators::isBuiltinType($ourType)
? strcasecmp($ourType, $givenType) === 0
: is_a($givenType, $ourType, allow_string: true),
),
);
}
}