Completed
Push — master ( 88b0fe...622348 )
by Hannes
02:02
created

DateVisitor::setDateAttribute()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
cc 3
nc 3
nop 2
1
<?php
2
/**
3
 * This file is part of byrokrat\autogiro.
4
 *
5
 * byrokrat\autogiro is free software: you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License as published
7
 * by the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * byrokrat\autogiro is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with byrokrat\autogiro. If not, see <http://www.gnu.org/licenses/>.
17
 *
18
 * Copyright 2016-18 Hannes Forsgård
19
 */
20
21
declare(strict_types = 1);
22
23
namespace byrokrat\autogiro\Visitor;
24
25
use byrokrat\autogiro\Tree\Node;
26
use byrokrat\autogiro\Tree\Obj;
27
28
/**
29
 * Visitor that expands date nodes
30
 *
31
 * Creates DateTime object as child 'Object'
32
 */
33
class DateVisitor extends ErrorAwareVisitor
34
{
35
    public function beforeDate(Node $node): void
36
    {
37
        if ($node->hasChild('Object')) {
38
            return;
39
        }
40
41
        $number = (string)$node->getChild('Number')->getValue();
42
43
        if (!trim($number)) {
44
            return;
45
        }
46
47
        $date = null;
48
49
        switch (strlen($number)) {
50
            case 6:
51
                $date = \DateTimeImmutable::createFromFormat('ymd', $number);
52
                break;
53
            case 8:
54
                $date = \DateTimeImmutable::createFromFormat('Ymd', $number);
55
                break;
56
            case 20:
57
                $date = \DateTimeImmutable::createFromFormat('YmdHis', substr($number, 0, -6));
58
                break;
59
        }
60
61
        if (!$date) {
62
            $this->getErrorObject()->addError(
63
                "Invalid date %s on line %s",
64
                $number,
65
                (string)$node->getLineNr()
66
            );
67
            return;
68
        }
69
70
        $node->addChild(new Obj($node->getLineNr(), $date));
0 ignored issues
show
Documentation introduced by
$date is of type object<DateTimeImmutable>, but the function expects a null|object<byrokrat\autogiro\Tree\object>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
71
    }
72
}
73