Completed
Pull Request — 2.x (#216)
by Akihito
09:47
created

InjectionPoint::getMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
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\Compiler;
6
7
use Ray\Aop\ReflectionMethod;
8
use Ray\Di\InjectionPointInterface;
9
10
final class InjectionPoint implements InjectionPointInterface
11
{
12
    /**
13
     * @var \ReflectionParameter
14
     */
15
    private $parameter;
16
17
    /**
18
     * @var string
19
     */
20
    private $scriptDir;
21
22
    public function __construct(\ReflectionParameter $parameter, string $scriptDir)
23
    {
24
        $this->parameter = $parameter;
25
        $this->scriptDir = $scriptDir;
26
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function getParameter() : \ReflectionParameter
32
    {
33
        return $this->parameter;
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function getMethod() : \ReflectionMethod
40
    {
41
        $reflectionMethod = $this->parameter->getDeclaringFunction();
42
        assert($reflectionMethod instanceof ReflectionMethod);
43
44
        return $reflectionMethod;
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function getClass() : \ReflectionClass
51
    {
52
        $class = $this->parameter->getDeclaringClass();
53
        if (! $class instanceof \ReflectionClass) {
54
            throw new \LogicException; // @codeCoverageIgnore
55
        }
56
57
        return $class;
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     *
63
     * @return array<null|object>
64
     * @psalm-suppress ImplementedReturnTypeMismatch
65
     */
66
    public function getQualifiers() : array
67
    {
68
        return [$this->getQualifier()];
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     *
74
     * @return null|object
75
     */
76
    public function getQualifier()
77
    {
78
        $class = $this->parameter->getDeclaringClass();
79
        if (! $class instanceof \ReflectionClass) {
80
            throw new \LogicException; // @codeCoverageIgnore
81
        }
82
        $qualifierFile = \sprintf(
83
            ScriptInjector::QUALIFIER,
84
            $this->scriptDir,
85
            \str_replace('\\', '_', $class->name),
86
            $this->parameter->getDeclaringFunction()->name,
87
            $this->parameter->name
88
        );
89
        if (! \file_exists($qualifierFile)) {
90
            return null;
91
        }
92
        $qualifier = \file_get_contents($qualifierFile);
93
        if (\is_bool($qualifier)) {
94
            throw new \RuntimeException; // @codeCoverageIgnore
95
        }
96
97
        return \unserialize($qualifier, ['allowed_classes' => true]);
98
    }
99
}
100