Completed
Push — master ( 7ee214...32fd8d )
by Alberto
19s
created

PHPUnitMockingStrategy   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 66
Duplicated Lines 15.15 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 10
loc 66
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A doBuild() 0 10 1
A doGet() 0 4 1
A doDecorateWithMethod() 10 10 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\PHPUnit;
5
6
use Moka\Exception\MissingDependencyException;
7
use Moka\Strategy\AbstractMockingStrategy;
8
use Moka\Stub\MethodStub;
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
    const CLASS_NAME = MockGenerator::class;
20
    const PACKAGE_NAME = 'phpunit/phpunit-mock-object';
21
22
    /**
23
     * @var MockGenerator
24
     */
25
    private $generator;
26
27
    /**
28
     * PHPUnitMockingStrategy constructor.
29
     *
30
     * @throws MissingDependencyException
31
     */
32 6
    public function __construct()
33
    {
34 6
        self::checkDependencies(self::CLASS_NAME, self::PACKAGE_NAME);
35
36 6
        $this->generator = new MockGenerator();
37 6
        $this->setMockType(MockObject::class);
38
    }
39
40
    /**
41
     * @param string $fqcn
42
     * @return MockObject
43
     *
44
     * @throws \InvalidArgumentException
45
     * @throws \PHPUnit_Framework_MockObject_RuntimeException
46
     */
47 29
    protected function doBuild(string $fqcn)
48
    {
49 29
        return $this->generator->getMock(
50 29
            $fqcn,
51 29
            $methods = [],
52 29
            $arguments = [],
53 29
            $mockClassName = '',
54 29
            $callOriginalConstructor = false
55
        );
56
    }
57
58
    /**
59
     * @param MockObject $mock
60
     * @param MethodStub $stub
61
     * @return void
62
     */
63 22 View Code Duplication
    protected function doDecorateWithMethod($mock, MethodStub $stub)
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...
64
    {
65 22
        $methodName = $stub->getName();
66 22
        $methodValue = $stub->getValue();
67
68 22
        $partial = $mock->expects(new AnyInvokedCountMatcher())->method($methodName);
69 22
        $methodValue instanceof \Throwable
70 22
            ? $partial->willThrowException($methodValue)
71 22
            : $partial->willReturn($methodValue);
72
    }
73
74
    /**
75
     * @param MockObject $mock
76
     * @return MockObject
77
     */
78 18
    protected function doGet($mock)
79
    {
80 18
        return $mock;
81
    }
82
}
83