Completed
Push — master ( beeb59...88b0fe )
by Hannes
01:47
created

AccountVisitor   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 0
loc 56
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A beforeAccount() 0 4 1
A beforePayeeBankgiro() 0 4 1
A writeAccount() 0 23 4
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\banking\AccountFactoryInterface;
28
use byrokrat\banking\Exception as BankingException;
29
30
/**
31
 * Validates the structure of account numbers in tree
32
 *
33
 * Creates account object as child 'Object'
34
 */
35
class AccountVisitor extends ErrorAwareVisitor
36
{
37
    /**
38
     * @var AccountFactoryInterface
39
     */
40
    private $accountFactory;
41
42
    /**
43
     * @var AccountFactoryInterface
44
     */
45
    private $bankgiroFactory;
46
47
    public function __construct(
48
        ErrorObject $errorObj,
49
        AccountFactoryInterface $accountFactory,
50
        AccountFactoryInterface $bankgiroFactory
51
    ) {
52
        parent::__construct($errorObj);
53
        $this->accountFactory = $accountFactory;
54
        $this->bankgiroFactory = $bankgiroFactory;
55
    }
56
57
    public function beforeAccount(Node $node): void
58
    {
59
        $this->writeAccount($node, $this->accountFactory);
60
    }
61
62
    public function beforePayeeBankgiro(Node $node): void
63
    {
64
        $this->writeAccount($node, $this->bankgiroFactory);
65
    }
66
67
    private function writeAccount(Node $node, AccountFactoryInterface $factory): void
68
    {
69
        if ($node->hasChild('Object')) {
70
            return;
71
        }
72
73
        $number = (string)$node->getChild('Number')->getValue();
74
75
        if (trim($number, '0') == '') {
76
            return;
77
        }
78
79
        try {
80
            $node->addChild(new Obj($node->getLineNr(), $factory->createAccount($number)));
0 ignored issues
show
Documentation introduced by
$factory->createAccount($number) is of type object<byrokrat\banking\AccountNumber>, 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...
81
        } catch (BankingException $e) {
82
            $this->getErrorObject()->addError(
83
                "Invalid account number %s (%s) on line %s",
84
                $number,
85
                $e->getMessage(),
86
                (string)$node->getLineNr()
87
            );
88
        }
89
    }
90
}
91