Skip to content

Commit 380a6c0

Browse files
committed
updated all code with cleancode standards et al
- made sure every method has a return value (KNAB) - changed [0-9] regexes to use \d instead - strict comparission checks for debit/credit values - removed case insensitive regex checks where applicable - array_merge in loop replaced with ```call_user_func_array('array_merge', $values)``` (@see https://github.com/dseguy/clearPHP/blob/master/rules/no-array_merge-in-loop.md)
1 parent 8ad20bc commit 380a6c0

File tree

15 files changed

+122
-127
lines changed

15 files changed

+122
-127
lines changed

src/Banking/Transaction.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -177,15 +177,15 @@ public function getTransactionCode()
177177
*/
178178
public function isDebit()
179179
{
180-
return $this->getDebitCredit() == self::DEBIT;
180+
return $this->getDebitCredit() === self::DEBIT;
181181
}
182182

183183
/**
184184
* @return bool
185185
*/
186186
public function isCredit()
187187
{
188-
return $this->getDebitCredit() == self::CREDIT;
188+
return $this->getDebitCredit() === self::CREDIT;
189189
}
190190

191191
/**

src/Parser/Banking/Mt940/Engine.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ public function parse()
170170
protected function parseStatementData()
171171
{
172172
$results = preg_split(
173-
'/(^:20:|^-X{,3}$|\Z)/sm',
173+
'/(^:20:|^-X{,3}$|\Z)/m',
174174
$this->getRawData(),
175175
-1,
176176
PREG_SPLIT_NO_EMPTY
@@ -310,7 +310,7 @@ protected function parseStatementTimestamp()
310310
{
311311
trigger_error('Deprecated in favor of splitting the start and end timestamps for a statement. '.
312312
'Please use parseStatementStartTimestamp($format) or parseStatementEndTimestamp($format) instead. '.
313-
'setTimestamp is now parseStatementStartTimestamp', E_USER_DEPRECATED);
313+
'parseStatementTimestamp is now parseStatementStartTimestamp', E_USER_DEPRECATED);
314314

315315
return $this->parseStatementStartTimestamp();
316316
}
@@ -529,7 +529,7 @@ protected function sanitizeAccount($string)
529529

530530
// crude IBAN to 'old' converter
531531
if (Mt940::$removeIBAN
532-
&& preg_match('#[A-Z]{2}[0-9]{2}[A-Z]{4}(.*)#', $string, $results)
532+
&& preg_match('#[A-Z]{2}[\d]{2}[A-Z]{4}(.*)#', $string, $results)
533533
&& !empty($results[1])
534534
) {
535535
$string = $results[1];
@@ -543,7 +543,7 @@ protected function sanitizeAccount($string)
543543
),
544544
'0'
545545
);
546-
if ($account != '' && strlen($account) < 9 && strpos($account, 'P') === false) {
546+
if ($account !== '' && strlen($account) < 9 && strpos($account, 'P') === false) {
547547
$account = 'P'.$account;
548548
}
549549

@@ -595,7 +595,7 @@ protected function sanitizeDescription($string)
595595
protected function sanitizeDebitCredit($string)
596596
{
597597
$debitOrCredit = strtoupper(substr((string) $string, 0, 1));
598-
if ($debitOrCredit != Transaction::DEBIT && $debitOrCredit != Transaction::CREDIT) {
598+
if ($debitOrCredit !== Transaction::DEBIT && $debitOrCredit !== Transaction::CREDIT) {
599599
trigger_error('wrong value for debit/credit ('.$string.')', E_USER_ERROR);
600600
$debitOrCredit = '';
601601
}

src/Parser/Banking/Mt940/Engine/Ing.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ protected function parseTransactionAccount()
4141
return $this->sanitizeAccount($account);
4242
}
4343
}
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)) {
44+
if (preg_match('#:86:([A-Z]{2}[\d]{2}[A-Z]{4}[\d]+?) [A-Z]{6}[A-Z0-9]{0,4} #', $transactionData, $results)) {
4545
$account = trim($results[1]);
4646
if (!empty($account)) {
4747
return $this->sanitizeAccount($account);

src/Parser/Banking/Mt940/Engine/Knab.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ protected function parseTransactionAccount()
7676
) {
7777
return $results['account'];
7878
}
79+
80+
return '';
7981
}
8082

8183
/**
@@ -99,11 +101,14 @@ protected function parseTransactionAccountName()
99101
&& !empty($results[1])
100102
) {
101103
return trim($results[1]);
102-
} elseif (preg_match('#/NAME/(.*?)\n?/(REMI|CSID)/#ms', $this->getCurrentTransactionData(), $results)
104+
}
105+
if (preg_match('#/NAME/(.*?)\n?/(REMI|CSID)/#ms', $this->getCurrentTransactionData(), $results)
103106
&& !empty($results[1])
104107
) {
105108
return trim($results[1]);
106109
}
110+
111+
return '';
107112
}
108113

109114
/**
@@ -126,6 +131,9 @@ protected function parseTransactionDescription()
126131
}
127132

128133
$name = $this->parseTransactionAccountName();
134+
if ($name === '') {
135+
return $description;
136+
}
129137
$accountNameIsInDescription = strpos($description, $name);
130138
if ($accountNameIsInDescription !== false) {
131139
return trim(substr($description, 0, $accountNameIsInDescription-6));

src/Parser/Banking/Mt940/Engine/Rabo.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ protected function parseTransactionAccount()
3535
return $this->sanitizeAccount($results[1]);
3636
}
3737

38-
if (preg_match('/^:61:.{26}(.{16})/im', $this->getCurrentTransactionData(), $results)
38+
if (preg_match('/^:61:.{26}(.{16})/m', $this->getCurrentTransactionData(), $results)
3939
&& !empty($results[1])
4040
) {
4141
return $this->sanitizeAccount($results[1]);
@@ -114,7 +114,7 @@ protected function parseTransactionValueTimestamp()
114114
protected function sanitizeAccount($string)
115115
{
116116
$account = parent::sanitizeAccount($string);
117-
if (strlen($account) > 20 && strpos($account, '80000') == 0) {
117+
if (strlen($account) > 20 && strpos($account, '80000') === 0) {
118118
$account = substr($account, 5);
119119
}
120120

src/Parser/Banking/Mt940/Engine/Spk.php

+3-7
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,8 @@ protected function parseTransactionDebitCredit()
105105
protected function parseTransactionCancellation()
106106
{
107107
$results = [];
108-
if (preg_match('/^:61:\d+(R)?[CD].?\d+/', $this->getCurrentTransactionData(), $results)
109-
&& !empty($results[1])
110-
) {
111-
return true;
112-
}
113-
return false;
108+
return preg_match('/^:61:\d+(R)?[CD].?\d+/', $this->getCurrentTransactionData(), $results)
109+
&& !empty($results[1]);
114110
}
115111

116112
/**
@@ -121,7 +117,7 @@ protected function parseTransactionCancellation()
121117
protected function parseStatementData()
122118
{
123119
return preg_split(
124-
'/(^:20:|^-X{,3}$|\Z)/sm',
120+
'/(^:20:|^-X{,3}$|\Z)/m',
125121
$this->getRawData(),
126122
-1,
127123
PREG_SPLIT_NO_EMPTY

src/Parser/Banking/Mt940/Engine/Triodos.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ protected function parseTransactionAccount()
6666
{
6767
$parts = $this->getDescriptionParts();
6868
$account = $parts[0];
69-
if (preg_match('#[A-Z]{2}[0-9]{2}[A-Z]{4}(.*)#', $parts[2], $results)) {
69+
if (preg_match('#[A-Z]{2}[\d]{2}[A-Z]{4}(.*)#', $parts[2], $results)) {
7070
$account = $parts[2];
7171
} elseif (preg_match('#10(\d+)#', $parts[0], $results)) {
7272
$account = $results[1];
@@ -91,7 +91,7 @@ private function getTransactionAccountParts()
9191
{
9292
$parts = $this->getDescriptionParts();
9393
array_shift($parts); // remove BBAN / BIC code
94-
if (preg_match('#[A-Z]{2}[0-9]{2}[A-Z]{4}(.*)#', $parts[1], $results)) {
94+
if (preg_match('#[A-Z]{2}[\d]{2}[A-Z]{4}(.*)#', $parts[1], $results)) {
9595
array_shift($parts); // remove IBAN too
9696
array_shift($parts); // remove IBAN some more
9797
}
@@ -136,7 +136,7 @@ private function getDescriptionParts()
136136
protected function parseStatementData()
137137
{
138138
return preg_split(
139-
'/(^:20:|^-X{,3}$|\Z)/sm',
139+
'/(^:20:|^-X{,3}$|\Z)/m',
140140
$this->getRawData(),
141141
-1,
142142
PREG_SPLIT_NO_EMPTY

test/Banking/StatementTest.php

+38-38
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,13 @@ public function testJsonSerialization()
137137
$expected = '{"bank":"ABN","account":"62.90.64.393","transactions":[],'.
138138
'"startPrice":16250,"endPrice":6250,"startTimestamp":123,"endTimestamp":0,"number":"2665487AAF"}';
139139
$params = [
140-
'bank' => 'ABN',
141-
'account' => '62.90.64.393',
142-
'transactions' => [],
143-
'startPrice' => 16250,
144-
'endPrice' => 6250,
145-
'startTimestamp' => 123,
146-
'number' => '2665487AAF',
140+
'bank' => 'ABN',
141+
'account' => '62.90.64.393',
142+
'transactions' => [],
143+
'startPrice' => 16250,
144+
'endPrice' => 6250,
145+
'startTimestamp' => 123,
146+
'number' => '2665487AAF',
147147
];
148148
$statement = new Statement();
149149
foreach ($params as $key => $value) {
@@ -159,39 +159,39 @@ public function testJsonSerialization()
159159
public function testJsonSerializationWithTransactions()
160160
{
161161
$expected = '{"bank":"ABN","account":"62.90.64.393","transactions":[{"account":"123123","accountName":'.
162-
'"Kingsquare BV","price":110,"debitcredit":"D","description":"test","valueTimestamp":1231,"entryTimestamp"'.
163-
':1234,"transactionCode":"13G"},{"account":"123123","accountName":"Kingsquare BV","price":110,"debitcredit"'.
164-
':"D","description":"test","valueTimestamp":1231,"entryTimestamp":1234,"transactionCode":"13G"}],'.
165-
'"startPrice":16250,"endPrice":6250,"number":"2665487AAF"}';
162+
'"Kingsquare BV","price":110,"debitcredit":"D","description":"test","valueTimestamp":1231,"entryTimestamp"'.
163+
':1234,"transactionCode":"13G"},{"account":"123123","accountName":"Kingsquare BV","price":110,"debitcredit"'.
164+
':"D","description":"test","valueTimestamp":1231,"entryTimestamp":1234,"transactionCode":"13G"}],'.
165+
'"startPrice":16250,"endPrice":6250,"number":"2665487AAF"}';
166166
$params = [
167-
'bank' => 'ABN',
168-
'account' => '62.90.64.393',
169-
'transactions' => [
170-
[
171-
'account' => '123123',
172-
'accountName' => 'Kingsquare BV',
173-
'price' => 110.0,
174-
'debitcredit' => Transaction::DEBIT,
175-
'description' => 'test',
176-
'valueTimestamp' => 1231,
177-
'entryTimestamp' => 1234,
178-
'transactionCode' => '13G',
179-
],
180-
[
181-
'account' => '123123',
182-
'accountName' => 'Kingsquare BV',
183-
'price' => 110.0,
184-
'debitcredit' => Transaction::DEBIT,
185-
'description' => 'test',
186-
'valueTimestamp' => 1231,
187-
'entryTimestamp' => 1234,
188-
'transactionCode' => '13G',
189-
],
167+
'bank' => 'ABN',
168+
'account' => '62.90.64.393',
169+
'transactions' => [
170+
[
171+
'account' => '123123',
172+
'accountName' => 'Kingsquare BV',
173+
'price' => 110.0,
174+
'debitcredit' => Transaction::DEBIT,
175+
'description' => 'test',
176+
'valueTimestamp' => 1231,
177+
'entryTimestamp' => 1234,
178+
'transactionCode' => '13G',
190179
],
191-
'startPrice' => 16250,
192-
'endPrice' => 6250,
193-
'timestamp' => 123,
194-
'number' => '2665487AAF',
180+
[
181+
'account' => '123123',
182+
'accountName' => 'Kingsquare BV',
183+
'price' => 110.0,
184+
'debitcredit' => Transaction::DEBIT,
185+
'description' => 'test',
186+
'valueTimestamp' => 1231,
187+
'entryTimestamp' => 1234,
188+
'transactionCode' => '13G',
189+
],
190+
],
191+
'startPrice' => 16250,
192+
'endPrice' => 6250,
193+
'timestamp' => 123,
194+
'number' => '2665487AAF',
195195
];
196196
$statement = new Statement();
197197
foreach ($params as $key => $value) {

test/Parser/Banking/Mt940/Engine/Abn/ParseTest.php

+2-5
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,14 @@ class ParseTest extends \PHPUnit_Framework_TestCase
1212
/**
1313
* @var Abn
1414
*/
15-
private $engine = null;
15+
private $engine;
1616

1717
protected function setUp()
1818
{
1919
$this->engine = new Abn();
2020
$this->engine->loadString(file_get_contents(__DIR__.'/sample'));
2121
}
2222

23-
/**
24-
*
25-
*/
2623
public function testParseStatementBank()
2724
{
2825
$method = new \ReflectionMethod($this->engine, 'parseStatementBank');
@@ -34,7 +31,7 @@ public function testParsesAllFoundStatements()
3431
{
3532
$statements = $this->engine->parse();
3633

37-
$this->assertEquals(4, count($statements));
34+
$this->assertCount(4, $statements);
3835
$first = $statements[0];
3936
$last = end($statements);
4037

test/Parser/Banking/Mt940/Engine/Ing/ParseTest.php

+2-5
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,14 @@ class ParseTest extends \PHPUnit_Framework_TestCase
1212
/**
1313
* @var Ing
1414
*/
15-
private $engine = null;
15+
private $engine;
1616

1717
protected function setUp()
1818
{
1919
$this->engine = new Ing();
2020
$this->engine->loadString(file_get_contents(__DIR__.'/sample'));
2121
}
2222

23-
/**
24-
*
25-
*/
2623
public function testParseStatementBank()
2724
{
2825
$method = new \ReflectionMethod($this->engine, 'parseStatementBank');
@@ -34,7 +31,7 @@ public function testParsesAllFoundStatements()
3431
{
3532
$statements = $this->engine->parse();
3633

37-
$this->assertEquals(1, count($statements));
34+
$this->assertCount(1, $statements);
3835
$first = $statements[0];
3936

4037
$this->assertEquals('22-07-2010', $first->getStartTimestamp('d-m-Y'));

test/Parser/Banking/Mt940/Engine/Knab/ParseTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class ParseTest extends \PHPUnit_Framework_TestCase
1212
/**
1313
* @var Knab
1414
*/
15-
private $engine = null;
15+
private $engine;
1616

1717
protected function setUp()
1818
{
@@ -31,7 +31,7 @@ public function testParsesAllFoundStatements()
3131
{
3232
$statements = $this->engine->parse();
3333

34-
$this->assertEquals(1, count($statements));
34+
$this->assertCount(1, $statements);
3535
$this->assertEquals('03-12-2015', $statements[0]->getStartTimestamp('d-m-Y'));
3636
$this->assertEquals('03-12-2015', $statements[0]->getStartTimestamp('d-m-Y'));
3737
}

test/Parser/Banking/Mt940/Engine/Rabo/ParseTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class ParseTest extends \PHPUnit_Framework_TestCase
1212
/**
1313
* @var Rabo
1414
*/
15-
private $engine = null;
15+
private $engine;
1616

1717
protected function setUp()
1818
{
@@ -31,7 +31,7 @@ public function testParsesAllFoundStatements()
3131
{
3232
$statements = $this->engine->parse();
3333

34-
$this->assertEquals(39, count($statements));
34+
$this->assertCount(39, $statements);
3535
$first = $statements[0];
3636
$last = end($statements);
3737
$this->assertEquals('06-01-2003', $first->getStartTimestamp('d-m-Y'));

0 commit comments

Comments
 (0)