|
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) |
|
|
|
|
|
|
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
|
|
|
|
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:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: