Range   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 33
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 33
loc 33
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 7 7 1
A validate() 12 12 4

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
declare(strict_types=1);
4
5
namespace Linio\Component\Input\Constraint;
6
7 View Code Duplication
class Range extends Constraint
8
{
9
    /**
10
     * @var int
11
     */
12
    protected $min;
13
14
    /**
15
     * @var int
16
     */
17
    protected $max;
18
19
    public function __construct(int $min, int $max = PHP_INT_MAX, string $errorMessage = null)
20
    {
21
        $this->min = $min;
22
        $this->max = $max;
23
24
        $this->setErrorMessage($errorMessage ?? sprintf('Value is not between %d and %d', $this->min, $this->max));
25
    }
26
27
    public function validate($content): bool
28
    {
29
        if (!is_scalar($content)) {
30
            return false;
31
        }
32
33
        if ($content === null) {
34
            return false;
35
        }
36
37
        return $content >= $this->min && $content <= $this->max;
38
    }
39
}
40