Completed
Push — master ( a5ff2b...bf9940 )
by Yann
05:35
created

Record::addEntries()   F

Complexity

Conditions 30
Paths 7681

Size

Total Lines 103
Code Lines 61

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 51
CRAP Score 72.8712

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 103
ccs 51
cts 80
cp 0.6375
rs 2
c 1
b 0
f 0
cc 30
eloc 61
nc 7681
nop 2
crap 72.8712

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
use \DateTimeImmutable;
11
12
class Record
13
{
14
    /**
15
     * @var Entry
16
     */
17
    private $entryDecoder;
18
19 31
    public function __construct(Entry $entryDecoder)
20
    {
21 31
        $this->entryDecoder = $entryDecoder;
22 31
    }
23
24
    /**
25
     * @param DTO\Record       $record
26
     * @param SimpleXMLElement $xmlRecord
27
     */
28 19
    public function addBalances(DTO\Record $record, SimpleXMLElement $xmlRecord)
29
    {
30 19
        $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...
31 19
        foreach ($xmlBalances as $xmlBalance) {
32 14
            $amount = StringToUnits::convert((string) $xmlBalance->Amt);
33 14
            $currency = (string)$xmlBalance->Amt['Ccy'];
34 14
            $date = (string)$xmlBalance->Dt->Dt;
35
36 14
            if ((string) $xmlBalance->CdtDbtInd === 'DBIT') {
37 9
                $amount = $amount * -1;
38 9
            }
39
40 14
            if (isset($xmlBalance->Tp)
41 14
                && isset($xmlBalance->Tp->CdOrPrtry)
42 14
                && (string) $xmlBalance->Tp->CdOrPrtry->Cd === 'OPBD'
43 14
            ) {
44 11
                $balance = DTO\Balance::opening(
45 11
                    new Money(
46 11
                        $amount,
47 11
                        new Currency($currency)
48 11
                    ),
49 11
                    new DateTimeImmutable($date)
50 11
                );
51 11
            } else {
52 13
                $balance = DTO\Balance::closing(
53 13
                    new Money(
54 13
                        $amount,
55 13
                        new Currency($currency)
56 13
                    ),
57 13
                    new DateTimeImmutable($date)
58 13
                );
59
            }
60
61 14
            $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...
62 19
        }
63 19
    }
64
65
    /**
66
     * @param DTO\Record       $record
67
     * @param SimpleXMLElement $xmlRecord
68
     */
69 22
    public function addEntries(DTO\Record $record, SimpleXMLElement $xmlRecord)
70
    {
71 22
        $index = 0;
72 22
        $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...
73 22
        foreach ($xmlEntries as $xmlEntry) {
74 21
            $amount      = StringToUnits::convert((string) $xmlEntry->Amt);
75 21
            $currency    = (string)$xmlEntry->Amt['Ccy'];
76 21
            $bookingDate = ((string) $xmlEntry->BookgDt->Dt) ?: (string) $xmlEntry->BookgDt->DtTm;
77 21
            $valueDate   = ((string) $xmlEntry->ValDt->Dt) ?: (string) $xmlEntry->ValDt->DtTm;
78 21
            $additionalInfo = ((string) $xmlEntry->AddtlNtryInf) ?: (string) $xmlEntry->AddtlNtryInf;
79
80 21
            if ((string) $xmlEntry->CdtDbtInd === 'DBIT') {
81 12
                $amount = $amount * -1;
82 12
            }
83
84 21
            $entry = new DTO\Entry(
85 21
                $record,
86 21
                $index,
87 21
                new Money($amount, new Currency($currency)),
88 21
                new DateTimeImmutable($bookingDate),
89 21
                new DateTimeImmutable($valueDate),
90
                $additionalInfo
91 21
            );
92
93 21
            if (isset($xmlEntry->RvslInd) && (string) $xmlEntry->RvslInd === 'true') {
94 1
                $entry->setReversalIndicator(true);
95 1
            }
96
97 21
            if (isset($xmlEntry->NtryRef) && (string) $xmlEntry->NtryRef) {
98 1
                $entry->setReference((string) $xmlEntry->NtryRef);
99 1
            }
100
101 21
            if (isset($xmlEntry->AcctSvcrRef) && (string) $xmlEntry->AcctSvcrRef) {
102 18
                $entry->setAccountServicerReference((string) $xmlEntry->AcctSvcrRef);
103 18
            }
104
105 21
            if (isset($xmlEntry->NtryDtls->Btch->PmtInfId) && (string) $xmlEntry->NtryDtls->Btch->PmtInfId) {
106 11
                $entry->setBatchPaymentId((string) $xmlEntry->NtryDtls->Btch->PmtInfId);
107 11
            }
108
            
109 21
            if (isset($xmlEntry->NtryDtls->TxDtls->Refs->PmtInfId) && (string) $xmlEntry->NtryDtls->TxDtls->Refs->PmtInfId) {
110 3
                $entry->setBatchPaymentId((string) $xmlEntry->NtryDtls->TxDtls->Refs->PmtInfId);
111 3
            }
112
113 21
            if (isset($xmlEntry->BkTxCd)) {
114 20
                $bankTransactionCode = new DTO\BankTransactionCode();
115
116 20
                if (isset($xmlEntry->BkTxCd->Prtry)) {
117 19
                    $proprietaryBankTransactionCode = new DTO\ProprietaryBankTransactionCode(
118 19
                        (string)$xmlEntry->BkTxCd->Prtry->Cd,
119 19
                        (string)$xmlEntry->BkTxCd->Prtry->Issr
120 19
                    );
121
122 19
                    $bankTransactionCode->setProprietary($proprietaryBankTransactionCode);
123 19
                }
124
125 20
                $entry->setBankTransactionCode($bankTransactionCode);
126 20
            }
127
128 21
            if (isset($xmlEntry->Chrgs)) {
129
                $charges = new DTO\Charges();
130
131
                if (isset($xmlEntry->Chrgs->TtlChrgsAndTaxAmt) && (string) $xmlEntry->Chrgs->TtlChrgsAndTaxAmt) {
132
                    $amount      = StringToUnits::convert((string) $xmlEntry->Chrgs->TtlChrgsAndTaxAmt);
133
                    $currency    = (string)$xmlEntry->Chrgs->TtlChrgsAndTaxAmt['Ccy'];
134
135
                    $charges->setTotalChargesAndTaxAmount(new Money($amount, new Currency($currency)));
136
                }
137
                
138
                $chargesRecords = $xmlEntry->Chrgs->Rcrd;
139
                if ($chargesRecords) {
140
                    foreach ($chargesRecords as $chargesRecord) {
141
                        
142
                        $chargesDetail = new DTO\ChargesRecord();
143
                        
144
                        if(isset($chargesRecord->Amt) && (string) $chargesRecord->Amt) {
145
                            $amount      = StringToUnits::convert((string) $chargesRecord->Amt);
146
                            $currency    = (string)$chargesRecord->Amt['Ccy'];
147
148
                            if ((string) $chargesRecord->CdtDbtInd === 'DBIT') {
149
                                $amount = $amount * -1;
150
                            }
151
                            
152
                            $chargesDetail->setAmount(new Money($amount, new Currency($currency)));
153
                        }
154
                        if (isset($chargesRecord->CdtDbtInd) && (string) $chargesRecord->CdtDbtInd === 'true') {
155
                            $chargesDetail->setChargesIncluded­Indicator(true);
156
                        }
157
                        if (isset($chargesRecord->Tp->Prtry->Id) && (string) $chargesRecord->Tp->Prtry->Id) {
158
                            $chargesDetail->setIdentification((string) $chargesRecord->Tp->Prtry->Id);
159
                        }
160
                        $charges->addRecord($chargesDetail);
161
                    }
162
                }
163
                $entry->setCharges($charges);
164
            }
165
            
166 21
            $this->entryDecoder->addTransactionDetails($entry, $xmlEntry);
167
168 21
            $record->addEntry($entry);
169 21
            $index++;
170 22
        }
171 22
    }
172
}
173