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
<?php
declare(strict_types=1);
namespace Laminas\Di\Definition\Reflection;
use Laminas\Di\Definition\ClassDefinitionInterface;
use Laminas\Di\Definition\ParameterInterface;
use ReflectionClass;
use ReflectionParameter;
use function uasort;
class ClassDefinition implements ClassDefinitionInterface
{
private ReflectionClass $reflection;
/** @var array<string, Parameter> */
private ?array $parameters = null;
/** @var list<class-string> */
private ?array $supertypes = null;
/**
* @param class-string|ReflectionClass $class
*/
public function __construct($class)
{
if (! $class instanceof ReflectionClass) {
$class = new ReflectionClass($class);
}
$this->reflection = $class;
}
private function reflectSupertypes(): void
{
$this->supertypes = [];
$class = $this->reflection;
while ($class = $class->getParentClass()) {
$this->supertypes[] = $class->name;
}
}
public function getReflection(): ReflectionClass
{
return $this->reflection;
}
/**
* @return list<class-string>
*/
public function getSupertypes(): array
{
if ($this->supertypes === null) {
$this->reflectSupertypes();
}
return $this->supertypes;
}
/**
* @return string[]
*/
public function getInterfaces(): array
{
return $this->reflection->getInterfaceNames();
}
private function reflectParameters(): void
{
$this->parameters = [];
if (! $this->reflection->hasMethod('__construct')) {
return;
}
$method = $this->reflection->getMethod('__construct');
/** @var ReflectionParameter $parameterReflection */
foreach ($method->getParameters() as $parameterReflection) {
$parameter = new Parameter($parameterReflection);
$this->parameters[$parameter->getName()] = $parameter;
}
uasort(
$this->parameters,
fn(ParameterInterface $a, ParameterInterface $b) => $a->getPosition() - $b->getPosition()
);
}
/**
* @return array<string, Parameter>
*/
public function getParameters(): array
{
if ($this->parameters === null) {
$this->reflectParameters();
}
return $this->parameters;
}
}