forked from Roave/BetterReflection
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReflectionProperty.php
More file actions
364 lines (307 loc) · 10.6 KB
/
Copy pathReflectionProperty.php
File metadata and controls
364 lines (307 loc) · 10.6 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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
<?php
declare(strict_types=1);
namespace Roave\BetterReflection\Reflection\Adapter;
use ArgumentCountError;
use OutOfBoundsException;
use PropertyHookType;
use ReflectionException as CoreReflectionException;
use ReflectionMethod as CoreReflectionMethod;
use ReflectionProperty as CoreReflectionProperty;
use Roave\BetterReflection\Reflection\Adapter\Exception\NotImplemented;
use Roave\BetterReflection\Reflection\Exception\NoObjectProvided;
use Roave\BetterReflection\Reflection\Exception\NotAnObject;
use Roave\BetterReflection\Reflection\ReflectionAttribute as BetterReflectionAttribute;
use Roave\BetterReflection\Reflection\ReflectionMethod as BetterReflectionMethod;
use Roave\BetterReflection\Reflection\ReflectionProperty as BetterReflectionProperty;
use Roave\BetterReflection\Reflection\ReflectionPropertyHookType as BetterReflectionPropertyHookType;
use Throwable;
use TypeError;
use ValueError;
use function array_map;
use function gettype;
use function sprintf;
/** @psalm-suppress PropertyNotSetInConstructor */
final class ReflectionProperty extends CoreReflectionProperty
{
/**
* @internal
*
* @see CoreReflectionProperty::IS_FINAL
*/
public const IS_FINAL_COMPATIBILITY = 32;
/**
* @internal
*
* @see CoreReflectionProperty::IS_ABSTRACT
*/
public const IS_ABSTRACT_COMPATIBILITY = 64;
/**
* @internal
*
* @see CoreReflectionProperty::IS_VIRTUAL
*/
public const IS_VIRTUAL_COMPATIBILITY = 512;
/**
* @internal
*
* @see CoreReflectionProperty::IS_PROTECTED_SET
*/
public const IS_PROTECTED_SET_COMPATIBILITY = 2048;
/**
* @internal
*
* @see CoreReflectionProperty::IS_PRIVATE_SET
*/
public const IS_PRIVATE_SET_COMPATIBILITY = 4096;
public function __construct(private BetterReflectionProperty $betterReflectionProperty)
{
unset($this->name);
unset($this->class);
}
/** @return non-empty-string */
public function __toString(): string
{
return $this->betterReflectionProperty->__toString();
}
/** @psalm-mutation-free */
public function getName(): string
{
return $this->betterReflectionProperty->getName();
}
public function getValue(object|null $object = null): mixed
{
try {
return $this->betterReflectionProperty->getValue($object);
} catch (NoObjectProvided) {
return null;
} catch (Throwable $e) {
throw new CoreReflectionException($e->getMessage(), previous: $e);
}
}
/** @psalm-suppress MethodSignatureMismatch */
public function setValue(mixed $objectOrValue, mixed $value = null): void
{
try {
$this->betterReflectionProperty->setValue($objectOrValue, $value);
} catch (NoObjectProvided) {
throw new ArgumentCountError('ReflectionProperty::setValue() expects exactly 2 arguments, 1 given');
} catch (NotAnObject) {
throw new TypeError(sprintf('ReflectionProperty::setValue(): Argument #1 ($objectOrValue) must be of type object, %s given', gettype($objectOrValue)));
} catch (Throwable $e) {
throw new CoreReflectionException($e->getMessage(), previous: $e);
}
}
public function setRawValueWithoutLazyInitialization(object $object, mixed $value): never
{
throw Exception\NotImplementedBecauseItTriggersAutoloading::create();
}
/** @return never */
public function isLazy(object $object): bool
{
throw Exception\NotImplementedBecauseItTriggersAutoloading::create();
}
public function skipLazyInitialization(object $object): never
{
throw Exception\NotImplementedBecauseItTriggersAutoloading::create();
}
/** @psalm-mutation-free */
public function hasType(): bool
{
return $this->betterReflectionProperty->hasType();
}
/** @psalm-mutation-free */
public function getType(): ReflectionUnionType|ReflectionNamedType|ReflectionIntersectionType|null
{
return ReflectionType::fromTypeOrNull($this->betterReflectionProperty->getType());
}
/** @psalm-mutation-free */
public function isPublic(): bool
{
return $this->betterReflectionProperty->isPublic();
}
/** @psalm-mutation-free */
public function isPrivate(): bool
{
return $this->betterReflectionProperty->isPrivate();
}
/** @psalm-mutation-free */
public function isPrivateSet(): bool
{
return $this->betterReflectionProperty->isPrivateSet();
}
/** @psalm-mutation-free */
public function isProtected(): bool
{
return $this->betterReflectionProperty->isProtected();
}
/** @psalm-mutation-free */
public function isProtectedSet(): bool
{
return $this->betterReflectionProperty->isProtectedSet();
}
/** @psalm-mutation-free */
public function isStatic(): bool
{
return $this->betterReflectionProperty->isStatic();
}
/** @psalm-mutation-free */
public function isFinal(): bool
{
return $this->betterReflectionProperty->isFinal();
}
/** @psalm-mutation-free */
public function isAbstract(): bool
{
return $this->betterReflectionProperty->isAbstract();
}
/** @psalm-mutation-free */
public function isDefault(): bool
{
return $this->betterReflectionProperty->isDefault();
}
/** @psalm-mutation-free */
public function isDynamic(): bool
{
return $this->betterReflectionProperty->isDynamic();
}
/**
* @return int-mask-of<self::IS_*>
*
* @psalm-mutation-free
*/
public function getModifiers(): int
{
return $this->betterReflectionProperty->getModifiers();
}
/** @psalm-mutation-free */
public function getDeclaringClass(): ReflectionClass
{
return new ReflectionClass($this->betterReflectionProperty->getImplementingClass());
}
/** @psalm-mutation-free */
public function getDocComment(): string|false
{
return $this->betterReflectionProperty->getDocComment() ?? false;
}
/**
* @codeCoverageIgnore
* @infection-ignore-all
* @psalm-mutation-free
*/
public function setAccessible(bool $accessible): void
{
}
/** @psalm-mutation-free */
public function hasDefaultValue(): bool
{
return $this->betterReflectionProperty->hasDefaultValue();
}
/** @psalm-mutation-free */
public function getDefaultValue(): mixed
{
return $this->betterReflectionProperty->getDefaultValue();
}
public function isInitialized(object|null $object = null): bool
{
try {
return $this->betterReflectionProperty->isInitialized($object);
} catch (Throwable $e) {
throw new CoreReflectionException($e->getMessage(), previous: $e);
}
}
/** @psalm-mutation-free */
public function isPromoted(): bool
{
return $this->betterReflectionProperty->isPromoted();
}
/**
* @param class-string|null $name
*
* @return list<ReflectionAttribute>
*/
public function getAttributes(string|null $name = null, int $flags = 0): array
{
if ($flags !== 0 && $flags !== ReflectionAttribute::IS_INSTANCEOF) {
throw new ValueError('Argument #2 ($flags) must be a valid attribute filter flag');
}
if ($name !== null && $flags & ReflectionAttribute::IS_INSTANCEOF) {
$attributes = $this->betterReflectionProperty->getAttributesByInstance($name);
} elseif ($name !== null) {
$attributes = $this->betterReflectionProperty->getAttributesByName($name);
} else {
$attributes = $this->betterReflectionProperty->getAttributes();
}
return array_map(static fn (BetterReflectionAttribute $betterReflectionAttribute): ReflectionAttribute => new ReflectionAttribute($betterReflectionAttribute), $attributes);
}
/** @psalm-mutation-free */
public function isReadOnly(): bool
{
return $this->betterReflectionProperty->isReadOnly();
}
/** @psalm-mutation-free */
public function isVirtual(): bool
{
return $this->betterReflectionProperty->isVirtual();
}
public function hasHooks(): bool
{
return $this->betterReflectionProperty->hasHooks();
}
public function hasHook(PropertyHookType $type): bool
{
return $this->betterReflectionProperty->hasHook(BetterReflectionPropertyHookType::fromCoreReflectionPropertyHookType($type));
}
public function getHook(PropertyHookType $type): ReflectionMethod|null
{
$hook = $this->betterReflectionProperty->getHook(BetterReflectionPropertyHookType::fromCoreReflectionPropertyHookType($type));
if ($hook === null) {
return null;
}
return new ReflectionMethod($hook);
}
/** @return array{get?: ReflectionMethod, set?: ReflectionMethod} */
public function getHooks(): array
{
return array_map(
static fn (BetterReflectionMethod $betterReflectionMethod): CoreReflectionMethod => new ReflectionMethod($betterReflectionMethod),
$this->betterReflectionProperty->getHooks(),
);
}
public function getSettableType(): ReflectionUnionType|ReflectionNamedType|ReflectionIntersectionType|null
{
$setHook = $this->betterReflectionProperty->getHook(BetterReflectionPropertyHookType::Set);
if ($setHook !== null) {
return ReflectionType::fromTypeOrNull($setHook->getParameters()[0]->getType());
}
if ($this->isVirtual()) {
return new ReflectionNamedType('never');
}
return $this->getType();
}
/** @return never */
public function getRawValue(object $object): mixed
{
throw Exception\NotImplementedBecauseItTriggersAutoloading::create();
}
public function setRawValue(object $object, mixed $value): void
{
if ($this->hasHooks()) {
throw Exception\NotImplementedBecauseItTriggersAutoloading::create();
}
$this->setValue($object, $value);
}
public function __get(string $name): mixed
{
if ($name === 'name') {
return $this->betterReflectionProperty->getName();
}
if ($name === 'class') {
return $this->betterReflectionProperty->getImplementingClass()->getName();
}
throw new OutOfBoundsException(sprintf('Property %s::$%s does not exist.', self::class, $name));
}
public function getMangledName(): string
{
throw new NotImplemented('Not implemented');
}
}