-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathReflectMethod.php
124 lines (96 loc) · 3.34 KB
/
ReflectMethod.php
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
<?php
declare(strict_types=1);
namespace Crell\AttributeUtils\Attributes\Reflect;
use Crell\AttributeUtils\FromReflectionMethod;
use Crell\AttributeUtils\ParseParameters;
use Crell\AttributeUtils\TypeDef;
#[\Attribute(\Attribute::TARGET_METHOD)]
class ReflectMethod implements FromReflectionMethod, ParseParameters
{
use HasVisibility;
/** @var ReflectParameter[] */
public readonly array $parameters;
/**
* The name of the method, as PHP defines it.
*/
public readonly string $phpName;
/**
* True if this method is defined by an extension, false if in userspace PHP code.
*/
public readonly bool $isInternal;
/**
* True if this method is a generator (contains yield), false otherwise.
*/
public readonly bool $isGenerator;
/**
* True if this method has an explicit variadic parameter, false otherwise.
*/
public readonly bool $isVariadic;
/**
* True if this method returns a value by reference, false otherwise.
*/
public readonly bool $returnsReference;
/**
* True if there is an explicit return type defined, false otherwise.
*/
public readonly bool $hasReturnType;
/**
* True if this is an abstract method, false otherwise.
*/
public readonly bool $isAbstract;
/**
* True if this is a final method, false otherwise.
*/
public readonly bool $isFinal;
/**
* True if this is a static method, false otherwise.
*/
public readonly bool $isStatic;
public readonly MethodType $methodType;
/**
* The return type of this method.
*
* A missing type declaration will be treated as "mixed".
*
* If you need to know whether a return type was specified at all,
* check the $hasReturnType property.
*/
public TypeDef $returnType;
public function fromReflection(\ReflectionMethod $subject): void
{
$this->phpName = $subject->getName();
// @todo I'm not convinced isDeprecated() is useful, so skipping that.
$this->isInternal = $subject->isInternal();
// isUserDefined() is the inverse of isInternal, so no need to cache that.
$this->isGenerator = $subject->isGenerator();
$this->isVariadic = $subject->isVariadic();
$this->returnsReference = $subject->returnsReference();
$this->isAbstract = $subject->isAbstract();
$this->isFinal = $subject->isFinal();
$this->isStatic = $subject->isStatic();
$this->parseVisibility($subject);
$this->methodType = match (true) {
$subject->isConstructor() => MethodType::Constructor,
$subject->isDestructor() => MethodType::Destructor,
default => MethodType::Normal,
};
// @todo Skipping extension info, file lines, doc comment, etc.
// @todo getNumberOfParameters and getNumberOfRequiredParameters seem redundant with having the parameters available.
$this->returnType = new TypeDef($subject->getReturnType());
}
/**
* @param ReflectParameter[] $parameters
*/
public function setParameters(array $parameters): void
{
$this->parameters = $parameters;
}
public function includeParametersByDefault(): bool
{
return true;
}
public function parameterAttribute(): string
{
return ReflectParameter::class;
}
}