Completed
Pull Request — master (#1016)
by Kamil
03:14
created

TraversableKeyMatcher::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 4
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 4
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of PhpSpec, A php toolset to drive emergent
5
 * design by specification.
6
 *
7
 * (c) Marcello Duarte <[email protected]>
8
 * (c) Konstantin Kudryashov <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace PhpSpec\Matcher;
15
16
use PhpSpec\Formatter\Presenter\Presenter;
17
use PhpSpec\Exception\Example\FailureException;
18
use ArrayAccess;
19
20 View Code Duplication
final class TraversableKeyMatcher extends BasicMatcher
0 ignored issues
show
Duplication introduced by
This class 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...
21
{
22
    /**
23
     * @var Presenter
24
     */
25
    private $presenter;
26
27
    /**
28
     * @param Presenter $presenter
29
     */
30
    public function __construct(Presenter $presenter)
31
    {
32
        $this->presenter = $presenter;
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function supports($name, $subject, array $arguments)
39
    {
40
        return 'haveKey' === $name
41
            && 1 === count($arguments)
42
            && $subject instanceof \Traversable
43
        ;
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    protected function matches($subject, array $arguments)
50
    {
51
        foreach ($subject as $key => $value) {
52
            if ($key === $arguments[0]) {
53
                return true;
54
            }
55
        }
56
57
        return false;
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    protected function getFailureException($name, $subject, array $arguments)
64
    {
65
        return new FailureException(sprintf(
66
            'Expected %s to have %s key, but it does not.',
67
            $this->presenter->presentValue($subject),
68
            $this->presenter->presentValue($arguments[0])
69
        ));
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    protected function getNegativeFailureException($name, $subject, array $arguments)
76
    {
77
        return new FailureException(sprintf(
78
            'Expected %s not to have %s key, but it does.',
79
            $this->presenter->presentValue($subject),
80
            $this->presenter->presentValue($arguments[0])
81
        ));
82
    }
83
}
84