Passed
Push — master ( b674ac...2bc77e )
by Kirill
04:21
created

AttributeReader::instantiate()   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
cc 1
eloc 1
nc 1
nop 3
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of Spiral Framework package.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Attributes\Internal;
13
14
use Spiral\Attributes\Reader;
15
16
abstract class AttributeReader extends Reader
17
{
18
    /**
19
     * @var Instantiator
20
     */
21
    private $instantiator;
22
23
    /**
24
     * @var Context
25
     */
26
    private $ctx;
27
28
    /**
29
     * AttributeReader constructor.
30
     */
31
    public function __construct()
32
    {
33
        $this->instantiator = new Instantiator();
34
        $this->ctx = new Context();
35
    }
36
37
    /**
38
     * {@inheritDoc}
39
     */
40
    public function getClassMetadata(\ReflectionClass $class, string $name = null): iterable
41
    {
42
        $attributes = $this->getClassAttributes($class, $name);
43
        $context = $this->ctx->getClassContext($class);
44
45
        foreach ($attributes as $attribute => $arguments) {
46
            yield $this->instantiate($attribute, $arguments, $context);
47
        }
48
    }
49
50
    /**
51
     * @param \ReflectionClass $class
52
     * @param string|null $name
53
     * @return iterable<\ReflectionClass, array>
54
     */
55
    abstract protected function getClassAttributes(\ReflectionClass $class, ?string $name): iterable;
56
57
    /**
58
     * @param \ReflectionClass $attribute
59
     * @param array $arguments
60
     * @param string $context
61
     * @return object
62
     */
63
    private function instantiate(\ReflectionClass $attribute, array $arguments, string $context): object
64
    {
65
        return $this->instantiator->instantiate($attribute, $arguments, $context);
66
    }
67
68
    /**
69
     * {@inheritDoc}
70
     */
71
    public function getFunctionMetadata(\ReflectionFunctionAbstract $function, string $name = null): iterable
72
    {
73
        $attributes = $this->getFunctionAttributes($function, $name);
74
        $context = $this->ctx->getCallableContext($function);
75
76
        foreach ($attributes as $attribute => $arguments) {
77
            yield $this->instantiate($attribute, $arguments, $context);
78
        }
79
    }
80
81
    /**
82
     * @param \ReflectionFunctionAbstract $function
83
     * @param string|null $name
84
     * @return iterable<\ReflectionClass, array>
85
     */
86
    abstract protected function getFunctionAttributes(\ReflectionFunctionAbstract $function, ?string $name): iterable;
87
88
    /**
89
     * {@inheritDoc}
90
     */
91
    public function getPropertyMetadata(\ReflectionProperty $property, string $name = null): iterable
92
    {
93
        $attributes = $this->getPropertyAttributes($property, $name);
94
        $context = $this->ctx->getPropertyContext($property);
95
96
        foreach ($attributes as $attribute => $arguments) {
97
            yield $this->instantiate($attribute, $arguments, $context);
98
        }
99
    }
100
101
    /**
102
     * @param \ReflectionProperty $property
103
     * @param string|null $name
104
     * @return iterable<\ReflectionClass, array>
105
     */
106
    abstract protected function getPropertyAttributes(\ReflectionProperty $property, ?string $name): iterable;
107
108
    /**
109
     * {@inheritDoc}
110
     */
111
    public function getConstantMetadata(\ReflectionClassConstant $constant, string $name = null): iterable
112
    {
113
        $attributes = $this->getConstantAttributes($constant, $name);
114
        $context = $this->ctx->getConstantContext($constant);
115
116
        foreach ($attributes as $attribute => $arguments) {
117
            yield $this->instantiate($attribute, $arguments, $context);
118
        }
119
    }
120
121
    /**
122
     * @param \ReflectionClassConstant $const
123
     * @param string|null $name
124
     * @return iterable<\ReflectionClass, array>
125
     */
126
    abstract protected function getConstantAttributes(\ReflectionClassConstant $const, ?string $name): iterable;
127
128
    /**
129
     * {@inheritDoc}
130
     */
131
    public function getParameterMetadata(\ReflectionParameter $parameter, string $name = null): iterable
132
    {
133
        $attributes = $this->getParameterAttributes($parameter, $name);
134
        $context = $this->ctx->getParameterContext($parameter);
135
136
        foreach ($attributes as $attribute => $arguments) {
137
            yield $this->instantiate($attribute, $arguments, $context);
138
        }
139
    }
140
141
    /**
142
     * @param \ReflectionParameter $param
143
     * @param string|null $name
144
     * @return iterable<\ReflectionClass, array>
145
     */
146
    abstract protected function getParameterAttributes(\ReflectionParameter $param, ?string $name): iterable;
147
148
    /**
149
     * @return bool
150
     */
151
    protected function isNativeAttributesAvailable(): bool
152
    {
153
        return \version_compare(\PHP_VERSION, '8.0') >= 0;
154
    }
155
}
156