Completed
Push — refactor ( 1ac315 )
by Akihito
05:56
created

InjectionPoint   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 0
dl 0
loc 89
rs 10
c 0
b 0
f 0

6 Methods

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