Completed
Pull Request — master (#12)
by Angelo
08:49
created

PHPUnitMockingStrategy   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 59
Duplicated Lines 22.03 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 5
dl 13
loc 59
c 0
b 0
f 0
rs 10

4 Methods

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

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\PHPUnit;
5
6
use Moka\Strategy\AbstractMockingStrategy;
7
use Moka\Stub\Stub;
8
use Moka\Stub\StubSet;
9
use PHPUnit_Framework_MockObject_Generator as MockGenerator;
10
use PHPUnit_Framework_MockObject_Matcher_AnyInvokedCount as AnyInvokedCountMatcher;
11
use PHPUnit_Framework_MockObject_MockObject as MockObject;
12
13
/**
14
 * Class PHPUnitMockingStrategy
15
 * @package Moka\Strategy
16
 */
17
class PHPUnitMockingStrategy extends AbstractMockingStrategy
18
{
19
    /**
20
     * @var MockGenerator
21
     */
22
    private $generator;
23
24
    /**
25
     * PHPUnitMockingStrategy constructor.
26
     */
27
    public function __construct()
28
    {
29
        $this->generator = new MockGenerator();
30
        $this->setMockType(MockObject::class);
31
    }
32
33
    /**
34
     * @param string $fqcn
35
     * @return MockObject
36
     */
37
    protected function doBuild(string $fqcn)
38
    {
39
        return $this->generator->getMock(
40
            $fqcn,
41
            $methods = [],
42
            $arguments = [],
43
            $mockClassName = '',
44
            $callOriginalConstructor = false
45
        );
46
    }
47
48
    /**
49
     * @param MockObject $mock
50
     * @param StubSet $stubs
51
     * @return void
52
     */
53 View Code Duplication
    protected function doDecorate($mock, StubSet $stubs)
1 ignored issue
show
Duplication introduced by
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...
54
    {
55
        /** @var Stub $stub */
56
        foreach ($stubs as $stub) {
57
            $methodName = $stub->getMethodName();
58
            $methodValue = $stub->getMethodValue();
59
60
            $partial = $mock->expects(new AnyInvokedCountMatcher())->method($methodName);
61
            $methodValue instanceof \Throwable
62
                ? $partial->willThrowException($methodValue)
63
                : $partial->willReturn($methodValue);
64
        }
65
    }
66
67
    /**
68
     * @param MockObject $mock
69
     * @return MockObject
70
     */
71
    protected function doGet($mock)
72
    {
73
        return $mock;
74
    }
75
}
76