GreaterThan   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 72
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 0
dl 72
loc 72
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 4 4 1
A validate() 4 4 1
A notGreaterThan() 8 8 2
A getMessageParameters() 6 6 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
 * Particle.
4
 *
5
 * @link      http://github.com/particle-php for the canonical source repository
6
 * @copyright Copyright (c) 2005-2016 Particle (http://particle-php.com)
7
 * @license   https://github.com/particle-php/validator/blob/master/LICENSE New BSD License
8
 */
9
namespace Particle\Validator\Rule;
10
11
use Particle\Validator\Rule;
12
13
/**
14
 * This rule will validate a value to be greater than a value.
15
 *
16
 * @package Particle\Validator\Rule
17
 */
18 View Code Duplication
class GreaterThan extends Rule
19
{
20
    /**
21
     * A constant for an error message if the value is not greater than the min.
22
     */
23
    const NOT_GREATER_THAN = 'GreaterThan::NOT_GREATER_THAN';
24
25
    /**
26
     * The message templates which can be returned by this validator.
27
     *
28
     * @var array
29
     */
30
    protected $messageTemplates = [
31
        self::NOT_GREATER_THAN => '{{ name }} must be greater than {{ min }}',
32
    ];
33
34
    /**
35
     * The lower boundary.
36
     *
37
     * @var int
38
     */
39
    protected $min;
40
41
    /**
42
     * Construct the GreaterThan rule.
43
     *
44
     * @param int $min
45
     */
46 3
    public function __construct($min)
47
    {
48 3
        $this->min = $min;
49 3
    }
50
51
    /**
52
     * Checks whether or not $value is greater than the min for this rule.
53
     *
54
     * @param mixed $value
55
     * @return bool
56
     */
57 5
    public function validate($value)
58
    {
59 5
        return !$this->notGreaterThan($value, self::NOT_GREATER_THAN);
60
    }
61
62
    /**
63
     * Returns whether or not the value is greater than the min, and logs an error if it isn't.
64
     *
65
     * @param mixed $value
66
     * @param string $error
67
     * @return bool
68
     */
69 5
    protected function notGreaterThan($value, $error)
70
    {
71 5
        if ($value <= $this->min) {
72 4
            $this->error($error);
73 4
            return true;
74
        }
75 1
        return false;
76
    }
77
78
    /**
79
     * Returns the parameters that may be used in a validation message.
80
     *
81
     * @return array
82
     */
83 4
    protected function getMessageParameters()
84
    {
85 4
        return array_merge(parent::getMessageParameters(), [
86 4
            'min' => $this->min,
87 4
        ]);
88
    }
89
}
90