Passed
Pull Request — master (#75)
by
unknown
02:44
created

MT940   B

Complexity

Total Complexity 39

Size/Duplication

Total Lines 223
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 89.15%

Importance

Changes 7
Bugs 1 Features 0
Metric Value
wmc 39
c 7
b 1
f 0
lcom 1
cbo 1
dl 0
loc 223
ccs 115
cts 129
cp 0.8915
rs 8.2857

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A parse() 0 10 2
D parseToArray() 0 93 17
C parseDescription() 0 71 18
A getDate() 0 6 1
1
<?php
2
3
namespace Fhp\Parser;
4
5
use Fhp\Parser\Exception\MT940Exception;
6
7
/**
8
 * Class MT940
9
 * @package Fhp\Parser
10
 */
11
class MT940
12
{
13
    const TARGET_ARRAY = 0;
14
15
    const CD_CREDIT = 'credit';
16
    const CD_DEBIT = 'debit';
17
18
    /** @var string */
19
    protected $rawData;
20
    /** @var string */
21
    protected $soaDate;
22
23
    /**
24
     * MT940 constructor.
25
     *
26
     * @param string $rawData
27
     */
28 1
    public function __construct($rawData)
29
    {
30 1
        $this->rawData = (string) $rawData;
31 1
    }
32
33
    /**
34
     * @param string $target
35
     * @return array
36
     * @throws MT940Exception
37
     */
38 1
    public function parse($target)
39
    {
40
        switch ($target) {
41 1
            case static::TARGET_ARRAY:
42 1
                return $this->parseToArray();
43
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
44
            default:
45
                throw new MT940Exception('Invalid parse type provided');
46
        }
47
    }
48
49
    /**
50
     * @return array
51
     * @throws MT940Exception
52
     */
53 1
    protected function parseToArray()
54
    {
55
        // The divider can be either \r\n or @@
56 1
        $divider = substr_count($this->rawData, "\r\n-") > substr_count($this->rawData, '@@-') ? "\r\n" : '@@';
57
58 1
        $result = array();
59 1
        $days = preg_split('%' . $divider . '-$%', $this->rawData);
60 1
        $satzart = '';
61 1
        foreach ($days as &$day) {
62 1
            $day = explode($divider . ':', $day);
63 1
            for ($i = 0, $cnt = count($day); $i < $cnt; $i++) {
64 1
                if (preg_match('/^20:/', $day[$i])) {
65
                    // Satzart
66
                    $satzart = substr($day[$i], 3);
67
                }
68
                // handle start balance
69
                // 60F:C160401EUR1234,56
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
70 1
                elseif (preg_match('/^60(F|M):/', $day[$i])) {
71
                    // remove 60(F|M): for better parsing
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
72 1
                    $day[$i] = substr($day[$i], 4);
73 1
                    $this->soaDate = $this->getDate(substr($day[$i], 1, 6));
74
75 1
                    if (!isset($result[$this->soaDate])) {
76 1
                        $result[$this->soaDate] = array('start_balance' => array());
77 1
                    }
78
79 1
                    $cdMark = substr($day[$i], 0, 1);
80 1
                    if ($cdMark == 'C') {
81 1
                        $result[$this->soaDate]['start_balance']['credit_debit'] = static::CD_CREDIT;
82 1
                    } elseif ($cdMark == 'D') {
83
                        $result[$this->soaDate]['start_balance']['credit_debit'] = static::CD_DEBIT;
84
                    }
85
86 1
                    $amount = str_replace(',', '.', substr($day[$i], 10));
87 1
                    $result[$this->soaDate]['start_balance']['amount'] = $amount;
88 1
                } elseif (
89
                    // found transaction
90
                    // trx:61:1603310331DR637,39N033NONREF
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
91 1
                    0 === strpos($day[$i], '61:')
92 1
                    && isset($day[$i + 1])
93 1
                    && 0 === strpos($day[$i + 1], '86:')
94 1
                ) {
95 1
                    $transaction = substr($day[$i], 3);
96 1
                    $description = substr($day[$i + 1], 3);
97
98 1
                    if (!isset($result[$this->soaDate]['transactions'])) {
99 1
                        $result[$this->soaDate]['transactions'] = array();
100 1
                    }
101
102
                    // short form for better handling
103 1
                    $trx = &$result[$this->soaDate]['transactions'];
104
105 1
                    preg_match('/^\d{6}(\d{4})?(C|D|RC|RD)([A-Z]{1})?([^N]+)N/', $transaction, $trxMatch);
106 1
                    if ($trxMatch[2] == 'C') {
107 1
                        $trx[count($trx)]['credit_debit'] = static::CD_CREDIT;
108 1
                    } elseif ($trxMatch[2] == 'D') {
109
                        $trx[count($trx)]['credit_debit'] = static::CD_DEBIT;
110
                    } else {
111
                        throw new MT940Exception('cd mark not found in: ' . $transaction);
112
                    }
113
114 1
                    $amount = $trxMatch[4];
115 1
                    $amount = str_replace(',', '.', $amount);
116 1
                    $trx[count($trx) - 1]['amount'] = floatval($amount);
117
118 1
                    $description = $this->parseDescription($description);
119 1
                    $trx[count($trx) - 1]['description'] = $description;
120
121
                    // :61:1605110509D198,02NMSCNONREF
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
122
                    // 16 = year
123
                    // 0511 = valuta date
124
                    // 0509 = booking date
125 1
                    $year = substr($transaction, 0, 2);
126 1
                    $valutaDate = $this->getDate($year . substr($transaction, 2, 4));
127
128 1
                    $bookingDate = substr($transaction, 6, 4);
129 1
                    if (preg_match('/^\d{4}$/', $bookingDate)) {
130
                        // if valuta date is earlier than booking date, then it must be in the new year.
131 1
                        $year = substr($transaction, 2, 2) < substr($transaction, 6, 2) ? --$year : $year;
132 1
                        $bookingDate = $this->getDate($year . $bookingDate);
133 1
                    } else {
134
                        // if booking date not set in :61, then we have to take it from :60F
135
                        $bookingDate = $this->soaDate;
136
                    }
137
138 1
                    $trx[count($trx) - 1]['booking_date'] = $bookingDate;
139 1
                    $trx[count($trx) - 1]['valuta_date'] = $valutaDate;
140 1
                    $trx[count($trx) - 1]['satzart'] = $satzart;                }
141 1
            }
142 1
        }
143
144 1
        return $result;
145
    }
146
147
    /**
148
     * @param string $descr
149
     * @return array
150
     */
151 1
    protected function parseDescription($descr)
152
    {
153 1
        $prepared = array();
154 1
        $result = array();
155
156
        // prefill with empty values
157 1
        for ($i = 0; $i <= 63; $i++) {
158 1
            $prepared[$i] = null;
159 1
        }
160
161 1
        $descr = str_replace('? ', '?', $descr);
162 1
        preg_match_all('/\?[\r\n]*(\d{2})([^\?]+)/', $descr, $matches, PREG_SET_ORDER);
163
164 1
        $descriptionLines = array();
165 1
        $description1 = ''; // Legacy, could be removed.
166 1
        $description2 = ''; // Legacy, could be removed.
167 1
        foreach ($matches as $m) {
168 1
            $index = (int) $m[1];
169 1
            if ((20 <= $index && $index <= 29) || (60 <= $index && $index <= 63)) {
170 1
                if (20 <= $index && $index <= 29) {
171 1
                    $description1 .= $m[2];
172 1
                } else {
173
                    $description2 .= $m[2];
174
                }
175 1
                $m[2] = trim(str_replace("\r\n", '', $m[2]));
176 1
                if (!empty($m[2])) {
177 1
                    $descriptionLines[] = $m[2];
178 1
                }
179 1
            } else {
180 1
                $prepared[$index] = $m[2];
181
            }
182 1
        }
183
184 1
        $description = array();
185 1
        if (empty($descriptionLines) || strlen($descriptionLines[0]) < 5 || $descriptionLines[0][4] !== '+') {
186
            $description['SVWZ'] = implode('', $descriptionLines);
187
        } else {
188 1
            $lastType = null;
189 1
            foreach ($descriptionLines as $line) {
190 1
                if (strlen($line) > 5 && $line[4] === '+') {
191 1
                    if ($lastType != null) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $lastType of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
192 1
                        $description[$lastType] = trim($description[$lastType]);
193 1
                    }
194 1
                    $lastType = substr($line, 0, 4);
195 1
                    $description[$lastType] = substr($line, 5);
196 1
                } else {
197 1
                    $description[$lastType] .= $line;
198
                }
199 1
                if (strlen($line) < 27) {
200
                    // Usually, lines are 27 characters long. In case characters are missing, then it's either the end
201
                    // of the current type or spaces have been trimmed from the end. We want to collapse multiple spaces
202
                    // into one and we don't want to leave trailing spaces behind. So add a single space here to make up
203
                    // for possibly missing spaces, and if it's the end of the type, it will be trimmed off later.
204 1
                    $description[$lastType] .= ' ';
205 1
                }
206 1
            }
207 1
            $description[$lastType] = trim($description[$lastType]);
208
        }
209
210 1
        $result['description']       = $description;
211 1
        $result['booking_text']      = trim(str_replace("\r\n", '', $prepared[0]));
212 1
        $result['primanoten_nr']     = trim(str_replace("\r\n", '', $prepared[10]));
213 1
        $result['description_1']     = trim(str_replace("\r\n", '', $description1));
214 1
        $result['bank_code']         = trim(str_replace("\r\n", '', $prepared[30]));
215 1
        $result['account_number']    = trim(str_replace("\r\n", '', $prepared[31]));
216 1
        $result['name']              = trim(str_replace("\r\n", '', $prepared[32] . $prepared[33]));
217 1
        $result['text_key_addition'] = trim(str_replace("\r\n", '', $prepared[34]));
218 1
        $result['description_2']     = trim(str_replace("\r\n", '', $description2));
219
220 1
        return $result;
221
    }
222
223
    /**
224
     * @param string $val
225
     * @return string
226
     */
227 1
    protected function getDate($val)
228
    {
229 1
        $val = '20' . $val;
230 1
        preg_match('/(\d{4})(\d{2})(\d{2})/', $val, $m);
231 1
        return $m[1] . '-' . $m[2] . '-' . $m[3];
232
    }
233
}
234