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

TypedParameter   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 30
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A type() 0 3 1
A valueFor() 0 13 4
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