Completed
Push — master ( ceb8bd...beeb59 )
by Hannes
02:44
created

StateIdVisitor::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 3
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
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
use byrokrat\id\IdFactoryInterface;
28
use byrokrat\id\Exception as IdException;
29
30
/**
31
 * Validates the structure of id numbers in tree
32
 *
33
 * Creates Id object as child 'Object'
34
 */
35
class StateIdVisitor extends ErrorAwareVisitor
36
{
37
    /**
38
     * @var IdFactoryInterface
39
     */
40
    private $organizationIdFactory;
41
42
    /**
43
     * @var IdFactoryInterface
44
     */
45
    private $personalIdFactory;
46
47
    public function __construct(
48
        ErrorObject $errorObj,
49
        IdFactoryInterface $organizationIdFactory,
50
        IdFactoryInterface $personalIdFactory
51
    ) {
52
        parent::__construct($errorObj);
53
        $this->organizationIdFactory = $organizationIdFactory;
54
        $this->personalIdFactory = $personalIdFactory;
55
    }
56
57
    public function beforeStateId(Node $node): void
58
    {
59
        if ($node->hasChild('Object')) {
60
            return;
61
        }
62
63
        $number = (string)$node->getChild('Number')->getValue();
64
65
        if (!trim($number, '0')) {
66
            return;
67
        }
68
69
        try {
70
            $id = in_array(substr($number, 0, 2), ['00', '99'])
71
                ? $this->organizationIdFactory->createId(substr($number, 2))
72
                : $this->personalIdFactory->createId($number);
73
74
            $node->addChild(new Obj($node->getLineNr(), $id));
0 ignored issues
show
Documentation introduced by
$id is of type object<byrokrat\id\IdInterface>, 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...
75
        } catch (IdException $exception) {
76
            $this->getErrorObject()->addError(
77
                "Invalid id number %s (%s) on line %s",
78
                $number,
79
                $exception->getMessage(),
80
                (string)$node->getLineNr()
81
            );
82
        }
83
    }
84
}
85