From 482bccc19a6ce0253b8fbfbaa4c37ae821f56745 Mon Sep 17 00:00:00 2001 From: Dominic Richter Date: Tue, 7 Feb 2023 00:38:58 +0100 Subject: [PATCH] added some returntypes, and fixes for php 7.4 and higher (#49) --- .github/workflows/phpunit.yml | 4 +- composer.json | 6 +- lib/Jejik/MT940/Parser/AbstractParser.php | 23 ++--- lib/Jejik/MT940/Parser/GermanBank.php | 99 +++++++------------ lib/Jejik/MT940/Reader.php | 11 ++- lib/Jejik/MT940/Transaction.php | 2 +- lib/Jejik/MT940/TransactionInterface.php | 6 +- .../Jejik/Tests/MT940/Parser/AbnAmroTest.php | 10 +- .../Tests/MT940/Parser/GermanBankTest.php | 3 +- tests/Jejik/Tests/MT940/ReaderTest.php | 4 +- 10 files changed, 66 insertions(+), 102 deletions(-) diff --git a/.github/workflows/phpunit.yml b/.github/workflows/phpunit.yml index a241592..d55d706 100644 --- a/.github/workflows/phpunit.yml +++ b/.github/workflows/phpunit.yml @@ -7,8 +7,8 @@ jobs: runs-on: ${{ matrix.operating-system }} strategy: matrix: - operating-system: ['ubuntu-18.04'] - php-versions: ['7.2', '7.3', '7.4', '8.0'] + operating-system: ['ubuntu-22.04'] + php-versions: ['7.4', '8.0'] steps: - uses: actions/checkout@v1 diff --git a/composer.json b/composer.json index 6b1ccf9..7828729 100644 --- a/composer.json +++ b/composer.json @@ -8,7 +8,7 @@ { "name": "Sander Marechal", "email": "s.marechal@jejik.com", - "homepage": "http://www.jejik.com" + "homepage": "https://www.jejik.com" }, { "name": "powercloud GmbH / Dominic Richter", @@ -17,10 +17,10 @@ } ], "require": { - "php": ">=7.2.0||^8.0" + "php": ">=7.4.0||^8.0" }, "require-dev": { - "phpunit/phpunit": "^8", + "phpunit/phpunit": "^9", "squizlabs/php_codesniffer": "3.*" }, "autoload": { diff --git a/lib/Jejik/MT940/Parser/AbstractParser.php b/lib/Jejik/MT940/Parser/AbstractParser.php index b23ca6c..dcdb20d 100644 --- a/lib/Jejik/MT940/Parser/AbstractParser.php +++ b/lib/Jejik/MT940/Parser/AbstractParser.php @@ -244,7 +244,7 @@ protected function splitTransactions(string $text): array * * @throws \Exception */ - protected function statement(string $text): ?Statement + protected function statement(string $text): ?StatementInterface { $text = trim($text); if (($pos = strpos($text, ':20:')) === false) { @@ -272,7 +272,7 @@ protected function statementHeader(string $text): void * * @throws \Exception */ - protected function statementBody(string $text): ?Statement + protected function statementBody(string $text): ?StatementInterface { $accountNumber = $this->accountNumber($text); $accountCurrency = $this->accountCurrency($text); @@ -351,10 +351,7 @@ protected function accountCurrency($text): ?string if ($line60F = $this->getLine('60F', $text)) { $pcreCurrency = '/(C|D)(\d{6})([A-Z]{3})([0-9,]{1,15})/'; preg_match($pcreCurrency, $text, $match); - if (isset($match[3])) { - return $match[3]; - } - return null; + return $match[3] ?? null; } } return $currency; @@ -468,7 +465,7 @@ protected function transaction(array $lines): TransactionInterface $bookDate = $bookDateCollection[min(array_keys($bookDateCollection))]; } - $description = isset($lines[1]) ? $lines[1] : null; + $description = $lines[1] ?? null; $transaction = $this->reader->createTransaction(); $transaction ->setAmount($amount) @@ -691,7 +688,7 @@ protected function svwz(array $lines): ?string { return null; } - + /** * Parse purp for provided transaction lines */ @@ -699,7 +696,7 @@ protected function purp(array $lines): ?string { return null; } - + /** * Parse debt for provided transaction lines */ @@ -707,7 +704,7 @@ protected function debt(array $lines): ?string { return null; } - + /** * Parse coam for provided transaction lines */ @@ -715,7 +712,7 @@ protected function coam(array $lines): ?string { return null; } - + /** * Parse oamt for provided transaction lines */ @@ -723,7 +720,7 @@ protected function oamt(array $lines): ?string { return null; } - + /** * Parse abwa for provided transaction lines */ @@ -731,7 +728,7 @@ protected function abwa(array $lines): ?string { return null; } - + /** * Parse abwe for provided transaction lines */ diff --git a/lib/Jejik/MT940/Parser/GermanBank.php b/lib/Jejik/MT940/Parser/GermanBank.php index 8c0ff59..d8f7d28 100755 --- a/lib/Jejik/MT940/Parser/GermanBank.php +++ b/lib/Jejik/MT940/Parser/GermanBank.php @@ -37,7 +37,7 @@ abstract class GermanBank extends AbstractParser protected function gvc(array $lines): ?string { // get :86: line -- it is second in provided array [:61:,:86:,....] - $gvcLine = isset($lines[1]) ? $lines[1] : null; + $gvcLine = $lines[1] ?? null; // assure gvc line if ($gvcLine == null) { @@ -55,7 +55,7 @@ protected function gvc(array $lines): ?string protected function code(array $lines): ?string { // get :61: line -- it is first in provided array [:61:,:86:,....] - $codeLine = isset($lines[0]) ? $lines[0] : null; + $codeLine = $lines[0] ?? null; // assure code line if ($codeLine == null) { @@ -79,11 +79,11 @@ protected function code(array $lines): ?string */ protected function supplementaryDetails(array $lines): ?string { - $refLine = isset($lines[0]) ? $lines[0] : null; + $refLine = $lines[0] ?? null; $parts = preg_split("/\\r\\n|\\r|\\n/", $refLine, 2); - return isset($parts[1]) ? $parts[1] : null; + return $parts[1] ?? null; } /** @@ -91,7 +91,7 @@ protected function supplementaryDetails(array $lines): ?string */ protected function ref(array $lines): ?string { - $refLine = isset($lines[0]) ? $lines[0] : null; + $refLine = $lines[0] ?? null; // assure ref line if ($refLine == null) { @@ -102,11 +102,7 @@ protected function ref(array $lines): ?string preg_match("/(?'valuta'\d{6})(?'bookingdate'\d{4})?(?'debitcreditid'R?(?:C|D))(?'amount'[0-9,]{1,15})(?:\s*)(?'bookingkey'N[a-zA-Z0-9]{3})(?'reference'[a-zA-Z0-9+]+)(?:\/\/)*(?'bankref'[0-9a-zA-Z]{1,16})*/", $refLine, $match); // assure match - if (!isset($match['reference'])) { - return null; - } - - return $match['reference']; + return $match['reference'] ?? null; } /** @@ -114,7 +110,7 @@ protected function ref(array $lines): ?string */ protected function bankRef(array $lines): ?string { - $refLine = isset($lines[0]) ? $lines[0] : null; + $refLine = $lines[0] ?? null; // assure ref line if ($refLine == null) { @@ -125,11 +121,7 @@ protected function bankRef(array $lines): ?string preg_match("/(?'valuta'\d{6})(?'bookingdate'\d{4})?(?'debitcreditid'R?(?:C|D))(?'amount'[0-9,]{1,15})(?:\s*)(?'bookingkey'N[a-zA-Z0-9]{3})(?'reference'[a-zA-Z0-9+]+)(?:\/\/)*(?'bankref'[0-9a-zA-Z]{1,16})*/", $refLine, $match); // assure match - if (!isset($match['bankref'])) { - return null; - } - - return $match['bankref']; + return $match['bankref'] ?? null; } /** @@ -138,7 +130,7 @@ protected function bankRef(array $lines): ?string protected function txText(array $lines): ?string { // get :86: line -- it is second in provided array [:61:,:86:,....] - $txTextLine = isset($lines[1]) ? $lines[1] : null; + $txTextLine = $lines[1] ?? null; // assure txText line if ($txTextLine === null) { @@ -150,12 +142,7 @@ protected function txText(array $lines): ?string preg_match('#\?00([a-zA-Z0-9\-\s\.]+)#', $this->removeNewLinesFromLine($txTextLine), $match); // assure match - if (!isset($match[1])) { - return null; - } - - // return - return $match[1]; + return $match[1] ?? null; } /** @@ -164,7 +151,7 @@ protected function txText(array $lines): ?string protected function primanota(array $lines): ?string { // get :86: line -- it is second in provided array [:61:,:86:,....] - $primanotaLine = isset($lines[1]) ? $lines[1] : null; + $primanotaLine = $lines[1] ?? null; // assure primanota line if ($primanotaLine === null) { @@ -175,12 +162,7 @@ protected function primanota(array $lines): ?string preg_match('#\?10([a-zA-Z0-9/]{1,10})#', $this->removeNewLinesFromLine($primanotaLine), $match); // assure match - if (!isset($match[1])) { - return null; - } - - // return - return $match[1]; + return $match[1] ?? null; } /** @@ -189,7 +171,7 @@ protected function primanota(array $lines): ?string protected function extCode(array $lines): ?string { // get :86: line -- it is second in provided array [:61:,:86:,....] - $extCodeLine = isset($lines[1]) ? $lines[1] : null; + $extCodeLine = $lines[1] ?? null; // assure extCode line if ($extCodeLine === null) { @@ -199,11 +181,7 @@ protected function extCode(array $lines): ?string /** @var string $extCodeLine */ preg_match('#\?34(\d{3})#', $this->removeNewLinesFromLine($extCodeLine), $match); - if (!isset($match[1])) { - return null; - } - - return $match[1]; + return $match[1] ?? null; } /** @@ -289,7 +267,7 @@ protected function eref(array $lines): ?string protected function bic(array $lines): ?string { // get :86: line -- it is second in provided array [:61:,:86:,....] - $bicLine = isset($lines[1]) ? $lines[1] : null; + $bicLine = $lines[1] ?? null; // assure bic line if ($bicLine === null) { @@ -300,11 +278,7 @@ protected function bic(array $lines): ?string preg_match('#\?30([a-zA-Z0-9]+)#', $this->removeNewLinesFromLine($bicLine), $match); // assure match - if (!isset($match[1])) { - return null; - } - - return $match[1]; + return $match[1] ?? null; } /** @@ -313,7 +287,7 @@ protected function bic(array $lines): ?string protected function iban(array $lines): ?string { // get :86: line -- it is second in provided array [:61:,:86:,....] - $ibanLine = isset($lines[1]) ? $lines[1] : null; + $ibanLine = $lines[1] ?? null; // assure iban line if ($ibanLine == null) { @@ -325,12 +299,7 @@ protected function iban(array $lines): ?string preg_match('#\?31([a-zA-Z0-9]+)#', $this->removeNewLinesFromLine($ibanLine), $match); // assure match - if (!isset($match[1])) { - return null; - } - - // return - return $match[1]; + return $match[1] ?? null; } /** @@ -339,7 +308,7 @@ protected function iban(array $lines): ?string protected function accountHolder(array $lines): ?string { // get :86: line -- it is second in provided array [:61:,:86:,....] - $accHolderLine = isset($lines[1]) ? $lines[1] : null; + $accHolderLine = $lines[1] ?? null; // assure acc holder line if ($accHolderLine == null) { @@ -378,21 +347,21 @@ protected function accountHolder(array $lines): ?string protected function kref(array $lines): ?string { // get :86: line -- it is second in provided array [:61:,:86:,....] - $krefLine = isset($lines[1]) ? $lines[1] : null; + $krefLine = $lines[1] ?? null; // pattern - $pattern = 'K(?:\?2[1-9])?R(?:\?2[1-9])?E(?:\?2[1-9])?F(?:\?2[1-9])?\+([a-zA-ZöäüÖÄÜß0-9\./?\+\-\s,]+)(C(?:\?2[1-9])?R(?:\?2[1-9])?E(?:\?2[1-9])?D|S(?:\?2[1-9])?V(?:\?2[1-9])?W(?:\?2[1-9])?Z)'; + $pattern = '#K(?:\?2[1-9])?R(?:\?2[1-9])?E(?:\?2[1-9])?F(?:\?2[1-9])?\+([a-zA-ZöäüÖÄÜß0-9\./?\+\-\s,]+)(C(?:\?2[1-9])?R(?:\?2[1-9])?E(?:\?2[1-9])?D|S(?:\?2[1-9])?V(?:\?2[1-9])?W(?:\?2[1-9])?Z)#'; /** @var string $krefLine */ - preg_match("#{$pattern}#", $this->removeNewLinesFromLine($krefLine), $match); + preg_match($pattern, $this->removeNewLinesFromLine($krefLine), $match); // assure match if (!isset($match[1])) { // try it without CRED|SVWZ info - $pattern = 'K(?:\?2[1-9])?R(?:\?2[1-9])?E(?:\?2[1-9])?F(?:\?2[1-9])?\+([a-zA-ZöäüÖÄÜß0-9\./?\+\-\s,]+?)(\?3[0-9])'; + $pattern = '#K(?:\?2[1-9])?R(?:\?2[1-9])?E(?:\?2[1-9])?F(?:\?2[1-9])?\+([a-zA-ZöäüÖÄÜß0-9\./?\+\-\s,]+?)(\?3[0-9])#'; // match it - preg_match("#{$pattern}#", $this->removeNewLinesFromLine($krefLine), $match); + preg_match($pattern, $this->removeNewLinesFromLine($krefLine), $match); } // assure match again after avoiding CRED|SVWZ info @@ -400,7 +369,6 @@ protected function kref(array $lines): ?string return null; } - // return return preg_replace('#(\?\d{0,2})#', '', $match[1]); } @@ -410,12 +378,11 @@ protected function kref(array $lines): ?string protected function mref(array $lines): ?string { // get :86: line -- it is second in provided array [:61:,:86:,....] - $mrefLine = isset($lines[1]) ? $lines[1] : null; + $mrefLine = $lines[1] ?? null; - $pattern = 'M(?:\?2[1-9])?R(?:\?2[1-9])?E(?:\?2[1-9])?F(?:\?2[1-9])?\+([a-zA-ZöäüÖÄÜß0-9\./?\+\-\s,]+)(C(?:\?2[1-9])?R(?:\?2[1-9])?E(?:\?2[1-9])?D|S(?:\?2[1-9])?V(?:\?2[1-9])?W(?:\?2[1-9])?Z)'; + $pattern = '#M(?:\?2[1-9])?R(?:\?2[1-9])?E(?:\?2[1-9])?F(?:\?2[1-9])?\+([a-zA-ZöäüÖÄÜß0-9\./?\+\-\s,]+)(C(?:\?2[1-9])?R(?:\?2[1-9])?E(?:\?2[1-9])?D|S(?:\?2[1-9])?V(?:\?2[1-9])?W(?:\?2[1-9])?Z)#'; - // match it - preg_match("#{$pattern}#", $this->removeNewLinesFromLine($mrefLine), $match); + preg_match($pattern, $this->removeNewLinesFromLine($mrefLine), $match); // assure match if (!isset($match[1])) { @@ -431,12 +398,12 @@ protected function mref(array $lines): ?string protected function cred(array $lines): ?string { // get :86: line -- it is second in provided array [:61:,:86:,....] - $credLine = isset($lines[1]) ? $lines[1] : null; + $credLine = $lines[1] ?? null; - $pattern = 'C(?:\?2[1-9])?R(?:\?2[1-9])?E(?:\?2[1-9])?D(?:\?2[1-9])?\+([a-zA-ZöäüÖÄÜß0-9\./?\+\-\s,]+)S(?:\?2[1-9])?V(?:\?2[1-9])?W(?:\?2[1-9])?Z'; + $pattern = '#C(?:\?2[1-9])?R(?:\?2[1-9])?E(?:\?2[1-9])?D(?:\?2[1-9])?\+([a-zA-ZöäüÖÄÜß0-9\./?\+\-\s,]+)S(?:\?2[1-9])?V(?:\?2[1-9])?W(?:\?2[1-9])?Z#'; // match it - preg_match("#{$pattern}#", $this->removeNewLinesFromLine($credLine), $match); + preg_match($pattern, $this->removeNewLinesFromLine($credLine), $match); // assure match if (!isset($match[1])) { @@ -452,12 +419,12 @@ protected function cred(array $lines): ?string protected function svwz(array $lines): ?string { // get :86: line -- it is second in provided array [:61:,:86:,....] - $svwzLine = isset($lines[1]) ? $lines[1] : null; + $svwzLine = $lines[1] ?? null; - $pattern = "(S(?:\?2[1-9])?V(?:\?2[1-9])?W(?:\?2[1-9])?Z(?:\?2[1-9])?\+)(?:\?(?:2[1-9]))?(?'SVWZ'.*)(?:\?30)"; + $pattern = "/(S(?:\?2[1-9])?V(?:\?2[1-9])?W(?:\?2[1-9])?Z(?:\?2[1-9])?\+)(?:\?(?:2[1-9]))?(?'SVWZ'.*)(?:\?30)/"; /** @var string $svwzLine */ - preg_match("/{$pattern}/", $this->removeNewLinesFromLine($svwzLine), $match); + preg_match($pattern, $this->removeNewLinesFromLine($svwzLine), $match); // assure match if (!isset($match['SVWZ'])) { diff --git a/lib/Jejik/MT940/Reader.php b/lib/Jejik/MT940/Reader.php index 5764097..b6faf1d 100644 --- a/lib/Jejik/MT940/Reader.php +++ b/lib/Jejik/MT940/Reader.php @@ -14,6 +14,8 @@ namespace Jejik\MT940; +use Jejik\MT940\Exception\NoParserFoundException; + /** * Read and parse MT940 documents * @@ -512,10 +514,9 @@ protected function createObject($className, $interface, $params = []) /** * Get MT940 statements from the input text * - * @param string $text - * @return Statement[] - * @throws \RuntimeException if no suitable parser is found - * @throws Exception\NoParserFoundException + * @param string|null $text + * @return array + * @throws NoParserFoundException * @throws \Exception */ public function getStatements(string $text = null): array @@ -523,7 +524,7 @@ public function getStatements(string $text = null): array if ($text === null) { $text = file_get_contents($this->getFileName()); } - if ($text === null || strlen(trim($text)) == 0) { + if ($text === null || strlen(trim($text)) === 0) { throw new \Exception("No text is found for parsing."); } if (($pos = strpos($text, ':20:')) === false) { diff --git a/lib/Jejik/MT940/Transaction.php b/lib/Jejik/MT940/Transaction.php index f2b0306..e90bd1c 100644 --- a/lib/Jejik/MT940/Transaction.php +++ b/lib/Jejik/MT940/Transaction.php @@ -257,7 +257,7 @@ public function getCode(): ?string /** * Set Code for this transaction - * @param string $code + * @param string|null $code * @return $this */ public function setCode(string $code = null): TransactionInterface diff --git a/lib/Jejik/MT940/TransactionInterface.php b/lib/Jejik/MT940/TransactionInterface.php index c3eb526..b7b7389 100644 --- a/lib/Jejik/MT940/TransactionInterface.php +++ b/lib/Jejik/MT940/TransactionInterface.php @@ -74,7 +74,7 @@ public function setBookDate(?\DateTime $bookDate = null): self; * Get Code for this transaction * @return null|string */ - public function getCode(); + public function getCode(): ?string; /** * Set Code for this transaction @@ -158,7 +158,7 @@ public function getEref(): ?string; /** * Set Eref for this transaction - * @param string $eref + * @param string|null $eref * @return $this */ public function setEref(string $eref = null): TransactionInterface; @@ -180,7 +180,7 @@ public function getIBAN(): ?string; /** * Set IBAN for this transaction - * @param string $iban + * @param string|null $iban * @return $this */ public function setIBAN(string $iban = null): TransactionInterface; diff --git a/tests/Jejik/Tests/MT940/Parser/AbnAmroTest.php b/tests/Jejik/Tests/MT940/Parser/AbnAmroTest.php index 2ceb231..c0d04e2 100644 --- a/tests/Jejik/Tests/MT940/Parser/AbnAmroTest.php +++ b/tests/Jejik/Tests/MT940/Parser/AbnAmroTest.php @@ -36,7 +36,7 @@ public function setUp(): void $this->statements = $reader->getStatements(file_get_contents(__DIR__ . '/../Fixture/document/abnamro.txt')); } - public function testStatement() + public function testStatement(): void { $this->assertCount(2, $this->statements); $statement = $this->statements[0]; @@ -45,7 +45,7 @@ public function testStatement() $this->assertEquals('517852257', $statement->getAccount()->getNumber()); } - public function testBalance() + public function testBalance(): void { $balance = $this->statements[0]->getOpeningBalance(); $this->assertInstanceOf(\Jejik\MT940\Balance::class, $balance); @@ -54,7 +54,7 @@ public function testBalance() $this->assertEquals(3236.28, $balance->getAmount()); } - public function testTransaction() + public function testTransaction(): void { $transactions = $this->statements[0]->getTransactions(); $this->assertCount(8, $transactions); @@ -76,7 +76,7 @@ public function testTransaction() $this->assertEquals('528939882', $transactions[1]->getContraAccount()->getNumber()); } - public function testContinuedStatement() + public function testContinuedStatement(): void { $this->assertEquals('19322/1', $this->statements[1]->getNumber()); @@ -89,7 +89,7 @@ public function testContinuedStatement() $this->assertEquals(1849.75, $balance->getAmount()); } - public function testContraAccountName() + public function testContraAccountName(): void { $transactions = $this->statements[0]->getTransactions(); $this->assertEquals('KPN - DIGITENNE', $transactions[0]->getContraAccount()->getName()); diff --git a/tests/Jejik/Tests/MT940/Parser/GermanBankTest.php b/tests/Jejik/Tests/MT940/Parser/GermanBankTest.php index d4b427d..4f5abe9 100644 --- a/tests/Jejik/Tests/MT940/Parser/GermanBankTest.php +++ b/tests/Jejik/Tests/MT940/Parser/GermanBankTest.php @@ -11,7 +11,7 @@ class GermanBankTest extends \PHPUnit\Framework\TestCase { - public function erefParserDataProvider() + public function erefParserDataProvider(): array { return [ 'PC-343@PC: Test Case 1' => [ @@ -206,7 +206,6 @@ public function erefParserDataProvider() * @dataProvider erefParserDataProvider * * @param string $expected - * @param string $account * @param string $statement * @throws \Exception */ diff --git a/tests/Jejik/Tests/MT940/ReaderTest.php b/tests/Jejik/Tests/MT940/ReaderTest.php index 57c2607..fff6d26 100644 --- a/tests/Jejik/Tests/MT940/ReaderTest.php +++ b/tests/Jejik/Tests/MT940/ReaderTest.php @@ -36,7 +36,7 @@ public function testDefaultParsers() $this->fail('Expected an exception'); } catch (\Exception $e) { // No parser can read an empty string - $this->assertTrue($e->getMessage() === 'No text is found for parsing.'); + $this->assertSame($e->getMessage(), 'No text is found for parsing.'); } $this->assertCount(16, $reader->getDefaultParsers()); @@ -145,7 +145,7 @@ public function testSkipStatement() }); $statements = $reader->getStatements(file_get_contents(__DIR__ . '/Fixture/document/generic.txt')); - $this->assertEquals(1, count($statements)); + $this->assertCount(1, $statements); $this->assertEquals('2', $statements[0]->getNumber()); } }