Completed
Push — master ( c628d7...8aa25c )
by Miloš
01:17
created

BindResolver::__invoke()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Laganica\Di\Resolver;
4
5
/**
6
 * Class BindResolver
7
 *
8
 * @package Laganica\Di\Resolver
9
 */
10
class BindResolver extends ReflectionResolver
11
{
12
    /**
13
     * @inheritDoc
14
     */
15 1
    public function resolve($definition)
16
    {
17 1
        $class = $definition->getValue();
0 ignored issues
show
Bug introduced by
The method getValue does only exist in Laganica\Di\Definition\DefinitionInterface, but not in Closure.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
18 1
        $params = $this->getConstructorParams($class);
19
20 1
        return new $class(...$params);
21
    }
22
}
23