Completed
Pull Request — master (#21)
by Erin
09:47
created

InclusionMatcher   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 10
Bugs 0 Features 3
Metric Value
wmc 5
c 10
b 0
f 3
lcom 1
cbo 2
dl 0
loc 36
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A doMatch() 0 13 4
A getDefaultTemplate() 0 7 1
1
<?php
2
3
namespace Peridot\Leo\Matcher;
4
5
use ArrayAccess;
6
use InvalidArgumentException;
7
use Peridot\Leo\Matcher\Template\ArrayTemplate;
8
use Peridot\Leo\Matcher\Template\TemplateInterface;
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
        //we will support ArrayAccess for now, even though array_search throws a warning about it
27
        if (is_array($actual) or $actual instanceof ArrayAccess) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
28
            return array_search($this->expected, $actual, true) !== false;
29
        }
30
31
        if (is_string($actual)) {
32
            return strpos($actual, $this->expected) !== false;
33
        }
34
35
        throw new InvalidArgumentException("Inclusion matcher requires a string or array");
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     *
41
     * @return TemplateInterface
42
     */
43
    public function getDefaultTemplate()
44
    {
45
        return new ArrayTemplate([
46
            'default' => 'Expected {{actual}} to include {{expected}}',
47
            'negated' => 'Expected {{actual}} to not include {{expected}}'
48
        ]);
49
    }
50
}
51