Passed
Push — master ( 762564...748440 )
by Rustam
02:08
created

HandlerParametersResolver::resolve()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\RequestModel;
6
7
use Psr\Http\Message\ServerRequestInterface;
8
use Yiisoft\RequestModel\Attribute\HandlerParameterAttributeInterface;
9
use Yiisoft\Router\CurrentRoute;
10
11
/**
12
 * @internal
13
 */
14
class HandlerParametersResolver
15
{
16 15
    public function __construct(private RequestModelFactory $factory, private CurrentRoute $currentRoute)
17
    {
18
    }
19
20
    /**
21
     * @throws \ReflectionException
22
     */
23 9
    public function resolve(array $parameters, ServerRequestInterface $request): array
24
    {
25 9
        return array_merge(
26 9
            $this->getAttributeParams($parameters, $request),
27 9
            $this->factory->createInstances($request, $parameters)
28
        );
29
    }
30
31
    /**
32
     * @param \ReflectionParameter[] $parameters
33
     * @param ServerRequestInterface $request
34
     *
35
     * @return array
36
     */
37 9
    private function getAttributeParams(array $parameters, ServerRequestInterface $request): array
38
    {
39 9
        $actionParameters = [];
40 9
        foreach ($parameters as $parameter) {
41 9
            $attributes = $parameter->getAttributes(
42
                HandlerParameterAttributeInterface::class,
43
                \ReflectionAttribute::IS_INSTANCEOF
44
            );
45 9
            foreach ($attributes as $attribute) {
46
                /** @var HandlerParameterAttributeInterface $attributeInstance */
47 3
                $attributeInstance = $attribute->newInstance();
48
49 3
                $actionParameters[$parameter->getName()] = match ($attributeInstance->getType()) {
50
                    HandlerParameterAttributeInterface::ROUTE_PARAM => $this
51 2
                        ->currentRoute
52 2
                        ->getArgument($attributeInstance->getName()),
0 ignored issues
show
Bug introduced by
It seems like $attributeInstance->getName() can also be of type null; however, parameter $name of Yiisoft\Router\CurrentRoute::getArgument() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

52
                        ->getArgument(/** @scrutinizer ignore-type */ $attributeInstance->getName()),
Loading history...
53 2
                    HandlerParameterAttributeInterface::REQUEST_BODY => $request->getParsedBody(),
54 2
                    HandlerParameterAttributeInterface::REQUEST_ATTRIBUTE => $request->getAttribute(
55 2
                        $attributeInstance->getName()
56
                    ),
57
                    HandlerParameterAttributeInterface::QUERY_PARAM => $request
58 1
                        ->getQueryParams()[$attributeInstance->getName()] ?? null,
59 1
                    HandlerParameterAttributeInterface::UPLOADED_FILES => $request->getUploadedFiles()
60
                };
61
            }
62
        }
63 9
        return $actionParameters;
64
    }
65
}
66