Completed
Push — master ( 7ee214...32fd8d )
by Alberto
19s
created

ProxyGenerator   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 51
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0
wmc 3
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A get() 0 13 1
A getInstance() 0 9 1
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\ClassTemplate;
9
use Moka\Proxy\ProxyInterface;
10
use Moka\Proxy\ProxyTrait;
11
use Moka\Strategy\MockingStrategyInterface;
12
13
/**
14
 * Class ProxyGenerator
15
 * @package Moka\Generator
16
 */
17
class ProxyGenerator
18
{
19
    /**
20
     * @var MockingStrategyInterface
21
     */
22
    private $mockingStrategy;
23
24
    /**
25
     * ProxyGenerator constructor.
26
     * @param MockingStrategyInterface $mockingStrategy
27
     */
28 4
    public function __construct(MockingStrategyInterface $mockingStrategy)
29
    {
30 4
        $this->mockingStrategy = $mockingStrategy;
31
    }
32
33
    /**
34
     * @param string $fqcn
35
     * @return ProxyInterface
36
     *
37
     * @throws MockNotCreatedException
38
     * @throws InvalidArgumentException
39
     */
40 10
    public function get(string $fqcn): ProxyInterface
41
    {
42 10
        $mock = $this->mockingStrategy->build($fqcn);
43 10
        $mockFQCN = get_class($this->mockingStrategy->get($mock));
44 10
        $mockClass = new \ReflectionClass($mockFQCN);
45
46 10
        $proxyCode = ClassTemplate::generate($mockClass);
47 10
        $proxyFQCN = eval($proxyCode);
48
49 10
        return $this->getInstance($proxyFQCN)
1 ignored issue
show
Bug introduced by
The method __moka_setMock does only exist in Moka\Proxy\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...
50 10
            ->__moka_setMock($mock)
51 10
            ->__moka_setMockingStrategy($this->mockingStrategy);
52
    }
53
54
    /**
55
     * @param string $proxyFQCN
56
     * @return ProxyInterface|ProxyTrait
57
     */
58 10
    protected function getInstance(string $proxyFQCN): ProxyInterface
59
    {
60 10
        $proxyClass = new \ReflectionClass($proxyFQCN);
61
62
        /** @var ProxyInterface|ProxyTrait $proxy */
63 10
        $proxy = $proxyClass->newInstance();
64
65 10
        return $proxy;
66
    }
67
}
68