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

CallableDefinition::resolve()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
c 1
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