Fix various code.

This commit is contained in:
James Cole
2025-05-27 17:06:15 +02:00
parent d8f512ca3a
commit 2cb14f6b72
123 changed files with 581 additions and 500 deletions

View File

@@ -44,12 +44,12 @@ class AccountDestroyService
// find and delete opening balance journal + opposing account
$this->destroyOpeningBalance($account);
if (null !== $moveTo) {
if ($moveTo instanceof Account) {
$this->moveTransactions($account, $moveTo);
$this->updateRecurrences($account, $moveTo);
}
// delete recurring transactions with this account:
if (null === $moveTo) {
if (!$moveTo instanceof Account) {
$this->destroyRecurrences($account);
}

View File

@@ -60,10 +60,10 @@ class CreditRecalculateService
if (true !== config('firefly.feature_flags.handle_debts')) {
return;
}
if (null !== $this->group && null === $this->account) {
if ($this->group instanceof TransactionGroup && !$this->account instanceof Account) {
$this->processGroup();
}
if (null !== $this->account && null === $this->group) {
if ($this->account instanceof Account && !$this->group instanceof TransactionGroup) {
// work based on account.
$this->processAccount();
}
@@ -163,7 +163,7 @@ class CreditRecalculateService
$this->repository->setUser($account->user);
$direction = (string) $this->repository->getMetaValue($account, 'liability_direction');
$openingBalance = $this->repository->getOpeningBalance($account);
if (null !== $openingBalance) {
if ($openingBalance instanceof TransactionJournal) {
// Log::debug(sprintf('Found opening balance transaction journal #%d', $openingBalance->id));
// if account direction is "debit" ("I owe this amount") the opening balance must always be AWAY from the account:
if ('debit' === $direction) {
@@ -358,7 +358,7 @@ class CreditRecalculateService
{
$usedAmount = $transaction->amount;
// Log::debug(sprintf('Amount of transaction is %s', app('steam')->bcround($usedAmount, 2)));
if (null !== $foreignCurrency && $foreignCurrency->id === $accountCurrency->id) {
if ($foreignCurrency instanceof TransactionCurrency && $foreignCurrency->id === $accountCurrency->id) {
$usedAmount = $transaction->foreign_amount;
// Log::debug(sprintf('Overruled by foreign amount. Amount of transaction is now %s', app('steam')->bcround($usedAmount, 2)));
}

View File

@@ -39,6 +39,8 @@ use FireflyIII\Rules\UniqueIban;
use FireflyIII\Support\NullArrayObject;
use Illuminate\Support\Facades\Log;
use function Safe\json_encode;
/**
* Trait JournalServiceTrait
*/
@@ -140,7 +142,7 @@ trait JournalServiceTrait
private function findAccountByIban(?Account $account, array $data, array $types): ?Account
{
if (null !== $account) {
if ($account instanceof Account) {
Log::debug(sprintf('Already have account #%d ("%s"), return that.', $account->id, $account->name));
return $account;
@@ -167,7 +169,7 @@ trait JournalServiceTrait
private function findAccountByNumber(?Account $account, array $data, array $types): ?Account
{
if (null !== $account) {
if ($account instanceof Account) {
Log::debug(sprintf('Already have account #%d ("%s"), return that.', $account->id, $account->name));
return $account;
@@ -196,7 +198,7 @@ trait JournalServiceTrait
private function findAccountByName(?Account $account, array $data, array $types): ?Account
{
if (null !== $account) {
if ($account instanceof Account) {
Log::debug(sprintf('Already have account #%d ("%s"), return that.', $account->id, $account->name));
return $account;
@@ -247,7 +249,7 @@ trait JournalServiceTrait
{
Log::debug('Now in createAccount()', $data);
// return new account.
if (null !== $account) {
if ($account instanceof Account) {
Log::debug(
sprintf(
'Was given %s account #%d ("%s") so will simply return that.',
@@ -257,10 +259,10 @@ trait JournalServiceTrait
)
);
}
if (null === $account) {
if (!$account instanceof Account) {
// final attempt, create it.
if (AccountTypeEnum::ASSET->value === $preferredType) {
throw new FireflyException(sprintf('TransactionFactory: Cannot create asset account with these values: %s', \Safe\json_encode($data)));
throw new FireflyException(sprintf('TransactionFactory: Cannot create asset account with these values: %s', json_encode($data)));
}
// fix name of account if only IBAN is given:
if ('' === (string) $data['name'] && '' !== (string) $data['iban']) {
@@ -320,7 +322,7 @@ trait JournalServiceTrait
private function getCashAccount(?Account $account, array $data, array $types): ?Account
{
// return cash account.
if (null === $account && '' === (string) $data['name']
if (!$account instanceof Account && '' === (string) $data['name']
&& in_array(AccountTypeEnum::CASH->value, $types, true)) {
$account = $this->accountRepository->getCashAccount();
}

View File

@@ -43,6 +43,8 @@ use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Validation\AccountValidator;
use Illuminate\Support\Facades\Log;
use function Safe\json_encode;
/**
* Trait RecurringTransactionTrait
*/
@@ -307,9 +309,9 @@ trait RecurringTransactionTrait
/** @var null|RecurrenceMeta $entry */
$entry = $transaction->recurrenceTransactionMeta()->where('name', 'tags')->first();
if (null === $entry) {
$entry = RecurrenceTransactionMeta::create(['rt_id' => $transaction->id, 'name' => 'tags', 'value' => \Safe\json_encode($tags)]);
$entry = RecurrenceTransactionMeta::create(['rt_id' => $transaction->id, 'name' => 'tags', 'value' => json_encode($tags)]);
}
$entry->value = \Safe\json_encode($tags);
$entry->value = json_encode($tags);
$entry->save();
}
if (0 === count($tags)) {

View File

@@ -238,7 +238,7 @@ class AccountUpdateService
// otherwise, update or create.
if (!(null === $data['latitude'] && null === $data['longitude'] && null === $data['zoom_level'])) {
$location = $this->accountRepository->getLocation($account);
if (null === $location) {
if (!$location instanceof Location) {
$location = new Location();
$location->locatable()->associate($account);
}

View File

@@ -24,6 +24,7 @@ declare(strict_types=1);
namespace FireflyIII\Services\Internal\Update;
use FireflyIII\Models\ObjectGroup;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Factory\TransactionCurrencyFactory;
use FireflyIII\Models\Bill;
@@ -100,7 +101,7 @@ class BillUpdateService
$objectGroupTitle = $data['object_group_title'] ?? '';
if ('' !== $objectGroupTitle) {
$objectGroup = $this->findOrCreateObjectGroup($objectGroupTitle);
if (null !== $objectGroup) {
if ($objectGroup instanceof ObjectGroup) {
$bill->objectGroups()->sync([$objectGroup->id]);
$bill->save();
}
@@ -116,7 +117,7 @@ class BillUpdateService
$objectGroupId = (int) ($data['object_group_id'] ?? 0);
if (0 !== $objectGroupId) {
$objectGroup = $this->findObjectGroupById($objectGroupId);
if (null !== $objectGroup) {
if ($objectGroup instanceof ObjectGroup) {
$bill->objectGroups()->sync([$objectGroup->id]);
$bill->save();
}
@@ -232,14 +233,14 @@ class BillUpdateService
/** @var Rule $rule */
foreach ($rules as $rule) {
$trigger = $this->getRuleTrigger($rule, $key);
if (null !== $trigger && $trigger->trigger_value === $oldValue) {
if ($trigger instanceof RuleTrigger && $trigger->trigger_value === $oldValue) {
app('log')->debug(sprintf('Updated rule trigger #%d from value "%s" to value "%s"', $trigger->id, $oldValue, $newValue));
$trigger->trigger_value = $newValue;
$trigger->save();
continue;
}
if (null !== $trigger && $trigger->trigger_value !== $oldValue && in_array($key, ['amount_more', 'amount_less'], true)
if ($trigger instanceof RuleTrigger && $trigger->trigger_value !== $oldValue && in_array($key, ['amount_more', 'amount_less'], true)
&& 0 === bccomp($trigger->trigger_value, $oldValue)) {
app('log')->debug(sprintf('Updated rule trigger #%d from value "%s" to value "%s"', $trigger->id, $oldValue, $newValue));
$trigger->trigger_value = $newValue;

View File

@@ -187,10 +187,10 @@ class GroupUpdateService
Log::debug('Call createTransactionJournal');
$newJournal = $this->createTransactionJournal($transactionGroup, $transaction);
Log::debug('Done calling createTransactionJournal');
if (null !== $newJournal) {
if ($newJournal instanceof TransactionJournal) {
$updated[] = $newJournal->id;
}
if (null === $newJournal) {
if (!$newJournal instanceof TransactionJournal) {
Log::error('createTransactionJournal returned NULL, indicating something went wrong.');
}
}

View File

@@ -226,7 +226,7 @@ class JournalUpdateService
private function getOriginalSourceAccount(): Account
{
if (null === $this->sourceAccount) {
if (!$this->sourceAccount instanceof Account) {
$source = $this->getSourceTransaction();
$this->sourceAccount = $source->account;
}
@@ -236,7 +236,7 @@ class JournalUpdateService
private function getSourceTransaction(): Transaction
{
if (null === $this->sourceTransaction) {
if (!$this->sourceTransaction instanceof Transaction) {
/** @var null|Transaction $result */
$result = $this->transactionJournal->transactions()->with(['account'])->where('amount', '<', 0)->first();
$this->sourceTransaction = $result;
@@ -304,7 +304,7 @@ class JournalUpdateService
private function getOriginalDestinationAccount(): Account
{
if (null === $this->destinationAccount) {
if (!$this->destinationAccount instanceof Account) {
$destination = $this->getDestinationTransaction();
$this->destinationAccount = $destination->account;
}
@@ -317,7 +317,7 @@ class JournalUpdateService
*/
private function getDestinationTransaction(): Transaction
{
if (null === $this->destinationTransaction) {
if (!$this->destinationTransaction instanceof Transaction) {
/** @var null|Transaction $result */
$result = $this->transactionJournal->transactions()->where('amount', '>', 0)->first();
$this->destinationTransaction = $result;

View File

@@ -145,7 +145,7 @@ class RecurrenceUpdateService
app('log')->debug('Loop and find');
foreach ($repetitions as $current) {
$match = $this->matchRepetition($recurrence, $current);
if (null === $match) {
if (!$match instanceof RecurrenceRepetition) {
throw new FireflyException('Cannot match recurring repetition to existing repetition. Not sure what to do. Break.');
}
$fields = [