InclusionMatcher   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 50
rs 10
wmc 8
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getDefaultTemplate() 0 7 1
A doMatch() 0 16 4
A matchTraversable() 0 10 3
1
<?php
2
3
namespace Peridot\Leo\Matcher;
4
5
use InvalidArgumentException;
6
use Peridot\Leo\Matcher\Template\ArrayTemplate;
7
use Peridot\Leo\Matcher\Template\TemplateInterface;
8
use Traversable;
9
10
/**
11
 * InclusionMatcher determines if an array or string includes the expected value.
12
 *
13
 * @package Peridot\Leo\Matcher
14
 */
15
class InclusionMatcher extends AbstractMatcher
16
{
17
    /**
18
     * Matches if an array or string contains the expected value.
19
     *
20
     * @param $actual
21
     * @return mixed
22
     * @throws InvalidArgumentException
23
     */
24
    protected function doMatch($actual)
25
    {
26
        if ($actual instanceof Traversable) {
27
            return $this->matchTraversable($actual);
28
        }
29
30
        if (is_array($actual)) {
31
            return array_search($this->expected, $actual, true) !== false;
32
        }
33
34
        if (is_string($actual)) {
35
            return strpos($actual, $this->expected) !== false;
36
        }
37
38
        throw new InvalidArgumentException('Inclusion matcher requires a string or array');
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     *
44
     * @return TemplateInterface
45
     */
46
    public function getDefaultTemplate()
47
    {
48
        return new ArrayTemplate([
49
            'default' => 'Expected {{actual}} to include {{expected}}',
50
            'negated' => 'Expected {{actual}} to not include {{expected}}',
51
        ]);
52
    }
53
54
    private function matchTraversable(Traversable $actual)
55
    {
56
        foreach ($actual as $value) {
57
            if ($value === $this->expected) {
58
                return true;
59
            }
60
        }
61
62
        return false;
63
    }
64
}
65