jerowork /
graphql-attribute-schema
| 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
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 |