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 0
Metric Value
dl 0
loc 20
c 0
b 0
f 0
ccs 13
cts 13
cp 1
rs 8.8571
cc 5
eloc 12
nc 5
nop 3
crap 5
1
<?php
2
/**
3
 * Created by IntelliJ IDEA.
4
 * User: sgoetz
5
 * Date: 15.08.14
6
 * Time: 15:17
7
 */
8
namespace Onigoetz\Imagecache;
9
10
use ReflectionMethod;
11
12
class MethodCaller
13
{
14 57
    public function call($object, $method, $args)
15
    {
16 57
        if (!method_exists($object, $method)) {
17 3
            throw new \LogicException("Method '$method' doesn't exist");
18
        }
19
20 54
        $reflected = new ReflectionMethod(get_class($object), $method);
21 54
        $parameters = $reflected->getParameters();
22
23 54
        $arguments = [];
24 54
        foreach ($parameters as $param) {
25 54
            if (array_key_exists($param->name, $args)) {
26 51
                $arguments[$param->name] = $args[$param->name];
27 17
            } else {
28 32
                $arguments[$param->name] = ($param->isOptional()) ? $param->getDefaultValue() : null;
29
            }
30 18
        }
31
32 54
        return call_user_func_array([$object, $method], $arguments);
33
    }
34
}
35