AbstractDateTime   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 0
cbo 0
dl 0
loc 39
ccs 12
cts 12
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B newDateTime() 0 25 5
1
<?php
2
/**
3
 *
4
 * This file is part of Aura for PHP.
5
 *
6
 * @license http://opensource.org/licenses/bsd-license.php BSD
7
 *
8
 */
9
namespace Aura\Filter\Rule;
10
11
use DateTime;
12
13
/**
14
 *
15
 * Abstract rule for date-time filters.
16
 *
17
 * @package Aura.Filter
18
 *
19
 */
20
abstract class AbstractDateTime
21
{
22
    /**
23
     *
24
     * Returns a new DateTime object.
25
     *
26
     * @param mixed $value The incoming date/time value.
27
     *
28
     * @return mixed If the value is already a DateTime then it is returned
29
     * as-is; if the value is invalid as a date/time then `false` is returned;
30
     * otherwise, a new DateTime is constructed from the value and returned.
31
     *
32
     */
33 22
    protected function newDateTime($value)
34
    {
35 22
        if ($value instanceof DateTime) {
36 2
            return $value;
37
        }
38
39 20
        if (! is_scalar($value)) {
40 2
            return false;
41
        }
42
43 18
        if (trim($value) === '') {
44 3
            return false;
45
        }
46
47 15
        $datetime = date_create($value);
48
49
        // invalid dates (like 1979-02-29) show up as warnings.
50 15
        $errors = DateTime::getLastErrors();
51 15
        if ($errors['warnings']) {
52 4
            return false;
53
        }
54
55
        // looks OK
56 11
        return $datetime;
57
    }
58
}
59