Completed
Push — master ( 6366df...fbe022 )
by Kirill
23s queued 19s
created

NativeAttributeReader   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 82
rs 10
wmc 11

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getFunctionAttributes() 0 3 1
A checkAvailability() 0 4 2
A getConstantAttributes() 0 3 1
A getPropertyAttributes() 0 3 1
A isAvailable() 0 3 1
A __construct() 0 5 1
A format() 0 6 2
A getClassAttributes() 0 3 1
A getParameterAttributes() 0 3 1
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\Exception\InitializationException;
15
use Spiral\Attributes\Exception\SemanticAttributeException;
16
use Spiral\Attributes\Internal\Instantiator\InstantiatorInterface;
17
18
/**
19
 * @internal NativeAttributeReader is an internal library class, please do not use it in your code.
20
 * @psalm-internal Spiral\Attributes
21
 */
22
final class NativeAttributeReader extends AttributeReader
23
{
24
    /**
25
     * @param InstantiatorInterface|null $instantiator
26
     */
27
    public function __construct(InstantiatorInterface $instantiator = null)
28
    {
29
        $this->checkAvailability();
30
31
        parent::__construct($instantiator);
32
    }
33
34
    /**
35
     * @return bool
36
     */
37
    public static function isAvailable(): bool
38
    {
39
        return \version_compare(\PHP_VERSION, '8.0') >= 0;
40
    }
41
42
    /**
43
     * {@inheritDoc}
44
     */
45
    protected function getClassAttributes(\ReflectionClass $class, ?string $name): iterable
46
    {
47
        return $this->format($class, $class->getAttributes($name, \ReflectionAttribute::IS_INSTANCEOF));
48
    }
49
50
    /**
51
     * {@inheritDoc}
52
     */
53
    protected function getFunctionAttributes(\ReflectionFunctionAbstract $function, ?string $name): iterable
54
    {
55
        return $this->format($function, $function->getAttributes($name, \ReflectionAttribute::IS_INSTANCEOF));
56
    }
57
58
    /**
59
     * {@inheritDoc}
60
     */
61
    protected function getPropertyAttributes(\ReflectionProperty $property, ?string $name): iterable
62
    {
63
        return $this->format($property, $property->getAttributes($name, \ReflectionAttribute::IS_INSTANCEOF));
64
    }
65
66
    /**
67
     * {@inheritDoc}
68
     */
69
    protected function getConstantAttributes(\ReflectionClassConstant $const, ?string $name): iterable
70
    {
71
        return $this->format($const, $const->getAttributes($name, \ReflectionAttribute::IS_INSTANCEOF));
72
    }
73
74
    /**
75
     * {@inheritDoc}
76
     */
77
    protected function getParameterAttributes(\ReflectionParameter $param, ?string $name): iterable
78
    {
79
        return $this->format($param, $param->getAttributes($name, \ReflectionAttribute::IS_INSTANCEOF));
80
    }
81
82
    /**
83
     * @return void
84
     */
85
    private function checkAvailability(): void
86
    {
87
        if (!self::isAvailable()) {
88
            throw new InitializationException('Requires the PHP >= 8.0');
89
        }
90
    }
91
92
    /**
93
     * @param \Reflector $context
94
     * @param iterable<\ReflectionAttribute> $attributes
95
     * @return iterable<\ReflectionClass, array>
96
     * @throws \ReflectionException
97
     */
98
    private function format(\Reflector $context, iterable $attributes): iterable
99
    {
100
        foreach ($attributes as $attribute) {
101
            $this->assertClassExists($attribute->getName(), $context);
102
103
            yield new \ReflectionClass($attribute->getName()) => $attribute->getArguments();
104
        }
105
    }
106
}
107