mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-25 13:10:35 +00:00
Various code cleanup.
This commit is contained in:
@@ -88,6 +88,8 @@ trait AccountServiceTrait
|
||||
/**
|
||||
* Update metadata for account. Depends on type which fields are valid.
|
||||
*
|
||||
* @SuppressWarnings(PHPMD.NPathComplexity)
|
||||
*
|
||||
* TODO this method treats expense accounts and liabilities the same way (tries to save interest)
|
||||
*/
|
||||
public function updateMetaData(Account $account, array $data): void
|
||||
@@ -336,7 +338,7 @@ trait AccountServiceTrait
|
||||
|
||||
/**
|
||||
* @throws FireflyException
|
||||
* */
|
||||
*/
|
||||
protected function getCurrency(int $currencyId, string $currencyCode): TransactionCurrency
|
||||
{
|
||||
// find currency, or use default currency instead.
|
||||
@@ -360,7 +362,7 @@ trait AccountServiceTrait
|
||||
* Create the opposing "credit liability" transaction for credit liabilities.
|
||||
*
|
||||
* @throws FireflyException
|
||||
* */
|
||||
*/
|
||||
protected function updateCreditTransaction(Account $account, string $direction, string $openingBalance, Carbon $openingBalanceDate): TransactionGroup
|
||||
{
|
||||
app('log')->debug(sprintf('Now in %s', __METHOD__));
|
||||
@@ -416,7 +418,7 @@ trait AccountServiceTrait
|
||||
|
||||
/**
|
||||
* @throws FireflyException
|
||||
* */
|
||||
*/
|
||||
protected function createCreditTransaction(Account $account, string $openingBalance, Carbon $openingBalanceDate): TransactionGroup
|
||||
{
|
||||
app('log')->debug('Now going to create an createCreditTransaction.');
|
||||
@@ -510,7 +512,7 @@ trait AccountServiceTrait
|
||||
* Since opening balance and date can still be empty strings, it may fail.
|
||||
*
|
||||
* @throws FireflyException
|
||||
* */
|
||||
*/
|
||||
protected function updateOBGroupV2(Account $account, string $openingBalance, Carbon $openingBalanceDate): TransactionGroup
|
||||
{
|
||||
app('log')->debug(sprintf('Now in %s', __METHOD__));
|
||||
@@ -566,7 +568,7 @@ trait AccountServiceTrait
|
||||
|
||||
/**
|
||||
* @throws FireflyException
|
||||
* */
|
||||
*/
|
||||
protected function createOBGroupV2(Account $account, string $openingBalance, Carbon $openingBalanceDate): TransactionGroup
|
||||
{
|
||||
app('log')->debug('Now going to create an OB group.');
|
||||
|
||||
@@ -243,7 +243,11 @@ class CreditRecalculateService
|
||||
}
|
||||
|
||||
/**
|
||||
* A complex and long method, but rarely used luckily.
|
||||
*
|
||||
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
|
||||
* @SuppressWarnings(PHPMD.NPathComplexity)
|
||||
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
|
||||
*/
|
||||
private function processTransaction(Account $account, string $direction, Transaction $transaction, string $leftOfDebt): string
|
||||
{
|
||||
|
||||
@@ -87,7 +87,9 @@ trait RecurringTransactionTrait
|
||||
* Store transactions of a recurring transactions. It's complex but readable.
|
||||
*
|
||||
* @throws FireflyException
|
||||
* */
|
||||
*
|
||||
* @SuppressWarnings(PHPMD.NPathComplexity)
|
||||
*/
|
||||
protected function createTransactions(Recurrence $recurrence, array $transactions): void
|
||||
{
|
||||
app('log')->debug('Now in createTransactions()');
|
||||
@@ -123,7 +125,7 @@ trait RecurringTransactionTrait
|
||||
if (!$validator->validateDestination(['id' => $destination->id])) {
|
||||
throw new FireflyException(sprintf('Destination invalid: %s', $validator->destError));
|
||||
}
|
||||
if (array_key_exists('foreign_amount', $array) && '' === (string)$array['foreign_amount']) {
|
||||
if (array_key_exists('foreign_amount', $array) && '' === (string) $array['foreign_amount']) {
|
||||
unset($array['foreign_amount']);
|
||||
}
|
||||
// TODO typeOverrule. The account validator may have a different opinion on the type of the transaction.
|
||||
@@ -135,25 +137,25 @@ trait RecurringTransactionTrait
|
||||
'source_id' => $source->id,
|
||||
'destination_id' => $destination->id,
|
||||
'amount' => $array['amount'],
|
||||
'foreign_amount' => array_key_exists('foreign_amount', $array) ? (string)$array['foreign_amount'] : null,
|
||||
'foreign_amount' => array_key_exists('foreign_amount', $array) ? (string) $array['foreign_amount'] : null,
|
||||
'description' => $array['description'],
|
||||
]
|
||||
);
|
||||
$transaction->save();
|
||||
|
||||
if (array_key_exists('budget_id', $array)) {
|
||||
$this->setBudget($transaction, (int)$array['budget_id']);
|
||||
$this->setBudget($transaction, (int) $array['budget_id']);
|
||||
}
|
||||
if (array_key_exists('bill_id', $array)) {
|
||||
$this->setBill($transaction, (int)$array['bill_id']);
|
||||
$this->setBill($transaction, (int) $array['bill_id']);
|
||||
}
|
||||
if (array_key_exists('category_id', $array)) {
|
||||
$this->setCategory($transaction, (int)$array['category_id']);
|
||||
$this->setCategory($transaction, (int) $array['category_id']);
|
||||
}
|
||||
|
||||
// same for piggy bank
|
||||
if (array_key_exists('piggy_bank_id', $array)) {
|
||||
$this->updatePiggyBank($transaction, (int)$array['piggy_bank_id']);
|
||||
$this->updatePiggyBank($transaction, (int) $array['piggy_bank_id']);
|
||||
}
|
||||
|
||||
if (array_key_exists('tags', $array) && is_array($array['tags'])) {
|
||||
@@ -165,8 +167,8 @@ trait RecurringTransactionTrait
|
||||
protected function findAccount(array $expectedTypes, ?int $accountId, ?string $accountName): Account
|
||||
{
|
||||
$result = null;
|
||||
$accountId = (int)$accountId;
|
||||
$accountName = (string)$accountName;
|
||||
$accountId = (int) $accountId;
|
||||
$accountName = (string) $accountName;
|
||||
|
||||
/** @var AccountRepositoryInterface $repository */
|
||||
$repository = app(AccountRepositoryInterface::class);
|
||||
|
||||
@@ -63,7 +63,7 @@ class AccountUpdateService
|
||||
* Update account data.
|
||||
*
|
||||
* @throws FireflyException
|
||||
* */
|
||||
*/
|
||||
public function update(Account $account, array $data): Account
|
||||
{
|
||||
app('log')->debug(sprintf('Now in %s', __METHOD__));
|
||||
|
||||
@@ -46,14 +46,14 @@ class BillUpdateService
|
||||
|
||||
/**
|
||||
* @throws FireflyException
|
||||
* */
|
||||
*/
|
||||
public function update(Bill $bill, array $data): Bill
|
||||
{
|
||||
$this->user = $bill->user;
|
||||
|
||||
if (array_key_exists('currency_id', $data) || array_key_exists('currency_code', $data)) {
|
||||
$factory = app(TransactionCurrencyFactory::class);
|
||||
$currency = $factory->find((int)($data['currency_id'] ?? null), $data['currency_code'] ?? null) ??
|
||||
$currency = $factory->find((int) ($data['currency_id'] ?? null), $data['currency_code'] ?? null) ??
|
||||
app('amount')->getDefaultCurrencyByUserGroup($bill->user->userGroup);
|
||||
|
||||
// enable the currency if it isn't.
|
||||
@@ -75,14 +75,14 @@ class BillUpdateService
|
||||
];
|
||||
// update note:
|
||||
if (array_key_exists('notes', $data)) {
|
||||
$this->updateNote($bill, (string)$data['notes']);
|
||||
$this->updateNote($bill, (string) $data['notes']);
|
||||
}
|
||||
|
||||
// update order.
|
||||
if (array_key_exists('order', $data)) {
|
||||
// update the order of the piggy bank:
|
||||
$oldOrder = $bill->order;
|
||||
$newOrder = (int)($data['order'] ?? $oldOrder);
|
||||
$newOrder = (int) ($data['order'] ?? $oldOrder);
|
||||
if ($oldOrder !== $newOrder) {
|
||||
$this->updateOrder($bill, $oldOrder, $newOrder);
|
||||
}
|
||||
@@ -112,7 +112,7 @@ class BillUpdateService
|
||||
}
|
||||
if (array_key_exists('object_group_id', $data)) {
|
||||
// try also with ID:
|
||||
$objectGroupId = (int)($data['object_group_id'] ?? 0);
|
||||
$objectGroupId = (int) ($data['object_group_id'] ?? 0);
|
||||
if (0 !== $objectGroupId) {
|
||||
$objectGroup = $this->findObjectGroupById($objectGroupId);
|
||||
if (null !== $objectGroup) {
|
||||
@@ -129,22 +129,25 @@ class BillUpdateService
|
||||
return $bill;
|
||||
}
|
||||
|
||||
/**
|
||||
* @SuppressWarnings(PHPMD.NPathComplexity)
|
||||
*/
|
||||
private function updateBillProperties(Bill $bill, array $data): Bill
|
||||
{
|
||||
if (array_key_exists('name', $data) && '' !== (string)$data['name']) {
|
||||
if (array_key_exists('name', $data) && '' !== (string) $data['name']) {
|
||||
$bill->name = $data['name'];
|
||||
}
|
||||
|
||||
if (array_key_exists('amount_min', $data) && '' !== (string)$data['amount_min']) {
|
||||
if (array_key_exists('amount_min', $data) && '' !== (string) $data['amount_min']) {
|
||||
$bill->amount_min = $data['amount_min'];
|
||||
}
|
||||
if (array_key_exists('amount_max', $data) && '' !== (string)$data['amount_max']) {
|
||||
if (array_key_exists('amount_max', $data) && '' !== (string) $data['amount_max']) {
|
||||
$bill->amount_max = $data['amount_max'];
|
||||
}
|
||||
if (array_key_exists('date', $data) && '' !== (string)$data['date']) {
|
||||
if (array_key_exists('date', $data) && '' !== (string) $data['date']) {
|
||||
$bill->date = $data['date'];
|
||||
}
|
||||
if (array_key_exists('repeat_freq', $data) && '' !== (string)$data['repeat_freq']) {
|
||||
if (array_key_exists('repeat_freq', $data) && '' !== (string) $data['repeat_freq']) {
|
||||
$bill->repeat_freq = $data['repeat_freq'];
|
||||
}
|
||||
if (array_key_exists('skip', $data)) {
|
||||
|
||||
@@ -41,7 +41,7 @@ class GroupUpdateService
|
||||
*
|
||||
* @throws DuplicateTransactionException
|
||||
* @throws FireflyException
|
||||
* */
|
||||
*/
|
||||
public function update(TransactionGroup $transactionGroup, array $data): TransactionGroup
|
||||
{
|
||||
app('log')->debug(sprintf('Now in %s', __METHOD__));
|
||||
@@ -149,7 +149,7 @@ class GroupUpdateService
|
||||
/**
|
||||
* @throws DuplicateTransactionException
|
||||
* @throws FireflyException
|
||||
* */
|
||||
*/
|
||||
private function updateTransactions(TransactionGroup $transactionGroup, array $transactions): array
|
||||
{
|
||||
app('log')->debug(sprintf('Now in %s', __METHOD__));
|
||||
@@ -206,7 +206,7 @@ class GroupUpdateService
|
||||
/**
|
||||
* @throws DuplicateTransactionException
|
||||
* @throws FireflyException
|
||||
* */
|
||||
*/
|
||||
private function createTransactionJournal(TransactionGroup $transactionGroup, array $data): ?TransactionJournal
|
||||
{
|
||||
$submission = [
|
||||
|
||||
@@ -71,7 +71,7 @@ class RecurrenceUpdateService
|
||||
$recurrence->repetitions = 0;
|
||||
}
|
||||
if (array_key_exists('nr_of_repetitions', $info)) {
|
||||
if (0 !== (int)$info['nr_of_repetitions']) {
|
||||
if (0 !== (int) $info['nr_of_repetitions']) {
|
||||
$recurrence->repeat_until = null;
|
||||
}
|
||||
$recurrence->repetitions = $info['nr_of_repetitions'];
|
||||
@@ -192,7 +192,7 @@ class RecurrenceUpdateService
|
||||
* TODO this method is very complex.
|
||||
*
|
||||
* @throws FireflyException
|
||||
* */
|
||||
*/
|
||||
private function updateTransactions(Recurrence $recurrence, array $transactions): void
|
||||
{
|
||||
app('log')->debug('Now in updateTransactions()');
|
||||
@@ -209,7 +209,7 @@ class RecurrenceUpdateService
|
||||
// First, make sure to loop all existing transactions and match them to a counterpart in the submitted transactions array.
|
||||
foreach ($originalTransactions as $i => $originalTransaction) {
|
||||
foreach ($transactions as $ii => $submittedTransaction) {
|
||||
if (array_key_exists('id', $submittedTransaction) && (int)$originalTransaction['id'] === (int)$submittedTransaction['id']) {
|
||||
if (array_key_exists('id', $submittedTransaction) && (int) $originalTransaction['id'] === (int) $submittedTransaction['id']) {
|
||||
app('log')->debug(sprintf('Match original transaction #%d with an entry in the submitted array.', $originalTransaction['id']));
|
||||
$combinations[] = [
|
||||
'original' => $originalTransaction,
|
||||
@@ -238,29 +238,34 @@ class RecurrenceUpdateService
|
||||
// anything left in the original transactions array can be deleted.
|
||||
foreach ($originalTransactions as $original) {
|
||||
app('log')->debug(sprintf('Original transaction #%d is unmatched, delete it!', $original['id']));
|
||||
$this->deleteTransaction($recurrence, (int)$original['id']);
|
||||
$this->deleteTransaction($recurrence, (int) $original['id']);
|
||||
}
|
||||
// anything left is new.
|
||||
$this->createTransactions($recurrence, $transactions);
|
||||
}
|
||||
|
||||
/**
|
||||
* It's a complex method but nothing surprising.
|
||||
*
|
||||
* @SuppressWarnings(PHPMD.NPathComplexity)
|
||||
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
|
||||
*/
|
||||
private function updateCombination(Recurrence $recurrence, array $combination): void
|
||||
{
|
||||
$original = $combination['original'];
|
||||
$submitted = $combination['submitted'];
|
||||
$original = $combination['original'];
|
||||
$submitted = $combination['submitted'];
|
||||
$currencyFactory = app(TransactionCurrencyFactory::class);
|
||||
|
||||
/** @var RecurrenceTransaction $transaction */
|
||||
$transaction = $recurrence->recurrenceTransactions()->find($original['id']);
|
||||
app('log')->debug(sprintf('Now in updateCombination(#%d)', $original['id']));
|
||||
|
||||
$currencyFactory = app(TransactionCurrencyFactory::class);
|
||||
|
||||
// loop all and try to match them:
|
||||
$currency = null;
|
||||
$foreignCurrency = null;
|
||||
if (array_key_exists('currency_id', $submitted) || array_key_exists('currency_code', $submitted)) {
|
||||
$currency = $currencyFactory->find(
|
||||
array_key_exists('currency_id', $submitted) ? (int)$submitted['currency_id'] : null,
|
||||
array_key_exists('currency_id', $submitted) ? (int) $submitted['currency_id'] : null,
|
||||
array_key_exists('currency_code', $submitted) ? $submitted['currency_code'] : null
|
||||
);
|
||||
}
|
||||
@@ -272,7 +277,7 @@ class RecurrenceUpdateService
|
||||
}
|
||||
if (array_key_exists('foreign_currency_id', $submitted) || array_key_exists('foreign_currency_code', $submitted)) {
|
||||
$foreignCurrency = $currencyFactory->find(
|
||||
array_key_exists('foreign_currency_id', $submitted) ? (int)$submitted['foreign_currency_id'] : null,
|
||||
array_key_exists('foreign_currency_id', $submitted) ? (int) $submitted['foreign_currency_id'] : null,
|
||||
array_key_exists('foreign_currency_code', $submitted) ? $submitted['foreign_currency_code'] : null
|
||||
);
|
||||
}
|
||||
@@ -301,29 +306,29 @@ class RecurrenceUpdateService
|
||||
}
|
||||
// update meta data
|
||||
if (array_key_exists('budget_id', $submitted)) {
|
||||
$this->setBudget($transaction, (int)$submitted['budget_id']);
|
||||
$this->setBudget($transaction, (int) $submitted['budget_id']);
|
||||
}
|
||||
if (array_key_exists('bill_id', $submitted)) {
|
||||
$this->setBill($transaction, (int)$submitted['bill_id']);
|
||||
$this->setBill($transaction, (int) $submitted['bill_id']);
|
||||
}
|
||||
// reset category if name is set but empty:
|
||||
// can be removed when v1 is retired.
|
||||
if (array_key_exists('category_name', $submitted) && '' === (string)$submitted['category_name']) {
|
||||
if (array_key_exists('category_name', $submitted) && '' === (string) $submitted['category_name']) {
|
||||
app('log')->debug('Category name is submitted but is empty. Set category to be empty.');
|
||||
$submitted['category_name'] = null;
|
||||
$submitted['category_id'] = 0;
|
||||
}
|
||||
|
||||
if (array_key_exists('category_id', $submitted)) {
|
||||
app('log')->debug(sprintf('Category ID is submitted, set category to be %d.', (int)$submitted['category_id']));
|
||||
$this->setCategory($transaction, (int)$submitted['category_id']);
|
||||
app('log')->debug(sprintf('Category ID is submitted, set category to be %d.', (int) $submitted['category_id']));
|
||||
$this->setCategory($transaction, (int) $submitted['category_id']);
|
||||
}
|
||||
|
||||
if (array_key_exists('tags', $submitted) && is_array($submitted['tags'])) {
|
||||
$this->updateTags($transaction, $submitted['tags']);
|
||||
}
|
||||
if (array_key_exists('piggy_bank_id', $submitted)) {
|
||||
$this->updatePiggyBank($transaction, (int)$submitted['piggy_bank_id']);
|
||||
$this->updatePiggyBank($transaction, (int) $submitted['piggy_bank_id']);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user