Completed
Pull Request — master (#14)
by Yann
02:46
created

EntryTransactionDetail::addRemittanceInformation()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 6.0052

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 22
ccs 18
cts 19
cp 0.9474
rs 8.6737
cc 6
eloc 14
nc 5
nop 2
crap 6.0052
1
<?php
2
3
namespace Genkgo\Camt\Camt053\Decoder;
4
5
use Genkgo\Camt\Camt053\DTO;
6
use \SimpleXMLElement;
7
use Genkgo\Camt\Iban;
8
9
class EntryTransactionDetail
10
{
11
    /**
12
     * @param DTO\EntryTransactionDetail $detail
13
     * @param SimpleXMLElement           $xmlDetail
14
     */
15 9
    public function addReferences(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail)
16
    {
17 9
        if (false === isset($xmlDetail->Refs->EndToEndId)) {
18 2
            return;
19
        }
20
21 7
        $endToEndId = (string)$xmlDetail->Refs->EndToEndId;
22 7
        $mandateId = null;
23
24 7
        if (isset($xmlDetail->Refs->MndtId)) {
25
            $mandateId = (string)$xmlDetail->Refs->MndtId;
26
        }
27
28 7
        $detail->addReference(new DTO\Reference($endToEndId, $mandateId));
29 7
    }
30
31
    /**
32
     * @param DTO\EntryTransactionDetail $detail
33
     * @param SimpleXMLElement           $xmlDetail
34
     */
35 9
    public function addRelatedParties(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail)
36
    {
37 9
        if (false === isset($xmlDetail->RltdPties)) {
38
            return;
39
        }
40
41 9
        foreach ($xmlDetail->RltdPties as $xmlRelatedParty) {
42 9
            if (isset($xmlRelatedParty->Cdtr)) {
43 9
                $xmlRelatedPartyType = $xmlRelatedParty->Cdtr;
44 9
                $xmlRelatedPartyTypeAccount = $xmlRelatedParty->CdtrAcct;
45 9
                $relatedPartyType = $creditor = new DTO\Creditor((string) $xmlRelatedPartyType->Nm);
0 ignored issues
show
Unused Code introduced by
$creditor is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
46 9
            } elseif (isset($xmlRelatedParty->Dbtr)) {
47
                $xmlRelatedPartyType = $xmlRelatedParty->Dbtr;
48
                $xmlRelatedPartyTypeAccount = $xmlRelatedParty->DbtrAcct;
49
                $relatedPartyType = $creditor = new DTO\Debtor((string) $xmlRelatedPartyType->Nm);
0 ignored issues
show
Unused Code introduced by
$creditor is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
50
            } else {
51
                continue;
52
            }
53
54 9
            if (isset($xmlRelatedPartyType->PstlAdr)) {
55 7
                $address = new DTO\Address();
56 7
                if (isset($xmlRelatedPartyType->PstlAdr->Ctry)) {
57 7
                    $address = $address->setCountry((string) $xmlRelatedPartyType->PstlAdr->Ctry);
58 7
                }
59 7
                if (isset($xmlRelatedPartyType->PstlAdr->AdrLine)) {
60
                    foreach ($xmlRelatedPartyType->PstlAdr->AdrLine as $line) {
61
                        $address = $address->addAddressLine((string)$line);
62
                    }
63
                }
64
65 7
                $relatedPartyType->setAddress($address);
66 7
            }
67
68 9
            $acount = null;
0 ignored issues
show
Unused Code introduced by
$acount is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
69 9
            if (isset($xmlRelatedPartyTypeAccount->Id->IBAN) && $ibanCode = (string) $xmlRelatedPartyTypeAccount->Id->IBAN) {
70 9
                $account = new DTO\Account(new Iban($ibanCode));
71 9
            }
72
73 9
            $relatedParty = new DTO\RelatedParty($relatedPartyType, $account);
0 ignored issues
show
Bug introduced by
The variable $account 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...
74 9
            $detail->addRelatedParty($relatedParty);
75 9
        }
76 9
    }
77
78
    /**
79
     * @param DTO\EntryTransactionDetail $detail
80
     * @param SimpleXMLElement           $xmlDetail
81
     */
82 9
    public function addRemittanceInformation(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail)
83
    {
84 9
        if (false === isset($xmlDetail->RmtInf)) {
85
            return;
86
        }
87
88 9
        if (isset($xmlDetail->RmtInf->Ustrd)) {
89 7
            $remittanceInformation = DTO\RemittanceInformation::fromUnstructured(
90 7
                (string)$xmlDetail->RmtInf->Ustrd
91 7
            );
92 7
            $detail->setRemittanceInformation($remittanceInformation);
93 9
        } elseif (isset($xmlDetail->RmtInf->Strd)) {
94 2
            if (isset($xmlDetail->RmtInf->Strd->CdtrRefInf) && isset($xmlDetail->RmtInf->Strd->CdtrRefInf->Ref)) {
95 2
                $creditorReferenceInformation = DTO\CreditorReferenceInformation::fromUnstructured(
96 2
                    (string)$xmlDetail->RmtInf->Strd->CdtrRefInf->Ref
97 2
                );
98 2
                $remittanceInformation = new DTO\RemittanceInformation();
99 2
                $remittanceInformation->setCreditorReferenceInformation($creditorReferenceInformation);
100 2
                $detail->setRemittanceInformation($remittanceInformation);
101 2
            }
102 2
        }
103 9
    }
104
105
    /**
106
     * @param DTO\EntryTransactionDetail $detail
107
     * @param SimpleXMLElement           $xmlDetail
108
     */
109 9
    public function addReturnInformation(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail)
110
    {
111 9
        if (isset($xmlDetail->RtrInf) && isset($xmlDetail->RtrInf->Rsn->Cd)) {
112
            $remittanceInformation = DTO\ReturnInformation::fromUnstructured(
113
                (string)$xmlDetail->RtrInf->Rsn->Cd,
114
                (string)$xmlDetail->RtrInf->AddtlInf
115
            );
116
            $detail->setReturnInformation($remittanceInformation);
117
        }
118 9
    }
119
120
    /**
121
     * @param DTO\EntryTransactionDetail $detail
122
     * @param SimpleXMLElement           $xmlDetail
123
     */
124 9
    public function addAdditionalTransactionInformation(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail)
125
    {
126 9
        if (isset($xmlDetail->AddtlTxInf)) {
127
            $additionalInformation = new DTO\AdditionalTransactionInformation(
128
                (string) $xmlDetail->AddtlTxInf
129
            );
130
            $detail->setAdditionalTransactionInformation($additionalInformation);
131
        }
132 9
    }
133
}
134