Passed
Push — main ( a74a35...66bb0f )
by Fractal
03:01
created

ParamConverterHelper::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 0
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 2
ccs 0
cts 0
cp 0
crap 2
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\ParamConverter;
9
use JetBrains\PhpStorm\Immutable;
10
use JetBrains\PhpStorm\Pure;
11
12
/** @internal */
13
#[Immutable]
14
final class ParamConverterHelper
15
{
16
    private const REQUEST_POSTFIXES = ['Request', 'Dto', 'DTO'];
17
18
    private function __construct()
19
    {
20
    }
21
22 20
    public static function getAttribute(\ReflectionParameter $parameter, array $attributes): ?ParamConverter
23
    {
24 20
        return $attributes[$parameter->getName()]
25 13
            ?? $attributes[$parameter->getType()?->/** @scrutinizer ignore-call */ getName()]
26 20
            ?? self::searchAttribute($parameter, $attributes);
27
    }
28
29 20
    #[Pure]
30
    public static function mapParamConverter(ParamConverter $paramConverter): array
31
    {
32 20
        return [$paramConverter->getParameterName() ?? $paramConverter->getParameterClass() => $paramConverter];
33
    }
34
35 20
    public static function fromReflectionAttribute(\ReflectionAttribute $attribute): ParamConverter
36
    {
37 20
        return $attribute->newInstance();
38
    }
39
40
    /** @return array<string, ParamConverter> */
41 20
    public static function fromReflectionAttributes(\ReflectionAttribute ...$attributes): array
42
    {
43 20
        return ArrayList::collect($attributes)
44 20
            ->map(static fn (\ReflectionAttribute $ra): ParamConverter => self::fromReflectionAttribute($ra))
45 20
            ->map(static fn (ParamConverter $pc): array => self::mapParamConverter($pc))
46 20
            ->reduce(static fn (array $prev, array $next) => [...$prev, ...$next])
47 20
            ->getOrElse([])
48
        ;
49
    }
50
51 7
    public static function fromReflectionParameter(\ReflectionParameter $parameter): ?ParamConverter
52
    {
53 7
        $parameterName = $parameter->getName();
54 7
        $parameterType = $parameter->getType()?->/** @scrutinizer ignore-call */ getName();
55
56
        return match (true) {
57 7
            !$parameterType, !ClassHelper::isNameContains($parameterType, ...self::REQUEST_POSTFIXES) => null,
58 7
            default => new ParamConverter(parameterClass: $parameterType, parameterName: $parameterName),
59
        };
60
    }
61
62 7
    private static function searchAttribute(\ReflectionParameter $parameter, array $attributes): ?ParamConverter
63
    {
64 7
        return ArrayList::collect($attributes)
65 7
            ->first(static fn (ParamConverter $pc): bool => $pc->equals($parameter))
66 7
            ->getOrElse(self::fromReflectionParameter($parameter))
67
        ;
68
    }
69
}
70