Skip to content

Commit

Permalink
Examples fix (#20)
Browse files Browse the repository at this point in the history
* Make examples run again

* Fix phpstan for simple.php example

* Fix complex example also

* Apply CS/Rector changes

* More readable

---------

Co-authored-by: johanwilfer <[email protected]>
  • Loading branch information
johanwilfer and johanwilfer authored Aug 10, 2024
1 parent 0c846e9 commit a510d99
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 35 deletions.
61 changes: 35 additions & 26 deletions examples/TSVtoSIE.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<int, array<int, string>>
*/
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);
Expand All @@ -56,13 +56,16 @@ protected function getTabularData($value, $delimiter = "\t"): array

/**
* Used for sorting the tsv-data
*
* @param array<int, string|int> $a
* @param array<int, string|int> $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;
}

Expand All @@ -76,18 +79,18 @@ 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);
}

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]),
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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
Expand All @@ -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],
Expand All @@ -162,16 +163,16 @@ 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']);
$verificationSeries->addVerification($verification);
}

// 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);
Expand All @@ -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
Expand All @@ -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
Expand All @@ -218,7 +227,7 @@ public function parseTransactions($value, Company $company, $skipHeaderLines = 1
$transaction->addObject($object);
}

$last_verification_id = $verification->getId();
$lastVerificationId = $verification->getId();
}
}
}
Expand All @@ -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
Expand All @@ -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
Expand Down
17 changes: 11 additions & 6 deletions examples/simple.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
;
Expand Down
3 changes: 1 addition & 2 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ parameters:
- tests/bootstrap.php
paths:
- src
# FIXME! Cleanup example dir and enable phpstan for that as well
#- examples
- examples

disallowedSuperglobals:
-
Expand Down
2 changes: 2 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
])
Expand Down
2 changes: 1 addition & 1 deletion src/SIE/Data/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

Expand Down

0 comments on commit a510d99

Please sign in to comment.