From 8a4985261a5e702470cabcbcadd08790e78dba0a Mon Sep 17 00:00:00 2001 From: James Cole Date: Thu, 21 Dec 2023 06:06:23 +0100 Subject: [PATCH] Fix various code --- .../Commands/System/CreateDatabase.php | 2 + .../Commands/Upgrade/MigrateToGroups.php | 183 +++++++++--------- app/Providers/FireflyServiceProvider.php | 2 + app/Rules/IsDuplicateTransaction.php | 3 + app/Support/Amount.php | 6 +- app/Support/Calendar/Calculator.php | 5 +- app/Validation/FireflyValidator.php | 30 +-- .../2016_06_16_000002_create_main_tables.php | 3 + .../2018_06_08_200526_changes_for_v475.php | 1 + .../2020_11_12_070604_changes_for_v550.php | 1 + 10 files changed, 111 insertions(+), 125 deletions(-) diff --git a/app/Console/Commands/System/CreateDatabase.php b/app/Console/Commands/System/CreateDatabase.php index 3ea634b033..345cf108a7 100644 --- a/app/Console/Commands/System/CreateDatabase.php +++ b/app/Console/Commands/System/CreateDatabase.php @@ -41,6 +41,8 @@ class CreateDatabase extends Command /** * Execute the console command. + * + * @suppressWarnings(PHPMD.MissingImport) */ public function handle(): int { diff --git a/app/Console/Commands/Upgrade/MigrateToGroups.php b/app/Console/Commands/Upgrade/MigrateToGroups.php index e56eaa62de..ed411e8cc1 100644 --- a/app/Console/Commands/Upgrade/MigrateToGroups.php +++ b/app/Console/Commands/Upgrade/MigrateToGroups.php @@ -34,8 +34,6 @@ use FireflyIII\Repositories\Journal\JournalRepositoryInterface; use FireflyIII\Services\Internal\Destroy\JournalDestroyService; use Illuminate\Console\Command; use Illuminate\Support\Collection; -use Psr\Container\ContainerExceptionInterface; -use Psr\Container\NotFoundExceptionInterface; /** * This command will take split transactions and migrate them to "transaction groups". @@ -59,9 +57,6 @@ class MigrateToGroups extends Command /** * Execute the console command. - * - * @throws ContainerExceptionInterface - * @throws NotFoundExceptionInterface */ public function handle(): int { @@ -105,10 +100,6 @@ class MigrateToGroups extends Command $this->cliRepository = app(JournalCLIRepositoryInterface::class); } - /** - * @throws ContainerExceptionInterface - * @throws NotFoundExceptionInterface - */ private function isMigrated(): bool { $configVar = app('fireflyconfig')->get(self::CONFIG_NAME, false); @@ -142,7 +133,7 @@ class MigrateToGroups extends Command { // double check transaction count. if ($journal->transactions->count() <= 2) { - app('log')->debug(sprintf('Will not try to convert journal #%d because it has 2 or less transactions.', $journal->id)); + app('log')->debug(sprintf('Will not try to convert journal #%d because it has 2 or fewer transactions.', $journal->id)); return; } @@ -158,95 +149,12 @@ class MigrateToGroups extends Command 'transactions' => [], ]; $destTransactions = $this->getDestinationTransactions($journal); - $budgetId = $this->cliRepository->getJournalBudgetId($journal); - $categoryId = $this->cliRepository->getJournalCategoryId($journal); - $notes = $this->cliRepository->getNoteText($journal); - $tags = $this->cliRepository->getTags($journal); - $internalRef = $this->cliRepository->getMetaField($journal, 'internal-reference'); - $sepaCC = $this->cliRepository->getMetaField($journal, 'sepa_cc'); - $sepaCtOp = $this->cliRepository->getMetaField($journal, 'sepa_ct_op'); - $sepaCtId = $this->cliRepository->getMetaField($journal, 'sepa_ct_id'); - $sepaDb = $this->cliRepository->getMetaField($journal, 'sepa_db'); - $sepaCountry = $this->cliRepository->getMetaField($journal, 'sepa_country'); - $sepaEp = $this->cliRepository->getMetaField($journal, 'sepa_ep'); - $sepaCi = $this->cliRepository->getMetaField($journal, 'sepa_ci'); - $sepaBatchId = $this->cliRepository->getMetaField($journal, 'sepa_batch_id'); - $externalId = $this->cliRepository->getMetaField($journal, 'external-id'); - $originalSource = $this->cliRepository->getMetaField($journal, 'original-source'); - $recurrenceId = $this->cliRepository->getMetaField($journal, 'recurrence_id'); - $bunq = $this->cliRepository->getMetaField($journal, 'bunq_payment_id'); - $hash = $this->cliRepository->getMetaField($journal, 'import_hash'); - $hashTwo = $this->cliRepository->getMetaField($journal, 'import_hash_v2'); - $interestDate = $this->cliRepository->getMetaDate($journal, 'interest_date'); - $bookDate = $this->cliRepository->getMetaDate($journal, 'book_date'); - $processDate = $this->cliRepository->getMetaDate($journal, 'process_date'); - $dueDate = $this->cliRepository->getMetaDate($journal, 'due_date'); - $paymentDate = $this->cliRepository->getMetaDate($journal, 'payment_date'); - $invoiceDate = $this->cliRepository->getMetaDate($journal, 'invoice_date'); app('log')->debug(sprintf('Will use %d positive transactions to create a new group.', $destTransactions->count())); /** @var Transaction $transaction */ foreach ($destTransactions as $transaction) { - app('log')->debug(sprintf('Now going to add transaction #%d to the array.', $transaction->id)); - $opposingTr = $this->findOpposingTransaction($journal, $transaction); - - if (null === $opposingTr) { - $this->friendlyError( - sprintf( - 'Journal #%d has no opposing transaction for transaction #%d. Cannot upgrade this entry.', - $journal->id, - $transaction->id - ) - ); - - continue; - } - - // overrule journal category with transaction category. - $budgetId = $this->getTransactionBudget($transaction, $opposingTr) ?? $budgetId; - $categoryId = $this->getTransactionCategory($transaction, $opposingTr) ?? $categoryId; - - $tArray = [ - 'type' => strtolower($journal->transactionType->type), - 'date' => $journal->date, - 'user' => $journal->user_id, - 'currency_id' => $transaction->transaction_currency_id, - 'foreign_currency_id' => $transaction->foreign_currency_id, - 'amount' => $transaction->amount, - 'foreign_amount' => $transaction->foreign_amount, - 'description' => $transaction->description ?? $journal->description, - 'source_id' => $opposingTr->account_id, - 'destination_id' => $transaction->account_id, - 'budget_id' => $budgetId, - 'category_id' => $categoryId, - 'bill_id' => $journal->bill_id, - 'notes' => $notes, - 'tags' => $tags, - 'internal_reference' => $internalRef, - 'sepa_cc' => $sepaCC, - 'sepa_ct_op' => $sepaCtOp, - 'sepa_ct_id' => $sepaCtId, - 'sepa_db' => $sepaDb, - 'sepa_country' => $sepaCountry, - 'sepa_ep' => $sepaEp, - 'sepa_ci' => $sepaCi, - 'sepa_batch_id' => $sepaBatchId, - 'external_id' => $externalId, - 'original-source' => $originalSource, - 'recurrence_id' => $recurrenceId, - 'bunq_payment_id' => $bunq, - 'import_hash' => $hash, - 'import_hash_v2' => $hashTwo, - 'interest_date' => $interestDate, - 'book_date' => $bookDate, - 'process_date' => $processDate, - 'due_date' => $dueDate, - 'payment_date' => $paymentDate, - 'invoice_date' => $invoiceDate, - ]; - - $data['transactions'][] = $tArray; + $data['transactions'][] = $this->generateTransaction($journal, $transaction); } app('log')->debug(sprintf('Now calling transaction journal factory (%d transactions in array)', count($data['transactions']))); $group = $this->groupFactory->create($data); @@ -301,6 +209,93 @@ class MigrateToGroups extends Command return $set->first(); } + private function generateTransaction(TransactionJournal $journal, Transaction $transaction): array + { + app('log')->debug(sprintf('Now going to add transaction #%d to the array.', $transaction->id)); + $opposingTr = $this->findOpposingTransaction($journal, $transaction); + + if (null === $opposingTr) { + $this->friendlyError( + sprintf( + 'Journal #%d has no opposing transaction for transaction #%d. Cannot upgrade this entry.', + $journal->id, + $transaction->id + ) + ); + + return []; + } + + $budgetId = $this->cliRepository->getJournalBudgetId($journal); + $categoryId = $this->cliRepository->getJournalCategoryId($journal); + $notes = $this->cliRepository->getNoteText($journal); + $tags = $this->cliRepository->getTags($journal); + $internalRef = $this->cliRepository->getMetaField($journal, 'internal-reference'); + $sepaCC = $this->cliRepository->getMetaField($journal, 'sepa_cc'); + $sepaCtOp = $this->cliRepository->getMetaField($journal, 'sepa_ct_op'); + $sepaCtId = $this->cliRepository->getMetaField($journal, 'sepa_ct_id'); + $sepaDb = $this->cliRepository->getMetaField($journal, 'sepa_db'); + $sepaCountry = $this->cliRepository->getMetaField($journal, 'sepa_country'); + $sepaEp = $this->cliRepository->getMetaField($journal, 'sepa_ep'); + $sepaCi = $this->cliRepository->getMetaField($journal, 'sepa_ci'); + $sepaBatchId = $this->cliRepository->getMetaField($journal, 'sepa_batch_id'); + $externalId = $this->cliRepository->getMetaField($journal, 'external-id'); + $originalSource = $this->cliRepository->getMetaField($journal, 'original-source'); + $recurrenceId = $this->cliRepository->getMetaField($journal, 'recurrence_id'); + $bunq = $this->cliRepository->getMetaField($journal, 'bunq_payment_id'); + $hash = $this->cliRepository->getMetaField($journal, 'import_hash'); + $hashTwo = $this->cliRepository->getMetaField($journal, 'import_hash_v2'); + $interestDate = $this->cliRepository->getMetaDate($journal, 'interest_date'); + $bookDate = $this->cliRepository->getMetaDate($journal, 'book_date'); + $processDate = $this->cliRepository->getMetaDate($journal, 'process_date'); + $dueDate = $this->cliRepository->getMetaDate($journal, 'due_date'); + $paymentDate = $this->cliRepository->getMetaDate($journal, 'payment_date'); + $invoiceDate = $this->cliRepository->getMetaDate($journal, 'invoice_date'); + + // overrule journal category with transaction category. + $budgetId = $this->getTransactionBudget($transaction, $opposingTr) ?? $budgetId; + $categoryId = $this->getTransactionCategory($transaction, $opposingTr) ?? $categoryId; + + return [ + 'type' => strtolower($journal->transactionType->type), + 'date' => $journal->date, + 'user' => $journal->user_id, + 'currency_id' => $transaction->transaction_currency_id, + 'foreign_currency_id' => $transaction->foreign_currency_id, + 'amount' => $transaction->amount, + 'foreign_amount' => $transaction->foreign_amount, + 'description' => $transaction->description ?? $journal->description, + 'source_id' => $opposingTr->account_id, + 'destination_id' => $transaction->account_id, + 'budget_id' => $budgetId, + 'category_id' => $categoryId, + 'bill_id' => $journal->bill_id, + 'notes' => $notes, + 'tags' => $tags, + 'internal_reference' => $internalRef, + 'sepa_cc' => $sepaCC, + 'sepa_ct_op' => $sepaCtOp, + 'sepa_ct_id' => $sepaCtId, + 'sepa_db' => $sepaDb, + 'sepa_country' => $sepaCountry, + 'sepa_ep' => $sepaEp, + 'sepa_ci' => $sepaCi, + 'sepa_batch_id' => $sepaBatchId, + 'external_id' => $externalId, + 'original-source' => $originalSource, + 'recurrence_id' => $recurrenceId, + 'bunq_payment_id' => $bunq, + 'import_hash' => $hash, + 'import_hash_v2' => $hashTwo, + 'interest_date' => $interestDate, + 'book_date' => $bookDate, + 'process_date' => $processDate, + 'due_date' => $dueDate, + 'payment_date' => $paymentDate, + 'invoice_date' => $invoiceDate, + ]; + } + private function getTransactionBudget(Transaction $left, Transaction $right): ?int { app('log')->debug('Now in getTransactionBudget()'); diff --git a/app/Providers/FireflyServiceProvider.php b/app/Providers/FireflyServiceProvider.php index 1cfe350dd1..a7ac424929 100644 --- a/app/Providers/FireflyServiceProvider.php +++ b/app/Providers/FireflyServiceProvider.php @@ -92,6 +92,8 @@ class FireflyServiceProvider extends ServiceProvider /** * Register stuff. + * + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function register(): void { diff --git a/app/Rules/IsDuplicateTransaction.php b/app/Rules/IsDuplicateTransaction.php index f368a40006..b729ff46c3 100644 --- a/app/Rules/IsDuplicateTransaction.php +++ b/app/Rules/IsDuplicateTransaction.php @@ -34,6 +34,9 @@ class IsDuplicateTransaction implements ValidationRule { private string $value; + /** + * @SuppressWarnings(PHPMD.UnusedFormalParameter) + */ public function validate(string $attribute, mixed $value, \Closure $fail): void { $this->value = $value; diff --git a/app/Support/Amount.php b/app/Support/Amount.php index b0a544c96a..7dea9e5b33 100644 --- a/app/Support/Amount.php +++ b/app/Support/Amount.php @@ -50,6 +50,8 @@ class Amount * as a currency, given two things: the currency required and the current locale. * * @throws FireflyException + * + * @SuppressWarnings(PHPMD.MissingImport) */ public function formatFlat(string $symbol, int $decimalPlaces, string $amount, bool $coloured = null): string { @@ -61,7 +63,7 @@ class Amount $fmt->setSymbol(\NumberFormatter::CURRENCY_SYMBOL, $symbol); $fmt->setAttribute(\NumberFormatter::MIN_FRACTION_DIGITS, $decimalPlaces); $fmt->setAttribute(\NumberFormatter::MAX_FRACTION_DIGITS, $decimalPlaces); - $result = (string)$fmt->format((float)$rounded); // intentional float + $result = (string) $fmt->format((float) $rounded); // intentional float if (true === $coloured) { if (1 === bccomp($rounded, '0')) { @@ -234,6 +236,8 @@ class Amount /** * @throws FireflyException + * + * @SuppressWarnings(PHPMD.MissingImport) */ private function getLocaleInfo(): array { diff --git a/app/Support/Calendar/Calculator.php b/app/Support/Calendar/Calculator.php index 3583febc23..e6e9255bc5 100644 --- a/app/Support/Calendar/Calculator.php +++ b/app/Support/Calendar/Calculator.php @@ -34,7 +34,7 @@ class Calculator { public const int DEFAULT_INTERVAL = 1; private static ?\SplObjectStorage $intervalMap = null; - private static array $intervals = []; + private static array $intervals = []; /** * @throws IntervalException @@ -62,6 +62,9 @@ class Calculator return self::loadIntervalMap()->contains($periodicity); } + /** + * @SuppressWarnings(PHPMD.MissingImport) + */ private static function loadIntervalMap(): \SplObjectStorage { if (null !== self::$intervalMap) { diff --git a/app/Validation/FireflyValidator.php b/app/Validation/FireflyValidator.php index dbefd8a128..ccebdadb2d 100644 --- a/app/Validation/FireflyValidator.php +++ b/app/Validation/FireflyValidator.php @@ -179,35 +179,7 @@ class FireflyValidator extends Validator // replace characters outside of ASCI range. $value = (string) iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $value); $search = [' ', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']; - $replace = [ - '', - '10', - '11', - '12', - '13', - '14', - '15', - '16', - '17', - '18', - '19', - '20', - '21', - '22', - '23', - '24', - '25', - '26', - '27', - '28', - '29', - '30', - '31', - '32', - '33', - '34', - '35', - ]; + $replace = ['', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35']; // take $first = substr($value, 0, 4); diff --git a/database/migrations/2016_06_16_000002_create_main_tables.php b/database/migrations/2016_06_16_000002_create_main_tables.php index 2a0208a514..6de53d641d 100644 --- a/database/migrations/2016_06_16_000002_create_main_tables.php +++ b/database/migrations/2016_06_16_000002_create_main_tables.php @@ -522,6 +522,9 @@ class CreateMainTables extends Migration } } + /** + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) + */ private function createTransactionTables(): void { if (!Schema::hasTable('transaction_journals')) { diff --git a/database/migrations/2018_06_08_200526_changes_for_v475.php b/database/migrations/2018_06_08_200526_changes_for_v475.php index 6575103788..7a73769960 100644 --- a/database/migrations/2018_06_08_200526_changes_for_v475.php +++ b/database/migrations/2018_06_08_200526_changes_for_v475.php @@ -49,6 +49,7 @@ class ChangesForV475 extends Migration * Run the migrations. * * @SuppressWarnings(PHPMD.ShortMethodName) + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function up(): void { diff --git a/database/migrations/2020_11_12_070604_changes_for_v550.php b/database/migrations/2020_11_12_070604_changes_for_v550.php index 8364c8224b..b607bbbb53 100644 --- a/database/migrations/2020_11_12_070604_changes_for_v550.php +++ b/database/migrations/2020_11_12_070604_changes_for_v550.php @@ -124,6 +124,7 @@ class ChangesForV550 extends Migration * Run the migrations. * * @SuppressWarnings(PHPMD.ShortMethodName) + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function up(): void {