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

AnnotationReader::getFunctionMetadata()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 9
rs 10
c 0
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 Doctrine\Common\Annotations\AnnotationReader as DoctrineReader;
15
use Doctrine\Common\Annotations\AnnotationRegistry;
16
use Doctrine\Common\Annotations\Reader;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Spiral\Attributes\Reader. 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...
17
use Spiral\Attributes\Exception\InitializationException;
18
use Spiral\Attributes\Reader as BaseReader;
19
20
final class AnnotationReader extends BaseReader
21
{
22
    /**
23
     * @var Reader|null
24
     */
25
    private $reader;
26
27
    /**
28
     * @param Reader|null $reader
29
     */
30
    public function __construct(Reader $reader = null)
31
    {
32
        $this->checkAvailability();
33
34
        $this->reader = $reader ?? new DoctrineReader();
35
36
        // doctrine/annotations ^1.0 compatibility.
37
        if (\method_exists(AnnotationRegistry::class, 'registerLoader')) {
38
            AnnotationRegistry::registerLoader('\\class_exists');
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\Common\Annotati...istry::registerLoader() has been deprecated: This method is deprecated and will be removed in doctrine/annotations 2.0. Annotations will be autoloaded in 2.0. ( Ignorable by Annotation )

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

38
            /** @scrutinizer ignore-deprecated */ AnnotationRegistry::registerLoader('\\class_exists');

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
39
        }
40
    }
41
42
    /**
43
     * @return void
44
     */
45
    private function checkAvailability(): void
46
    {
47
        if ($this->isAvailable()) {
48
            return;
49
        }
50
51
        throw new InitializationException('Requires the "doctrine/annotations" package');
52
    }
53
54
    /**
55
     * @return bool
56
     */
57
    protected function isAvailable(): bool
58
    {
59
        return \interface_exists(Reader::class);
60
    }
61
62
    /**
63
     * {@inheritDoc}
64
     */
65
    public function getClassMetadata(\ReflectionClass $class, string $name = null): iterable
66
    {
67
        $result = $this->reader->getClassAnnotations($class);
0 ignored issues
show
Bug introduced by
The method getClassAnnotations() does not exist on null. ( Ignorable by Annotation )

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

67
        /** @scrutinizer ignore-call */ 
68
        $result = $this->reader->getClassAnnotations($class);

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...
68
69
        return $this->filter($name, $result);
70
    }
71
72
    /**
73
     * @param string|null       $name
74
     * @param iterable|object[] $annotations
75
     * @return object[]
76
     */
77
    private function filter(?string $name, iterable $annotations): iterable
78
    {
79
        if ($name === null) {
80
            yield from $annotations;
81
82
            return;
83
        }
84
85
        foreach ($annotations as $annotation) {
86
            if ($annotation instanceof $name) {
87
                yield $annotation;
88
            }
89
        }
90
    }
91
92
    /**
93
     * {@inheritDoc}
94
     */
95
    public function getFunctionMetadata(\ReflectionFunctionAbstract $function, string $name = null): iterable
96
    {
97
        if ($function instanceof \ReflectionMethod) {
98
            $result = $this->reader->getMethodAnnotations($function);
99
100
            return $this->filter($name, $result);
101
        }
102
103
        return [];
104
    }
105
106
    /**
107
     * {@inheritDoc}
108
     */
109
    public function getPropertyMetadata(\ReflectionProperty $property, string $name = null): iterable
110
    {
111
        $result = $this->reader->getPropertyAnnotations($property);
112
113
        return $this->filter($name, $result);
114
    }
115
116
    /**
117
     * {@inheritDoc}
118
     */
119
    public function getConstantMetadata(\ReflectionClassConstant $constant, string $name = null): iterable
120
    {
121
        return [];
122
    }
123
124
    /**
125
     * {@inheritDoc}
126
     */
127
    public function getParameterMetadata(\ReflectionParameter $parameter, string $name = null): iterable
128
    {
129
        return [];
130
    }
131
}
132