Passed
Push — php8.1-serializalble ( bb75a7...daf8b5 )
by Akihito
01:36
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 Doctrine\Common\Annotations\Reader;
8
use Ray\Di\Di\Qualifier;
9
use ReflectionClass;
10
use ReflectionMethod;
11
use ReflectionParameter;
12
use Serializable;
13
14
use function assert;
15
use function class_exists;
16
use function serialize;
17
use function unserialize;
18
19
final class InjectionPoint implements InjectionPointInterface, Serializable
20
{
21
    /** @var ?ReflectionParameter */
22
    private $parameter;
23
24
    /** @var Reader */
25
    private $reader;
26
27
    /** @var string */
28
    private $pClass;
29
30
    /** @var string */
31
    private $pFunction;
32
33
    /** @var string */
34
    private $pName;
35
36
    public function __construct(ReflectionParameter $parameter, Reader $reader)
37
    {
38
        $this->parameter = $parameter;
39
        $this->pFunction = (string) $parameter->getDeclaringFunction()->name;
40
        $class = $parameter->getDeclaringClass();
41
        $this->pClass = $class instanceof ReflectionClass ? $class->name : '';
42
        $this->pName = $parameter->name;
43
        $this->reader = $reader;
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function getParameter(): ReflectionParameter
50
    {
51
        return $this->parameter ?? new ReflectionParameter([$this->pClass, $this->pFunction], $this->pName);
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function getMethod(): ReflectionMethod
58
    {
59
        $this->parameter = $this->getParameter();
60
        $class = $this->parameter->getDeclaringClass();
61
        assert($class instanceof ReflectionClass);
62
        $method = $this->parameter->getDeclaringFunction()->getShortName();
63
        assert(class_exists($class->name));
64
65
        return new ReflectionMethod($class->name, $method);
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function getClass(): ReflectionClass
72
    {
73
        $this->parameter = $this->getParameter();
74
        $class = $this->parameter->getDeclaringClass();
75
        assert($class instanceof ReflectionClass);
76
77
        return $class;
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function getQualifiers(): array
84
    {
85
        $qualifiers = [];
86
        $annotations = $this->reader->getMethodAnnotations($this->getMethod());
87
        foreach ($annotations as $annotation) {
88
            $qualifier = $this->reader->getClassAnnotation(
89
                new ReflectionClass($annotation),
90
                Qualifier::class
91
            );
92
            if ($qualifier instanceof Qualifier) {
93
                $qualifiers[] = $annotation;
94
            }
95
        }
96
97
        return $qualifiers;
98
    }
99
100
    /**
101
     * @return array{0: Reader, 1: string, 2: string, 3: string}
102
     */
103
    public function __serialize(): array
104
    {
105
        return [$this->reader, $this->pClass, $this->pFunction, $this->pName];
106
    }
107
108
    /**
109
     * {@inheritDoc}
110
     *
111
     * @psalm-param string $serialized
112
     */
113
    public function __unserialize(array $data): void
114
    {
115
        [$this->reader, $this->pClass, $this->pFunction, $this->pName] = $data;
116
    }
117
118
    /**
119
     * {@inheritDoc}
120
     */
121
    public function serialize()
122
    {
123
        return serialize($this->__serialize());
124
    }
125
126
    /**
127
     * {@inheritDoc}
128
     *
129
     * @psalm-param string $serializedData
130
     */
131
    public function unserialize($serializedData)
132
    {
133
        /** @var array{0: Reader, 1: string, 2: string, 3: string} $array */
134
        $array = unserialize($serializedData, ['allowed_classes' => Reader::class]);
135
        $this->__unserialize($array);
136
    }
137
}
138