BuiltinAnnotationReader   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 12
c 0
b 0
f 0
dl 0
loc 34
ccs 7
cts 7
cp 1
rs 10
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A getAnnotations() 0 15 5
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 Generator;
17
use LogicException;
18
use ReflectionAttribute;
19
use ReflectionParameter;
20
use ReflectionProperty;
21
use Sunrise\Hydrator\AnnotationReaderInterface;
22
23
use function sprintf;
24
25
use const PHP_MAJOR_VERSION;
26
27
/**
28
 * @since 3.1.0
29
 */
30
final class BuiltinAnnotationReader implements AnnotationReaderInterface
31
{
32
    /**
33
     * @throws LogicException If the PHP version less than 8.0.
34
     */
35
    public function __construct()
36
    {
37
        // @codeCoverageIgnoreStart
38
        if (PHP_MAJOR_VERSION < 8) {
39
            throw new LogicException(sprintf(
40
                'The annotation reader {%s} requires PHP version greater than or equal to 8.0.',
41
                __CLASS__,
42
            ));
43
        } // @codeCoverageIgnoreEnd
44
    }
45
46
    /**
47
     * @inheritDoc
48
     */
49 2079
    public function getAnnotations(string $name, $holder): Generator
50
    {
51
        // @codeCoverageIgnoreStart
52
        if (PHP_MAJOR_VERSION < 8) {
53
            return;
54
        } // @codeCoverageIgnoreEnd
55
56 2079
        if (! $holder instanceof ReflectionProperty &&
57 2079
            ! $holder instanceof ReflectionParameter) {
58 10
            return;
59
        }
60
61 2069
        $attributes = $holder->getAttributes($name, ReflectionAttribute::IS_INSTANCEOF);
62 2069
        foreach ($attributes as $attribute) {
63 124
            yield $attribute->newInstance();
64
        }
65
    }
66
}
67