Completed
Push — master ( b72f37...0c7d03 )
by Alberto
15s
created

ProphecyMockingStrategy   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 62
Duplicated Lines 17.74 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 6
dl 11
loc 62
ccs 19
cts 19
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A doBuild() 0 4 1
A doDecorate() 11 11 2
A doGet() 0 13 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
declare(strict_types=1);
3
4
namespace Moka\Plugin\Prophecy;
5
6
use Moka\Exception\MockNotCreatedException;
7
use Moka\Plugin\Prophecy\Token\MaxPriorityToken;
8
use Moka\Strategy\AbstractMockingStrategy;
9
use Moka\Stub\Stub;
10
use Prophecy\Exception\Prophecy\ObjectProphecyException;
11
use Prophecy\Prophecy\MethodProphecy;
12
use Prophecy\Prophecy\ObjectProphecy;
13
use Prophecy\Prophet;
14
15
/**
16
 * Class ProphecyMockingStrategy
17
 * @package Moka\Strategy
18
 */
19
class ProphecyMockingStrategy extends AbstractMockingStrategy
20
{
21
    /**
22
     * @var Prophet
23
     */
24
    private $prophet;
25
26
    /**
27
     * PHPUnitMockingStrategy constructor.
28
     */
29 3
    public function __construct()
30
    {
31 3
        $this->prophet = new Prophet();
32 3
        $this->setMockType(ObjectProphecy::class);
33
    }
34
35
    /**
36
     * @param string $fqcn
37
     * @return ObjectProphecy
38
     */
39 23
    protected function doBuild(string $fqcn)
40
    {
41 23
        return $this->prophet->prophesize($fqcn);
42
    }
43
44
    /**
45
     * @param ObjectProphecy $mock
46
     * @param Stub $stub
47
     * @return void
48
     */
49 23 View Code Duplication
    protected function doDecorate($mock, Stub $stub)
1 ignored issue
show
Duplication introduced by Alberto Villa
This method seems to be duplicated in your project.

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.

Loading history...
50
    {
51 23
        $methodName = $stub->getMethodName();
52 23
        $methodValue = $stub->getMethodValue();
53
54
        /** @var MethodProphecy $partial */
55 23
        $partial = $mock->$methodName(new MaxPriorityToken());
56 23
        $methodValue instanceof \Throwable
57 23
            ? $partial->willThrow($methodValue)
58 23
            : $partial->willReturn($methodValue);
59
    }
60
61
    /**
62
     * @param object $mock
63
     * @return mixed
64
     *
65
     * @throws MockNotCreatedException
66
     */
67 13
    protected function doGet($mock)
68
    {
69
        try {
70 13
            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