CallableMethodTrait   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 13
dl 0
loc 26
rs 10
c 2
b 1
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getMethodCall() 0 3 1
A addMethodCall() 0 14 3
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