Various code cleanup and fixed alignments.

This commit is contained in:
James Cole
2024-01-01 14:43:56 +01:00
parent 1368aafe5f
commit f963ac63f1
443 changed files with 3668 additions and 3672 deletions

View File

@@ -73,7 +73,7 @@ class AccountFactory
{
app('log')->debug(sprintf('findOrCreate("%s", "%s")', $accountName, $accountType));
$type = $this->accountRepository->getAccountTypeByType($accountType);
$type = $this->accountRepository->getAccountTypeByType($accountType);
if (null === $type) {
throw new FireflyException(sprintf('Cannot find account type "%s"', $accountType));
}
@@ -108,13 +108,13 @@ class AccountFactory
$data['iban'] = $this->filterIban($data['iban'] ?? null);
// account may exist already:
$return = $this->find($data['name'], $type->type);
$return = $this->find($data['name'], $type->type);
if (null !== $return) {
return $return;
}
$return = $this->createAccount($type, $data);
$return = $this->createAccount($type, $data);
event(new StoredAccount($return));
@@ -198,11 +198,11 @@ class AccountFactory
$databaseData['virtual_balance'] = null;
}
// create account!
$account = Account::create($databaseData);
$account = Account::create($databaseData);
Log::channel('audit')->info(sprintf('Account #%d ("%s") has been created.', $account->id, $account->name));
// update meta data:
$data = $this->cleanMetaDataArray($account, $data);
$data = $this->cleanMetaDataArray($account, $data);
$this->storeMetaData($account, $data);
// create opening balance (only asset accounts)
@@ -222,7 +222,7 @@ class AccountFactory
}
// create notes
$notes = array_key_exists('notes', $data) ? $data['notes'] : '';
$notes = array_key_exists('notes', $data) ? $data['notes'] : '';
$this->updateNote($account, $notes);
// create location
@@ -242,10 +242,10 @@ 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;
$currency = $this->getCurrency($currencyId, $currencyCode);
$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:
if (AccountType::ASSET !== $account->accountType->type) {
@@ -263,7 +263,7 @@ class AccountFactory
private function storeMetaData(Account $account, array $data): void
{
$fields = $this->validFields;
$fields = $this->validFields;
if (AccountType::ASSET === $account->accountType->type) {
$fields = $this->validAssetFields;
}
@@ -272,8 +272,8 @@ class AccountFactory
}
// remove currency_id if necessary.
$type = $account->accountType->type;
$list = config('firefly.valid_currency_account_types');
$type = $account->accountType->type;
$list = config('firefly.valid_currency_account_types');
if (!in_array($type, $list, true)) {
$pos = array_search('currency_id', $fields, true);
if (false !== $pos) {
@@ -350,9 +350,9 @@ class AccountFactory
*/
private function storeOrder(Account $account, array $data): void
{
$accountType = $account->accountType->type;
$maxOrder = $this->accountRepository->maxOrder($accountType);
$order = null;
$accountType = $account->accountType->type;
$maxOrder = $this->accountRepository->maxOrder($accountType);
$order = null;
if (!array_key_exists('order', $data)) {
$order = $maxOrder + 1;
}

View File

@@ -43,13 +43,13 @@ class AttachmentFactory
public function create(array $data): ?Attachment
{
// append if necessary.
$model = !str_contains($data['attachable_type'], 'FireflyIII') ? sprintf('FireflyIII\\Models\\%s', $data['attachable_type'])
$model = !str_contains($data['attachable_type'], 'FireflyIII') ? sprintf('FireflyIII\\Models\\%s', $data['attachable_type'])
: $data['attachable_type'];
// 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');
}
@@ -74,7 +74,7 @@ class AttachmentFactory
);
$notes = (string)($data['notes'] ?? '');
if ('' !== $notes) {
$note = new Note();
$note = new Note();
$note->noteable()->associate($attachment);
$note->text = $notes;
$note->save();

View File

@@ -47,8 +47,8 @@ class BillFactory
public function create(array $data): ?Bill
{
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)) ??
$factory = app(TransactionCurrencyFactory::class);
$currency = $factory->find((int)($data['currency_id'] ?? null), (string)($data['currency_code'] ?? null)) ??
app('amount')->getDefaultCurrencyByUserGroup($this->user->userGroup);
try {
@@ -56,7 +56,7 @@ class BillFactory
$active = array_key_exists('active', $data) ? $data['active'] : 0;
/** @var Bill $bill */
$bill = Bill::create(
$bill = Bill::create(
[
'name' => $data['name'],
'match' => 'MIGRATED_TO_RULES',
@@ -93,7 +93,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) {

View File

@@ -54,7 +54,7 @@ class PiggyBankEventFactory
return;
}
app('log')->debug('Found repetition');
$amount = $piggyRepos->getExactAmount($piggyBank, $repetition, $journal);
$amount = $piggyRepos->getExactAmount($piggyBank, $repetition, $journal);
if (0 === bccomp($amount, '0')) {
app('log')->debug('Amount is zero, will not create event.');

View File

@@ -98,7 +98,7 @@ class RecurrenceFactory
}
$repeatUntilString = $repeatUntil?->format('Y-m-d');
$recurrence = new Recurrence(
$recurrence = new Recurrence(
[
'user_id' => $this->user->id,
'user_group_id' => $this->user->user_group_id,

View File

@@ -36,11 +36,11 @@ class TagFactory
public function findOrCreate(string $tag): ?Tag
{
$tag = trim($tag);
$tag = trim($tag);
app('log')->debug(sprintf('Now in TagFactory::findOrCreate("%s")', $tag));
/** @var null|Tag $dbTag */
$dbTag = $this->user->tags()->where('tag', $tag)->first();
$dbTag = $this->user->tags()->where('tag', $tag)->first();
if (null !== $dbTag) {
app('log')->debug(sprintf('Tag exists (#%d), return it.', $dbTag->id));
@@ -84,7 +84,7 @@ class TagFactory
];
/** @var null|Tag $tag */
$tag = Tag::create($array);
$tag = Tag::create($array);
if (null !== $tag && null !== $latitude && null !== $longitude) {
// create location object.
$location = new Location();

View File

@@ -43,7 +43,7 @@ class TransactionCurrencyFactory
$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();
$count = TransactionCurrency::withTrashed()->whereCode($data['code'])->count();
if (1 === $count) {
$old = TransactionCurrency::withTrashed()->whereCode($data['code'])->first();
$old->forceDelete();

View File

@@ -221,7 +221,7 @@ class TransactionFactory
}
app('log')->debug('Will update account with IBAN information.');
$service = app(AccountUpdateService::class);
$service = app(AccountUpdateService::class);
$service->update($this->account, ['iban' => $this->accountInformation['iban']]);
}
}

View File

@@ -63,8 +63,8 @@ class TransactionGroupFactory
throw new DuplicateTransactionException($e->getMessage(), 0, $e);
}
$title = $data['group_title'] ?? null;
$title = '' === $title ? null : $title;
$title = $data['group_title'] ?? null;
$title = '' === $title ? null : $title;
if (null !== $title) {
$title = substr($title, 0, 1000);
@@ -73,7 +73,7 @@ class TransactionGroupFactory
throw new FireflyException('Created zero transaction journals.');
}
$group = new TransactionGroup();
$group = new TransactionGroup();
$group->user()->associate($this->user);
$group->userGroup()->associate($data['user_group'] ?? $this->user->userGroup);
$group->title = $title;

View File

@@ -96,7 +96,7 @@ class TransactionJournalFactory
{
app('log')->debug('Now in TransactionJournalFactory::create()');
// convert to special object.
$dataObject = new NullArrayObject($data);
$dataObject = new NullArrayObject($data);
app('log')->debug('Start of TransactionJournalFactory::create()');
$collection = new Collection();
@@ -163,7 +163,7 @@ class TransactionJournalFactory
protected function storeMeta(TransactionJournal $journal, NullArrayObject $data, string $field): void
{
$set = [
$set = [
'journal' => $journal,
'name' => $field,
'data' => (string) ($data[$field] ?? ''),
@@ -197,14 +197,14 @@ class TransactionJournalFactory
$this->errorIfDuplicate($row['import_hash_v2']);
/** Some basic fields */
$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']);
$foreignCurrency = $this->currencyRepository->findCurrencyNull($row['foreign_currency_id'], $row['foreign_currency_code']);
$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'];
$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']);
$foreignCurrency = $this->currencyRepository->findCurrencyNull($row['foreign_currency_id'], $row['foreign_currency_code']);
$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'];
// Manipulate basic fields
$carbon->setTimezone(config('app.timezone'));
@@ -220,7 +220,7 @@ class TransactionJournalFactory
}
/** create or get source and destination accounts */
$sourceInfo = [
$sourceInfo = [
'id' => $row['source_id'],
'name' => $row['source_name'],
'iban' => $row['source_iban'],
@@ -229,7 +229,7 @@ class TransactionJournalFactory
'currency_id' => $currency->id,
];
$destInfo = [
$destInfo = [
'id' => $row['destination_id'],
'name' => $row['destination_name'],
'iban' => $row['destination_iban'],
@@ -239,8 +239,8 @@ class TransactionJournalFactory
];
app('log')->debug('Source info:', $sourceInfo);
app('log')->debug('Destination info:', $destInfo);
$sourceAccount = $this->getAccount($type->type, 'source', $sourceInfo);
$destinationAccount = $this->getAccount($type->type, 'destination', $destInfo);
$sourceAccount = $this->getAccount($type->type, 'source', $sourceInfo);
$destinationAccount = $this->getAccount($type->type, 'destination', $destInfo);
app('log')->debug('Done with getAccount(2x)');
// this is the moment for a reconciliation sanity check (again).
@@ -248,15 +248,15 @@ class TransactionJournalFactory
[$sourceAccount, $destinationAccount] = $this->reconciliationSanityCheck($sourceAccount, $destinationAccount);
}
$currency = $this->getCurrencyByAccount($type->type, $currency, $sourceAccount, $destinationAccount);
$foreignCurrency = $this->compareCurrencies($currency, $foreignCurrency);
$foreignCurrency = $this->getForeignByAccount($type->type, $foreignCurrency, $destinationAccount);
$description = $this->getDescription($description);
$currency = $this->getCurrencyByAccount($type->type, $currency, $sourceAccount, $destinationAccount);
$foreignCurrency = $this->compareCurrencies($currency, $foreignCurrency);
$foreignCurrency = $this->getForeignByAccount($type->type, $foreignCurrency, $destinationAccount);
$description = $this->getDescription($description);
app('log')->debug(sprintf('Date: %s (%s)', $carbon->toW3cString(), $carbon->getTimezone()->getName()));
/** Create a basic journal. */
$journal = TransactionJournal::create(
$journal = TransactionJournal::create(
[
'user_id' => $this->user->id,
'user_group_id' => $this->user->user_group_id,
@@ -273,7 +273,7 @@ class TransactionJournalFactory
app('log')->debug(sprintf('Created new journal #%d: "%s"', $journal->id, $journal->description));
/** Create two transactions. */
$transactionFactory = app(TransactionFactory::class);
$transactionFactory = app(TransactionFactory::class);
$transactionFactory->setUser($this->user);
$transactionFactory->setJournal($journal);
$transactionFactory->setAccount($sourceAccount);
@@ -292,7 +292,7 @@ class TransactionJournalFactory
}
/** @var TransactionFactory $transactionFactory */
$transactionFactory = app(TransactionFactory::class);
$transactionFactory = app(TransactionFactory::class);
$transactionFactory->setUser($this->user);
$transactionFactory->setJournal($journal);
$transactionFactory->setAccount($destinationAccount);
@@ -310,7 +310,7 @@ class TransactionJournalFactory
throw new FireflyException($e->getMessage(), 0, $e);
}
$journal->completed = true;
$journal->completed = true;
$journal->save();
$this->storeBudget($journal, $row);
$this->storeCategory($journal, $row);
@@ -334,7 +334,7 @@ class TransactionJournalFactory
app('log')->error(sprintf('Could not encode dataRow: %s', $e->getMessage()));
$json = microtime();
}
$hash = hash('sha256', $json);
$hash = hash('sha256', $json);
app('log')->debug(sprintf('The hash is: %s', $hash), $dataRow);
return $hash;
@@ -378,18 +378,18 @@ class TransactionJournalFactory
private function validateAccounts(NullArrayObject $data): void
{
app('log')->debug(sprintf('Now in %s', __METHOD__));
$transactionType = $data['type'] ?? 'invalid';
$transactionType = $data['type'] ?? 'invalid';
$this->accountValidator->setUser($this->user);
$this->accountValidator->setTransactionType($transactionType);
// validate source account.
$array = [
$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,
];
$validSource = $this->accountValidator->validateSource($array);
$validSource = $this->accountValidator->validateSource($array);
// do something with result:
if (false === $validSource) {
@@ -398,7 +398,7 @@ class TransactionJournalFactory
app('log')->debug('Source seems valid.');
// validate destination account
$array = [
$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,
@@ -466,7 +466,7 @@ class TransactionJournalFactory
// return user's default:
return app('amount')->getDefaultCurrencyByUserGroup($this->user->userGroup);
}
$result = $preference ?? $currency;
$result = $preference ?? $currency;
app('log')->debug(sprintf('Currency is now #%d (%s) because of account #%d (%s)', $result->id, $result->code, $account->id, $account->name));
return $result;

View File

@@ -34,10 +34,10 @@ class TransactionJournalMetaFactory
public function updateOrCreate(array $data): ?TransactionJournalMeta
{
// app('log')->debug('In updateOrCreate()');
$value = $data['data'];
$value = $data['data'];
/** @var null|TransactionJournalMeta $entry */
$entry = $data['journal']->transactionJournalMeta()->where('name', $data['name'])->first();
$entry = $data['journal']->transactionJournalMeta()->where('name', $data['name'])->first();
if (null === $value && null !== $entry) {
// app('log')->debug('Value is empty, delete meta value.');
$entry->delete();
@@ -63,7 +63,7 @@ class TransactionJournalMetaFactory
if (null === $entry) {
// app('log')->debug('Will create new object.');
app('log')->debug(sprintf('Going to create new meta-data entry to store "%s".', $data['name']));
$entry = new TransactionJournalMeta();
$entry = new TransactionJournalMeta();
$entry->transactionJournal()->associate($data['journal']);
$entry->name = $data['name'];
}

View File

@@ -40,12 +40,12 @@ class UserGroupFactory
*/
public function create(array $data): UserGroup
{
$userGroup = new UserGroup();
$userGroup->title = $data['title'];
$userGroup = new UserGroup();
$userGroup->title = $data['title'];
$userGroup->save();
// grab the OWNER role:
$role = UserRole::whereTitle(UserRoleEnum::OWNER->value)->first();
$role = UserRole::whereTitle(UserRoleEnum::OWNER->value)->first();
if (null === $role) {
throw new FireflyException('Role "owner" does not exist.');
}