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

NativeAttributeReader::format()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
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;
13
14
use Spiral\Attributes\Exception\InitializationException;
15
use Spiral\Attributes\Internal\AttributeReader;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Spiral\Attributes\AttributeReader. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
16
17
class NativeAttributeReader extends AttributeReader
18
{
19
    /**
20
     * NativeAttributeReader constructor.
21
     */
22
    public function __construct()
23
    {
24
        $this->checkAvailability();
25
26
        parent::__construct();
27
    }
28
29
    /**
30
     * @return bool
31
     */
32
    public static function isAvailable(): bool
33
    {
34
        return \version_compare(\PHP_VERSION, '8.0') >= 0;
35
    }
36
37
    /**
38
     * @return void
39
     */
40
    private function checkAvailability(): void
41
    {
42
        if (! self::isAvailable()) {
43
            throw new InitializationException('Requires the PHP >= 8.0');
44
        }
45
    }
46
47
    /**
48
     * @param \ReflectionAttribute[] $attributes
49
     * @return iterable<\ReflectionClass, array>
50
     */
51
    private function format(iterable $attributes): iterable
52
    {
53
        foreach ($attributes as $attribute) {
54
            yield new \ReflectionClass($attribute->getName()) => $attribute->getArguments();
55
        }
56
    }
57
58
    /**
59
     * {@inheritDoc}
60
     */
61
    protected function getClassAttributes(\ReflectionClass $class, ?string $name): iterable
62
    {
63
        return $this->format(
64
            $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

64
            $class->/** @scrutinizer ignore-call */ 
65
                    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...
65
        );
66
    }
67
68
    /**
69
     * {@inheritDoc}
70
     */
71
    protected function getFunctionAttributes(\ReflectionFunctionAbstract $function, ?string $name): iterable
72
    {
73
        return $this->format(
74
            $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

74
            $function->/** @scrutinizer ignore-call */ 
75
                       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...
75
        );
76
    }
77
78
    /**
79
     * {@inheritDoc}
80
     */
81
    protected function getPropertyAttributes(\ReflectionProperty $property, ?string $name): iterable
82
    {
83
        return $this->format(
84
            $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

84
            $property->/** @scrutinizer ignore-call */ 
85
                       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...
85
        );
86
    }
87
88
    /**
89
     * {@inheritDoc}
90
     */
91
    protected function getConstantAttributes(\ReflectionClassConstant $const, ?string $name): iterable
92
    {
93
        return $this->format(
94
            $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

94
            $const->/** @scrutinizer ignore-call */ 
95
                    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...
95
        );
96
    }
97
98
    /**
99
     * {@inheritDoc}
100
     */
101
    protected function getParameterAttributes(\ReflectionParameter $param, ?string $name): iterable
102
    {
103
        return $this->format(
104
            $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

104
            $param->/** @scrutinizer ignore-call */ 
105
                    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...
105
        );
106
    }
107
}
108