Code cleanup.

This commit is contained in:
James Cole
2023-12-20 19:35:52 +01:00
parent c4f6366642
commit 64ec0cf62e
997 changed files with 12908 additions and 28136 deletions

View File

@@ -30,20 +30,15 @@ use FireflyIII\Models\Tag;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use Throwable;
/**
* Trait ModelInformation
*
*/
trait ModelInformation
{
/**
* Get actions based on a bill.
*
* @param Bill $bill
*
* @return array
* @throws FireflyException
*/
protected function getActionsForBill(Bill $bill): array // get info and augument
@@ -58,10 +53,11 @@ trait ModelInformation
'count' => 1,
]
)->render();
} catch (Throwable $e) {
} catch (\Throwable $e) {
app('log')->error(sprintf('Throwable was thrown in getActionsForBill(): %s', $e->getMessage()));
app('log')->error($e->getTraceAsString());
$result = 'Could not render view. See log files.';
throw new FireflyException($result, 0, $e);
}
@@ -69,7 +65,6 @@ trait ModelInformation
}
/**
*
* @return string[]
*
* @psalm-return array<int|null, string>
@@ -78,11 +73,14 @@ trait ModelInformation
{
/** @var AccountRepositoryInterface $repository */
$repository = app(AccountRepositoryInterface::class);
// types of liability:
/** @var AccountType $debt */
$debt = $repository->getAccountTypeByType(AccountType::DEBT);
/** @var AccountType $loan */
$loan = $repository->getAccountTypeByType(AccountType::LOAN);
/** @var AccountType $mortgage */
$mortgage = $repository->getAccountTypeByType(AccountType::MORTGAGE);
$liabilityTypes = [
@@ -95,9 +93,6 @@ trait ModelInformation
return $liabilityTypes;
}
/**
* @return array
*/
protected function getRoles(): array
{
$roles = [];
@@ -111,9 +106,6 @@ trait ModelInformation
/**
* Create fake triggers to match the bill's properties
*
* @param Bill $bill
*
* @return array
* @throws FireflyException
*/
protected function getTriggersForBill(Bill $bill): array // get info and augument
@@ -148,9 +140,10 @@ trait ModelInformation
'triggers' => $triggers,
]
)->render();
} catch (Throwable $e) {
} catch (\Throwable $e) {
app('log')->debug(sprintf('Throwable was thrown in getTriggersForBill(): %s', $e->getMessage()));
app('log')->debug($e->getTraceAsString());
throw new FireflyException(sprintf('Could not render trigger: %s', $e->getMessage()), 0, $e);
}
if ('' !== $string) {
@@ -162,9 +155,6 @@ trait ModelInformation
}
/**
* @param TransactionJournal $journal
*
* @return array
* @throws FireflyException
*/
private function getTriggersForJournal(TransactionJournal $journal): array
@@ -183,11 +173,13 @@ trait ModelInformation
$journalTriggers = [];
$values = [];
$index = 0;
// amount, description, category, budget, tags, source, destination, notes, currency type
//,type
/** @var Transaction|null $source */
// ,type
/** @var null|Transaction $source */
$source = $journal->transactions()->where('amount', '<', 0)->first();
/** @var Transaction|null $destination */
/** @var null|Transaction $destination */
$destination = $journal->transactions()->where('amount', '>', 0)->first();
if (null === $destination || null === $source) {
return $result;
@@ -195,54 +187,55 @@ trait ModelInformation
// type
$journalTriggers[$index] = 'transaction_type';
$values[$index] = $journal->transactionType->type;
$index++;
++$index;
// currency
$journalTriggers[$index] = 'currency_is';
$values[$index] = sprintf('%s (%s)', $journal->transactionCurrency?->name, $journal->transactionCurrency?->code);
$index++;
++$index;
// amount_exactly:
$journalTriggers[$index] = 'amount_exactly';
$values[$index] = $destination->amount;
$index++;
++$index;
// description_is:
$journalTriggers[$index] = 'description_is';
$values[$index] = $journal->description;
$index++;
++$index;
// from_account_is
$journalTriggers[$index] = 'source_account_is';
$values[$index] = $source->account->name;
$index++;
++$index;
// to_account_is
$journalTriggers[$index] = 'destination_account_is';
$values[$index] = $destination->account->name;
$index++;
++$index;
// category (if)
$category = $journal->categories()->first();
if (null !== $category) {
$journalTriggers[$index] = 'category_is';
$values[$index] = $category->name;
$index++;
++$index;
}
// budget (if)
$budget = $journal->budgets()->first();
if (null !== $budget) {
$journalTriggers[$index] = 'budget_is';
$values[$index] = $budget->name;
$index++;
++$index;
}
// tags (if)
$tags = $journal->tags()->get();
/** @var Tag $tag */
foreach ($tags as $tag) {
$journalTriggers[$index] = 'tag_is';
$values[$index] = $tag->tag;
$index++;
++$index;
}
// notes (if)
$notes = $journal->notes()->first();
@@ -261,15 +254,17 @@ trait ModelInformation
'triggers' => $triggers,
];
$string = view('rules.partials.trigger', $renderInfo)->render();
} catch (Throwable $e) {
} catch (\Throwable $e) {
app('log')->debug(sprintf('Throwable was thrown in getTriggersForJournal(): %s', $e->getMessage()));
app('log')->debug($e->getTraceAsString());
throw new FireflyException(sprintf('Could not render trigger: %s', $e->getMessage()), 0, $e);
}
if ('' !== $string) {
$result[] = $string;
}
}
return $result;
}
}