Passed
Pull Request — main (#27)
by Anatoly
41:39
created

DoctrineAnnotationReader   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 13
c 1
b 0
f 0
dl 0
loc 48
ccs 9
cts 9
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A default() 0 12 2
A __construct() 0 3 1
A getAnnotations() 0 6 3
1
<?php
2
3
/**
4
 * It's free open-source software released under the MIT License.
5
 *
6
 * @author Anatoly Nekhay <[email protected]>
7
 * @copyright Copyright (c) 2021, Anatoly Nekhay
8
 * @license https://github.com/sunrise-php/hydrator/blob/master/LICENSE
9
 * @link https://github.com/sunrise-php/hydrator
10
 */
11
12
declare(strict_types=1);
13
14
namespace Sunrise\Hydrator;
15
16
use Doctrine\Common\Annotations\AnnotationReader;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Sunrise\Hydrator\AnnotationReader. 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 Doctrine\Common\Annotations\Reader;
18
use Generator;
19
use LogicException;
20
use ReflectionProperty;
21
22
use function class_exists;
23
use function sprintf;
24
25
/**
26
 * @link https://github.com/doctrine/annotations
27
 *
28
 * @since 3.1.0
29
 */
30
final class DoctrineAnnotationReader implements AnnotationReaderInterface
31
{
32
33
    /**
34
     * @var Reader
35
     */
36
    private Reader $reader;
37
38
    /**
39
     * Constructor of the class
40
     *
41
     * @param Reader $reader
42
     */
43 1
    public function __construct(Reader $reader)
44
    {
45 1
        $this->reader = $reader;
46
    }
47
48
    /**
49
     * Creates the class instance with the doctrine's default annotation reader
50
     *
51
     * @return self
52
     *
53
     * @throws LogicException If the doctrine/annotations package isn't installed on the server.
54
     */
55 1
    public static function default(): self
56
    {
57
        // @codeCoverageIgnoreStart
58
        if (!class_exists(AnnotationReader::class)) {
59
            throw new LogicException(sprintf(
60
                'The annotation reader {%s} requires the package doctrine/annotations, ' .
61
                'run the command `composer require doctrine/annotations` to resolve it.',
62
                __CLASS__,
63
            ));
64
        } // @codeCoverageIgnoreEnd
65
66 1
        return new self(new AnnotationReader());
67
    }
68
69
    /**
70
     * @inheritDoc
71
     */
72 1
    public function getAnnotations(ReflectionProperty $target, string $name): Generator
73
    {
74 1
        $annotations = $this->reader->getPropertyAnnotations($target);
75 1
        foreach ($annotations as $annotation) {
76 1
            if ($annotation instanceof $name) {
77 1
                yield $annotation;
78
            }
79
        }
80
    }
81
}
82