Passed
Push — main ( e48091...6c9276 )
by Anatoly
05:36 queued 13s
created

AnnotationReader::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2.864

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 6
ccs 2
cts 5
cp 0.4
rs 10
cc 2
nc 2
nop 0
crap 2.864
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 PHP version less than 8.0.
35
     */
36 506
    public function __construct()
37
    {
38 506
        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
        }
44
    }
45
46
    /**
47
     * @inheritDoc
48
     */
49 501
    public function getAnnotations(ReflectionProperty $target, string $name): Generator
50
    {
51 501
        if (PHP_MAJOR_VERSION < 8) {
52
            return;
53
        }
54
55 501
        $attributes = $target->getAttributes($name, ReflectionAttribute::IS_INSTANCEOF);
56 501
        foreach ($attributes as $attribute) {
57 55
            yield $attribute->newInstance();
58
        }
59
    }
60
}
61