Passed
Push — master ( 5992dd...593539 )
by Alain
02:25
created

Helper::parseParams()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4.128

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 20
ccs 8
cts 10
cp 0.8
rs 9.2
cc 4
eloc 12
nc 4
nop 2
crap 4.128
1
<?php
2
/**
3
 * Invoker Helper.
4
 *
5
 * @package   BrightNucleus\Invoker
6
 * @author    Alain Schlesser <[email protected]>
7
 * @license   GPL-2.0+
8
 * @link      http://www.brightnucleus.com/
9
 * @copyright 2015-2016 Alain Schlesser, Bright Nucleus
10
 */
11
12
namespace BrightNucleus\Invoker;
13
14
use BrightNucleus\Exception\RuntimeException;
15
use ReflectionParameter;
16
17
/**
18
 * Helper class that provides a function to parse and reorder parameters.
19
 *
20
 * @since   0.1.5
21
 *
22
 * @package BrightNucleus\Invoker
23
 * @author  Alain Schlesser <[email protected]>
24
 */
25
class Helper
26
{
27
28
    /**
29
     *
30
     *
31
     * @since 0.1.0
32
     *
33
     * @param ReflectionParameter[] $params The reflection parameters to parse.
34
     * @param array                 $args   The arguments to check against.
35
     * @return array The correctly ordered arguments to pass to the reflected callable.
36
     * @throws RuntimeException If a $param does not have a name() method.
37
     */
38 10
    public static function parseParams(array $params, $args)
39
    {
40 10
        $ordered_args = array();
41
42 10
        foreach ($params as $param) {
43 10
            if (! $param instanceof ReflectionParameter) {
44
                throw new RuntimeException(
45
                    sprintf(
46
                        _('Parameter %1$s is not an instance of ReflectionParameter.'),
47
                        $param
48
                    )
49
                );
50
            }
51 10
            $ordered_args[] = array_key_exists($param->name, $args)
52 8
                ? $args[$param->name]
53 10
                : $param->getDefaultValue();
54
        }
55
56 8
        return $ordered_args;
57
    }
58
}
59