AbstractDateTime::newDateTime()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 11
nc 5
nop 1
dl 0
loc 23
ccs 12
cts 12
cp 1
crap 5
rs 9.6111
c 0
b 0
f 0
1
<?php
2
3
namespace Mbright\Validation\Rule;
4
5
class AbstractDateTime
6
{
7
    /**
8
     * Returns a new DateTime object.
9
     *
10
     * @param mixed $value The incoming date/time value.
11
     *
12
     * @return mixed If the value is already a DateTime then it is returned
13
     * as-is; if the value is invalid as a date/time then `false` is returned;
14
     * otherwise, a new DateTime is constructed from the value and returned.
15
     */
16 66
    protected function newDateTime($value)
17
    {
18 66
        if ($value instanceof \DateTime) {
19 6
            return $value;
20
        }
21
22 60
        if (!is_scalar($value)) {
23 6
            return false;
24
        }
25
26 54
        if (trim($value) === '') {
27 9
            return false;
28
        }
29
30 45
        $datetime = date_create($value);
31
32
        // invalid dates (like 1979-02-29) show up as warnings.
33 45
        $errors = \DateTime::getLastErrors();
34 45
        if ($errors['warnings']) {
35 12
            return false;
36
        }
37
38 33
        return $datetime;
39
    }
40
}
41