DateValidator   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 81
Duplicated Lines 27.16 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 22
loc 81
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A format() 0 9 2
A min() 11 11 2
A max() 11 11 2
A timestamp() 0 10 2
A iso() 0 4 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
namespace Comfort\Validator;
3
4
class DateValidator extends AbstractValidator
5
{
6
    /**
7
     * Specify format date must be in
8
     *
9
     * @param string $format
10
     * @return $this
11
     */
12
    public function format($format)
13
    {
14
        return $this->add(function ($value, $namekey) use ($format) {
15
            $date = \DateTime::createFromFormat($format, $value);
16
            if (false === $date) {
17
                return $this->createError('date.format', $value, $namekey);
18
            }
19
        });
20
    }
21
22
    /**
23
     * Specify min date
24
     *
25
     * @param $date
26
     * @return $this
27
     */
28 View Code Duplication
    public function min($date)
0 ignored issues
show
Duplication introduced by
This method 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
        return $this->add(function ($value, $nameKey) use ($date) {
31
            $minDate = strtotime($date);
32
            $curDate = strtotime($value);
33
34
            if ($curDate < $minDate) {
35
                return $this->createError('date.min', $value, $nameKey);
36
            }
37
        });
38
    }
39
40
    /**
41
     * Specify max date
42
     *
43
     * @param $date
44
     * @return $this
45
     */
46 View Code Duplication
    public function max($date)
0 ignored issues
show
Duplication introduced by
This method 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...
47
    {
48
        return $this->add(function ($value, $nameKey) use ($date) {
49
            $maxDate = strtotime($date);
50
            $curDate = strtotime($value);
51
52
            if ($curDate > $maxDate) {
53
                return $this->createError('date.max', $value, $nameKey);
54
            }
55
        });
56
    }
57
58
    /**
59
     * Date must be a unix timestamp
60
     *
61
     * @return $this
62
     */
63
    public function timestamp()
64
    {
65
        return $this->add(function ($value, $nameKey) {
66
            try {
67
                new \DateTime('@' . $value);
68
            } catch (\Exception $e) {
69
                return $this->createError('date.timestamp', $value, $nameKey);
70
            }
71
        });
72
    }
73
74
75
    /**
76
     * Date must be in ISO format
77
     *
78
     * @return $this
79
     */
80
    public function iso()
81
    {
82
        return $this->format(DATE_ISO8601);
83
    }
84
}
85