Passed
Pull Request — 1.x (#321)
by Akihito
02:35
created

InputParam::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 10
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\Resource;
6
7
use Ray\Di\InjectorInterface;
8
use ReflectionNamedType;
9
use ReflectionParameter;
10
11
final class InputParam implements ParamInterface
12
{
13
    private readonly string $type;
14
    private readonly bool $isDefaultAvailable;
15
    private readonly mixed $defaultValue;
16
    private readonly InputParamFactory $factory;
17
18
    public function __construct(
19
        ReflectionNamedType $type,
20
        private readonly ReflectionParameter $parameter,
21
    ) {
22
        $this->type = $type->getName();
0 ignored issues
show
Bug introduced by
The property type is declared read-only in BEAR\Resource\InputParam.
Loading history...
23
        $this->isDefaultAvailable = $parameter->isDefaultValueAvailable();
0 ignored issues
show
Bug introduced by
The property isDefaultAvailable is declared read-only in BEAR\Resource\InputParam.
Loading history...
24
        $this->defaultValue = $this->isDefaultAvailable ? $parameter->getDefaultValue() : null;
0 ignored issues
show
Bug introduced by
The property defaultValue is declared read-only in BEAR\Resource\InputParam.
Loading history...
25
        $this->factory = new InputParamFactory(
0 ignored issues
show
Bug introduced by
The property factory is declared read-only in BEAR\Resource\InputParam.
Loading history...
26
            new InputParamEnumHandler(),
27
            new InputParamObjectHandler(),
28
        );
29
    }
30
31
    /**
32
     * {@inheritDoc}
33
     */
34
35
    /** @param array<string, mixed> $query */
36
    public function __invoke(string $varName, array $query, InjectorInterface $injector): mixed
37
    {
38
        return $this->factory->create(
39
            $this->type,
40
            $varName,
41
            $query,
42
            $injector,
43
            $this->parameter,
44
            $this->isDefaultAvailable,
45
            $this->defaultValue,
46
        );
47
    }
48
}
49