Completed
Push — master ( f5773e...1d76e5 )
by Adelar
02:11
created

Ofx::getMovements()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
namespace Adelarcubs\OFXParser;
3
4
use JsonSerializable;
5
use SimpleXMLElement;
6
7
/**
8
 *
9
 * @author Adelar Tiemann Junior <[email protected]>
10
 */
11
class Ofx implements JsonSerializable
12
{
13
14
    public $ofx;
15
16
    private $movements = [];
17
18 4
    public function __construct(SimpleXMLElement $xml)
19
    {
20 4
        $this->ofx = $xml;
21
22 4
        if (isset($this->ofx->BANKMSGSRSV1->STMTTRNRS->STMTRS->BANKTRANLIST->STMTTRN)) {
23 3
            $movements = $this->ofx->BANKMSGSRSV1->STMTTRNRS->STMTRS->BANKTRANLIST->STMTTRN;
24 3
        }
25 4
        if (isset($this->ofx->CREDITCARDMSGSRSV1->CCSTMTTRNRS->CCSTMTRS->BANKTRANLIST)) {
26 1
            $movements = $this->ofx->CREDITCARDMSGSRSV1->CCSTMTTRNRS->CCSTMTRS->BANKTRANLIST;
27 1
        }
28
29 4
        $this->exportMovements($movements);
0 ignored issues
show
Bug introduced by
The variable $movements 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...
30 4
    }
31
32 4
    public function getMovements()
33
    {
34 4
        return $this->movements;
35
    }
36
37 4
    private function exportMovements(SimpleXMLElement $xml)
38
    {
39 4
        foreach ($xml as $value) {
40 4
            $this->movements[] = new OfxMovement($value);
41 4
        }
42 4
    }
43
44
    public function jsonSerialize()
45
    {
46
        return json_decode(json_encode($this->ofx), true);
47
    }
48
}
49