AttributeHelper   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 75%

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A hasAttribute() 0 3 1
A getAttributes() 0 5 1
A getAttribute() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FRZB\Component\RequestMapper\Helper;
6
7
use Fp\Collections\ArrayList;
8
use JetBrains\PhpStorm\Immutable;
9
10
/** @internal */
11
#[Immutable]
12
final class AttributeHelper
13
{
14
    private function __construct()
15
    {
16
    }
17
18
    public static function hasAttribute(\ReflectionProperty|\ReflectionClass|\ReflectionMethod|\ReflectionFunction|\ReflectionParameter $target, string $attributeClass): bool
19
    {
20
        return null !== self::getAttribute($target, $attributeClass);
21
    }
22
23
    /**
24
     * @template T
25
     *
26
     * @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...
27
     *
28
     * @return null|T
29
     */
30 15
    public static function getAttribute(\ReflectionProperty|\ReflectionClass|\ReflectionMethod|\ReflectionFunction|\ReflectionParameter $target, string $attributeClass): ?object
31
    {
32 15
        return ArrayList::collect(self::getAttributes($target, $attributeClass))
33
            ->firstElement()
34
            ->get()
35
        ;
36
    }
37
38
    /**
39
     * @template T
40
     *
41
     * @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...
42 29
     *
43
     * @return T[]
44 29
     */
45 29
    public static function getAttributes(\ReflectionProperty|\ReflectionClass|\ReflectionMethod|\ReflectionFunction|\ReflectionParameter $target, string $attributeClass): array
46 29
    {
47
        return ArrayList::collect($target->getAttributes($attributeClass, \ReflectionAttribute::IS_INSTANCEOF))
48
            ->map(static fn (\ReflectionAttribute $a) => $a->newInstance())
49
            ->toArray()
50
        ;
51
    }
52
}
53