Test Setup Failed
Push — main ( c2f1dd...5e437f )
by Fractal
11:05
created

PropertyHelper::getTypeName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 7
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FRZB\Component\RequestMapper\Helper;
6
7
use Fp\Collections\ArrayList;
8
use FRZB\Component\RequestMapper\Attribute\ArrayType;
9
10
final class PropertyHelper
11
{
12
    public static function getTypeName(\ReflectionProperty $property): string
13
    {
14
        $typeName = $property->getType()?->/** @scrutinizer ignore-call */ getName();
15
16
        return match ($typeName) {
17
            'array' => self::getAttribute($property, ArrayType::class)?->type ?? $typeName,
18
            default => $typeName,
19
        };
20
    }
21
22
    /**
23
     * @template T
24
     *
25
     * @param class-string<T> $attribute
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...
26
     *
27
     * @return null|T
28
     */
29
    public static function getAttribute(\ReflectionProperty $property, string $attribute): ?object
30
    {
31
        return ArrayList::collect($property->getAttributes($attribute, \ReflectionAttribute::IS_INSTANCEOF))
32
            ->firstElement()
33
            ->get()
34
            ?->newInstance()
35
        ;
36
    }
37
}
38