Completed
Pull Request — master (#66)
by
unknown
04:23
created

Record::addBalances()   C

Complexity

Conditions 7
Paths 9

Size

Total Lines 37
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 37
ccs 24
cts 24
cp 1
rs 6.7272
cc 7
eloc 23
nc 9
nop 2
crap 7
1
<?php
2
3
namespace Genkgo\Camt\Decoder;
4
5
use Genkgo\Camt\DTO;
6
use Genkgo\Camt\Util\StringToUnits;
7
use Money\Money;
8
use Money\Currency;
9
use \SimpleXMLElement;
10
11
class Record
12
{
13
    /**
14
     * @var Entry
15
     */
16
    private $entryDecoder;
17
    /**
18
     * @var DateDecoderInterface
19
     */
20
    private $dateDecoder;
21
22
    /**
23
     * Record constructor.
24
     * @param Entry $entryDecoder
25
     * @param DateDecoderInterface $dateDecoder
26
     */
27 28
    public function __construct(Entry $entryDecoder, DateDecoderInterface $dateDecoder)
28
    {
29 28
        $this->entryDecoder = $entryDecoder;
30 28
        $this->dateDecoder = $dateDecoder;
31 28
    }
32
33
    /**
34
     * @param DTO\Record       $record
35
     * @param SimpleXMLElement $xmlRecord
36
     */
37 16
    public function addBalances(DTO\Record $record, SimpleXMLElement $xmlRecord)
38
    {
39 16
        $xmlBalances = $xmlRecord->Bal;
0 ignored issues
show
Bug introduced by
The property Bal does not seem to exist in SimpleXMLElement.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
40 16
        foreach ($xmlBalances as $xmlBalance) {
41 11
            $amount = StringToUnits::convert((string) $xmlBalance->Amt);
42 11
            $currency = (string)$xmlBalance->Amt['Ccy'];
43 11
            $date = (string)$xmlBalance->Dt->Dt;
44
45 11
            if ((string) $xmlBalance->CdtDbtInd === 'DBIT') {
46 9
                $amount = $amount * -1;
47
            }
48
49 11
            if (isset($xmlBalance->Tp) && isset($xmlBalance->Tp->CdOrPrtry)) {
50 11
                $code = (string) $xmlBalance->Tp->CdOrPrtry->Cd;
51
52 11
                if (in_array($code, ['OPBD', 'PRCD'])) {
53 11
                    $balance = DTO\Balance::opening(
54 11
                        new Money(
55 11
                            $amount,
56 11
                            new Currency($currency)
57
                        ),
58 11
                        $this->dateDecoder->decode($date)
59
                    );
60 10
                } elseif ($code === 'CLBD') {
61 10
                    $balance = DTO\Balance::closing(
62 10
                        new Money(
63 10
                            $amount,
64 10
                            new Currency($currency)
65
                        ),
66 10
                        $this->dateDecoder->decode($date)
67
                    );
68
                }
69
70 11
                $record->addBalance($balance);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Genkgo\Camt\DTO\Record as the method addBalance() does only exist in the following sub-classes of Genkgo\Camt\DTO\Record: Genkgo\Camt\Camt052\DTO\Report, Genkgo\Camt\Camt053\DTO\Statement. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
Bug introduced by
The variable $balance does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
71
            }
72
        }
73 16
    }
74
75
    /**
76
     * @param DTO\Record       $record
77
     * @param SimpleXMLElement $xmlRecord
78
     */
79 19
    public function addEntries(DTO\Record $record, SimpleXMLElement $xmlRecord)
80
    {
81 19
        $index = 0;
82 19
        $xmlEntries = $xmlRecord->Ntry;
0 ignored issues
show
Bug introduced by
The property Ntry does not seem to exist in SimpleXMLElement.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
83 19
        foreach ($xmlEntries as $xmlEntry) {
84 18
            $amount      = StringToUnits::convert((string) $xmlEntry->Amt);
85 18
            $currency    = (string)$xmlEntry->Amt['Ccy'];
86 18
            $bookingDate = ((string) $xmlEntry->BookgDt->Dt) ?: (string) $xmlEntry->BookgDt->DtTm;
87 18
            $valueDate   = ((string) $xmlEntry->ValDt->Dt) ?: (string) $xmlEntry->ValDt->DtTm;
88 18
            $additionalInfo = ((string) $xmlEntry->AddtlNtryInf) ?: (string) $xmlEntry->AddtlNtryInf;
89
90 18
            if ((string) $xmlEntry->CdtDbtInd === 'DBIT') {
91 9
                $amount = $amount * -1;
92
            }
93
94 18
            $entry = new DTO\Entry(
95 18
                $record,
96 18
                $index,
97 18
                new Money($amount, new Currency($currency)),
98 18
                $this->dateDecoder->decode($bookingDate),
99 18
                $this->dateDecoder->decode($valueDate),
100 18
                $additionalInfo
101
            );
102
103 18
            if (isset($xmlEntry->RvslInd) && (string) $xmlEntry->RvslInd === 'true') {
104 1
                $entry->setReversalIndicator(true);
105
            }
106
107 18
            if (isset($xmlEntry->NtryRef) && (string) $xmlEntry->NtryRef) {
108 1
                $entry->setReference((string) $xmlEntry->NtryRef);
109
            }
110
111 18
            if (isset($xmlEntry->AcctSvcrRef) && (string) $xmlEntry->AcctSvcrRef) {
112 15
                $entry->setAccountServicerReference((string) $xmlEntry->AcctSvcrRef);
113
            }
114
115 18
            if (isset($xmlEntry->NtryDtls->Btch->PmtInfId) && (string) $xmlEntry->NtryDtls->Btch->PmtInfId) {
116 8
                $entry->setBatchPaymentId((string) $xmlEntry->NtryDtls->Btch->PmtInfId);
117
            }
118
119 18
            if (isset($xmlEntry->NtryDtls->TxDtls->Refs->PmtInfId) && (string) $xmlEntry->NtryDtls->TxDtls->Refs->PmtInfId) {
120 3
                $entry->setBatchPaymentId((string) $xmlEntry->NtryDtls->TxDtls->Refs->PmtInfId);
121
            }
122
123 18
            if (isset($xmlEntry->BkTxCd)) {
124 17
                $bankTransactionCode = new DTO\BankTransactionCode();
125
126 17
                if (isset($xmlEntry->BkTxCd->Prtry)) {
127 16
                    $proprietaryBankTransactionCode = new DTO\ProprietaryBankTransactionCode(
128 16
                        (string)$xmlEntry->BkTxCd->Prtry->Cd,
129 16
                        (string)$xmlEntry->BkTxCd->Prtry->Issr
130
                    );
131
132 16
                    $bankTransactionCode->setProprietary($proprietaryBankTransactionCode);
133
                }
134
135 17
                if (isset($xmlEntry->BkTxCd->Domn)) {
136 14
                    $domainBankTransactionCode = new DTO\DomainBankTransactionCode(
137 14
                        (string)$xmlEntry->BkTxCd->Domn->Cd
138
                    );
139
140 14
                    if (isset($xmlEntry->BkTxCd->Domn->Fmly)) {
141 14
                        $domainFamilyBankTransactionCode = new DTO\DomainFamilyBankTransactionCode(
142 14
                            (string)$xmlEntry->BkTxCd->Domn->Fmly->Cd,
143 14
                            (string)$xmlEntry->BkTxCd->Domn->Fmly->SubFmlyCd
144
                        );
145
146 14
                        $domainBankTransactionCode->setFamily($domainFamilyBankTransactionCode);
147
                    }
148
149 14
                    $bankTransactionCode->setDomain($domainBankTransactionCode);
150
                }
151
152 17
                $entry->setBankTransactionCode($bankTransactionCode);
153
            }
154
155 18
            if (isset($xmlEntry->Chrgs)) {
156
                $charges = new DTO\Charges();
157
158
                if (isset($xmlEntry->Chrgs->TtlChrgsAndTaxAmt) && (string) $xmlEntry->Chrgs->TtlChrgsAndTaxAmt) {
159
                    $amount      = StringToUnits::convert((string) $xmlEntry->Chrgs->TtlChrgsAndTaxAmt);
160
                    $currency    = (string)$xmlEntry->Chrgs->TtlChrgsAndTaxAmt['Ccy'];
161
162
                    $charges->setTotalChargesAndTaxAmount(new Money($amount, new Currency($currency)));
163
                }
164
165
                $chargesRecords = $xmlEntry->Chrgs->Rcrd;
166
                if ($chargesRecords) {
167
                    foreach ($chargesRecords as $chargesRecord) {
168
                        $chargesDetail = new DTO\ChargesRecord();
169
170
                        if (isset($chargesRecord->Amt) && (string) $chargesRecord->Amt) {
171
                            $amount      = StringToUnits::convert((string) $chargesRecord->Amt);
172
                            $currency    = (string)$chargesRecord->Amt['Ccy'];
173
174
                            if ((string) $chargesRecord->CdtDbtInd === 'DBIT') {
175
                                $amount = $amount * -1;
176
                            }
177
178
                            $chargesDetail->setAmount(new Money($amount, new Currency($currency)));
179
                        }
180
                        if (isset($chargesRecord->CdtDbtInd) && (string) $chargesRecord->CdtDbtInd === 'true') {
181
                            $chargesDetail->setChargesIncluded­Indicator(true);
182
                        }
183
                        if (isset($chargesRecord->Tp->Prtry->Id) && (string) $chargesRecord->Tp->Prtry->Id) {
184
                            $chargesDetail->setIdentification((string) $chargesRecord->Tp->Prtry->Id);
185
                        }
186
                        $charges->addRecord($chargesDetail);
187
                    }
188
                }
189
                $entry->setCharges($charges);
190
            }
191
192 18
            $this->entryDecoder->addTransactionDetails($entry, $xmlEntry);
193
194 18
            $record->addEntry($entry);
195 18
            $index++;
196
        }
197 19
    }
198
}
199