ToBeGreaterThan   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 73
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 4 4 1
A match() 6 6 1
A reportFailed() 9 9 1
A reportNegativeFailed() 9 9 1
A createDetailMessage() 14 14 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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