Completed
Pull Request — master (#37)
by Angelo
03:16 queued 01:16
created

ProxyGenerator   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 2
dl 0
loc 22
ccs 9
cts 9
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A generate() 0 12 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Moka\Generator;
5
6
use Moka\Proxy\ProxyInterface;
7
use Moka\Strategy\MockingStrategyInterface;
8
9
class ProxyGenerator
10
{
11
    private $mockingStrategy;
12
13 8
    public function __construct(MockingStrategyInterface $mockingStrategy)
14
    {
15 8
        $this->mockingStrategy = $mockingStrategy;
16
    }
17
18 8
    final public function generate($fqcn): ProxyInterface
19
    {
20 8
        $object = $this->mockingStrategy->build($fqcn);
21 8
        $codeWillBeEvaluated = ProxyClassGenerator::generateCode(get_class($object));
22
23
        /** @var ProxyInterface|ProxyTrait $proxy */
24 8
        $proxy = eval($codeWillBeEvaluated);
25 8
        $proxy->_moka_setObject($object);
0 ignored issues
show
Bug introduced by
The method _moka_setObject does only exist in Moka\Generator\ProxyTrait, but not in Moka\Proxy\ProxyInterface.

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...
26 8
        $proxy->_moka_setMockingStrategy($this->mockingStrategy);
0 ignored issues
show
Bug introduced by
The method _moka_setMockingStrategy does only exist in Moka\Generator\ProxyTrait, but not in Moka\Proxy\ProxyInterface.

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...
27
28 8
        return $proxy;
29
    }
30
}
31