Passed
Pull Request — main (#28)
by Anatoly
04:30
created

AnnotationReader::getAnnotations()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 10
ccs 4
cts 4
cp 1
rs 10
cc 3
nc 3
nop 2
crap 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 Generator;
17
use LogicException;
18
use ReflectionAttribute;
19
use ReflectionProperty;
20
21
use function sprintf;
22
23
use const PHP_MAJOR_VERSION;
24
25
/**
26
 * @since 3.1.0
27
 */
28
final class AnnotationReader implements AnnotationReaderInterface
29
{
30
31
    /**
32
     * Constructor of the class
33
     *
34
     * @throws LogicException If the PHP version less than 8.0.
35
     */
36
    public function __construct()
37
    {
38
        // @codeCoverageIgnoreStart
39
        if (PHP_MAJOR_VERSION < 8) {
40
            throw new LogicException(sprintf(
41
                'The annotation reader {%s} requires PHP version greater than or equal to 8.0.',
42
                __CLASS__,
43
            ));
44
        } // @codeCoverageIgnoreEnd
45
    }
46
47
    /**
48
     * @inheritDoc
49
     */
50 506
    public function getAnnotations(ReflectionProperty $target, string $name): Generator
51
    {
52
        // @codeCoverageIgnoreStart
53
        if (PHP_MAJOR_VERSION < 8) {
54
            return;
55
        } // @codeCoverageIgnoreEnd
56
57 506
        $attributes = $target->getAttributes($name, ReflectionAttribute::IS_INSTANCEOF);
58 506
        foreach ($attributes as $attribute) {
59 56
            yield $attribute->newInstance();
60
        }
61
    }
62
}
63