-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
ReflectClass.php
126 lines (100 loc) · 3.51 KB
/
ReflectClass.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
125
126
<?php
declare(strict_types=1);
namespace Crell\AttributeUtils\Attributes\Reflect;
use Crell\AttributeUtils\ClassType;
use Crell\AttributeUtils\FromReflectionClass;
use Crell\AttributeUtils\ParseClassConstants;
use Crell\AttributeUtils\ParseMethods;
use Crell\AttributeUtils\ParseProperties;
use Crell\AttributeUtils\ParseStaticMethods;
use Crell\AttributeUtils\ParseStaticProperties;
#[\Attribute(\Attribute::TARGET_CLASS)]
class ReflectClass implements FromReflectionClass, ParseMethods, ParseStaticMethods, ParseProperties, ParseStaticProperties, ParseClassConstants
{
use CollectProperties;
use CollectStaticProperties;
use CollectMethods;
use CollectStaticMethods;
use CollectClassConstants;
/**
* The full of the class, including namespace.
*/
public readonly string $phpName;
/**
* The short name of the class, without namespace.
*/
public readonly string $shortName;
/**
* The namespace of the class.
*/
public readonly string $namespace;
/**
* True if this class is defined by an extension, false if in userspace PHP code.
*/
public readonly bool $isInternal;
/**
* True if the class can be instantiated, false otherwise.
*
* @todo Not sure if this is worth capturing.
*/
public readonly bool $isInstantiable;
/**
* True if this class may be cloned, false otherwise.
*/
public readonly bool $isCloneable;
/**
* True if this class can be iterated (is Traversable), false otherwise.
*/
public readonly bool $isIterable;
/**
* True for a final class, false otherwise.
*/
public readonly bool $isFinal;
/**
* The type of struct-y type this is.
*/
public readonly ClassType $structType;
public function fromReflection(\ReflectionClass $subject): void
{
$this->phpName = $subject->getName();
$this->shortName = $subject->getShortName();
$this->namespace = $subject->getNamespaceName();
$this->isInternal = $subject->isInternal();
// isUserDefined() is the inverse of isInternal, so no need to cache that.
$this->isInstantiable = $subject->isInstantiable();
$this->isCloneable = $subject->isCloneable();
$this->structType = match (true) {
$subject->isInterface() => ClassType::Interface,
$subject->isTrait() => ClassType::Trait,
$subject->isAnonymous() => ClassType::AnonymousClass,
default => ClassType::NormalClass,
};
// @todo getFileName, getStartLine, getEndLine - Needed or no? Should they go in a separate struct?
// @todo do we want getDocComment, or is that too much data to cache?
// @todo getTraits(), getTraitNames(), Do we include traits or not?
$this->isFinal = $subject->isFinal();
// @todo getParentClass() returns a ReflectionClass. What do we do with that?
$this->isIterable = $subject->isIterable();
// @todo We're ignoring extension information for now.
}
public function constantAttribute(): string
{
return ReflectClassConstant::class;
}
public function methodAttribute(): string
{
return ReflectMethod::class;
}
public function staticMethodAttribute(): string
{
return ReflectMethod::class;
}
public function propertyAttribute(): string
{
return ReflectProperty::class;
}
public function staticPropertyAttribute(): string
{
return ReflectProperty::class;
}
}