Passed
Push — master ( cee9ed...b93948 )
by Alexey
16:09
created

Invoker::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php declare(strict_types = 1);
2
3
namespace Venta\Container;
4
5
use InvalidArgumentException;
6
use Venta\Contracts\Container\ArgumentResolver as ArgumentResolverContract;
7
use Venta\Contracts\Container\Container as ContainerContract;
8
use Venta\Contracts\Container\Invoker as InvokerContract;
9
10
/**
11
 * Class Invoker
12
 *
13
 * @package Venta\Container
14
 */
15
class Invoker implements InvokerContract
16
{
17
18
    /**
19
     * @var ContainerContract
20
     */
21
    private $container;
22
23
    /**
24
     * @var ArgumentResolverContract
25
     */
26
    private $resolver;
27
28
    /**
29
     * Invoker constructor.
30
     *
31
     * @param ContainerContract $container
32
     * @param ArgumentResolverContract $resolver
33
     */
34 44
    public function __construct(ContainerContract $container, ArgumentResolverContract $resolver)
35
    {
36 44
        $this->container = $container;
37 44
        $this->resolver = $resolver;
38 44
    }
39
40
    /**
41
     * @inheritDoc
42
     */
43 15
    public function call($callable, array $arguments = [])
44
    {
45 15
        return $this->invoke(new Invokable($callable), $arguments);
46
    }
47
48
    /**
49
     * @inheritDoc
50
     */
51 32
    public function invoke(Invokable $invokable, array $arguments = [])
52
    {
53 32
        $reflection = $invokable->reflection();
54 32
        $arguments = $this->resolver->resolve($reflection, $arguments);
55
56 28
        if ($invokable->isFunction()) {
57
            // We have Closure or "functionName" string.
58 9
            $callable = $invokable->callable();
59
60 9
            return $callable(...$arguments);
61
        }
62 21
        list($object, $method) = $invokable->callable();
63 21
        if ($reflection->isStatic()) {
64 3
            return $object::$method(...$arguments);
65
        }
66
67 18
        if ($method === '__construct') {
68 12
            return new $object(...$arguments);
69
        }
70
71 11
        if (is_string($object)) {
72 7
            $object = $this->container->get($object);
73
        }
74
75 10
        return $object->$method(...$arguments);
76
    }
77
78
    /**
79
     * @inheritDoc
80
     */
81 1
    public function isCallable($callable): bool
82
    {
83
        try {
84 1
            $invokable = new Invokable($callable);
0 ignored issues
show
Documentation introduced by
$callable is of type *, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
85 1
            if ($invokable->isFunction()) {
86
                return true;
87
            }
88 1
            $object = $invokable->callable()[0];
89
90 1
            return is_object($object) || $this->container->has($object);
91
        } catch (InvalidArgumentException $e) {
92
            return false;
93
        }
94
    }
95
96
97
}