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

@@ -55,8 +55,8 @@ class AvailableBudgetRepository implements AvailableBudgetRepositoryInterface
{
try {
$availableBudget->delete();
} catch (Exception $e) {
Log::error(sprintf('Could not delete available budget: %s', $e->getMessage()));
} catch (Exception $e) { // @phpstan-ignore-line
// @ignoreException
}
}
@@ -262,7 +262,7 @@ class AvailableBudgetRepository implements AvailableBudgetRepositoryInterface
*/
public function update(AvailableBudget $availableBudget, array $data): AvailableBudget
{
if (isset($data['amount'])) {
if (array_key_exists('amount', $data)) {
$availableBudget->amount = $data['amount'];
}
$availableBudget->save();

View File

@@ -124,8 +124,8 @@ class BudgetLimitRepository implements BudgetLimitRepositoryInterface
{
try {
$budgetLimit->delete();
} catch (Exception $e) {
Log::info(sprintf('Could not delete budget limit: %s', $e->getMessage()));
} catch (Exception $e) { // @phpstan-ignore-line
// @ignoreException
}
}
@@ -320,25 +320,25 @@ class BudgetLimitRepository implements BudgetLimitRepositoryInterface
// find the budget:
$budget = $this->user->budgets()->find((int)$data['budget_id']);
if (null === $budget) {
throw new FireflyException('200004: Budget does not exist.'); // @codeCoverageIgnore
throw new FireflyException('200004: Budget does not exist.');
}
// find limit with same date range and currency.
$limit = $budget->budgetlimits()
->where('budget_limits.start_date', $data['start_date']->format('Y-m-d 00:00:00'))
->where('budget_limits.end_date', $data['end_date']->format('Y-m-d 23:59:59'))
->where('budget_limits.start_date', $data['start_date']->format('Y-m-d'))
->where('budget_limits.end_date', $data['end_date']->format('Y-m-d'))
->where('budget_limits.transaction_currency_id', $currency->id)
->get(['budget_limits.*'])->first();
->first(['budget_limits.*']);
if (null !== $limit) {
throw new FireflyException('200027: Budget limit already exists.'); // @codeCoverageIgnore
throw new FireflyException('200027: Budget limit already exists.');
}
Log::debug('No existing budget limit, create a new one');
// or create one and return it.
$limit = new BudgetLimit;
$limit->budget()->associate($budget);
$limit->start_date = $data['start_date']->format('Y-m-d 00:00:00');
$limit->end_date = $data['end_date']->format('Y-m-d 23:59:59');
$limit->start_date = $data['start_date']->format('Y-m-d');
$limit->end_date = $data['end_date']->format('Y-m-d');
$limit->amount = $data['amount'];
$limit->transaction_currency_id = $currency->id;
$limit->save();
@@ -397,7 +397,7 @@ class BudgetLimitRepository implements BudgetLimitRepositoryInterface
$limits = $budget->budgetlimits()
->where('budget_limits.start_date', $start->format('Y-m-d 00:00:00'))
->where('budget_limits.end_date', $end->format('Y-m-d 00:00:00'))
->get(['budget_limits.*'])->count();
->count(['budget_limits.*']);
Log::debug(sprintf('Found %d budget limits.', $limits));
// there might be a budget limit for these dates:
@@ -423,8 +423,8 @@ class BudgetLimitRepository implements BudgetLimitRepositoryInterface
Log::debug(sprintf('%s is zero, delete budget limit #%d', $amount, $limit->id));
try {
$limit->delete();
} catch (Exception $e) {
Log::debug(sprintf('Could not delete limit: %s', $e->getMessage()));
} catch (Exception $e) { // @phpstan-ignore-line
// @ignoreException
}
return null;
}

View File

@@ -58,8 +58,8 @@ class BudgetRepository implements BudgetRepositoryInterface
// delete limits with amount 0:
try {
BudgetLimit::where('amount', 0)->delete();
} catch (Exception $e) {
Log::debug(sprintf('Could not delete budget limit: %s', $e->getMessage()));
} catch (Exception $e) { // @phpstan-ignore-line
// @ignoreException
}
$budgets = $this->getActiveBudgets();
/**
@@ -326,7 +326,7 @@ class BudgetRepository implements BudgetRepositoryInterface
} catch (QueryException $e) {
Log::error($e->getMessage());
Log::error($e->getTraceAsString());
throw new FireflyException('400002: Could not store budget.');
throw new FireflyException('400002: Could not store budget.', 0, $e);
}
if (!array_key_exists('auto_budget_type', $data)) {
return $newBudget;

View File

@@ -75,7 +75,7 @@ class NoBudgetRepository implements NoBudgetRepositoryInterface
];
$date = $journal['date']->format($carbonFormat);
if (!isset($data[$currencyId]['entries'][$date])) {
if (!array_key_exists($date, $data[$currencyId]['entries'])) {
$data[$currencyId]['entries'][$date] = '0';
}
$data[$currencyId]['entries'][$date] = bcadd($data[$currencyId]['entries'][$date], $journal['amount']);
@@ -118,7 +118,7 @@ class NoBudgetRepository implements NoBudgetRepositoryInterface
/** @var array $journal */
foreach ($journals as $journal) {
$code = $journal['currency_code'];
if (!isset($currencies[$code])) {
if (!array_key_exists($code, $currencies)) {
$currencies[$code] = [
'id' => $journal['currency_id'],
'name' => $journal['currency_name'],
@@ -126,7 +126,7 @@ class NoBudgetRepository implements NoBudgetRepositoryInterface
'decimal_places' => $journal['currency_decimal_places'],
];
}
$total[$code] = isset($total[$code]) ? bcadd($total[$code], $journal['amount']) : $journal['amount'];
$total[$code] = array_key_exists($code, $total) ? bcadd($total[$code], $journal['amount']) : $journal['amount'];
}
foreach ($total as $code => $spent) {
/** @var TransactionCurrency $currency */

View File

@@ -237,7 +237,7 @@ class OperationsRepository implements OperationsRepositoryInterface
/** @var array $transaction */
foreach ($group['transactions'] as $transaction) {
$code = $transaction['currency_code'];
if (!isset($currencies[$code])) {
if (!array_key_exists($code, $currencies)) {
$currencies[$code] = [
'id' => $transaction['currency_id'],
'decimal_places' => $transaction['currency_decimal_places'],
@@ -246,7 +246,7 @@ class OperationsRepository implements OperationsRepositoryInterface
'symbol' => $transaction['currency_symbol'],
];
}
$total[$code] = isset($total[$code]) ? bcadd($total[$code], $transaction['amount']) : $transaction['amount'];
$total[$code] = array_key_exists($code, $total) ? bcadd($total[$code], $transaction['amount']) : $transaction['amount'];
}
}
/**