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

FactoryResolver::__invoke()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

Changes 0
Metric Value
cc 3
nc 2
nop 2
dl 0
loc 12
ccs 6
cts 7
cp 0.8571
crap 3.0261
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
namespace Laganica\Di\Resolver;
4
5
use Laganica\Di\Exception\ContainerException;
6
use Laganica\Di\FactoryInterface;
7
8
/**
9
 * Class FactoryResolver
10
 *
11
 * @package Laganica\Di\Resolver
12
 */
13
class FactoryResolver extends Resolver
14
{
15
    /**
16
     * @inheritDoc
17
     */
18 2
    public function resolve($definition)
19
    {
20 2
        $factoryClass = $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...
21 2
        $factoryInterface = FactoryInterface::class;
22 2
        $interfaces = class_implements($factoryClass);
23
24 2
        if (empty($interfaces) || !in_array($factoryInterface, $interfaces, true)) {
25
            throw new ContainerException("$factoryClass must implement $factoryInterface");
26
        }
27
28 2
        return (new $factoryClass)($this->getContainer());
29
    }
30
}
31