Passed
Pull Request — main (#27)
by Anatoly
41:39
created

AnnotationReader   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Test Coverage

Coverage 58.33%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 9
c 1
b 0
f 0
dl 0
loc 30
ccs 7
cts 12
cp 0.5833
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A getAnnotations() 0 9 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 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