Completed
Push — 2.x ( 39dc4e...b7fd10 )
by Akihito
01:28
created

InjectionPoint::serialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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 42
27
    /**
28 42
     * @var string
29 42
     */
30 42
    private $pFunction;
31
32
    /**
33
     * @var string
34
     */
35 2
    private $pName;
36
37 2
    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 3
        $this->pName = $parameter->name;
44
        $this->reader = $reader;
45 3
    }
46 3
47
    /**
48
     * {@inheritdoc}
49 3
     */
50
    public function getParameter() : \ReflectionParameter
51 3
    {
52
        return $this->parameter ?? new \ReflectionParameter([$this->pClass, $this->pFunction], $this->pName);
53
    }
54
55
    /**
56
     * {@inheritdoc}
57 1
     */
58
    public function getMethod() : \ReflectionMethod
59 1
    {
60 1
        $class = $this->parameter->getDeclaringClass();
61 1
        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 2
    /**
70
     * {@inheritdoc}
71 2
     */
72 2
    public function getClass() : \ReflectionClass
73 2
    {
74 2
        $class = $this->parameter->getDeclaringClass();
75 2
        if ($class instanceof \ReflectionClass) {
76 2
            return $class;
77
        }
78 2
79 2
        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 2
     * {@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