Completed
Pull Request — master (#9)
by Brent
18:51 queued 17:13
created

DateLessThan   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 40
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 73.32%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 40
loc 40
ccs 11
cts 15
cp 0.7332
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 4 4 1
A orEquals() 6 6 1
A passes() 10 10 2
A message() 8 8 2

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
namespace Spatie\ValidationRules\Rules;
4
5
use Illuminate\Contracts\Validation\Rule;
6
use Carbon\Carbon;
7
8 View Code Duplication
class DateLessThan implements Rule
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...
9
{
10
    /** @var \Carbon\Carbon */
11
    protected $date;
12
13
    /** @var bool */
14
    protected $orEquals = false;
15
16 12
    public function __construct(Carbon $date)
17
    {
18 12
        $this->date = $date->copy();
19 12
    }
20
21 4
    public function orEquals(bool $orEquals = true): DateLessThan
22
    {
23 4
        $this->orEquals = $orEquals;
24
25 4
        return $this;
26
    }
27
28 12
    public function passes($attribute, $value)
29
    {
30 12
        $inputDate = Carbon::make($value);
31
32 12
        if ($this->orEquals) {
33 4
            return $inputDate->lessThanOrEqualTo($this->date);
34
        }
35
36 8
        return $inputDate->lessThan($this->date);
37
    }
38
39
    public function message()
40
    {
41
        if ($this->orEquals) {
42
            return __('validationRules.date_less_than_or_equals');
43
        }
44
45
        return __('validationRules.date_less_than');
46
    }
47
}
48