Completed
Push — master ( 66d031...d772d5 )
by Eugene
01:25
created

ArgumentsResolver::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 5
cp 0.8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 2.032
1
<?php
2
3
namespace ArgumentsResolver;
4
5
abstract class ArgumentsResolver
6
{
7
    /**
8
     * @var \ReflectionFunctionAbstract
9
     */
10
    protected $reflection;
11
12
    /**
13
     * @param mixed $function
14
     */
15 16
    public function __construct($function)
16
    {
17 16
        $this->reflection = $function instanceof \ReflectionFunctionAbstract
18 16
            ? $function
19
            : ReflectionFactory::create($function);
20 16
    }
21
22
    /**
23
     * Resolves function arguments.
24
     *
25
     * @param array $parameters
26
     *
27
     * @return array
28
     *
29
     * @throws UnresolvableArgumentException
30
     */
31 16
    public function resolve(array $parameters)
32
    {
33 16
        if (!$number = $this->reflection->getNumberOfParameters()) {
34 2
            return [];
35
        }
36
37 14
        $arguments = array_fill(0, $number, null);
38
39 14
        foreach ($this->getParameters() as $pos => $parameter) {
40 14
            $result = $this->match($parameter, $parameters);
41
42 14
            if ($result) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $result of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
43 6
                $arguments[$pos] = $result[1];
44 6
                unset($parameters[$result[0]]);
45 6
                continue;
46
            }
47
48 8
            if ($parameter->isDefaultValueAvailable()) {
49 3
                $arguments[$pos] = $parameter->getDefaultValue();
50 3
                continue;
51
            }
52
53 5
            throw new UnresolvableArgumentException($parameter);
54
        }
55
56 9
        return $arguments;
57
    }
58
59
    /**
60
     * Returns an array of reflection parameters.
61
     *
62
     * @return \ReflectionParameter[]
63
     */
64 3
    protected function getParameters()
65
    {
66 3
        return $this->reflection->getParameters();
67
    }
68
69
    /**
70
     * Returns the [key, value] pair if the parameter is matched or null otherwise.
71
     *
72
     * @param \ReflectionParameter $parameter
73
     * @param array                $parameters
74
     *
75
     * @return array|null
76
     */
77
    abstract protected function match(\ReflectionParameter $parameter, array $parameters);
78
}
79