Passed
Push — php82 ( f26871 )
by Akihito
02:19
created

InjectionPoint::serialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
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
    /** @var ?ReflectionParameter */
18
    private $parameter;
19
20
    /** @var string */
21
    private $pClass;
22
23
    /** @var string */
24
    private $pFunction;
25
26
    /** @var string */
27
    private $pName;
28
29
    public function __construct(ReflectionParameter $parameter)
30
    {
31
        $this->parameter = $parameter;
32
        $this->pFunction = $parameter->getDeclaringFunction()->name;
33
        $class = $parameter->getDeclaringClass();
34
        $this->pClass = $class instanceof ReflectionClass ? $class->name : '';
35
        $this->pName = $parameter->name;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function getParameter(): ReflectionParameter
42
    {
43
        return $this->parameter ?? new ReflectionParameter([$this->pClass, $this->pFunction], $this->pName);
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function getMethod(): ReflectionMethod
50
    {
51
        $this->parameter = $this->getParameter();
52
        $class = $this->parameter->getDeclaringClass();
53
        $method = $this->parameter->getDeclaringFunction()->getShortName();
54
        assert($class instanceof \ReflectionClass);
55
        assert(class_exists($class->getName()));
56
57
        return new ReflectionMethod($class->getName(), $method);
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function getClass(): ReflectionClass
64
    {
65
        $this->parameter = $this->getParameter();
66
        $class = $this->parameter->getDeclaringClass();
67
        assert($class instanceof \ReflectionClass);
68
69
        return new ReflectionClass($class->getName());
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function getQualifiers(): array
76
    {
77
        $qualifiers = [];
78
        $annotations = $this->getMethod()->getAnnotations();
79
        foreach ($annotations as $annotation) {
80
            $maybeQualifier = (new ReflectionClass($annotation))->getAnnotation(Qualifier::class);
81
            if ($maybeQualifier instanceof Qualifier) {
82
                $qualifiers[] = $annotation;
83
            }
84
        }
85
86
        return $qualifiers;
87
    }
88
89
    /**
90
     * @return array<string>
91
     */
92
    public function __serialize(): array
93
    {
94
        return [$this->pClass, $this->pFunction, $this->pName];
95
    }
96
97
    /**
98
     * @param array<string> $array
99
     */
100
    public function __unserialize(array $array): void
101
    {
102
        [$this->pClass, $this->pFunction, $this->pName] = $array;
103
    }
104
}
105