Passed
Push — master ( 55fca5...85d75f )
by Kirill
04:44 queued 10s
created

NativeAttributeReader   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 89
rs 10
c 0
b 0
f 0
wmc 11

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getFunctionAttributes() 0 4 1
A checkAvailability() 0 4 2
A getConstantAttributes() 0 4 1
A getPropertyAttributes() 0 4 1
A isAvailable() 0 3 1
A __construct() 0 5 1
A format() 0 4 2
A getClassAttributes() 0 4 1
A getParameterAttributes() 0 4 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
16
/**
17
 * @internal NativeAttributeReader is an internal library class, please do not use it in your code.
18
 * @psalm-internal Spiral\Attributes
19
 */
20
class NativeAttributeReader extends AttributeReader
21
{
22
    /**
23
     * NativeAttributeReader constructor.
24
     */
25
    public function __construct()
26
    {
27
        $this->checkAvailability();
28
29
        parent::__construct();
30
    }
31
32
    /**
33
     * @return bool
34
     */
35
    public static function isAvailable(): bool
36
    {
37
        return \version_compare(\PHP_VERSION, '8.0') >= 0;
38
    }
39
40
    /**
41
     * {@inheritDoc}
42
     */
43
    protected function getClassAttributes(\ReflectionClass $class, ?string $name): iterable
44
    {
45
        return $this->format(
46
            $class->getAttributes($name, \ReflectionAttribute::IS_INSTANCEOF)
0 ignored issues
show
Bug introduced by
The method getAttributes() does not exist on ReflectionClass. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

46
            $class->/** @scrutinizer ignore-call */ 
47
                    getAttributes($name, \ReflectionAttribute::IS_INSTANCEOF)

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
47
        );
48
    }
49
50
    /**
51
     * {@inheritDoc}
52
     */
53
    protected function getFunctionAttributes(\ReflectionFunctionAbstract $function, ?string $name): iterable
54
    {
55
        return $this->format(
56
            $function->getAttributes($name, \ReflectionAttribute::IS_INSTANCEOF)
0 ignored issues
show
Bug introduced by
The method getAttributes() does not exist on ReflectionFunctionAbstract. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

56
            $function->/** @scrutinizer ignore-call */ 
57
                       getAttributes($name, \ReflectionAttribute::IS_INSTANCEOF)

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
57
        );
58
    }
59
60
    /**
61
     * {@inheritDoc}
62
     */
63
    protected function getPropertyAttributes(\ReflectionProperty $property, ?string $name): iterable
64
    {
65
        return $this->format(
66
            $property->getAttributes($name, \ReflectionAttribute::IS_INSTANCEOF)
0 ignored issues
show
Bug introduced by
The method getAttributes() does not exist on ReflectionProperty. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

66
            $property->/** @scrutinizer ignore-call */ 
67
                       getAttributes($name, \ReflectionAttribute::IS_INSTANCEOF)

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
67
        );
68
    }
69
70
    /**
71
     * {@inheritDoc}
72
     */
73
    protected function getConstantAttributes(\ReflectionClassConstant $const, ?string $name): iterable
74
    {
75
        return $this->format(
76
            $const->getAttributes($name, \ReflectionAttribute::IS_INSTANCEOF)
0 ignored issues
show
Bug introduced by
The method getAttributes() does not exist on ReflectionClassConstant. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

76
            $const->/** @scrutinizer ignore-call */ 
77
                    getAttributes($name, \ReflectionAttribute::IS_INSTANCEOF)

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
77
        );
78
    }
79
80
    /**
81
     * {@inheritDoc}
82
     */
83
    protected function getParameterAttributes(\ReflectionParameter $param, ?string $name): iterable
84
    {
85
        return $this->format(
86
            $param->getAttributes($name, \ReflectionAttribute::IS_INSTANCEOF)
0 ignored issues
show
Bug introduced by
The method getAttributes() does not exist on ReflectionParameter. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

86
            $param->/** @scrutinizer ignore-call */ 
87
                    getAttributes($name, \ReflectionAttribute::IS_INSTANCEOF)

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
87
        );
88
    }
89
90
    /**
91
     * @return void
92
     */
93
    private function checkAvailability(): void
94
    {
95
        if (!self::isAvailable()) {
96
            throw new InitializationException('Requires the PHP >= 8.0');
97
        }
98
    }
99
100
    /**
101
     * @param iterable<\ReflectionAttribute> $attributes
102
     * @return iterable<\ReflectionClass, array>
103
     * @throws \ReflectionException
104
     */
105
    private function format(iterable $attributes): iterable
106
    {
107
        foreach ($attributes as $attribute) {
108
            yield new \ReflectionClass($attribute->getName()) => $attribute->getArguments();
109
        }
110
    }
111
}
112