CallableMethodTrait::getMethodCall()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Habemus\Definition\MethodCall;
5
6
use Closure;
7
use Habemus\Definition\Build\ArrayDefinition;
8
use Habemus\Exception\DefinitionException;
9
use Psr\Container\ContainerInterface;
10
11
trait CallableMethodTrait
12
{
13
    /**
14
     * @var Closure|null
15
     */
16
    protected $methodsCallback = null;
17
18
    public function addMethodCall(string $method, array $parameters = [])
19
    {
20
        $current = $this->getMethodCall();
21
        $newCallback =
22
            function ($instance, ContainerInterface $container) use ($current, $method, $parameters) {
23
                $_parameters = (new ArrayDefinition($parameters))->getConcrete($container);
24
                $current($instance, $container);
25
                if (!is_object($instance) || !method_exists($instance, $method)) {
26
                    throw DefinitionException::invalidMethodCall($this, $instance, $method);
0 ignored issues
show
Bug introduced by
$this of type Habemus\Definition\MethodCall\CallableMethodTrait is incompatible with the type Habemus\Definition\Definition expected by parameter $definition of Habemus\Exception\Defini...on::invalidMethodCall(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

26
                    throw DefinitionException::invalidMethodCall(/** @scrutinizer ignore-type */ $this, $instance, $method);
Loading history...
27
                }
28
                call_user_func_array([$instance, $method], $_parameters);
29
            };
30
        $this->methodsCallback = $newCallback;
31
        return $this;
32
    }
33
34
    public function getMethodCall(): Closure
35
    {
36
        return $this->methodsCallback ?? function ($i, $c) {
37
        } ;
38
    }
39
}
40