Various code cleanup.

This commit is contained in:
James Cole
2021-04-07 07:28:43 +02:00
parent 4ddcb0c965
commit f12744ad8c
180 changed files with 714 additions and 721 deletions

View File

@@ -257,7 +257,7 @@ class AccountFactory
$fields = $this->validAssetFields;
}
if ($account->accountType->type === AccountType::ASSET && 'ccAsset' === $data['account_role']) {
$fields = $this->validCCFields; // @codeCoverageIgnore
$fields = $this->validCCFields;
}
/** @var AccountMetaFactory $factory */
@@ -265,14 +265,14 @@ class AccountFactory
foreach ($fields as $field) {
// if the field is set but NULL, skip it.
// if the field is set but "", update it.
if (isset($data[$field]) && null !== $data[$field]) {
if (array_key_exists($field, $data) && null !== $data[$field]) {
// convert boolean value:
if (is_bool($data[$field]) && false === $data[$field]) {
$data[$field] = 0; // @codeCoverageIgnore
$data[$field] = 0;
}
if (is_bool($data[$field]) && true === $data[$field]) {
$data[$field] = 1; // @codeCoverageIgnore
$data[$field] = 1;
}
$factory->crud($account, $field, (string)$data[$field]);

View File

@@ -67,8 +67,8 @@ class AccountMetaFactory
if ('' === $value && null !== $entry) {
try {
$entry->delete();
} catch (Exception $e) { // @codeCoverageIgnore
Log::debug(sprintf('Could not delete entry: %s', $e->getMessage())); // @codeCoverageIgnore
} catch (Exception $e) { // @phpstan-ignore-line
Log::debug(sprintf('Could not delete entry: %s', $e->getMessage()));
}
return null;

View File

@@ -54,7 +54,7 @@ class AttachmentFactory
/** @var Transaction $transaction */
$transaction = $this->user->transactions()->find((int)$data['attachable_id']);
if (null === $transaction) {
throw new FireflyException('Unexpectedly could not find transaction'); // @codeCoverageIgnore
throw new FireflyException('Unexpectedly could not find transaction');
}
$data['attachable_id'] = $transaction->transaction_journal_id;
$model = TransactionJournal::class;

View File

@@ -76,7 +76,7 @@ class BillFactory
} catch (QueryException $e) {
Log::error($e->getMessage());
Log::error($e->getTraceAsString());
throw new FireflyException('400000: Could not store bill.');
throw new FireflyException('400000: Could not store bill.', 0, $e);
}
if (array_key_exists('notes', $data)) {

View File

@@ -76,7 +76,7 @@ class CategoryFactory
);
} catch (QueryException $e) {
Log::error($e->getMessage());
throw new FireflyException('400003: Could not store new category.');
throw new FireflyException('400003: Could not store new category.', 0, $e);
}
}

View File

@@ -69,7 +69,7 @@ class RecurrenceFactory
Log::error($message);
Log::error($e->getTraceAsString());
throw new FireflyException($message);
throw new FireflyException($message, 0, $e);
}
$firstDate = null;
$repeatUntil = null;
@@ -129,17 +129,16 @@ class RecurrenceFactory
$this->createRepetitions($recurrence, $data['repetitions'] ?? []);
try {
$this->createTransactions($recurrence, $data['transactions'] ?? []);
// @codeCoverageIgnoreStart
} catch (FireflyException $e) {
Log::error($e->getMessage());
$recurrence->forceDelete();
$message = sprintf('Could not create recurring transaction: %s', $e->getMessage());
$this->errors->add('store', $message);
throw new FireflyException($message);
throw new FireflyException($message, 0, $e);
}
// @codeCoverageIgnoreEnd
return $recurrence;
}

View File

@@ -59,7 +59,7 @@ class TransactionCurrencyFactory
} catch (QueryException $e) {
$result = null;
Log::error(sprintf('Could not create new currency: %s', $e->getMessage()));
throw new FireflyException('400004: Could not store new currency.');
throw new FireflyException('400004: Could not store new currency.', 0, $e);
}
return $result;

View File

@@ -102,17 +102,17 @@ class TransactionFactory
];
try {
$result = Transaction::create($data);
// @codeCoverageIgnoreStart
} catch (QueryException $e) {
Log::error(sprintf('Could not create transaction: %s', $e->getMessage()), $data);
Log::error($e->getMessage());
Log::error($e->getTraceAsString());
throw new FireflyException('Query exception when creating transaction.');
throw new FireflyException('Query exception when creating transaction.', 0, $e);
}
if (null === $result) {
throw new FireflyException('Transaction is NULL.');
}
// @codeCoverageIgnoreEnd
if (null !== $result) {
Log::debug(
sprintf(

View File

@@ -66,7 +66,7 @@ class TransactionGroupFactory
$collection = $this->journalFactory->create($data);
} catch (DuplicateTransactionException $e) {
Log::warning('GroupFactory::create() caught journalFactory::create() with a duplicate!');
throw new DuplicateTransactionException($e->getMessage());
throw new DuplicateTransactionException($e->getMessage(), 0, $e);
}
$title = $data['group_title'] ?? null;
$title = '' === $title ? null : $title;

View File

@@ -128,13 +128,13 @@ class TransactionJournalFactory
Log::error($e->getMessage());
Log::error($e->getTraceAsString());
$this->forceDeleteOnError($collection);
throw new DuplicateTransactionException($e->getMessage());
throw new DuplicateTransactionException($e->getMessage(), 0, $e);
} catch (FireflyException $e) {
Log::warning('TransactionJournalFactory::create() caught an exception.');
Log::error($e->getMessage());
Log::error($e->getTraceAsString());
$this->forceDeleteOnError($collection);
throw new FireflyException($e->getMessage());
throw new FireflyException($e->getMessage(), 0, $e);
}
return $collection;
@@ -240,7 +240,7 @@ class TransactionJournalFactory
Log::error($e->getMessage());
Log::error($e->getTraceAsString());
$this->forceDeleteOnError(new Collection([$journal]));
throw new FireflyException($e->getMessage());
throw new FireflyException($e->getMessage(), 0, $e);
}
// and the destination one:
@@ -261,7 +261,7 @@ class TransactionJournalFactory
Log::warning('Delete negative transaction.');
$this->forceTrDelete($negative);
$this->forceDeleteOnError(new Collection([$journal]));
throw new FireflyException($e->getMessage());
throw new FireflyException($e->getMessage(), 0, $e);
}
// verify that journal has two transactions. Otherwise, delete and cancel.
$journal->completed = true;
@@ -302,10 +302,10 @@ class TransactionJournalFactory
unset($dataRow['import_hash_v2'], $dataRow['original_source']);
$json = json_encode($dataRow, JSON_THROW_ON_ERROR, 512);
if (false === $json) {
// @codeCoverageIgnoreStart
$json = json_encode((string)microtime(), JSON_THROW_ON_ERROR, 512);
Log::error(sprintf('Could not hash the original row! %s', json_last_error_msg()), $dataRow);
// @codeCoverageIgnoreEnd
}
$hash = hash('sha256', $json);
Log::debug(sprintf('The hash is: %s', $hash), $dataRow);
@@ -358,7 +358,7 @@ class TransactionJournalFactory
// do something with result:
if (false === $validSource) {
throw new FireflyException(sprintf('Source: %s', $this->accountValidator->sourceError)); // @codeCoverageIgnore
throw new FireflyException(sprintf('Source: %s', $this->accountValidator->sourceError));
}
Log::debug('Source seems valid.');
// validate destination account
@@ -367,7 +367,7 @@ class TransactionJournalFactory
$validDestination = $this->accountValidator->validateDestination($destinationId, $destinationName, null);
// do something with result:
if (false === $validDestination) {
throw new FireflyException(sprintf('Destination: %s', $this->accountValidator->destError)); // @codeCoverageIgnore
throw new FireflyException(sprintf('Destination: %s', $this->accountValidator->destError));
}
}
@@ -485,7 +485,7 @@ class TransactionJournalFactory
{
try {
$transaction->delete();
} catch (Exception $e) {
} catch (Exception $e) { // @phpstan-ignore-line
Log::error($e->getMessage());
Log::error($e->getTraceAsString());
Log::error('Could not delete negative transaction.');

View File

@@ -49,8 +49,8 @@ class TransactionJournalMetaFactory
Log::debug('Value is empty, delete meta value.');
try {
$entry->delete();
} catch (Exception $e) { // @codeCoverageIgnore
Log::error(sprintf('Could not delete transaction journal meta: %s', $e->getMessage())); // @codeCoverageIgnore
} catch (Exception $e) { // @phpstan-ignore-line
Log::error(sprintf('Could not delete transaction journal meta: %s', $e->getMessage()));
}
return null;
@@ -67,8 +67,8 @@ class TransactionJournalMetaFactory
Log::debug('Will not store empty strings, delete meta value');
try {
$entry->delete();
} catch (Exception $e) { // @codeCoverageIgnore
Log::error(sprintf('Could not delete transaction journal meta: %s', $e->getMessage())); // @codeCoverageIgnore
} catch (Exception $e) { // @phpstan-ignore-line
Log::error(sprintf('Could not delete transaction journal meta: %s', $e->getMessage()));
}
}