Passed
Push — 1.x ( 44281e...e2dae5 )
by Kevin
02:12
created

TypedParameter::type()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Zenstruck\Callback\Parameter;
4
5
use Zenstruck\Callback\Exception\UnresolveableArgument;
6
use Zenstruck\Callback\Parameter;
7
8
/**
9
 * @author Kevin Bond <[email protected]>
10
 */
11
final class TypedParameter extends Parameter
12
{
13
    /** @var string */
14
    private $type;
15
    private $value;
16
17
    public function __construct(string $type, $value)
18
    {
19
        $this->type = $type;
20
        $this->value = $value;
21
    }
22
23
    public function type(): string
24
    {
25
        return $this->type;
26
    }
27
28
    protected function valueFor(\ReflectionParameter $parameter)
29
    {
30
        $parameterType = $parameter->getType();
31
32
        if (!$parameterType instanceof \ReflectionNamedType) {
33
            throw new UnresolveableArgument('Argument has no type.');
34
        }
35
36
        if ($this->type === $parameterType->getName() || \is_a($parameterType->getName(), $this->type, true)) {
37
            return $this->value;
38
        }
39
40
        throw new UnresolveableArgument('Unable to resolve.');
41
    }
42
}
43