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

ProxyGenerator::generate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 1
crap 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