Completed
Push — master ( dbd889...95c92d )
by Alberto
02:45
created

PHPUnitMockingStrategy   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 59
Duplicated Lines 22.03 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

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