Code cleanup

This commit is contained in:
James Cole
2024-12-22 08:43:12 +01:00
parent 5751f7e5a3
commit 565bd87959
574 changed files with 4600 additions and 4604 deletions

View File

@@ -40,7 +40,7 @@ class UpdateRequest implements UpdateRequestInterface
app('log')->debug(sprintf('Now in getUpdateInformation(%s)', $channel));
$information = [
'level' => 'error',
'message' => (string)trans('firefly.unknown_error'),
'message' => (string) trans('firefly.unknown_error'),
];
// try to get array from update server:
@@ -65,7 +65,7 @@ class UpdateRequest implements UpdateRequestInterface
'version' => config('firefly.version'),
'date' => today(config('app.timezone'))->startOfDay(),
'level' => 'error',
'message' => (string)trans('firefly.unknown_error'),
'message' => (string) trans('firefly.unknown_error'),
];
$url = config('firefly.update_endpoint');
@@ -91,12 +91,12 @@ class UpdateRequest implements UpdateRequestInterface
if (200 !== $res->getStatusCode()) {
app('log')->error(sprintf('Response status from server is %d.', $res->getStatusCode()));
app('log')->error((string)$res->getBody());
app('log')->error((string) $res->getBody());
$return['message'] = sprintf('Error: %d', $res->getStatusCode());
return $return;
}
$body = (string)$res->getBody();
$body = (string) $res->getBody();
try {
$json = json_decode($body, true, 512, JSON_THROW_ON_ERROR);
@@ -135,8 +135,8 @@ class UpdateRequest implements UpdateRequestInterface
private function parseResult(array $information): array
{
app('log')->debug('Now in parseResult()', $information);
$current = (string)config('firefly.version');
$latest = (string)$information['version'];
$current = (string) config('firefly.version');
$latest = (string) $information['version'];
// strip the 'v' from the version if it's there.
if (str_starts_with($latest, 'v')) {
@@ -189,13 +189,13 @@ class UpdateRequest implements UpdateRequestInterface
if ($devDate->lte($information['date'])) {
Log::debug(sprintf('This development release is older, release = %s, latest version %s = %s', $devDate->format('Y-m-d'), $latest, $information['date']->format('Y-m-d')));
$return['level'] = 'info';
$return['message'] = (string)trans('firefly.update_current_dev_older', ['version' => $current, 'new_version' => $latest]);
$return['message'] = (string) trans('firefly.update_current_dev_older', ['version' => $current, 'new_version' => $latest]);
return $return;
}
Log::debug(sprintf('This development release is newer, release = %s, latest version %s = %s', $devDate->format('Y-m-d'), $latest, $information['date']->format('Y-m-d')));
$return['level'] = 'info';
$return['message'] = (string)trans('firefly.update_current_dev_newer', ['version' => $current, 'new_version' => $latest]);
$return['message'] = (string) trans('firefly.update_current_dev_newer', ['version' => $current, 'new_version' => $latest]);
return $return;
}
@@ -204,7 +204,7 @@ class UpdateRequest implements UpdateRequestInterface
{
$return = [
'level' => 'info',
'message' => (string)trans('firefly.update_newer_version_alert', ['your_version' => $current, 'new_version' => $latest]),
'message' => (string) trans('firefly.update_newer_version_alert', ['your_version' => $current, 'new_version' => $latest]),
];
app('log')->debug('User is running a newer version', $return);
@@ -215,7 +215,7 @@ class UpdateRequest implements UpdateRequestInterface
{
$return = [
'level' => 'info',
'message' => (string)trans('firefly.update_current_version_alert', ['version' => $current]),
'message' => (string) trans('firefly.update_current_version_alert', ['version' => $current]),
];
app('log')->debug('User is the current version.', $return);
@@ -225,12 +225,12 @@ class UpdateRequest implements UpdateRequestInterface
private function releasedNewAlpha(string $current, string $latest, Carbon $date): array
{
app('log')->debug('New release is also a alpha!');
$message = (string)trans(
$message = (string) trans(
'firefly.update_new_version_alert',
[
'your_version' => $current,
'new_version' => $latest,
'date' => $date->isoFormat((string)trans('config.month_and_day_js')),
'date' => $date->isoFormat((string) trans('config.month_and_day_js')),
]
);
@@ -243,12 +243,12 @@ class UpdateRequest implements UpdateRequestInterface
private function releasedNewBeta(string $current, string $latest, Carbon $date): array
{
app('log')->debug('New release is also a beta!');
$message = (string)trans(
$message = (string) trans(
'firefly.update_new_version_alert',
[
'your_version' => $current,
'new_version' => $latest,
'date' => $date->isoFormat((string)trans('config.month_and_day_js')),
'date' => $date->isoFormat((string) trans('config.month_and_day_js')),
]
);
@@ -261,12 +261,12 @@ class UpdateRequest implements UpdateRequestInterface
private function releasedNewVersion(string $current, string $latest, Carbon $date): array
{
app('log')->debug('New release is old enough.');
$message = (string)trans(
$message = (string) trans(
'firefly.update_new_version_alert',
[
'your_version' => $current,
'new_version' => $latest,
'date' => $date->isoFormat((string)trans('config.month_and_day_js')),
'date' => $date->isoFormat((string) trans('config.month_and_day_js')),
]
);
app('log')->debug('New release is here!', [$message]);

View File

@@ -119,7 +119,7 @@ class AccountDestroyService
/** @var \stdClass $row */
foreach ($collection as $row) {
if ((int)$row->the_count > 1) {
if ((int) $row->the_count > 1) {
$journalId = $row->transaction_journal_id;
$journal = $user->transactionJournals()->find($journalId);
if (null !== $journal) {
@@ -168,7 +168,7 @@ class AccountDestroyService
/** @var RecurrenceDestroyService $destroyService */
$destroyService = app(RecurrenceDestroyService::class);
foreach ($recurrences as $recurrenceId) {
$destroyService->destroyById((int)$recurrenceId);
$destroyService->destroyById((int) $recurrenceId);
}
}
}

View File

@@ -144,7 +144,7 @@ trait AccountServiceTrait
$data[$field] = $data[$field]->toAtomString();
}
$factory->crud($account, $field, (string)$data[$field]);
$factory->crud($account, $field, (string) $data[$field]);
}
}
}
@@ -174,7 +174,7 @@ trait AccountServiceTrait
*/
public function validOBData(array $data): bool
{
$data['opening_balance'] = (string)($data['opening_balance'] ?? '');
$data['opening_balance'] = (string) ($data['opening_balance'] ?? '');
if ('' !== $data['opening_balance'] && 0 === bccomp($data['opening_balance'], '0')) {
$data['opening_balance'] = '';
}
@@ -201,7 +201,7 @@ trait AccountServiceTrait
if (is_array($language)) {
$language = 'en_US';
}
$language = (string)$language;
$language = (string) $language;
$sourceId = null;
$sourceName = null;
$destId = null;
@@ -434,7 +434,7 @@ trait AccountServiceTrait
if (is_array($language)) {
$language = 'en_US';
}
$language = (string)$language;
$language = (string) $language;
// set source and/or destination based on whether the amount is positive or negative.
// first, assume the amount is positive and go from there:
@@ -623,7 +623,7 @@ trait AccountServiceTrait
if (is_array($language)) {
$language = 'en_US';
}
$language = (string)$language;
$language = (string) $language;
$sourceId = null;
$sourceName = null;
$destId = null;

View File

@@ -75,7 +75,7 @@ trait JournalServiceTrait
// if $result (find by name) is NULL, but IBAN is set, any result of the search by NAME can't overrule
// this account. In such a case, the name search must be retried with a new name.
if (null !== $nameResult && null === $numberResult && null === $ibanResult && '' !== (string)$data['iban'] && '' !== (string)$nameResult->iban) {
if (null !== $nameResult && null === $numberResult && null === $ibanResult && '' !== (string) $data['iban'] && '' !== (string) $nameResult->iban) {
$data['name'] = sprintf('%s (%s)', $data['name'], $data['iban']);
app('log')->debug(sprintf('Search again using the new name, "%s".', $data['name']));
$result = $this->findAccountByName(null, $data, $expectedTypes[$transactionType]);
@@ -115,7 +115,7 @@ trait JournalServiceTrait
{
// first attempt, find by ID.
if (null !== $data['id']) {
$search = $this->accountRepository->find((int)$data['id']);
$search = $this->accountRepository->find((int) $data['id']);
if (null !== $search && in_array($search->accountType->type, $types, true)) {
app('log')->debug(
sprintf('Found "account_id" object: #%d, "%s" of type %s (1)', $search->id, $search->name, $search->accountType->type)
@@ -176,10 +176,10 @@ trait JournalServiceTrait
return null;
}
// find by preferred type.
$source = $this->accountRepository->findByAccountNumber((string)$data['number'], [$types[0]]);
$source = $this->accountRepository->findByAccountNumber((string) $data['number'], [$types[0]]);
// or any expected type.
$source ??= $this->accountRepository->findByAccountNumber((string)$data['number'], $types);
$source ??= $this->accountRepository->findByAccountNumber((string) $data['number'], $types);
if (null !== $source) {
app('log')->debug(sprintf('Found account: #%d, %s', $source->id, $source->name));
@@ -261,17 +261,17 @@ trait JournalServiceTrait
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']) {
if ('' === (string) $data['name'] && '' !== (string) $data['iban']) {
app('log')->debug(sprintf('Account name is now IBAN ("%s")', $data['iban']));
$data['name'] = $data['iban'];
}
// fix name of account if only number is given:
if ('' === (string)$data['name'] && '' !== (string)$data['number']) {
if ('' === (string) $data['name'] && '' !== (string) $data['number']) {
app('log')->debug(sprintf('Account name is now account number ("%s")', $data['number']));
$data['name'] = $data['number'];
}
// if name is still NULL, return NULL.
if ('' === (string)$data['name']) {
if ('' === (string) $data['name']) {
app('log')->debug('Account name is still NULL, return NULL.');
return null;
@@ -310,7 +310,7 @@ trait JournalServiceTrait
private function getCashAccount(?Account $account, array $data, array $types): ?Account
{
// return cash account.
if (null === $account && '' === (string)$data['name']
if (null === $account && '' === (string) $data['name']
&& in_array(AccountType::CASH, $types, true)) {
$account = $this->accountRepository->getCashAccount();
}
@@ -390,7 +390,7 @@ trait JournalServiceTrait
protected function storeNotes(TransactionJournal $journal, ?string $notes): void
{
$notes = (string)$notes;
$notes = (string) $notes;
$note = $journal->notes()->first();
if ('' !== $notes) {
if (null === $note) {
@@ -422,7 +422,7 @@ trait JournalServiceTrait
}
app('log')->debug('Start of loop.');
foreach ($tags as $string) {
$string = (string)$string;
$string = (string) $string;
app('log')->debug(sprintf('Now at tag "%s"', $string));
if ('' !== $string) {
$tag = $this->tagFactory->findOrCreate($string);

View File

@@ -127,7 +127,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.
@@ -139,25 +139,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'])) {
@@ -169,8 +169,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);

View File

@@ -75,7 +75,7 @@ class AccountUpdateService
// find currency, or use default currency instead.
if (array_key_exists('currency_id', $data) || array_key_exists('currency_code', $data)) {
$currency = $this->getCurrency((int)($data['currency_id'] ?? null), (string)($data['currency_code'] ?? null));
$currency = $this->getCurrency((int) ($data['currency_id'] ?? null), (string) ($data['currency_code'] ?? null));
unset($data['currency_code'], $data['currency_id']);
$data['currency_id'] = $currency->id;
}
@@ -94,7 +94,7 @@ class AccountUpdateService
// update note:
if (array_key_exists('notes', $data) && null !== $data['notes']) {
$this->updateNote($account, (string)$data['notes']);
$this->updateNote($account, (string) $data['notes']);
}
// update preferences if inactive:
@@ -120,7 +120,7 @@ class AccountUpdateService
$account->active = $data['active'];
}
if (array_key_exists('iban', $data)) {
$account->iban = app('steam')->filterSpaces((string)$data['iban']);
$account->iban = app('steam')->filterSpaces((string) $data['iban']);
}
// set liability, but account must already be a liability.
@@ -132,7 +132,7 @@ class AccountUpdateService
// set liability, alternative method used in v1 layout:
if ($this->isLiability($account) && array_key_exists('account_type_id', $data)) {
$type = AccountType::find((int)$data['account_type_id']);
$type = AccountType::find((int) $data['account_type_id']);
if (null !== $type && in_array($type->type, config('firefly.valid_liabilities'), true)) {
$account->account_type_id = $type->id;
@@ -305,9 +305,9 @@ class AccountUpdateService
$removeAccountId = $account->id;
$new = [];
foreach ($array as $value) {
if ((int)$value !== $removeAccountId) {
if ((int) $value !== $removeAccountId) {
app('log')->debug(sprintf('Will include: %d', $value));
$new[] = (int)$value;
$new[] = (int) $value;
}
}
app('log')->debug('Final new array is', $new);

View File

@@ -54,7 +54,7 @@ class BillUpdateService
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.
@@ -76,14 +76,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);
}
@@ -113,7 +113,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) {
@@ -135,21 +135,21 @@ class BillUpdateService
*/
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'];
$bill->date_tz = $data['date']->format('e');
}
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)) {

View File

@@ -33,15 +33,15 @@ class CurrencyUpdateService
{
public function update(TransactionCurrency $currency, array $data): TransactionCurrency
{
if (array_key_exists('code', $data) && '' !== (string)$data['code']) {
if (array_key_exists('code', $data) && '' !== (string) $data['code']) {
$currency->code = e($data['code']);
}
if (array_key_exists('symbol', $data) && '' !== (string)$data['symbol']) {
if (array_key_exists('symbol', $data) && '' !== (string) $data['symbol']) {
$currency->symbol = e($data['symbol']);
}
if (array_key_exists('name', $data) && '' !== (string)$data['name']) {
if (array_key_exists('name', $data) && '' !== (string) $data['name']) {
$currency->name = e($data['name']);
}

View File

@@ -108,7 +108,7 @@ class GroupUpdateService
/** @var string $deletedId */
foreach ($result as $deletedId) {
/** @var TransactionJournal $journal */
$journal = $transactionGroup->transactionJournals()->find((int)$deletedId);
$journal = $transactionGroup->transactionJournals()->find((int) $deletedId);
/** @var JournalDestroyService $service */
$service = app(JournalDestroyService::class);
@@ -163,7 +163,7 @@ class GroupUpdateService
*/
foreach ($transactions as $index => $transaction) {
app('log')->debug(sprintf('Now at #%d of %d', $index + 1, count($transactions)), $transaction);
$journalId = (int)($transaction['transaction_journal_id'] ?? 0);
$journalId = (int) ($transaction['transaction_journal_id'] ?? 0);
/** @var null|TransactionJournal $journal */
$journal = $transactionGroup->transactionJournals()->find($journalId);

View File

@@ -74,7 +74,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'];
@@ -212,7 +212,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,
@@ -241,7 +241,7 @@ 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);
@@ -268,7 +268,7 @@ class RecurrenceUpdateService
$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
);
}
@@ -280,7 +280,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
);
}
@@ -309,29 +309,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']);
}
}

View File

@@ -128,7 +128,7 @@ class StandardWebhookSender implements WebhookSenderInterface
if (method_exists($e, 'hasResponse') && method_exists($e, 'getResponse')) {
$attempt->status_code = $e->hasResponse() ? $e->getResponse()->getStatusCode() : 0;
app('log')->error(sprintf('The status code of the error response is: %d', $attempt->status_code));
$body = (string)($e->hasResponse() ? $e->getResponse()->getBody() : '');
$body = (string) ($e->hasResponse() ? $e->getResponse()->getBody() : '');
app('log')->error(sprintf('The body of the error response is: %s', $body));
}
$attempt->logs = $logs;