Passed
Push — main ( a5f538...a74a35 )
by Fractal
03:00
created

AttributeHelper::getAttributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FRZB\Component\RequestMapper\Helper;
6
7
use Fp\Collections\ArrayList;
8
9
final class AttributeHelper
10
{
11
    public static function hasAttribute(\ReflectionProperty|\ReflectionClass|\ReflectionMethod|\ReflectionFunction|\ReflectionParameter $target, string $attributeClass): bool
12
    {
13
        return null !== self::getAttribute($target, $attributeClass);
14
    }
15
16
    /**
17
     * @template T
18
     *
19
     * @param class-string<T> $attributeClass
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
     * @return null|T
22
     */
23 15
    public static function getAttribute(\ReflectionProperty|\ReflectionClass|\ReflectionMethod|\ReflectionFunction|\ReflectionParameter $target, string $attributeClass): ?object
24
    {
25 15
        return ArrayList::collect(self::getAttributes($target, $attributeClass))->firstElement()->get();
26
    }
27
28
    /**
29
     * @template T
30
     *
31
     * @param class-string<T> $attributeClass
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...
32
     *
33
     * @return T[]
34
     */
35 32
    public static function getAttributes(\ReflectionProperty|\ReflectionClass|\ReflectionMethod|\ReflectionFunction|\ReflectionParameter $target, string $attributeClass): array
36
    {
37 32
        return ArrayList::collect($target->getAttributes($attributeClass, \ReflectionAttribute::IS_INSTANCEOF))
38 32
            ->map(static fn (\ReflectionAttribute $a) => $a->newInstance())
39 32
            ->toArray()
40
        ;
41
    }
42
}
43