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

UnionParameter   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 7 2
A valueFor() 0 11 3
A type() 0 3 1
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 UnionParameter extends Parameter
12
{
13
    /** @var Parameter[] */
14
    private $parameters;
15
16
    public function __construct(Parameter ...$parameters)
17
    {
18
        if (empty($parameters)) {
19
            throw new \InvalidArgumentException('At least one argument is required.');
20
        }
21
22
        $this->parameters = $parameters;
23
    }
24
25
    public function type(): string
26
    {
27
        return \implode('|', \array_map(static function(Parameter $param) { return $param->type(); }, $this->parameters));
28
    }
29
30
    protected function valueFor(\ReflectionParameter $refParameter)
31
    {
32
        foreach ($this->parameters as $parameter) {
33
            try {
34
                return $parameter->resolve($refParameter);
35
            } catch (UnresolveableArgument $e) {
36
                continue;
37
            }
38
        }
39
40
        throw new UnresolveableArgument('Unable to resolve.');
41
    }
42
}
43