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

FactoryResolver   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 18
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 85.71%

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 6
cts 7
cp 0.8571
rs 10
c 0
b 0
f 0
wmc 3
lcom 0
cbo 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A resolve() 0 12 3
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