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

Helper   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 80%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 4
c 2
b 0
f 0
lcom 0
cbo 1
dl 0
loc 34
ccs 8
cts 10
cp 0.8
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A parseParams() 0 20 4
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