MethodCaller::call()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 20
ccs 13
cts 13
cp 1
rs 8.8571
cc 5
eloc 12
nc 5
nop 3
crap 5
1
<?php
2
3
namespace Onigoetz\Deployer;
4
5
use ReflectionMethod;
6
7
class MethodCaller
8
{
9 9
    public function call($object, $method, $args)
10
    {
11 9
        if (!method_exists($object, $method)) {
12 3
            throw new \LogicException("Method '$method' doesn't exist");
13
        }
14
15 6
        $reflected = new ReflectionMethod(get_class($object), $method);
16 6
        $parameters = $reflected->getParameters();
17
18 6
        $arguments = [];
19 6
        foreach ($parameters as $param) {
20 6
            if (array_key_exists($param->name, $args)) {
21 6
                $arguments[$param->name] = $args[$param->name];
22 4
            } else {
23 4
                $arguments[$param->name] = ($param->isOptional()) ? $param->getDefaultValue() : null;
24
            }
25 4
        }
26
27 6
        return call_user_func_array([$object, $method], $arguments);
28
    }
29
}
30