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 |
|
|
|
|
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
|
|
|
|
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.