GetAttributeTrait   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 20
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
eloc 5
dl 0
loc 20
c 1
b 0
f 0
ccs 5
cts 5
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A getAttribute() 0 9 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