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

CallableDefinition   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Test Coverage

Coverage 91.67%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
dl 0
loc 26
ccs 11
cts 12
cp 0.9167
rs 10
c 1
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A prepareCallable() 0 10 4
A resolve() 0 5 1
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