ToBeGreaterThan::reportNegativeFailed()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 9
Ratio 100 %

Importance

Changes 5
Bugs 0 Features 2
Metric Value
c 5
b 0
f 2
dl 9
loc 9
rs 9.6666
cc 1
eloc 7
nc 1
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 expect\FailedMessage;
14
15
/**
16
 * Verify whether the value is greater than the value of the expected value.
17
 *
18
 * <code>
19
 * $matcher = new ToBeGreaterThan(100);
20
 * $matcher->match(100); //return true
21
 *
22
 * $matcher->match(1); //return false
23
 * </code>
24
 *
25
 * @author Noritaka Horio <[email protected]>
26
 * @copyright Noritaka Horio <[email protected]>
27
 */
28 View Code Duplication
final class ToBeGreaterThan implements ReportableMatcher
0 ignored issues
show
Duplication introduced by
This class 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...
29
{
30
    /**
31
     * @var int|float
32
     */
33
    private $actual;
34
35
    /**
36
     * @var int|float
37
     */
38
    private $expected;
39
40
    /**
41
     * Create a new matcher.
42
     *
43
     * @param int|float $expected expected value
44
     */
45
    public function __construct($expected)
46
    {
47
        $this->expected = $expected;
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function match($actual)
54
    {
55
        $this->actual = $actual;
56
57
        return $this->actual >= $this->expected;
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function reportFailed(FailedMessage $message)
64
    {
65
        $detail = $this->createDetailMessage();
66
        $message->appendText('Expected ')
67
            ->appendValue($this->actual)
68
            ->appendText(' to be greater than ')
69
            ->appendValue($this->expected)
70
            ->appendText($detail);
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function reportNegativeFailed(FailedMessage $message)
77
    {
78
        $detail = $this->createDetailMessage(' not');
79
        $message->appendText('Expected ')
80
            ->appendValue($this->actual)
81
            ->appendText(' not to be greater than ')
82
            ->appendValue($this->expected)
83
            ->appendText($detail);
84
    }
85
86
    private function createDetailMessage($prefixMessage = '')
87
    {
88
        $message = FailedMessage::fromString("\n\n");
89
        $message->appendText('    expected')
90
            ->appendText($prefixMessage)
91
            ->appendText(': >= ')
92
            ->appendValue($this->expected)
93
            ->appendText("\n")
94
            ->appendSpace(strlen($prefixMessage))
95
            ->appendText('         got:    ')
96
            ->appendValue($this->actual);
97
98
        return (string) $message;
99
    }
100
}
101