Completed
Pull Request — master (#1016)
by Kamil
06:57 queued 03:56
created

TraversableContainMatcher::getFailureException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 8
loc 8
rs 9.4285
cc 1
eloc 5
nc 1
nop 3
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\Exception\Example\FailureException;
17
use PhpSpec\Formatter\Presenter\Presenter;
18
19 View Code Duplication
final class TraversableContainMatcher 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...
20
{
21
    /**
22
     * @var Presenter
23
     */
24
    private $presenter;
25
26
    /**
27
     * @param Presenter $presenter
28
     */
29
    public function __construct(Presenter $presenter)
30
    {
31
        $this->presenter = $presenter;
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function supports($name, $subject, array $arguments)
38
    {
39
        return 'contain' === $name
40
            && 1 === count($arguments)
41
            && $subject instanceof \Traversable
42
        ;
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    protected function matches($subject, array $arguments)
49
    {
50
        foreach ($subject as $value) {
51
            if ($value === $arguments[0]) {
52
                return true;
53
            }
54
        }
55
56
        return false;
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    protected function getFailureException($name, $subject, array $arguments)
63
    {
64
        return new FailureException(sprintf(
65
            'Expected %s to contain %s, but it does not.',
66
            $this->presenter->presentValue($subject),
67
            $this->presenter->presentValue($arguments[0])
68
        ));
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    protected function getNegativeFailureException($name, $subject, array $arguments)
75
    {
76
        return new FailureException(sprintf(
77
            'Expected %s not to contain %s, but it does.',
78
            $this->presenter->presentValue($subject),
79
            $this->presenter->presentValue($arguments[0])
80
        ));
81
    }
82
}
83