|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of Spiral Framework package. |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace Spiral\Attributes\Internal; |
|
13
|
|
|
|
|
14
|
|
|
use Spiral\Attributes\Exception\InitializationException; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @internal NativeAttributeReader is an internal library class, please do not use it in your code. |
|
18
|
|
|
* @psalm-internal Spiral\Attributes |
|
19
|
|
|
*/ |
|
20
|
|
|
class NativeAttributeReader extends AttributeReader |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* NativeAttributeReader constructor. |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct() |
|
26
|
|
|
{ |
|
27
|
|
|
$this->checkAvailability(); |
|
28
|
|
|
|
|
29
|
|
|
parent::__construct(); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @return bool |
|
34
|
|
|
*/ |
|
35
|
|
|
public static function isAvailable(): bool |
|
36
|
|
|
{ |
|
37
|
|
|
return \version_compare(\PHP_VERSION, '8.0') >= 0; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* {@inheritDoc} |
|
42
|
|
|
*/ |
|
43
|
|
|
protected function getClassAttributes(\ReflectionClass $class, ?string $name): iterable |
|
44
|
|
|
{ |
|
45
|
|
|
return $this->format( |
|
46
|
|
|
$class->getAttributes($name, \ReflectionAttribute::IS_INSTANCEOF) |
|
|
|
|
|
|
47
|
|
|
); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* {@inheritDoc} |
|
52
|
|
|
*/ |
|
53
|
|
|
protected function getFunctionAttributes(\ReflectionFunctionAbstract $function, ?string $name): iterable |
|
54
|
|
|
{ |
|
55
|
|
|
return $this->format( |
|
56
|
|
|
$function->getAttributes($name, \ReflectionAttribute::IS_INSTANCEOF) |
|
|
|
|
|
|
57
|
|
|
); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* {@inheritDoc} |
|
62
|
|
|
*/ |
|
63
|
|
|
protected function getPropertyAttributes(\ReflectionProperty $property, ?string $name): iterable |
|
64
|
|
|
{ |
|
65
|
|
|
return $this->format( |
|
66
|
|
|
$property->getAttributes($name, \ReflectionAttribute::IS_INSTANCEOF) |
|
|
|
|
|
|
67
|
|
|
); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* {@inheritDoc} |
|
72
|
|
|
*/ |
|
73
|
|
|
protected function getConstantAttributes(\ReflectionClassConstant $const, ?string $name): iterable |
|
74
|
|
|
{ |
|
75
|
|
|
return $this->format( |
|
76
|
|
|
$const->getAttributes($name, \ReflectionAttribute::IS_INSTANCEOF) |
|
|
|
|
|
|
77
|
|
|
); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* {@inheritDoc} |
|
82
|
|
|
*/ |
|
83
|
|
|
protected function getParameterAttributes(\ReflectionParameter $param, ?string $name): iterable |
|
84
|
|
|
{ |
|
85
|
|
|
return $this->format( |
|
86
|
|
|
$param->getAttributes($name, \ReflectionAttribute::IS_INSTANCEOF) |
|
|
|
|
|
|
87
|
|
|
); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @return void |
|
92
|
|
|
*/ |
|
93
|
|
|
private function checkAvailability(): void |
|
94
|
|
|
{ |
|
95
|
|
|
if (!self::isAvailable()) { |
|
96
|
|
|
throw new InitializationException('Requires the PHP >= 8.0'); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @param iterable<\ReflectionAttribute> $attributes |
|
102
|
|
|
* @return iterable<\ReflectionClass, array> |
|
103
|
|
|
* @throws \ReflectionException |
|
104
|
|
|
*/ |
|
105
|
|
|
private function format(iterable $attributes): iterable |
|
106
|
|
|
{ |
|
107
|
|
|
foreach ($attributes as $attribute) { |
|
108
|
|
|
yield new \ReflectionClass($attribute->getName()) => $attribute->getArguments(); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.