FirstStubMatcher   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 72
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 3
A doArgumentsMatch() 0 11 2
A __toString() 0 4 1
A setNextMatcher() 0 3 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Moka\Plugin\Phake\Matcher;
5
6
use Phake_Exception_MethodMatcherException as MethodMatcherException;
7
use Phake_IMock as PhakeMock;
8
use Phake_Matchers_AbstractChainableArgumentMatcher as AbstractChainableArgumentMatcher;
9
use Phake_Matchers_IChainableArgumentMatcher;
10
11
/**
12
 * Class FirstStubMatcher
13
 * @package Moka\Plugin\Phake\Matcher
14
 */
15
class FirstStubMatcher extends AbstractChainableArgumentMatcher
16
{
17
    /**
18
     * @var array
19
     */
20
    private static $stubsPerMock = [];
21
22
    /**
23
     * @var string
24
     */
25
    private $methodName;
26
27
    /**
28
     * @var bool
29
     */
30
    private $isFirstStub = true;
31
32
    /**
33
     * FirstStubMatcher constructor.
34
     * @param PhakeMock $mock
35
     * @param string $methodName
36
     */
37 23
    public function __construct(PhakeMock $mock, string $methodName)
38
    {
39 23
        $this->methodName = $methodName;
40
41 23
        $mockHash = spl_object_hash($mock);
42 23
        if (!isset(self::$stubsPerMock[$mockHash])) {
43 23
            self::$stubsPerMock[$mockHash] = [];
44
        }
45
46 23
        if (\in_array($methodName, self::$stubsPerMock[$mockHash], $strict = true)) {
47 2
            $this->isFirstStub = false;
48 2
            return;
49
        }
50
51 23
        self::$stubsPerMock[$mockHash][] = $methodName;
52
    }
53
54
    /**
55
     * @param array $arguments
56
     *
57
     * @throws MethodMatcherException
58
     */
59 9
    public function doArgumentsMatch(array &$arguments): void
60
    {
61 9
        if (!$this->isFirstStub) {
62 2
            throw new MethodMatcherException(
63 2
                sprintf(
64 2
                    'Cannot override definition for method "%s()"',
65 2
                    $this->methodName
66
                )
67
            );
68
        }
69
    }
70
71
    /**
72
     * @return string
73
     */
74 1
    public function __toString()
75
    {
76 1
        return \get_class($this);
77
    }
78
79
    /**
80
     * @param Phake_Matchers_IChainableArgumentMatcher $nextMatcher
81
     * @return void
82
     */
83
    public function setNextMatcher(Phake_Matchers_IChainableArgumentMatcher $nextMatcher): void
84
    {
85
    }
86
}
87