Completed
Pull Request — master (#9)
by Brent
05:26
created

DateLessThan   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 47
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 37.93%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 47
loc 47
ccs 11
cts 29
cp 0.3793
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 4 4 1
A includeBoundaries() 6 6 1
A excludeBoundaries() 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 = true;
15
16 9
    public function __construct(Carbon $date)
17
    {
18 9
        $this->date = $date->copy();
19 9
    }
20
21
    public function includeBoundaries(): DateLessThan
22
    {
23
        $this->orEquals = true;
24
25
        return $this;
26
    }
27
28 3
    public function excludeBoundaries(): DateLessThan
29
    {
30 3
        $this->orEquals = false;
31
32 3
        return $this;
33
    }
34
35 9
    public function passes($attribute, $value)
36
    {
37 9
        $inputDate = Carbon::make($value);
38
39 9
        if ($this->orEquals) {
40 6
            return $inputDate->lessThanOrEqualTo($this->date);
41
        }
42
43 3
        return $inputDate->lessThan($this->date);
44
    }
45
46
    public function message()
47
    {
48
        if ($this->orEquals) {
49
            return __('validationRules.date_less_than_or_equals');
50
        }
51
52
        return __('validationRules.date_less_than');
53
    }
54
}
55