Completed
Push — master ( 26e69a...ab0b77 )
by Robin
03:27
created

Ing   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 129
Duplicated Lines 26.36 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 8
Bugs 0 Features 1
Metric Value
wmc 25
c 8
b 0
f 1
lcom 1
cbo 1
dl 34
loc 129
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A parseStatementBank() 0 4 1
B parseTransactionAccount() 0 25 6
B parseTransactionAccountName() 6 22 5
B parseNameFromTransactionData() 12 18 5
B sanitizeDescription() 16 16 7
A isApplicable() 0 6 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Kingsquare\Parser\Banking\Mt940\Engine;
4
5
use Kingsquare\Parser\Banking\Mt940\Engine;
6
7
/**
8
 * @author Kingsquare ([email protected])
9
 * @license http://opensource.org/licenses/MIT MIT
10
 */
11
class Ing extends Engine
12
{
13
    /**
14
     * returns the name of the bank.
15
     *
16
     * @return string
17
     */
18
    protected function parseStatementBank()
19
    {
20
        return 'ING';
21
    }
22
23
    /**
24
     * Overloaded: Added simple IBAN transaction handling.
25
     *
26
     * {@inheritdoc}
27
     */
28
    protected function parseTransactionAccount()
29
    {
30
        $account = parent::parseTransactionAccount();
31
        if ($account !== '') {
32
            return $account;
33
        }
34
35
        // IBAN
36
        $transactionData = str_replace('Europese Incasso, doorlopend ', '', $this->getCurrentTransactionData());
37
        $transactionData = preg_replace('![\r\n]+!', '', $transactionData);
38
        if (preg_match('#/CNTP/(.*?)/#', $transactionData, $results)) {
39
            $account = trim($results[1]);
40
            if (!empty($account)) {
41
                return $this->sanitizeAccount($account);
42
            }
43
        }
44
        if (preg_match('#:86:([A-Z]{2}[0-9]{2}[A-Z]{4}[\d]+?) [A-Z]{6}[A-Z0-9]{0,4} #', $transactionData, $results)) {
45
            $account = trim($results[1]);
46
            if (!empty($account)) {
47
                return $this->sanitizeAccount($account);
48
            }
49
        }
50
51
        return '';
52
    }
53
54
    /**
55
     * Overloaded: Added simple IBAN transaction handling.
56
     *
57
     * {@inheritdoc}
58
     */
59
    protected function parseTransactionAccountName()
60
    {
61
        $name = parent::parseTransactionAccountName();
62
        if ($name !== '') {
63
            return $name;
64
        }
65
66
        // IBAN
67
        $transactionData = str_replace('Europese Incasso, doorlopend ', '', $this->getCurrentTransactionData());
68
        $transactionData = preg_replace('![\r\n]+!', '', $transactionData);
69 View Code Duplication
        if (preg_match('#/CNTP/[^/]*/[^/]*/(.*?)/#', $transactionData, $results)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
70
            $name = trim($results[1]);
71
            if (!empty($name)) {
72
                return $this->sanitizeAccountName($name);
73
            }
74
        }
75
        if (preg_match('#:86:.*? [^ ]+ (.*)#', $transactionData, $results) !== 1) {
76
            return '';
77
        }
78
79
        return $this->parseNameFromTransactionData($results[1]);
80
    }
81
82
    /**
83
     * @param $transactionData
84
     *
85
     * @return string
86
     */
87
    private function parseNameFromTransactionData($transactionData)
88
    {
89 View Code Duplication
        if (preg_match('#(.*) (Not-Provided|NOTPROVIDED)#', $transactionData, $results) === 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
90
            $name = trim($results[1]);
91
            if (!empty($name)) {
92
                return $this->sanitizeAccountName($name);
93
            }
94
        }
95
96 View Code Duplication
        if (preg_match('#\D+#', $transactionData, $results)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
97
            $name = trim($results[0]);
98
            if (!empty($name)) {
99
                return $this->sanitizeAccountName($name);
100
            }
101
        }
102
103
        return '';
104
    }
105
106
    /**
107
     * Overloaded: ING encapsulates the description with /REMI/ for SEPA.
108
     *
109
     * {@inheritdoc}
110
     */
111 View Code Duplication
    protected function sanitizeDescription($string)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
112
    {
113
        $description = parent::sanitizeDescription($string);
114
        if (strpos($description, '/REMI/USTD//') !== false
115
                && preg_match('#/REMI/USTD//(.*?)/#s', $description, $results) && !empty($results[1])
116
        ) {
117
            return $results[1];
118
        }
119
        if (strpos($description, '/REMI/STRD/CUR/') !== false
120
                && preg_match('#/REMI/STRD/CUR/(.*?)/#s', $description, $results) && !empty($results[1])
121
        ) {
122
            return $results[1];
123
        }
124
125
        return $description;
126
    }
127
128
    /**
129
     * Overloaded: Is applicable if first line has INGB.
130
     *
131
     * {@inheritdoc}
132
     */
133
    public static function isApplicable($string)
134
    {
135
        $firstline = strtok($string, "\r\n\t");
136
137
        return strpos($firstline, 'INGB') !== false;
138
    }
139
}
140