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

Knab   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 101
Duplicated Lines 8.91 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 15
c 2
b 0
f 1
lcom 1
cbo 1
dl 9
loc 101
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A parseStatementBank() 0 4 1
A parseStatementStartPrice() 0 4 1
A parseStatementEndPrice() 0 4 1
A parseStatementStartTimestamp() 0 4 1
A parseStatementEndTimestamp() 0 4 1
A parseTransactionAccount() 0 11 3
A parseTransactionAccountName() 9 9 3
A parseTransactionDescription() 0 15 3
A isApplicable() 0 5 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
namespace Kingsquare\Parser\Banking\Mt940\Engine;
3
4
use Kingsquare\Parser\Banking\Mt940\Engine;
5
6
/**
7
 * Knab parser for Kingsquare mt940 package.
8
 *
9
 * @package Kingsquare\Parser\Banking\Mt940\Engine
10
 * @author Kingsquare ([email protected])
11
 * @author Sam Mousa ([email protected])
12
 * @license http://opensource.org/licenses/MIT MIT
13
 */
14
class Knab extends Engine
15
{
16
    const IBAN = '[a-zA-Z]{2}[0-9]{2}[a-zA-Z0-9]{4}[0-9]{7}(?:[a-zA-Z0-9]?){0,16}';
17
18
    /**
19
     * @inheritdoc
20
     */
21
    protected function parseStatementBank()
22
    {
23
        return 'KNAB';
24
    }
25
26
    /**
27
     * @inheritdoc
28
     */
29
    protected function parseStatementStartPrice()
30
    {
31
        return $this->parseStatementPrice('60M');
32
    }
33
34
    /**
35
     * @inheritdoc
36
     */
37
    protected function parseStatementEndPrice()
38
    {
39
        return $this->parseStatementPrice('62M');
40
    }
41
42
    /**
43
     * @inheritdoc
44
     */
45
    protected function parseStatementStartTimestamp()
46
    {
47
        return $this->parseTimestampFromStatement('60M');
48
    }
49
50
    /**
51
     * @inheritdoc
52
     */
53
    protected function parseStatementEndTimestamp()
54
    {
55
        return $this->parseTimestampFromStatement('62M');
56
    }
57
58
    /**
59
     * @inheritdoc
60
     */
61
    protected function parseTransactionAccount()
62
    {
63
        $results = [];
64
65
        $pattern = '/^:86:.*?REK:\s*(?<account>' . self::IBAN . '|\d+)/ims';
66
        if (preg_match($pattern, $this->getCurrentTransactionData(), $results)
67
                && !empty($results['account'])
68
        ) {
69
            return $results['account'];
70
        }
71
    }
72
73
    /**
74
     * @inheritdoc
75
     */
76 View Code Duplication
    protected function parseTransactionAccountName()
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...
77
    {
78
        $results = [];
79
        if (preg_match('/NAAM: (.+)/', $this->getCurrentTransactionData(), $results)
80
                && !empty($results[1])
81
        ) {
82
            return trim($results[1]);
83
        }
84
    }
85
86
    /**
87
     * @inheritdoc
88
     */
89
    protected function parseTransactionDescription()
90
    {
91
        $description = parent::parseTransactionDescription();
92
        $accountIsInDescription = strpos($description, 'REK:');
93
        if ($accountIsInDescription !== false) {
94
            return trim(substr($description, 0, $accountIsInDescription));
95
        }
96
97
        $name = $this->parseTransactionAccountName();
98
        $accountNameIsInDescription = strpos($description, $name);
99
        if ($accountNameIsInDescription !== false) {
100
            return trim(substr($description, 0, $accountNameIsInDescription-6));
101
        }
102
        return $description;
103
    }
104
105
    /**
106
     * @inheritdoc
107
     */
108
    public static function isApplicable($string)
109
    {
110
        $firstline = strtok($string, "\r\n\t");
111
        return strpos($firstline, 'F01KNABNL2HAXXX0000000000') !== false;
112
    }
113
114
}
115