Completed
Pull Request — master (#5)
by Eugene
01:02
created

ArgumentsResolver::resolve()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 15
cts 15
cp 1
rs 9.1768
c 0
b 0
f 0
cc 5
nc 5
nop 1
crap 5
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the rybakit/arguments-resolver package.
7
 *
8
 * (c) Eugene Leonovich <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace ArgumentsResolver;
15
16
abstract class ArgumentsResolver
17
{
18
    /**
19
     * @var \ReflectionFunctionAbstract
20
     */
21
    protected $reflection;
22
23 16
    public function __construct($function)
24
    {
25 16
        $this->reflection = $function instanceof \ReflectionFunctionAbstract
26 16
            ? $function
27
            : ReflectionFactory::create($function);
28 16
    }
29
30
    /**
31
     * Resolves function arguments.
32
     *
33
     * @param array $parameters
34
     *
35
     * @throws UnresolvableArgumentException
36
     * @throws \ReflectionException
37
     *
38
     * @return array
39
     */
40 16
    public function resolve(array $parameters) : array
41
    {
42 16
        if (!$number = $this->reflection->getNumberOfParameters()) {
43 2
            return [];
44
        }
45
46 14
        $arguments = \array_fill(0, $number, null);
47
48 14
        foreach ($this->getParameters() as $pos => $parameter) {
49 14
            $result = $this->match($parameter, $parameters);
50
51 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...
52 6
                $arguments[$pos] = $result[1];
53 6
                unset($parameters[$result[0]]);
54 6
                continue;
55
            }
56
57 8
            if ($parameter->isDefaultValueAvailable()) {
58 3
                $arguments[$pos] = $parameter->getDefaultValue();
59 3
                continue;
60
            }
61
62 5
            throw UnresolvableArgumentException::fromParameter($parameter);
63
        }
64
65 9
        return $arguments;
66
    }
67
68
    /**
69
     * Returns an array of reflection parameters.
70
     *
71
     * @return \ReflectionParameter[]
72
     */
73 3
    protected function getParameters() : array
74
    {
75 3
        return $this->reflection->getParameters();
76
    }
77
78
    /**
79
     * Returns the [key, value] pair if the parameter is matched or null otherwise.
80
     *
81
     * @param \ReflectionParameter $parameter
82
     * @param array                $parameters
83
     *
84
     * @return array|null
85
     */
86
    abstract protected function match(\ReflectionParameter $parameter, array $parameters) : ?array;
87
}
88