DateParam::parse()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 9
cts 9
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 3
nop 2
crap 3
1
<?php declare(strict_types=1);
2
3
namespace Bigwhoop\Trumpet\Config\Params;
4
5
use Bigwhoop\Trumpet\Config\ConfigException;
6
use Bigwhoop\Trumpet\Config\Presentation;
7
use Bigwhoop\Trumpet\Config\Date;
8
9
final class DateParam implements Param
10
{
11
    /**
12
     * {@inheritdoc}
13
     */
14 7
    public function parse($value, Presentation $presentation)
15
    {
16 7
        if (is_int($value)) {
17
            // We don't use format 'U' as the TZ would be UTC
18 1
            $date = \DateTime::createFromFormat('Y-m-d', date('Y-m-d', $value));
19 6
        } elseif (preg_match('#\d{4}-\d{1,2}-\d{1,2}#', $value)) {
20 1
            $date = \DateTime::createFromFormat('Y-m-d', $value);
21
        } else {
22 5
            throw new ConfigException('Dates must be a string in the format YYYY-MM-DD.');
23
        }
24
25 2
        $date->setTime(0, 0, 0);
26
27 2
        $presentation->date = new Date($date);
0 ignored issues
show
Security Bug introduced by
It seems like $date can also be of type false; however, Bigwhoop\Trumpet\Config\Date::__construct() does only seem to accept object<DateTime>, did you maybe forget to handle an error condition?
Loading history...
28 2
    }
29
}
30