Passed
Push — main ( 6c9276...76dcd0 )
by Anatoly
06:53 queued 02:07
created

BuiltinAnnotationReader   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
eloc 9
c 0
b 0
f 0
dl 0
loc 32
ccs 4
cts 4
cp 1
rs 10

2 Methods

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