These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | declare(strict_types=1); |
||
3 | |||
4 | namespace Moka\Strategy; |
||
5 | |||
6 | use Moka\Exception\MockNotCreatedException; |
||
7 | use Moka\Strategy\Prophecy\NoPriorityToken; |
||
8 | use Moka\Stub\Stub; |
||
9 | use Moka\Stub\StubSet; |
||
10 | use Prophecy\Exception\Prophecy\ObjectProphecyException; |
||
11 | use Prophecy\Prophecy\ObjectProphecy; |
||
12 | use Prophecy\Prophet; |
||
13 | |||
14 | /** |
||
15 | * Class ProphecyMockingStrategy |
||
16 | * @package Moka\Strategy |
||
17 | */ |
||
18 | class ProphecyMockingStrategy extends AbstractMockingStrategy |
||
19 | { |
||
20 | /** |
||
21 | * @var Prophet |
||
22 | */ |
||
23 | private $prophet; |
||
24 | |||
25 | /** |
||
26 | * PHPUnitMockingStrategy constructor. |
||
27 | */ |
||
28 | 2 | public function __construct() |
|
29 | { |
||
30 | 2 | $this->prophet = new Prophet(); |
|
31 | 2 | $this->setMockType(ObjectProphecy::class); |
|
32 | } |
||
33 | |||
34 | /** |
||
35 | * @param string $fqcn |
||
36 | * @return ObjectProphecy |
||
37 | */ |
||
38 | 4 | protected function doBuild(string $fqcn) |
|
39 | { |
||
40 | 4 | return $this->prophet->prophesize($fqcn); |
|
41 | } |
||
42 | |||
43 | /** |
||
44 | * @param ObjectProphecy $mock |
||
45 | * @param StubSet $stubs |
||
46 | * @return void |
||
47 | */ |
||
48 | 1 | View Code Duplication | protected function doDecorate($mock, StubSet $stubs) |
1 ignored issue
–
show
|
|||
49 | { |
||
50 | /** @var Stub $stub */ |
||
51 | 1 | foreach ($stubs as $stub) { |
|
52 | 1 | $methodValue = $stub->getMethodValue(); |
|
53 | |||
54 | 1 | $partial = $mock->{$stub->getMethodName()}(new NoPriorityToken()); |
|
55 | 1 | $methodValue instanceof \Throwable |
|
56 | 1 | ? $partial->willThrow($methodValue) |
|
57 | 1 | : $partial->willReturn($methodValue); |
|
58 | } |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * @param object $mock |
||
63 | * @return mixed |
||
64 | * |
||
65 | * @throws MockNotCreatedException |
||
66 | */ |
||
67 | 4 | protected function doGet($mock) |
|
68 | { |
||
69 | try { |
||
70 | 4 | return $mock->reveal(); |
|
71 | 1 | } catch (ObjectProphecyException $exception) { |
|
72 | 1 | throw new MockNotCreatedException( |
|
73 | 1 | sprintf( |
|
74 | 1 | 'Cannot create mock object: %s', |
|
75 | 1 | $exception->getMessage() |
|
76 | ) |
||
77 | ); |
||
78 | } |
||
79 | } |
||
80 | } |
||
81 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.