-
-
Notifications
You must be signed in to change notification settings - Fork 133
Expand file tree
/
Copy pathReflectionHierarchy.php
More file actions
110 lines (94 loc) · 2.52 KB
/
Copy pathReflectionHierarchy.php
File metadata and controls
110 lines (94 loc) · 2.52 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
<?php
/*
* This file is part of the PHPBench package
*
* (c) Daniel Leech <daniel@dantleech.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
*/
namespace PhpBench\Reflection;
use IteratorAggregate;
/**
* Contains a reflected class (the "top" class) and all it's ancestors.
*
* @implements IteratorAggregate<int,ReflectionClass>
*/
class ReflectionHierarchy implements IteratorAggregate
{
/**
* @var ReflectionClass[] ordered by leaf class ("top") first
*/
private $reflectionClasses;
/**
* @param ReflectionClass[] $reflectionClasses
*/
public function __construct(array $reflectionClasses = [])
{
$this->reflectionClasses = $reflectionClasses;
}
/**
* Add a reflection class.
*
*/
public function addReflectionClass(ReflectionClass $reflectionClass): void
{
$this->reflectionClasses[] = $reflectionClass;
}
public function getIterator(): \ArrayObject
{
return new \ArrayObject($this->reflectionClasses);
}
/**
* Return the "top" class.
*
* @throws \InvalidArgumentException
*/
public function getTop(): ReflectionClass
{
if (!isset($this->reflectionClasses[0])) {
throw new \InvalidArgumentException(
'Cannot get top reflection class, reflection hierarchy is empty.'
);
}
return $this->reflectionClasses[0];
}
/**
* Return true if the class hierarchy contains the named method.
*
*/
public function hasMethod(string $name): bool
{
foreach ($this->reflectionClasses as $reflectionClass) {
if (isset($reflectionClass->methods[$name])) {
return true;
}
}
return false;
}
/**
* Return true if the class hierarchy contains the named static method.
*
*/
public function hasStaticMethod(string $name): bool
{
foreach ($this->reflectionClasses as $reflectionClass) {
if (isset($reflectionClass->methods[$name])) {
$method = $reflectionClass->methods[$name];
if ($method->isStatic) {
return true;
}
break;
}
}
return false;
}
/**
* Return true if there are no reflection classes here.
*/
public function isEmpty(): bool
{
return 0 === count($this->reflectionClasses);
}
}