diff --git a/examples/TSVtoSIE.php b/examples/TSVtoSIE.php index 893ce53..60125e7 100644 --- a/examples/TSVtoSIE.php +++ b/examples/TSVtoSIE.php @@ -32,10 +32,10 @@ class TSVLoader /** * Parse TSV data and returns array of rows, that holds an array of fields. * - * @param string $value - * @param string $delimiter + * @param non-empty-string $delimiter + * @return array> */ - protected function getTabularData($value, $delimiter = "\t"): array + private function getTabularData(string $value, string $delimiter): array { // fix line endings to be \n $value = str_replace(["\r\n", "\r"], "\n", $value); @@ -56,13 +56,16 @@ protected function getTabularData($value, $delimiter = "\t"): array /** * Used for sorting the tsv-data + * + * @param array $a + * @param array $b */ - protected function tabularDataCompareRows($a, $b): int + private function tabularDataCompareRows(array $a, array $b): int { // compare ver_no $field = 0; // same ver_no, compare ver_row instead - if ($a[$field] == $b[$field]) { + if ($a[$field] === $b[$field]) { $field = 14; } @@ -76,10 +79,10 @@ protected function tabularDataCompareRows($a, $b): int * @param Data\FiscalYear $fiscalYear The fiscal year * @param int $skipHeaderLines */ - public function parseBalance($value, Company $company, FiscalYear $fiscalYear, $skipHeaderLines = 1): void + public function parseBalance(string $value, Company $company, FiscalYear $fiscalYear, $skipHeaderLines = 1): void { // parse text - $rows = $this->getTabularData($value); + $rows = $this->getTabularData($value, "\t"); // kill header lines for ($i = 0; $i < $skipHeaderLines; ++$i) { array_shift($rows); @@ -87,7 +90,7 @@ public function parseBalance($value, Company $company, FiscalYear $fiscalYear, $ foreach ($rows as $row) { $data = [ - 'account_id' => $row[0], + 'account_id' => (int) $row[0], 'account_name' => $row[1], 'incoming' => (float) str_replace([' ', ' ', '.', ','], ['', '', '', '.'], $row[2]), 'outgoing' => (float) str_replace([' ', ' ', '.', ','], ['', '', '', '.'], $row[5]), @@ -96,7 +99,7 @@ public function parseBalance($value, Company $company, FiscalYear $fiscalYear, $ // account - try fetch it from the company $account = $company->getAccount($data['account_id']); // account not found? create it. - if (! $account instanceof Account) { + if ($account === null) { $account = (new Account($data['account_id'])) ->setName($data['account_name']); $company->addAccount($account); @@ -113,13 +116,11 @@ public function parseBalance($value, Company $company, FiscalYear $fiscalYear, $ /** * Parse transaction-data form TSV-file - * - * @param int $skipHeaderLines */ - public function parseTransactions($value, Company $company, $skipHeaderLines = 1): void + public function parseTransactions(string $value, Company $company, int $skipHeaderLines = 1): void { // parse text - $rows = $this->getTabularData($value); + $rows = $this->getTabularData($value, "\t"); // kill header lines for ($i = 0; $i < $skipHeaderLines; ++$i) { array_shift($rows); @@ -134,7 +135,7 @@ public function parseTransactions($value, Company $company, $skipHeaderLines = 1 ->addDimension(new Dimension(Dimension::DIMENSION_COST_CENTRE)) ->addDimension(new Dimension(Dimension::DIMENSION_PROJECT)); - $last_verification_id = null; + $lastVerificationId = null; foreach ($rows as $row) { /* -- Our columns -- * 0: Verification number @@ -151,7 +152,7 @@ public function parseTransactions($value, Company $company, $skipHeaderLines = 1 $data = [ 'ver_no' => $row[0], 'date' => $row[1], - 'account_no' => $row[3], + 'account_no' => (int) $row[3], 'account_name' => $row[4], 'result_unit' => $row[5], 'project' => $row[6], @@ -162,7 +163,7 @@ public function parseTransactions($value, Company $company, $skipHeaderLines = 1 ]; // verification - if ($last_verification_id !== $data['ver_no']) { + if ($lastVerificationId !== $data['ver_no']) { $verification = (new Verification($data['ver_no'])) ->setDate($data['date']) ->setText($data['ver_name']); @@ -170,8 +171,8 @@ public function parseTransactions($value, Company $company, $skipHeaderLines = 1 } // account - $account = $company->getAccount($row[3]); - if (! $account instanceof Account) { + $account = $company->getAccount($data['account_no']); + if ($account === null) { $account = (new Account($data['account_no'])) ->setName($data['account_name']); $company->addAccount($account); @@ -185,12 +186,16 @@ public function parseTransactions($value, Company $company, $skipHeaderLines = 1 $verification->addTransaction($transaction); // dimension - result unit - if ($data['result_unit']) { + if ($data['result_unit'] !== '') { // find dimension (pre-defined) $dim = $company->getDimension(Dimension::DIMENSION_COST_CENTRE); + if ($dim === null) { + throw new \LogicException('Expected to find dimension: DIMENSION_COST_CENTRE'); + } + // find / create object $object = $dim->getObject($data['result_unit']); - if (! $object instanceof DimensionObject) { + if ($object === null) { $object = (new DimensionObject($data['result_unit'])) ->setDimension($dim) ->setName('Resultatenhet ' . $data['result_unit']); //We don't have this data, so just set it @@ -202,12 +207,16 @@ public function parseTransactions($value, Company $company, $skipHeaderLines = 1 } // dimension - project - if ($data['project']) { + if ($data['project'] !== '') { // find dimension (pre-defined) $dim = $company->getDimension(Dimension::DIMENSION_PROJECT); + if ($dim === null) { + throw new \LogicException('Expected to find dimension: DIMENSION_PROJECT'); + } + // find / create object $object = $dim->getObject($data['project']); - if (! $object instanceof DimensionObject) { + if ($object === null) { $object = (new DimensionObject($data['project'])) ->setDimension($dim) ->setName('Projekt ' . $data['project']); //We don't have this data, so just set it @@ -218,7 +227,7 @@ public function parseTransactions($value, Company $company, $skipHeaderLines = 1 $transaction->addObject($object); } - $last_verification_id = $verification->getId(); + $lastVerificationId = $verification->getId(); } } } @@ -241,7 +250,7 @@ public function parseTransactions($value, Company $company, $skipHeaderLines = 1 $company = (new Company())->setCompanyName('Imported company name åäöÅÄÖ'); // load transaction data from TSV $loader = new TSVLoader(); -$loader->parseTransactions(file_get_contents($paths['transaction-data']), $company); +$loader->parseTransactions((string) file_get_contents($paths['transaction-data']), $company); /* * Balance data @@ -250,12 +259,12 @@ public function parseTransactions($value, Company $company, $skipHeaderLines = 1 // add fiscal year (defaults to current calendar year) $fiscalYear = new FiscalYear(); $company->addFiscalYear($fiscalYear); -$loader->parseBalance(file_get_contents($paths['balance-data-year-0']), $company, $fiscalYear); +$loader->parseBalance((string) file_get_contents($paths['balance-data-year-0']), $company, $fiscalYear); // the year before that $fiscalYear = $fiscalYear->createPreviousFiscalYear(); $company->addFiscalYear($fiscalYear); -$loader->parseBalance(file_get_contents($paths['balance-data-year-1']), $company, $fiscalYear); +$loader->parseBalance((string) file_get_contents($paths['balance-data-year-1']), $company, $fiscalYear); /* * Dump as SIE diff --git a/examples/simple.php b/examples/simple.php index d045c3c..d955789 100644 --- a/examples/simple.php +++ b/examples/simple.php @@ -27,21 +27,26 @@ ->setCompanyName('My company') // add a verification series ->addVerificationSeries(new VerificationSeries()) - // add two accounts - ->addAccount((new Account(1511))->setName('Kundfordringar')) - ->addAccount((new Account(3741))->setName('Öresutjämning')) +; + +// add two accounts +$account1511 = (new Account(1511))->setName('Kundfordringar'); +$account3741 = (new Account(3741))->setName('Öresutjämning'); +$company + ->addAccount($account1511) + ->addAccount($account3741) ; // add a verification with two transactions -$verification = (new Verification(591000490))->setDate('20150105') +$verification = (new Verification('591000490'))->setDate('20150105') ->addTransaction( (new Transaction()) - ->setAccount($company->getAccount(1511)) + ->setAccount($account1511) ->setAmount(-0.24) ) ->addTransaction( (new Transaction()) - ->setAccount($company->getAccount(3741)) + ->setAccount($account3741) ->setAmount(0.24) ) ; diff --git a/phpstan.neon b/phpstan.neon index b4dd14d..88718ca 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -5,8 +5,7 @@ parameters: - tests/bootstrap.php paths: - src - # FIXME! Cleanup example dir and enable phpstan for that as well - #- examples + - examples disallowedSuperglobals: - diff --git a/rector.php b/rector.php index 9eb4e30..d872222 100644 --- a/rector.php +++ b/rector.php @@ -2,6 +2,7 @@ declare(strict_types=1); +use Rector\CodeQuality\Rector\Identical\FlipTypeControlToUseExclusiveTypeRector; use Rector\Config\RectorConfig; use Rector\Php53\Rector\Ternary\TernaryToElvisRector; use Rector\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector; @@ -38,6 +39,7 @@ // DTOs looks a bit ugly with this, lets consider if we want this ->withSkip([ ClassPropertyAssignToConstructorPromotionRector::class, + FlipTypeControlToUseExclusiveTypeRector::class, // this is conflicting with our phpstan rules - either they should change or this needs to be skipped TernaryToElvisRector::class, ]) diff --git a/src/SIE/Data/Transaction.php b/src/SIE/Data/Transaction.php index fc20479..2660531 100644 --- a/src/SIE/Data/Transaction.php +++ b/src/SIE/Data/Transaction.php @@ -189,7 +189,7 @@ public function setRegistrationSign(string $registrationSign): self */ public function validate(): void { - if (! $this->account instanceof Account) { + if ($this->account === null) { throw new DomainException('Mandatory field: account'); }