DoctrineAnnotationReader::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
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\AnnotationReader;
15
16
use Doctrine\Common\Annotations\AnnotationReader;
17
use Doctrine\Common\Annotations\Reader;
18
use Generator;
19
use LogicException;
20
use ReflectionProperty;
21
use Sunrise\Hydrator\AnnotationReaderInterface;
22
23
use function class_exists;
24
use function sprintf;
25
26
/**
27
 * @link https://github.com/doctrine/annotations
28
 *
29
 * @since 3.1.0
30
 */
31
final class DoctrineAnnotationReader implements AnnotationReaderInterface
32
{
33
    private Reader $reader;
34
35
    public function __construct(Reader $reader)
36
    {
37
        $this->reader = $reader;
38
    }
39
40
    /**
41
     * @throws LogicException If the doctrine/annotations package isn't installed on the server.
42
     */
43
    public static function default(): self
44
    {
45
        // @codeCoverageIgnoreStart
46
        if (!class_exists(AnnotationReader::class)) {
47
            throw new LogicException(sprintf(
48
                'The annotation reader {%s} requires the doctrine/annotations package, ' .
49
                'run the command `composer require doctrine/annotations` to resolve it.',
50
                __CLASS__,
51
            ));
52
        } // @codeCoverageIgnoreEnd
53
54
        return new self(new AnnotationReader());
55
    }
56
57
    /**
58
     * @inheritDoc
59
     */
60
    public function getAnnotations(string $name, $holder): Generator
61
    {
62
        if ($holder instanceof ReflectionProperty) {
63
            $annotations = $this->reader->getPropertyAnnotations($holder);
64
            foreach ($annotations as $annotation) {
65
                if ($annotation instanceof $name) {
66
                    yield $annotation;
67
                }
68
            }
69
        }
70
    }
71
}
72