ToHaveLength::match()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 17
rs 9.2
cc 4
eloc 12
nc 4
nop 1
1
<?php
2
3
/**
4
 * This file is part of expect package.
5
 *
6
 * (c) Noritaka Horio <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
namespace expect\matcher;
12
13
use Countable;
14
use expect\FailedMessage;
15
16
/**
17
 * Verify whether the value has a length that was expected.
18
 *
19
 * <code>
20
 * $matcher = new ToHaveLength(3);
21
 * $matcher->match("foo"); //return true
22
 *
23
 * $matcher->match("fo"); //return false
24
 * $matcher->match([ 1, 2 ]); //return false
25
 * $matcher->match(new ArrayIterator([ 1, 2 ])); //return false
26
 * <code>
27
 *
28
 * @author Noritaka Horio <[email protected]>
29
 * @copyright Noritaka Horio <[email protected]>
30
 */
31
final class ToHaveLength implements ReportableMatcher
32
{
33
    /**
34
     * @var string|array|Countable
35
     */
36
    private $actual;
37
38
    /**
39
     * @var int
40
     */
41
    private $expected;
42
43
    /**
44
     * @var string
45
     */
46
    private $type;
47
48
    /**
49
     * @var int
50
     */
51
    private $length;
52
53
    /**
54
     * Create a new matcher.
55
     *
56
     * @param int $expected
57
     */
58
    public function __construct($expected)
59
    {
60
        $this->expected = $expected;
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function match($actual)
67
    {
68
        $this->actual = $actual;
69
70
        if (is_string($this->actual)) {
71
            $this->type = 'string';
72
            $this->length = mb_strlen($this->actual);
73
        } elseif (is_array($this->actual)) {
74
            $this->type = 'array';
75
            $this->length = count($this->actual);
76
        } elseif ($this->actual instanceof Countable) {
77
            $this->type = get_class($this->actual);
78
            $this->length = count($this->actual);
79
        }
80
81
        return ($this->length === $this->expected);
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87 View Code Duplication
    public function reportFailed(FailedMessage $message)
0 ignored issues
show
Duplication introduced by
This method 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...
88
    {
89
        $message->appendText('Expected ')
90
            ->appendText($this->type)
91
            ->appendText(' to have a length of ')
92
            ->appendText($this->expected)
93
            ->appendText("\n\n")
94
            ->appendText('    expected: ')
95
            ->appendValue($this->expected)
96
            ->appendText("\n")
97
            ->appendText('      length: ')
98
            ->appendValue($this->length);
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104 View Code Duplication
    public function reportNegativeFailed(FailedMessage $message)
0 ignored issues
show
Duplication introduced by
This method 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...
105
    {
106
        $message->appendText('Expected ')
107
            ->appendText($this->type)
108
            ->appendText(' not to have a length of ')
109
            ->appendValue($this->expected)
110
            ->appendText("\n\n")
111
            ->appendText('    expected not: ')
112
            ->appendValue($this->expected)
113
            ->appendText("\n")
114
            ->appendText('          length: ')
115
            ->appendValue($this->length);
116
    }
117
}
118