InjectionPoint::getClass()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 7
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ray\Di;
6
7
use Ray\Aop\ReflectionClass;
8
use Ray\Aop\ReflectionMethod;
9
use Ray\Di\Di\Qualifier;
10
use ReflectionParameter;
11
12
use function assert;
13
use function class_exists;
14
15
final class InjectionPoint implements InjectionPointInterface
16
{
17
    private string $pClass;
18
19
    /** @var string */
20
    private $pFunction;
21
22
    /** @var string */
23
    private $pName;
24
25
    public function __construct(private ReflectionParameter $parameter)
26
    {
27
        $this->pFunction = $this->parameter->getDeclaringFunction()->name;
28
        $class = $this->parameter->getDeclaringClass();
29
        $this->pClass = $class instanceof ReflectionClass ? $class->name : '';
30
        $this->pName = $this->parameter->name;
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function getParameter(): ReflectionParameter
37
    {
38
        return $this->parameter;
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function getMethod(): ReflectionMethod
45
    {
46
        $this->parameter = $this->getParameter();
47
        $class = $this->parameter->getDeclaringClass();
48
        $method = $this->parameter->getDeclaringFunction()->getShortName();
49
        assert($class instanceof \ReflectionClass);
50
        assert(class_exists($class->getName()));
51
52
        return new ReflectionMethod($class->getName(), $method);
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function getClass(): ReflectionClass
59
    {
60
        $this->parameter = $this->getParameter();
61
        $class = $this->parameter->getDeclaringClass();
62
        assert($class instanceof \ReflectionClass);
63
64
        return new ReflectionClass($class->getName());
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function getQualifiers(): array
71
    {
72
        $qualifiers = [];
73
        $annotations = $this->getMethod()->getAnnotations();
74
        foreach ($annotations as $annotation) {
75
            $maybeQualifier = (new ReflectionClass($annotation))->getAnnotation(Qualifier::class);
76
            if ($maybeQualifier instanceof Qualifier) {
77
                $qualifiers[] = $annotation;
78
            }
79
        }
80
81
        return $qualifiers;
82
    }
83
84
    /**
85
     * @return array<string>
86
     */
87
    public function __serialize(): array
88
    {
89
        return [$this->pClass, $this->pFunction, $this->pName];
90
    }
91
92
    /**
93
     * @param array<string> $array
94
     */
95
    public function __unserialize(array $array): void
96
    {
97
        [$this->pClass, $this->pFunction, $this->pName] = $array;
98
    }
99
}
100