Completed
Push — master ( 6366df...fbe022 )
by Kirill
23s queued 19s
created

Reader   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 22
c 2
b 0
f 0
dl 0
loc 79
rs 10
wmc 14

6 Methods

Rating   Name   Duplication   Size   Complexity  
A firstClassMetadata() 0 7 2
A firstParameterMetadata() 0 7 2
A firstPropertyMetadata() 0 7 2
A filter() 0 11 4
A firstFunctionMetadata() 0 7 2
A firstConstantMetadata() 0 7 2
1
<?php
2
3
/**
4
 * This file is part of Spiral Framework package.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Attributes;
13
14
abstract class Reader implements ReaderInterface
15
{
16
    /**
17
     * {@inheritDoc}
18
     */
19
    public function firstClassMetadata(\ReflectionClass $class, string $name): ?object
20
    {
21
        foreach ($this->getClassMetadata($class, $name) as $attribute) {
22
            return $attribute;
23
        }
24
25
        return null;
26
    }
27
28
    /**
29
     * {@inheritDoc}
30
     */
31
    public function firstFunctionMetadata(\ReflectionFunctionAbstract $function, string $name): ?object
32
    {
33
        foreach ($this->getFunctionMetadata($function, $name) as $attribute) {
34
            return $attribute;
35
        }
36
37
        return null;
38
    }
39
40
    /**
41
     * {@inheritDoc}
42
     */
43
    public function firstPropertyMetadata(\ReflectionProperty $property, string $name): ?object
44
    {
45
        foreach ($this->getPropertyMetadata($property, $name) as $attribute) {
46
            return $attribute;
47
        }
48
49
        return null;
50
    }
51
52
    /**
53
     * {@inheritDoc}
54
     */
55
    public function firstConstantMetadata(\ReflectionClassConstant $constant, string $name): ?object
56
    {
57
        foreach ($this->getConstantMetadata($constant, $name) as $attribute) {
58
            return $attribute;
59
        }
60
61
        return null;
62
    }
63
64
    /**
65
     * {@inheritDoc}
66
     */
67
    public function firstParameterMetadata(\ReflectionParameter $parameter, string $name): ?object
68
    {
69
        foreach ($this->getParameterMetadata($parameter, $name) as $attribute) {
70
            return $attribute;
71
        }
72
73
        return null;
74
    }
75
76
    /**
77
     * @template T of object
78
     * @param class-string<T>|null $name
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<T>|null at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<T>|null.
Loading history...
79
     * @param iterable<T|object> $annotations
80
     * @return iterable<T>
81
     */
82
    protected function filter(?string $name, iterable $annotations): iterable
83
    {
84
        if ($name === null) {
85
            yield from $annotations;
86
87
            return;
88
        }
89
90
        foreach ($annotations as $annotation) {
91
            if ($annotation instanceof $name) {
92
                yield $annotation;
93
            }
94
        }
95
    }
96
}
97