Completed
Pull Request — master (#41)
by Guillaume
02:25
created

DateValidation::isDateValid()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.0312

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 7
cts 8
cp 0.875
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 4
nop 1
crap 4.0312
1
<?php
2
declare(strict_types = 1);
3
4
namespace Imedia\Ammit\Domain;
5
6
/**
7
 * @author Guillaume MOREL <[email protected]>
8
 */
9
class DateValidation
10
{
11
    const FORMAT_SIMPLE = 'Y-m-d';
12
13
    public function isDateValid(string $dateString): bool
14
    {
15 1
        if (!$this->isDateValidAgainstRegex($dateString)) {
16 1
            return false;
17
        }
18
19 1
        $date = \DateTime::createFromFormat(self::FORMAT_SIMPLE, $dateString);
20 1
        if (false === $date) {
21
            return false;
22
        }
23
24 1
        if ($date->format(self::FORMAT_SIMPLE) === $dateString) {
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return $date->format(sel...IMPLE) === $dateString;.
Loading history...
25 1
            return true;
26
        }
27
28 1
        return false;
29
    }
30
31
    private function isDateValidAgainstRegex(string $date): bool
32
    {
33 1
        if (preg_match('/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/', $date)) {
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return (bool) preg_match...-9]|3[0-1])$/', $date);.
Loading history...
34 1
            return true;
35
        }
36
37 1
        return false;
38
    }
39
}
40