Completed
Pull Request — master (#37)
by Alberto
06:07
created

ProxyGenerator::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 1
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Moka\Generator;
5
6
use Moka\Exception\InvalidArgumentException;
7
use Moka\Exception\MockNotCreatedException;
8
use Moka\Generator\Template\ProxyClassTemplate;
9
use Moka\Proxy\ProxyInterface;
10
use Moka\Strategy\MockingStrategyInterface;
11
12
/**
13
 * Class ProxyGenerator
14
 * @package Moka\Generator
15
 */
16
class ProxyGenerator
17
{
18
    /**
19
     * @var MockingStrategyInterface
20
     */
21
    private $mockingStrategy;
22
23
    /**
24
     * ProxyGenerator constructor.
25
     * @param MockingStrategyInterface $mockingStrategy
26
     */
27
    public function __construct(MockingStrategyInterface $mockingStrategy)
28
    {
29
        $this->mockingStrategy = $mockingStrategy;
30
    }
31
32
    /**
33
     * @param string $fqcn
34
     * @return ProxyInterface
35
     *
36
     * @throws MockNotCreatedException
37
     * @throws InvalidArgumentException
38
     */
39
    public function get(string $fqcn): ProxyInterface
40
    {
41
        $mock = $this->mockingStrategy->build($fqcn);
42
        $mockFQCN = get_class($this->mockingStrategy->get($mock));
43
        $mockClass = new \ReflectionClass($mockFQCN);
44
45
        $proxyCode = ProxyClassTemplate::generate($mockClass);
46
        $proxyFQCN = eval($proxyCode);
47
48
        return $this->getInstance($proxyFQCN)
1 ignored issue
show
Bug introduced by
The method __moka_setMock 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...
49
            ->__moka_setMock($mock)
50
            ->__moka_setMockingStrategy($this->mockingStrategy);
51
    }
52
53
    /**
54
     * @param string $proxyFQCN
55
     * @return ProxyInterface|ProxyTrait
56
     */
57
    protected function getInstance(string $proxyFQCN): ProxyInterface
58
    {
59
        $proxyClass = new \ReflectionClass($proxyFQCN);
60
61
        /** @var ProxyInterface|ProxyTrait $proxy */
62
        $proxy = $proxyClass->newInstanceWithoutConstructor();
63
64
        return $proxy;
65
    }
66
}
67