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

@@ -126,7 +126,7 @@ class AccountFactory
*/
protected function getAccountType(array $data): ?AccountType
{
$accountTypeId = array_key_exists('account_type_id', $data) ? (int)$data['account_type_id'] : 0;
$accountTypeId = array_key_exists('account_type_id', $data) ? (int) $data['account_type_id'] : 0;
$accountTypeName = array_key_exists('account_type_name', $data) ? $data['account_type_name'] : null;
$result = null;
// find by name or ID
@@ -184,7 +184,7 @@ class AccountFactory
'iban' => $data['iban'],
];
// fix virtual balance when it's empty
if ('' === (string)$databaseData['virtual_balance']) {
if ('' === (string) $databaseData['virtual_balance']) {
$databaseData['virtual_balance'] = null;
}
// remove virtual balance when not an asset account
@@ -236,9 +236,9 @@ class AccountFactory
*/
private function cleanMetaDataArray(Account $account, array $data): array
{
$currencyId = array_key_exists('currency_id', $data) ? (int)$data['currency_id'] : 0;
$currencyCode = array_key_exists('currency_code', $data) ? (string)$data['currency_code'] : '';
$accountRole = array_key_exists('account_role', $data) ? (string)$data['account_role'] : null;
$currencyId = array_key_exists('currency_id', $data) ? (int) $data['currency_id'] : 0;
$currencyCode = array_key_exists('currency_code', $data) ? (string) $data['currency_code'] : '';
$accountRole = array_key_exists('account_role', $data) ? (string) $data['account_role'] : null;
$currency = $this->getCurrency($currencyId, $currencyCode);
// only asset account may have a role:
@@ -289,7 +289,7 @@ class AccountFactory
$data[$field] = 1;
}
$factory->crud($account, $field, (string)$data[$field]);
$factory->crud($account, $field, (string) $data[$field]);
}
}
}
@@ -351,7 +351,7 @@ class AccountFactory
$order = $maxOrder + 1;
}
if (array_key_exists('order', $data)) {
$order = (int)($data['order'] > $maxOrder ? $maxOrder + 1 : $data['order']);
$order = (int) ($data['order'] > $maxOrder ? $maxOrder + 1 : $data['order']);
$order = 0 === $order ? $maxOrder + 1 : $order;
}

View File

@@ -50,7 +50,7 @@ class AttachmentFactory
// get journal instead of transaction.
if (Transaction::class === $model) {
/** @var null|Transaction $transaction */
$transaction = $this->user->transactions()->find((int)$data['attachable_id']);
$transaction = $this->user->transactions()->find((int) $data['attachable_id']);
if (null === $transaction) {
throw new FireflyException('Unexpectedly could not find transaction');
}
@@ -73,7 +73,7 @@ class AttachmentFactory
'uploaded' => 0,
]
);
$notes = (string)($data['notes'] ?? '');
$notes = (string) ($data['notes'] ?? '');
if ('' !== $notes) {
$note = new Note();
$note->noteable()->associate($attachment);

View File

@@ -48,7 +48,7 @@ class BillFactory
{
app('log')->debug(sprintf('Now in %s', __METHOD__), $data);
$factory = app(TransactionCurrencyFactory::class);
$currency = $factory->find((int)($data['currency_id'] ?? null), (string)($data['currency_code'] ?? null)) ??
$currency = $factory->find((int) ($data['currency_id'] ?? null), (string) ($data['currency_code'] ?? null)) ??
app('amount')->getDefaultCurrencyByUserGroup($this->user->userGroup);
try {
@@ -58,23 +58,23 @@ class BillFactory
/** @var Bill $bill */
$bill = Bill::create(
[
'name' => $data['name'],
'match' => 'MIGRATED_TO_RULES',
'amount_min' => $data['amount_min'],
'user_id' => $this->user->id,
'user_group_id' => $this->user->user_group_id,
'transaction_currency_id' => $currency->id,
'amount_max' => $data['amount_max'],
'date' => $data['date'],
'date_tz' => $data['date']->format('e'),
'end_date' => $data['end_date'] ?? null,
'end_date_tz' => $data['end_date']?->format('e'),
'extension_date' => $data['extension_date'] ?? null,
'extension_date_tz' => $data['extension_date']?->format('e'),
'repeat_freq' => $data['repeat_freq'],
'skip' => $skip,
'automatch' => true,
'active' => $active,
'name' => $data['name'],
'match' => 'MIGRATED_TO_RULES',
'amount_min' => $data['amount_min'],
'user_id' => $this->user->id,
'user_group_id' => $this->user->user_group_id,
'transaction_currency_id' => $currency->id,
'amount_max' => $data['amount_max'],
'date' => $data['date'],
'date_tz' => $data['date']->format('e'),
'end_date' => $data['end_date'] ?? null,
'end_date_tz' => $data['end_date']?->format('e'),
'extension_date' => $data['extension_date'] ?? null,
'extension_date_tz' => $data['extension_date']?->format('e'),
'repeat_freq' => $data['repeat_freq'],
'skip' => $skip,
'automatch' => true,
'active' => $active,
]
);
} catch (QueryException $e) {
@@ -85,7 +85,7 @@ class BillFactory
}
if (array_key_exists('notes', $data)) {
$this->updateNote($bill, (string)$data['notes']);
$this->updateNote($bill, (string) $data['notes']);
}
$objectGroupTitle = $data['object_group_title'] ?? '';
if ('' !== $objectGroupTitle) {
@@ -96,7 +96,7 @@ class BillFactory
}
}
// 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) {
@@ -110,8 +110,8 @@ class BillFactory
public function find(?int $billId, ?string $billName): ?Bill
{
$billId = (int)$billId;
$billName = (string)$billName;
$billId = (int) $billId;
$billName = (string) $billName;
$bill = null;
// first find by ID:
if ($billId > 0) {

View File

@@ -35,8 +35,8 @@ class BudgetFactory
public function find(?int $budgetId, ?string $budgetName): ?Budget
{
$budgetId = (int)$budgetId;
$budgetName = (string)$budgetName;
$budgetId = (int) $budgetId;
$budgetName = (string) $budgetName;
if (0 === $budgetId && '' === $budgetName) {
return null;

View File

@@ -40,8 +40,8 @@ class CategoryFactory
*/
public function findOrCreate(?int $categoryId, ?string $categoryName): ?Category
{
$categoryId = (int)$categoryId;
$categoryName = (string)$categoryName;
$categoryId = (int) $categoryId;
$categoryName = (string) $categoryName;
app('log')->debug(sprintf('Going to find category with ID %d and name "%s"', $categoryId, $categoryName));

View File

@@ -49,8 +49,8 @@ class PiggyBankFactory
$this->piggyBankRepository->setUser($value);
}
}
private CurrencyRepositoryInterface $currencyRepository;
private AccountRepositoryInterface $accountRepository;
private CurrencyRepositoryInterface $currencyRepository;
private PiggyBankRepositoryInterface $piggyBankRepository;
public function __construct()
@@ -120,6 +120,21 @@ class PiggyBankFactory
return $piggyBank;
}
private function getCurrency(array $data): TransactionCurrency
{
// currency:
$defaultCurrency = app('amount')->getDefaultCurrency();
$currency = null;
if (array_key_exists('transaction_currency_code', $data)) {
$currency = $this->currencyRepository->findByCode((string) ($data['transaction_currency_code'] ?? ''));
}
if (array_key_exists('transaction_currency_id', $data)) {
$currency = $this->currencyRepository->find((int) ($data['transaction_currency_id'] ?? 0));
}
$currency ??= $defaultCurrency;
return $currency;
}
public function find(?int $piggyBankId, ?string $piggyBankName): ?PiggyBank
{
$piggyBankId = (int) $piggyBankId;
@@ -162,21 +177,6 @@ class PiggyBankFactory
->first(['piggy_banks.*']);
}
private function getCurrency(array $data): TransactionCurrency
{
// currency:
$defaultCurrency = app('amount')->getDefaultCurrency();
$currency = null;
if (array_key_exists('transaction_currency_code', $data)) {
$currency = $this->currencyRepository->findByCode((string) ($data['transaction_currency_code'] ?? ''));
}
if (array_key_exists('transaction_currency_id', $data)) {
$currency = $this->currencyRepository->find((int) ($data['transaction_currency_id'] ?? 0));
}
$currency ??= $defaultCurrency;
return $currency;
}
private function setOrder(PiggyBank $piggyBank, array $data): PiggyBank
{
$this->resetOrder();
@@ -227,10 +227,10 @@ class PiggyBankFactory
// collect current current_amount so the sync does not remove them.
// TODO this is a tedious check. Feels like a hack.
$toBeLinked = [];
foreach($piggyBank->accounts as $account) {
foreach($accounts as $info) {
if($account->id === $info['account_id']) {
if(array_key_exists($account->id, $accounts)) {
foreach ($piggyBank->accounts as $account) {
foreach ($accounts as $info) {
if ($account->id === $info['account_id']) {
if (array_key_exists($account->id, $accounts)) {
$toBeLinked[$account->id] = ['current_amount' => $account->pivot->current_amount];
Log::debug(sprintf('Prefilled for account #%d with amount %s', $account->id, $account->pivot->current_amount));
}

View File

@@ -80,7 +80,7 @@ class RecurrenceFactory
$firstDate = $data['recurrence']['first_date'];
}
if (array_key_exists('nr_of_repetitions', $data['recurrence'])) {
$repetitions = (int)$data['recurrence']['nr_of_repetitions'];
$repetitions = (int) $data['recurrence']['nr_of_repetitions'];
}
if (array_key_exists('repeat_until', $data['recurrence'])) {
$repeatUntil = $data['recurrence']['repeat_until'];
@@ -117,7 +117,7 @@ class RecurrenceFactory
$recurrence->save();
if (array_key_exists('notes', $data['recurrence'])) {
$this->updateNote($recurrence, (string)$data['recurrence']['notes']);
$this->updateNote($recurrence, (string) $data['recurrence']['notes']);
}
$this->createRepetitions($recurrence, $data['repetitions'] ?? []);

View File

@@ -69,9 +69,9 @@ class TagFactory
public function create(array $data): ?Tag
{
$zoomLevel = 0 === (int)$data['zoom_level'] ? null : (int)$data['zoom_level'];
$latitude = 0.0 === (float)$data['latitude'] ? null : (float)$data['latitude']; // intentional float
$longitude = 0.0 === (float)$data['longitude'] ? null : (float)$data['longitude']; // intentional float
$zoomLevel = 0 === (int) $data['zoom_level'] ? null : (int) $data['zoom_level'];
$latitude = 0.0 === (float) $data['latitude'] ? null : (float) $data['latitude']; // intentional float
$longitude = 0.0 === (float) $data['longitude'] ? null : (float) $data['longitude']; // intentional float
$array = [
'user_id' => $this->user->id,
'user_group_id' => $this->user->user_group_id,

View File

@@ -41,7 +41,7 @@ class TransactionCurrencyFactory
$data['code'] = e($data['code']);
$data['symbol'] = e($data['symbol']);
$data['name'] = e($data['name']);
$data['decimal_places'] = (int)$data['decimal_places'];
$data['decimal_places'] = (int) $data['decimal_places'];
// if the code already exists (deleted)
// force delete it and then create the transaction:
$count = TransactionCurrency::withTrashed()->whereCode($data['code'])->count();
@@ -76,7 +76,7 @@ class TransactionCurrencyFactory
public function find(?int $currencyId, ?string $currencyCode): ?TransactionCurrency
{
$currencyCode = e($currencyCode);
$currencyId = (int)$currencyId;
$currencyId = (int) $currencyId;
if ('' === $currencyCode && 0 === $currencyId) {
app('log')->debug('Cannot find anything on empty currency code and empty currency ID!');

View File

@@ -142,7 +142,7 @@ class TransactionFactory
return;
}
if ('' !== (string)$this->account->iban) {
if ('' !== (string) $this->account->iban) {
app('log')->debug('Account already has IBAN information, will not update.');
return;

View File

@@ -163,11 +163,11 @@ class TransactionJournalFactory
$type = $this->typeRepository->findTransactionType(null, $row['type']);
$carbon = $row['date'] ?? today(config('app.timezone'));
$order = $row['order'] ?? 0;
$currency = $this->currencyRepository->findCurrency((int)$row['currency_id'], $row['currency_code']);
$currency = $this->currencyRepository->findCurrency((int) $row['currency_id'], $row['currency_code']);
$foreignCurrency = $this->currencyRepository->findCurrencyNull($row['foreign_currency_id'], $row['foreign_currency_code']);
$bill = $this->billRepository->findBill((int)$row['bill_id'], $row['bill_name']);
$bill = $this->billRepository->findBill((int) $row['bill_id'], $row['bill_name']);
$billId = TransactionType::WITHDRAWAL === $type->type && null !== $bill ? $bill->id : null;
$description = (string)$row['description'];
$description = (string) $row['description'];
// Manipulate basic fields
$carbon->setTimezone(config('app.timezone'));
@@ -254,7 +254,7 @@ class TransactionJournalFactory
$transactionFactory->setReconciled($row['reconciled'] ?? false);
try {
$negative = $transactionFactory->createNegative((string)$row['amount'], (string)$row['foreign_amount']);
$negative = $transactionFactory->createNegative((string) $row['amount'], (string) $row['foreign_amount']);
} catch (FireflyException $e) {
app('log')->error(sprintf('Exception creating negative transaction: %s', $e->getMessage()));
$this->forceDeleteOnError(new Collection([$journal]));
@@ -276,15 +276,15 @@ class TransactionJournalFactory
// Firefly III will save the foreign currency information in such a way that both
// asset accounts can look at the "amount" and "transaction_currency_id" column and
// see the currency they expect to see.
$amount = (string)$row['amount'];
$foreignAmount = (string)$row['foreign_amount'];
$amount = (string) $row['amount'];
$foreignAmount = (string) $row['foreign_amount'];
if (null !== $foreignCurrency && $foreignCurrency->id !== $currency->id
&& TransactionType::TRANSFER === $type->type
&& TransactionType::TRANSFER === $type->type
) {
$transactionFactory->setCurrency($foreignCurrency);
$transactionFactory->setForeignCurrency($currency);
$amount = (string)$row['foreign_amount'];
$foreignAmount = (string)$row['amount'];
$amount = (string) $row['foreign_amount'];
$foreignAmount = (string) $row['amount'];
Log::debug('Swap native/foreign amounts in transfer for new save method.');
}
@@ -354,7 +354,7 @@ class TransactionJournalFactory
app('log')->warning(sprintf('Found a duplicate in errorIfDuplicate because hash %s is not unique!', $hash));
$journal = $result->transactionJournal()->withTrashed()->first();
$group = $journal?->transactionGroup()->withTrashed()->first();
$groupId = (int)$group?->id;
$groupId = (int) $group?->id;
throw new DuplicateTransactionException(sprintf('Duplicate of transaction #%d.', $groupId));
}
@@ -372,10 +372,10 @@ class TransactionJournalFactory
// validate source account.
$array = [
'id' => null !== $data['source_id'] ? (int)$data['source_id'] : null,
'name' => null !== $data['source_name'] ? (string)$data['source_name'] : null,
'iban' => null !== $data['source_iban'] ? (string)$data['source_iban'] : null,
'number' => null !== $data['source_number'] ? (string)$data['source_number'] : null,
'id' => null !== $data['source_id'] ? (int) $data['source_id'] : null,
'name' => null !== $data['source_name'] ? (string) $data['source_name'] : null,
'iban' => null !== $data['source_iban'] ? (string) $data['source_iban'] : null,
'number' => null !== $data['source_number'] ? (string) $data['source_number'] : null,
];
$validSource = $this->accountValidator->validateSource($array);
@@ -387,10 +387,10 @@ class TransactionJournalFactory
// validate destination account
$array = [
'id' => null !== $data['destination_id'] ? (int)$data['destination_id'] : null,
'name' => null !== $data['destination_name'] ? (string)$data['destination_name'] : null,
'iban' => null !== $data['destination_iban'] ? (string)$data['destination_iban'] : null,
'number' => null !== $data['destination_number'] ? (string)$data['destination_number'] : null,
'id' => null !== $data['destination_id'] ? (int) $data['destination_id'] : null,
'name' => null !== $data['destination_name'] ? (string) $data['destination_name'] : null,
'iban' => null !== $data['destination_iban'] ? (string) $data['destination_iban'] : null,
'number' => null !== $data['destination_number'] ? (string) $data['destination_number'] : null,
];
$validDestination = $this->accountValidator->validateDestination($array);
@@ -537,7 +537,7 @@ class TransactionJournalFactory
{
app('log')->debug('Will now store piggy event.');
$piggyBank = $this->piggyRepository->findPiggyBank((int)$data['piggy_bank_id'], $data['piggy_bank_name']);
$piggyBank = $this->piggyRepository->findPiggyBank((int) $data['piggy_bank_id'], $data['piggy_bank_name']);
if (null !== $piggyBank) {
$this->piggyEventFactory->create($journal, $piggyBank);
@@ -560,7 +560,7 @@ class TransactionJournalFactory
$set = [
'journal' => $journal,
'name' => $field,
'data' => (string)($data[$field] ?? ''),
'data' => (string) ($data[$field] ?? ''),
];
if ($data[$field] instanceof Carbon) {
$data[$field]->setTimezone(config('app.timezone'));

View File

@@ -50,7 +50,7 @@ class TransactionJournalMetaFactory
app('log')->debug('Is a carbon object.');
$value = $data['data']->toW3cString();
}
if ('' === (string)$value) {
if ('' === (string) $value) {
// app('log')->debug('Is an empty string.');
// don't store blank strings.
if (null !== $entry) {