Fix various code.

This commit is contained in:
James Cole
2025-05-27 17:06:15 +02:00
parent d8f512ca3a
commit 2cb14f6b72
123 changed files with 581 additions and 500 deletions

View File

@@ -65,7 +65,7 @@ class AvailableBudgetRepository implements AvailableBudgetRepositoryInterface, U
public function get(?Carbon $start = null, ?Carbon $end = null): Collection
{
$query = $this->user->availableBudgets()->with(['transactionCurrency']);
if (null !== $start && null !== $end) {
if ($start instanceof Carbon && $end instanceof Carbon) {
$query->where(
static function (Builder $q1) use ($start, $end): void {
$q1->where('start_date', '=', $start->format('Y-m-d'));
@@ -123,7 +123,7 @@ class AvailableBudgetRepository implements AvailableBudgetRepositoryInterface, U
->where('end_date', $end->format('Y-m-d'))->first()
;
if (null !== $availableBudget) {
$amount = $availableBudget->amount;
return $availableBudget->amount;
}
return $amount;
@@ -172,10 +172,10 @@ class AvailableBudgetRepository implements AvailableBudgetRepositoryInterface, U
{
$query = $this->user->availableBudgets();
if (null !== $start) {
if ($start instanceof Carbon) {
$query->where('start_date', '>=', $start->format('Y-m-d'));
}
if (null !== $end) {
if ($end instanceof Carbon) {
$query->where('end_date', '<=', $end->format('Y-m-d'));
}

View File

@@ -88,7 +88,7 @@ class BudgetLimitRepository implements BudgetLimitRepositoryInterface, UserGroup
->where('budgets.active', true)
->where('budgets.user_id', $this->user->id)
;
if (null !== $budgets && $budgets->count() > 0) {
if ($budgets instanceof Collection && $budgets->count() > 0) {
$query->whereIn('budget_limits.budget_id', $budgets->pluck('id')->toArray());
}
@@ -135,7 +135,7 @@ class BudgetLimitRepository implements BudgetLimitRepositoryInterface, UserGroup
public function getAllBudgetLimits(?Carbon $start = null, ?Carbon $end = null): Collection
{
// both are NULL:
if (null === $start && null === $end) {
if (!$start instanceof Carbon && !$end instanceof Carbon) {
return BudgetLimit::leftJoin('budgets', 'budgets.id', '=', 'budget_limits.budget_id')
->with(['budget'])
->where('budgets.user_id', $this->user->id)
@@ -144,17 +144,17 @@ class BudgetLimitRepository implements BudgetLimitRepositoryInterface, UserGroup
;
}
// one of the two is NULL.
if (null === $start xor null === $end) {
if (!$start instanceof Carbon xor !$end instanceof Carbon) {
$query = BudgetLimit::leftJoin('budgets', 'budgets.id', '=', 'budget_limits.budget_id')
->with(['budget'])
->whereNull('budgets.deleted_at')
->where('budgets.user_id', $this->user->id)
;
if (null !== $end) {
if ($end instanceof Carbon) {
// end date must be before $end.
$query->where('end_date', '<=', $end->format('Y-m-d 00:00:00'));
}
if (null !== $start) {
if ($start instanceof Carbon) {
// start date must be after $start.
$query->where('start_date', '>=', $start->format('Y-m-d 00:00:00'));
}
@@ -201,17 +201,17 @@ class BudgetLimitRepository implements BudgetLimitRepositoryInterface, UserGroup
public function getBudgetLimits(Budget $budget, ?Carbon $start = null, ?Carbon $end = null): Collection
{
if (null === $end && null === $start) {
if (!$end instanceof Carbon && !$start instanceof Carbon) {
return $budget->budgetlimits()->with(['transactionCurrency'])->orderBy('budget_limits.start_date', 'DESC')->get(['budget_limits.*']);
}
if (null === $end xor null === $start) {
if (!$end instanceof Carbon xor !$start instanceof Carbon) {
$query = $budget->budgetlimits()->with(['transactionCurrency'])->orderBy('budget_limits.start_date', 'DESC');
// one of the two is null
if (null !== $end) {
if ($end instanceof Carbon) {
// end date must be before $end.
$query->where('end_date', '<=', $end->format('Y-m-d 00:00:00'));
}
if (null !== $start) {
if ($start instanceof Carbon) {
// start date must be after $start.
$query->where('start_date', '>=', $start->format('Y-m-d 00:00:00'));
}

View File

@@ -303,16 +303,16 @@ class BudgetRepository implements BudgetRepositoryInterface, UserGroupInterface
// first things first: delete when no longer required:
$autoBudgetType = array_key_exists('auto_budget_type', $data) ? $data['auto_budget_type'] : null;
if (0 === $autoBudgetType && null !== $autoBudget) {
if (0 === $autoBudgetType && $autoBudget instanceof AutoBudget) {
// delete!
$autoBudget->delete();
return $budget;
}
if (0 === $autoBudgetType && null === $autoBudget) {
if (0 === $autoBudgetType && !$autoBudget instanceof AutoBudget) {
return $budget;
}
if (null === $autoBudgetType && null === $autoBudget) {
if (null === $autoBudgetType && !$autoBudget instanceof AutoBudget) {
return $budget;
}
$this->updateAutoBudget($budget, $data);
@@ -393,7 +393,7 @@ class BudgetRepository implements BudgetRepositoryInterface, UserGroupInterface
// grab default currency:
$currency = app('amount')->getNativeCurrencyByUserGroup($this->user->userGroup);
if (null === $autoBudget) {
if (!$autoBudget instanceof AutoBudget) {
// at this point it's a blind assumption auto_budget_type is 1 or 2.
$autoBudget = new AutoBudget();
$autoBudget->auto_budget_type = $data['auto_budget_type'];
@@ -488,14 +488,14 @@ class BudgetRepository implements BudgetRepositoryInterface, UserGroupInterface
app('log')->debug('Now in findBudget()');
app('log')->debug(sprintf('Searching for budget with ID #%d...', $budgetId));
$result = $this->find((int) $budgetId);
if (null === $result && null !== $budgetName && '' !== $budgetName) {
if (!$result instanceof Budget && null !== $budgetName && '' !== $budgetName) {
app('log')->debug(sprintf('Searching for budget with name %s...', $budgetName));
$result = $this->findByName($budgetName);
}
if (null !== $result) {
if ($result instanceof Budget) {
app('log')->debug(sprintf('Found budget #%d: %s', $result->id, $result->name));
}
app('log')->debug(sprintf('Found result is null? %s', var_export(null === $result, true)));
app('log')->debug(sprintf('Found result is null? %s', var_export(!$result instanceof Budget, true)));
return $result;
}

View File

@@ -85,10 +85,10 @@ class NoBudgetRepository implements NoBudgetRepositoryInterface, UserGroupInterf
$collector = app(GroupCollectorInterface::class);
$collector->setUser($this->user)->setRange($start, $end)->setTypes([TransactionTypeEnum::WITHDRAWAL->value]);
if (null !== $accounts && $accounts->count() > 0) {
if ($accounts instanceof Collection && $accounts->count() > 0) {
$collector->setAccounts($accounts);
}
if (null !== $currency) {
if ($currency instanceof TransactionCurrency) {
$collector->setCurrency($currency);
}
$collector->withoutBudget();

View File

@@ -126,13 +126,13 @@ class OperationsRepository implements OperationsRepositoryInterface, UserGroupIn
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
$collector->setUser($this->user)->setRange($start, $end)->setTypes([TransactionTypeEnum::WITHDRAWAL->value]);
if (null !== $accounts && $accounts->count() > 0) {
if ($accounts instanceof Collection && $accounts->count() > 0) {
$collector->setAccounts($accounts);
}
if (null !== $budgets && $budgets->count() > 0) {
if ($budgets instanceof Collection && $budgets->count() > 0) {
$collector->setBudgets($budgets);
}
if (null === $budgets || 0 === $budgets->count()) {
if (!$budgets instanceof Collection || 0 === $budgets->count()) {
$collector->setBudgets($this->getBudgets());
}
$collector->withBudgetInformation()->withAccountInformation()->withCategoryInformation();
@@ -229,13 +229,13 @@ class OperationsRepository implements OperationsRepositoryInterface, UserGroupIn
->setTypes([TransactionTypeEnum::WITHDRAWAL->value])
;
if (null !== $accounts) {
if ($accounts instanceof Collection) {
$collector->setAccounts($accounts);
}
if (null === $budgets) {
if (!$budgets instanceof Collection) {
$budgets = $this->getBudgets();
}
if (null !== $currency) {
if ($currency instanceof TransactionCurrency) {
Log::debug(sprintf('Limit to normal currency %s', $currency->code));
$collector->setNormalCurrency($currency);
}
@@ -245,7 +245,7 @@ class OperationsRepository implements OperationsRepositoryInterface, UserGroupIn
$journals = $collector->getExtractedJournals();
// same but for transactions in the foreign currency:
if (null !== $currency) {
if ($currency instanceof TransactionCurrency) {
Log::debug('STOP looking for transactions in the foreign currency.');
}
$summarizer = new TransactionSummarizer($this->user);