InjectionPoint::getParameter()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ray\Compiler;
6
7
use Override;
0 ignored issues
show
Bug introduced by
The type Override was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Ray\Aop\ReflectionClass;
9
use Ray\Aop\ReflectionMethod;
10
use Ray\Di\Di\Qualifier;
11
use Ray\Di\InjectionPointInterface;
12
use ReflectionAttribute;
13
use ReflectionException;
14
use ReflectionParameter;
15
16
use function assert;
17
use function class_exists;
18
19
/**
20
 * @psalm-import-type ScriptDir from Types
21
 * @psalm-import-type Ip from Types
22
 * @psalm-import-type IpParameters from Types
23
 */
24
final class InjectionPoint implements InjectionPointInterface
25
{
26
    /** @deprecated use getInstance */
27
    public function __construct(private readonly ReflectionParameter $parameter)
28
    {
29
    }
30
31
    /**
32
     * @param Ip $ip
33
     *
34
     * @throws ReflectionException
35
     */
36
    public static function getInstance(array $ip): self
37
    {
38
        return new self(new ReflectionParameter([$ip[0], $ip[1]], $ip[2]));
39
    }
40
41
    /**
42
     * {@inheritDoc}
43
     */
44
    #[Override]
45
    public function getParameter(): ReflectionParameter
46
    {
47
        return $this->parameter;
48
    }
49
50
    /**
51
     * {@inheritDoc}
52
     */
53
    #[Override]
54
    public function getMethod(): ReflectionMethod
55
    {
56
        $class = $this->parameter->getDeclaringClass();
57
        $method = $this->parameter->getDeclaringFunction()->getShortName();
58
        assert($class instanceof \ReflectionClass);
59
        assert(class_exists($class->getName()));
60
61
        return new ReflectionMethod($class->getName(), $method);
62
    }
63
64
    /**
65
     * {@inheritDoc}
66
     */
67
    #[Override]
68
    public function getClass(): ReflectionClass
69
    {
70
        $class = $this->parameter->getDeclaringClass();
71
        assert($class instanceof \ReflectionClass);
72
73
        return new ReflectionClass($class->getName());
74
    }
75
76
    /**
77
     * {@inheritDoc}
78
     *
79
     * @return IpParameters
0 ignored issues
show
Bug introduced by
The type Ray\Compiler\IpParameters was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
80
     *
81
     * @psalm-suppress ImplementedReturnTypeMismatch
82
     */
83
    #[Override]
84
    public function getQualifiers(): array
85
    {
86
        return [$this->getQualifier()];
0 ignored issues
show
Bug Best Practice introduced by
The expression return array($this->getQualifier()) returns the type array<integer,null|object> which is incompatible with the documented return type Ray\Compiler\IpParameters.
Loading history...
87
    }
88
89
    /**
90
     * {@inheritDoc}
91
     *
92
     * @throws ReflectionException
93
     */
94
    public function getQualifier(): object|null
95
    {
96
        // Try method attributes first
97
        $parameter = $this->parameter;
98
        $class = $parameter->getDeclaringClass();
99
        $methodName = $parameter->getDeclaringFunction()->getShortName();
100
        assert($class instanceof \ReflectionClass);
101
102
        $nativeMethod = new \ReflectionMethod($class->getName(), $methodName);
103
        $methodAttributes = $nativeMethod->getAttributes();
104
105
        foreach ($methodAttributes as $attribute) {
106
            if ($this->isQualifier($attribute)) {
107
                return $attribute->newInstance();
108
            }
109
        }
110
111
        // Try parameter attributes
112
        $paramAttributes = $parameter->getAttributes();
113
114
        foreach ($paramAttributes as $attribute) {
115
            if ($this->isQualifier($attribute)) {
116
                return $attribute->newInstance();
117
            }
118
        }
119
120
        return null;
121
    }
122
123
    /**
124
     * @phpstan-param ReflectionAttribute<object> $attribute
125
     *
126
     * @throws ReflectionException
127
     *
128
     * @psalm-suppress TooManyTemplateParams
129
     */
130
    private function isQualifier(ReflectionAttribute $attribute): bool
131
    {
132
        $attributeClass = $attribute->getName();
133
        $reflectionClass = new \ReflectionClass($attributeClass);
134
        $classAttributes = $reflectionClass->getAttributes(Qualifier::class);
135
136
        return $classAttributes !== [];
137
    }
138
}
139