TypedParameter   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 39
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A type() 0 3 1
A valueFor() 0 11 3
1
<?php
2
3
namespace Zenstruck\Callback\Parameter;
4
5
use Zenstruck\Callback\Argument;
6
use Zenstruck\Callback\Exception\UnresolveableArgument;
7
use Zenstruck\Callback\Parameter;
8
use Zenstruck\Callback\ValueFactory;
9
10
/**
11
 * @author Kevin Bond <[email protected]>
12
 */
13
final class TypedParameter extends Parameter
14
{
15
    /** @var string */
16
    private $type;
17
18
    /** @var mixed */
19
    private $value;
20
21
    /** @var int */
22
    private $options;
23
24
    /**
25
     * @param string             $type    The supported type (native or class)
26
     * @param mixed|ValueFactory $value
27
     * @param int                $options {@see Argument::supports()}
28
     */
29
    public function __construct(string $type, $value, int $options = Argument::EXACT|Argument::COVARIANCE|Argument::CONTRAVARIANCE|Argument::VERY_STRICT)
30
    {
31
        $this->type = $type;
32
        $this->value = $value;
33
        $this->options = $options;
34
    }
35
36
    public function type(): string
37
    {
38
        return $this->type;
39
    }
40
41
    protected function valueFor(Argument $argument)
42
    {
43
        if (!$argument->hasType()) {
44
            throw new UnresolveableArgument('Argument has no type.');
45
        }
46
47
        if ($argument->supports($this->type, $this->options)) {
48
            return $this->value;
49
        }
50
51
        throw new UnresolveableArgument('Unable to resolve.');
52
    }
53
}
54