Completed
Push — master ( aabf4d...606f02 )
by Frederik
9s
created

EntryTransactionDetail::addRelatedParties()   C

Complexity

Conditions 11
Paths 23

Size

Total Lines 42
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 29
CRAP Score 11.3851

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 42
ccs 29
cts 34
cp 0.8529
rs 5.2653
cc 11
eloc 27
nc 23
nop 2
crap 11.3851

How to fix   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\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 11
    public function addReferences(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail)
16
    {
17 11
        if (false === isset($xmlDetail->Refs->EndToEndId)) {
18 3
            return;
19
        }
20
21 8
        $endToEndId = (string)$xmlDetail->Refs->EndToEndId;
22 9
        $mandateId = null;
23
24 8
        if (isset($xmlDetail->Refs->MndtId)) {
25 1
            $mandateId = (string)$xmlDetail->Refs->MndtId;
26 1
        }
27
28 8
        $detail->addReference(new DTO\Reference($endToEndId, $mandateId));
29 8
    }
30
31
    /**
32
     * @param DTO\EntryTransactionDetail $detail
33
     * @param SimpleXMLElement           $xmlDetail
34
     */
35 11
    public function addRelatedParties(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail)
36
    {
37 11
        if (false === isset($xmlDetail->RltdPties)) {
38 1
            return;
39
        }
40
41 10
        foreach ($xmlDetail->RltdPties as $xmlRelatedParty) {
42 10
            if (isset($xmlRelatedParty->Cdtr)) {
43 10
                $xmlRelatedPartyType = $xmlRelatedParty->Cdtr;
44 10
                $xmlRelatedPartyTypeAccount = $xmlRelatedParty->CdtrAcct;
45 10
                $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 10
            } 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 10
            if (isset($xmlRelatedPartyType->PstlAdr)) {
55 8
                $address = new DTO\Address();
56 8
                if (isset($xmlRelatedPartyType->PstlAdr->Ctry)) {
57 8
                    $address = $address->setCountry((string) $xmlRelatedPartyType->PstlAdr->Ctry);
58 8
                }
59 8
                if (isset($xmlRelatedPartyType->PstlAdr->AdrLine)) {
60 1
                    foreach ($xmlRelatedPartyType->PstlAdr->AdrLine as $line) {
61 1
                        $address = $address->addAddressLine((string)$line);
62 1
                    }
63 1
                }
64
65 8
                $relatedPartyType->setAddress($address);
66 8
            }
67
68 10
            $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 10
            if (isset($xmlRelatedPartyTypeAccount->Id->IBAN) && $ibanCode = (string) $xmlRelatedPartyTypeAccount->Id->IBAN) {
70 10
                $account = new DTO\Account(new Iban($ibanCode));
71 10
            }
72
73 10
            $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 10
            $detail->addRelatedParty($relatedParty);
75 10
        }
76 10
    }
77
78
    /**
79
     * @param DTO\EntryTransactionDetail $detail
80
     * @param SimpleXMLElement           $xmlDetail
81
     */
82 12
    public function addRemittanceInformation(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail)
83
    {
84 12
        if (false === isset($xmlDetail->RmtInf)) {
85 1
            return;
86
        }
87
88 11
        if (isset($xmlDetail->RmtInf->Ustrd)) {
89 8
            $remittanceInformation = DTO\RemittanceInformation::fromUnstructured(
90 8
                (string)$xmlDetail->RmtInf->Ustrd
91 8
            );
92 8
            $detail->setRemittanceInformation($remittanceInformation);
93
94 8
            return;
95
        }
96
        
97 3
        if (isset($xmlDetail->RmtInf->Strd)
98 3
            && isset($xmlDetail->RmtInf->Strd->CdtrRefInf)
99 3
            && isset($xmlDetail->RmtInf->Strd->CdtrRefInf->Ref)
100 3
        ) {
101 3
            $creditorReferenceInformation = DTO\CreditorReferenceInformation::fromUnstructured(
102 3
                (string)$xmlDetail->RmtInf->Strd->CdtrRefInf->Ref
103 3
            );
104 3
            $remittanceInformation = new DTO\RemittanceInformation();
105 3
            $remittanceInformation->setCreditorReferenceInformation($creditorReferenceInformation);
106 3
            $detail->setRemittanceInformation($remittanceInformation);
107 3
        }
108 3
    }
109
110
    /**
111
     * @param DTO\EntryTransactionDetail $detail
112
     * @param SimpleXMLElement           $xmlDetail
113
     */
114 11
    public function addReturnInformation(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail)
115
    {
116 11
        if (isset($xmlDetail->RtrInf) && isset($xmlDetail->RtrInf->Rsn->Cd)) {
117 1
            $remittanceInformation = DTO\ReturnInformation::fromUnstructured(
118 1
                (string)$xmlDetail->RtrInf->Rsn->Cd,
119 1
                (string)$xmlDetail->RtrInf->AddtlInf
120 1
            );
121 1
            $detail->setReturnInformation($remittanceInformation);
122 1
        }
123 11
    }
124
125
    /**
126
     * @param DTO\EntryTransactionDetail $detail
127
     * @param SimpleXMLElement           $xmlDetail
128
     */
129 11
    public function addAdditionalTransactionInformation(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail)
130
    {
131 11
        if (isset($xmlDetail->AddtlTxInf)) {
132 1
            $additionalInformation = new DTO\AdditionalTransactionInformation(
133 1
                (string) $xmlDetail->AddtlTxInf
134 1
            );
135 1
            $detail->setAdditionalTransactionInformation($additionalInformation);
136 1
        }
137 11
    }
138
}
139