Completed
Push — php7.4 ( 693674...ee18bf )
by Akihito
01:59
created

InjectionPoint::serialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ray\Di;
6
7
use Doctrine\Common\Annotations\Reader;
8
use Ray\Di\Di\Qualifier;
9
10
final class InjectionPoint implements InjectionPointInterface, \Serializable
11
{
12
    /**
13
     * @var \ReflectionParameter
14
     */
15
    private $parameter;
16
17
    /**
18
     * @var Reader
19
     */
20
    private $reader;
21
22
    /**
23
     * @var string
24
     */
25
    private $pClass;
26
27
    /**
28
     * @var string
29
     */
30
    private $pFunction;
31
32
    /**
33
     * @var string
34
     */
35
    private $pName;
36
37
    public function __construct(\ReflectionParameter $parameter, Reader $reader)
38
    {
39
        $this->parameter = $parameter;
40
        $this->pFunction = $parameter->getDeclaringFunction()->name;
41
        $class = $parameter->getDeclaringClass();
42
        $this->pClass = $class instanceof \ReflectionClass ? $class->name : '';
43
        $this->pName = $parameter->name;
44
        $this->reader = $reader;
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function getParameter() : \ReflectionParameter
51
    {
52
        return $this->parameter ?? new \ReflectionParameter([$this->pClass, $this->pFunction], $this->pName);
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function getMethod() : \ReflectionMethod
59
    {
60
        $class = $this->parameter->getDeclaringClass();
61
        if (! $class instanceof \ReflectionClass) {
62
            throw new \LogicException($this->parameter->getName());
0 ignored issues
show
Bug introduced by
Consider using $this->parameter->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
63
        }
64
        $method = $this->parameter->getDeclaringFunction()->getShortName();
65
66
        return new \ReflectionMethod($class->name, $method);
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function getClass() : \ReflectionClass
73
    {
74
        $class = $this->parameter->getDeclaringClass();
75
        if ($class instanceof \ReflectionClass) {
76
            return $class;
77
        }
78
79
        throw new \LogicException($this->parameter->getName());
0 ignored issues
show
Bug introduced by
Consider using $this->parameter->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85
    public function getQualifiers() : array
86
    {
87
        $qualifiers = [];
88
        $annotations = $this->reader->getMethodAnnotations($this->getMethod());
89
        foreach ($annotations as $annotation) {
90
            $qualifier = $this->reader->getClassAnnotation(
91
                new \ReflectionClass($annotation),
92
                Qualifier::class
93
            );
94
            if ($qualifier instanceof Qualifier) {
95
                $qualifiers[] = $annotation;
96
            }
97
        }
98
99
        return $qualifiers;
100
    }
101
102
    public function serialize() : string
103
    {
104
        return serialize([$this->reader, $this->pClass, $this->pFunction, $this->pName]);
105
    }
106
107
    public function unserialize($serialized)
108
    {
109
        [$this->reader, $this->pClass, $this->pFunction, $this->pName] = unserialize($serialized);
110
    }
111
}
112