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

FirstStubMatcher::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 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_Matchers_AbstractChainableArgumentMatcher as AbstractChainableArgumentMatcher;
8
use Phake_Matchers_IChainableArgumentMatcher;
9
use Phake_IMock as PhakeMock;
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 27
    public function __construct(PhakeMock $mock, string $methodName)
38
    {
39 27
        $this->methodName = $methodName;
40
41 27
        $mockHash = spl_object_hash($mock);
42 27
        if (!isset(self::$stubsPerMock[$mockHash])) {
43 27
            self::$stubsPerMock[$mockHash] = [];
44
        }
45
46 27
        if (in_array($methodName, self::$stubsPerMock[$mockHash])) {
47 2
            $this->isFirstStub = false;
48 2
            return;
49
        }
50
51 27
        self::$stubsPerMock[$mockHash][] = $methodName;
52
    }
53
54
    /**
55
     * @param array $arguments
56
     *
57
     * @throws MethodMatcherException
58
     */
59 9
    public function doArgumentsMatch(array &$arguments)
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 '';
77
    }
78
79
    /**
80
     * @param Phake_Matchers_IChainableArgumentMatcher $nextMatcher
81
     */
82
    public function setNextMatcher(Phake_Matchers_IChainableArgumentMatcher $nextMatcher)
83
    {
84
    }
85
}
86