Passed
Push — master ( b954d1...3633ed )
by Alexander
13:18 queued 11:45
created

CallableDefinition::prepareCallable()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4.074

Importance

Changes 0
Metric Value
cc 4
eloc 5
nc 3
nop 2
dl 0
loc 10
ccs 5
cts 6
cp 0.8333
crap 4.074
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Factory\Definitions;
6
7
use Psr\Container\ContainerInterface;
8
use ReflectionMethod;
9
use Yiisoft\Injector\Injector;
10
11
class CallableDefinition implements DefinitionInterface
12
{
13
    private $method;
14
15 2
    public function __construct($method)
16
    {
17 2
        $this->method = $method;
18 2
    }
19
20 2
    public function resolve(ContainerInterface $container)
21
    {
22 2
        $callable = $this->prepareCallable($this->method, $container);
23
24 2
        return $container->get(Injector::class)->invoke($callable);
25
    }
26
27 2
    private function prepareCallable($callable, ContainerInterface $container): callable
28
    {
29 2
        if (is_array($callable) && !is_object($callable[0])) {
30 1
            $reflection = new ReflectionMethod($callable[0], $callable[1]);
31 1
            if (!$reflection->isStatic()) {
32
                $callable[0] = $container->get($callable[0]);
33
            }
34
        }
35
36 2
        return $callable;
37
    }
38
}
39