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

DateValidation   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 90.91%

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 0
dl 0
loc 31
ccs 10
cts 11
cp 0.9091
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A isDateValid() 0 17 4
A isDateValidAgainstRegex() 0 8 2
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