GetAttributeTrait::getAttribute()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 9
c 0
b 0
f 0
ccs 5
cts 5
cp 1
rs 10
cc 2
nc 2
nop 2
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Jerowork\GraphqlAttributeSchema\NodeParser;
6
7
use ReflectionClass;
8
use ReflectionMethod;
9
use ReflectionParameter;
10
11
/**
12
 * @internal
13
 */
14
trait GetAttributeTrait
15
{
16
    /**
17
     * @template T of object
18
     *
19
     * @param class-string<T> $attributeName
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<T> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<T>.
Loading history...
20
     *
21
     * @throws ParseException
22
     *
23
     * @return T
24
     */
25 25
    public function getAttribute(ReflectionClass|ReflectionMethod|ReflectionParameter $reflector, string $attributeName): object
26
    {
27 25
        $attributes = $reflector->getAttributes($attributeName);
28
29 25
        if ($attributes === []) {
30 9
            throw ParseException::missingAttribute($reflector->getName(), $attributeName);
31
        }
32
33 22
        return array_pop($attributes)->newInstance();
34
    }
35
}
36