ToBeWithin   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 5
Bugs 1 Features 1
Metric Value
wmc 6
c 5
b 1
f 1
lcom 1
cbo 1
dl 0
loc 68
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A match() 0 6 2
A reportFailed() 0 8 1
A reportNegativeFailed() 0 8 1
A appendRange() 0 6 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 expect\FailedMessage;
14
15
/**
16
 * Verify whether the value is no range.
17
 *
18
 * <code>
19
 * $matcher = new ToBeWithin([0, 10]);
20
 * $matcher->match(1); //return true
21
 *
22
 * $matcher->match(11); //return false
23
 * </code>
24
 *
25
 * @author Noritaka Horio <[email protected]>
26
 * @copyright Noritaka Horio <[email protected]>
27
 */
28
final class ToBeWithin implements ReportableMatcher
29
{
30
    /**
31
     * @var int|float
32
     */
33
    private $actual;
34
35
    /**
36
     * @var int|float
37
     */
38
    private $from;
39
40
    /**
41
     * @var int|float
42
     */
43
    private $to;
44
45
    /**
46
     * Create a new matcher.
47
     *
48
     * @param array $expected
49
     */
50
    public function __construct($expected)
51
    {
52
        list($this->from, $this->to) = $expected;
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function match($actual)
59
    {
60
        $this->actual = $actual;
61
62
        return ($this->actual >= $this->from && $this->actual <= $this->to);
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function reportFailed(FailedMessage $message)
69
    {
70
        $message->appendText('Expected ')
71
            ->appendValue($this->actual)
72
            ->appendText(' to be within ');
73
74
        $this->appendRange($message);
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function reportNegativeFailed(FailedMessage $message)
81
    {
82
        $message->appendText('Expected ')
83
            ->appendValue($this->actual)
84
            ->appendText(' not to be within ');
85
86
        $this->appendRange($message);
87
    }
88
89
    private function appendRange(FailedMessage $message)
90
    {
91
        $message->appendValue($this->from)
92
            ->appendText(' between ')
93
            ->appendValue($this->to);
94
    }
95
}
96