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

AttributeHelper   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 75%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 31
ccs 6
cts 8
cp 0.75
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getAttributes() 0 5 1
A getAttribute() 0 3 1
A hasAttribute() 0 3 1
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