Completed
Pull Request — master (#6)
by
unknown
03:15
created

ForeignCreditTransfer::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 11
ccs 7
cts 7
cp 1
rs 9.4285
cc 3
eloc 6
nc 2
nop 8
crap 3

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace Z38\SwissPayment\TransactionInformation;
4
5
use Z38\SwissPayment\BIC;
6
use Z38\SwissPayment\FinancialInstitutionAddress;
7
use Z38\SwissPayment\FinancialInstitutionInterface;
8
use Z38\SwissPayment\IBAN;
9
use Z38\SwissPayment\IntermediarySwift;
10
use Z38\SwissPayment\Money\Money;
11
use Z38\SwissPayment\PaymentInformation\PaymentInformation;
12
use Z38\SwissPayment\PostalAddressInterface;
13
14
/**
15
 * ForeignCreditTransfer contains all the information about a foreign (type 6) transaction.
16
 */
17
class ForeignCreditTransfer extends CreditTransfer
18
{
19
    /**
20
     * @var IBAN
21
     */
22
    protected $creditorIBAN;
23
24
    /**
25
     * @var BIC|FinancialInstitutionAddress
26
     */
27
    protected $creditorAgent;
28
29
    /**
30
     * {@inheritdoc}
31
     *
32
     * @param IBAN                              $creditorIBAN  IBAN of the creditor
33
     * @param BIC|FinancialInstitutionAddress   $creditorAgent BIC or address of the creditor's financial institution
34
     * @param IntermediarySwift|null            $intermediarySwift IntermediarySwift
35
     */
36 4
    public function __construct($instructionId, $endToEndId, Money $amount, $creditorName, PostalAddressInterface $creditorAddress, IBAN $creditorIBAN, FinancialInstitutionInterface $creditorAgent, $intermediarySwift = null)
37
    {
38 4
        parent::__construct($instructionId, $endToEndId, $amount, $creditorName, $creditorAddress, $intermediarySwift);
39
40 4
        if (!$creditorAgent instanceof BIC && !$creditorAgent instanceof FinancialInstitutionAddress) {
41 1
            throw new \InvalidArgumentException('The creditor agent must be an instance of BIC or FinancialInstitutionAddress.');
42
        }
43
44 3
        $this->creditorIBAN = $creditorIBAN;
45 3
        $this->creditorAgent = $creditorAgent;
0 ignored issues
show
Documentation Bug introduced by
It seems like $creditorAgent can also be of type object<Z38\SwissPayment\...cialInstitutionAddress>. However, the property $creditorAgent is declared as type object<Z38\SwissPayment\BIC>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
46 3
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 2
    public function asDom(\DOMDocument $doc, PaymentInformation $paymentInformation)
52
    {
53 2
        $root = $this->buildHeader($doc, $paymentInformation);
54
55 2
        $creditorAgent = $doc->createElement('CdtrAgt');
56 2
        $creditorAgent->appendChild($this->creditorAgent->asDom($doc));
57 2
        $root->appendChild($creditorAgent);
58
59 2
        if ($this->intermediarySwift) {
60
            $intermediary = $doc->createElement('IntrmyAgt1');
61
            $intermediary->appendChild($this->intermediarySwift->asDom($doc));
62
            $root->appendChild($intermediary);
63
        }
64
65 2
        $root->appendChild($this->buildCreditor($doc));
66
67 2
        $creditorAccount = $doc->createElement('CdtrAcct');
68 2
        $creditorAccountId = $doc->createElement('Id');
69 2
        $creditorAccountId->appendChild($doc->createElement($this->creditorIBAN->getElementName(), $this->creditorIBAN->normalize()));
70 2
        $creditorAccount->appendChild($creditorAccountId);
71 2
        $root->appendChild($creditorAccount);
72
73 2
        if ($this->hasRemittanceInformation()) {
74
            $root->appendChild($this->buildRemittanceInformation($doc));
75
        }
76
77 2
        return $root;
78
    }
79
}
80