Issues (31)

Helper/RequestBodyHelper.php (1 issue)

Labels
Severity
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\RequestBody;
9
use JetBrains\PhpStorm\Immutable;
10
use JetBrains\PhpStorm\Pure;
11
12
/** @internal */
13
#[Immutable]
14
final class RequestBodyHelper
15
{
16
    private const REQUEST_POSTFIXES = ['Request', 'Dto', 'DTO'];
17
18
    private function __construct()
19
    {
20
    }
21
22
    public static function getAttribute(\ReflectionParameter $parameter, array $attributes): ?RequestBody
23
    {
24
        return $attributes[PropertyHelper::getName($parameter)]
25
            ?? $attributes[PropertyHelper::getTypeName($parameter)]
26
            ?? self::searchAttribute($parameter, $attributes);
27
    }
28
29
    #[Pure]
30
    public static function mapAttribute(RequestBody $paramConverter): array
31
    {
32
        return [$paramConverter->argumentName ?? $paramConverter->requestClass => $paramConverter];
33
    }
34
35
    /** @return array<string, RequestBody> */
36
    public static function fromReflectionAttributes(\ReflectionAttribute ...$attributes): array
37
    {
38
        return ArrayList::collect($attributes)
39
            ->map(static fn (\ReflectionAttribute $ra): RequestBody => $ra->newInstance())
40
            ->map(self::mapAttribute(...))
41
            ->reduce(array_merge(...))
0 ignored issues
show
The type FRZB\Component\RequestMapper\Helper\array_merge was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
42
            ->getOrElse([])
43
        ;
44
    }
45
46
    public static function fromReflectionParameter(\ReflectionParameter $parameter): ?RequestBody
47
    {
48
        $parameterName = $parameter->getName();
49
        $parameterType = PropertyHelper::getTypeName($parameter);
50
51
        return match (true) {
52
            !$parameterType, !ClassHelper::isNameContains($parameterType, ...self::REQUEST_POSTFIXES) => null,
53
            default => new RequestBody(requestClass: $parameterType, argumentName: $parameterName),
54
        };
55
    }
56
57
    private static function searchAttribute(\ReflectionParameter $parameter, array $attributes): ?RequestBody
58
    {
59
        return ArrayList::collect($attributes)
60
            ->first(static fn (RequestBody $pc): bool => $pc->equals($parameter))
61
            ->getOrElse(self::fromReflectionParameter($parameter))
62
        ;
63
    }
64
}
65