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

@@ -55,7 +55,7 @@ class CreateAutoBudgetLimits implements ShouldQueue
*/
public function __construct(?Carbon $date)
{
if (null !== $date) {
if ($date instanceof Carbon) {
$newDate = clone $date;
$newDate->startOfDay();
$this->date = $newDate;
@@ -127,7 +127,7 @@ class CreateAutoBudgetLimits implements ShouldQueue
// find budget limit:
$budgetLimit = $this->findBudgetLimit($autoBudget->budget, $start, $end);
if (null === $budgetLimit && AutoBudgetType::AUTO_BUDGET_RESET->value === (int) $autoBudget->auto_budget_type) {
if (!$budgetLimit instanceof BudgetLimit && AutoBudgetType::AUTO_BUDGET_RESET->value === (int) $autoBudget->auto_budget_type) {
// that's easy: create one.
// do nothing else.
$this->createBudgetLimit($autoBudget, $start, $end);
@@ -136,14 +136,14 @@ class CreateAutoBudgetLimits implements ShouldQueue
return;
}
if (null === $budgetLimit && AutoBudgetType::AUTO_BUDGET_ROLLOVER->value === (int) $autoBudget->auto_budget_type) {
if (!$budgetLimit instanceof BudgetLimit && AutoBudgetType::AUTO_BUDGET_ROLLOVER->value === (int) $autoBudget->auto_budget_type) {
// budget limit exists already,
$this->createRollover($autoBudget);
app('log')->debug(sprintf('Done with auto budget #%d', $autoBudget->id));
return;
}
if (null === $budgetLimit && AutoBudgetType::AUTO_BUDGET_ADJUSTED->value === (int) $autoBudget->auto_budget_type) {
if (!$budgetLimit instanceof BudgetLimit && AutoBudgetType::AUTO_BUDGET_ADJUSTED->value === (int) $autoBudget->auto_budget_type) {
// budget limit exists already,
$this->createAdjustedLimit($autoBudget);
app('log')->debug(sprintf('Done with auto budget #%d', $autoBudget->id));
@@ -256,7 +256,7 @@ class CreateAutoBudgetLimits implements ShouldQueue
// has budget limit in previous period?
$budgetLimit = $this->findBudgetLimit($autoBudget->budget, $previousStart, $previousEnd);
if (null === $budgetLimit) {
if (!$budgetLimit instanceof BudgetLimit) {
app('log')->debug('No budget limit exists in previous period, so create one.');
// if not, create it and we're done.
$this->createBudgetLimit($autoBudget, $start, $end);
@@ -316,7 +316,7 @@ class CreateAutoBudgetLimits implements ShouldQueue
// has budget limit in previous period?
$budgetLimit = $this->findBudgetLimit($autoBudget->budget, $previousStart, $previousEnd);
if (null === $budgetLimit) {
if (!$budgetLimit instanceof BudgetLimit) {
app('log')->debug('No budget limit exists in previous period, so create one.');
// if not, create standard amount, and we're done.
$this->createBudgetLimit($autoBudget, $start, $end);

View File

@@ -74,7 +74,7 @@ class CreateRecurringTransactions implements ShouldQueue
$newDate->startOfDay();
$this->date = $newDate;
if (null !== $date) {
if ($date instanceof Carbon) {
$newDate = clone $date;
$newDate->startOfDay();
$this->date = $newDate;
@@ -257,7 +257,7 @@ class CreateRecurringTransactions implements ShouldQueue
{
$startDate = clone $recurrence->first_date;
if (null !== $recurrence->latest_date && $recurrence->latest_date->gte($startDate)) {
$startDate = clone $recurrence->latest_date;
return clone $recurrence->latest_date;
}
return $startDate;
@@ -321,7 +321,7 @@ class CreateRecurringTransactions implements ShouldQueue
/** @var Carbon $date */
foreach ($occurrences as $date) {
$result = $this->handleOccurrence($recurrence, $repetition, $date);
if (null !== $result) {
if ($result instanceof TransactionGroup) {
$collection->push($result);
}
}

View File

@@ -24,6 +24,7 @@ declare(strict_types=1);
namespace FireflyIII\Jobs;
use FireflyIII\Models\CurrencyExchangeRate;
use Carbon\Carbon;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
@@ -39,6 +40,8 @@ use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Collection;
use function Safe\json_decode;
/**
* Class DownloadExchangeRates
*/
@@ -67,7 +70,7 @@ class DownloadExchangeRates implements ShouldQueue
$userRepository = app(UserRepositoryInterface::class);
$this->users = $userRepository->all();
if (null !== $date) {
if ($date instanceof Carbon) {
$newDate = clone $date;
$newDate->startOfDay();
$this->date = $newDate;
@@ -113,14 +116,14 @@ class DownloadExchangeRates implements ShouldQueue
return;
}
$body = (string) $res->getBody();
$json = \Safe\json_decode($body, true);
$json = json_decode($body, true);
if (false === $json || null === $json) {
app('log')->warning(sprintf('Trying to grab "%s" resulted in bad JSON.', $url));
return;
}
$date = Carbon::createFromFormat('Y-m-d', $json['date'], config('app.timezone'));
if (null === $date) {
if (!$date instanceof Carbon) {
return;
}
$this->saveRates($currency, $date, $json['rates']);
@@ -130,7 +133,7 @@ class DownloadExchangeRates implements ShouldQueue
{
foreach ($rates as $code => $rate) {
$to = $this->getCurrency($code);
if (null === $to) {
if (!$to instanceof TransactionCurrency) {
app('log')->debug(sprintf('Currency %s is not in use, do not save rate.', $code));
continue;
@@ -150,7 +153,7 @@ class DownloadExchangeRates implements ShouldQueue
}
// find it in the database.
$currency = $this->repository->findByCode($code);
if (null === $currency) {
if (!$currency instanceof TransactionCurrency) {
app('log')->debug(sprintf('Did not find currency %s.', $code));
$this->active[$code] = null;
@@ -173,7 +176,7 @@ class DownloadExchangeRates implements ShouldQueue
foreach ($this->users as $user) {
$this->repository->setUser($user);
$existing = $this->repository->getExchangeRate($from, $to, $date);
if (null === $existing) {
if (!$existing instanceof CurrencyExchangeRate) {
app('log')->debug(sprintf('Saved rate from %s to %s for user #%d.', $from->code, $to->code, $user->id));
$this->repository->setExchangeRate($from, $to, $date, $rate);
}

View File

@@ -23,6 +23,7 @@ declare(strict_types=1);
namespace FireflyIII\Jobs;
use Carbon\Carbon;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Message;
use Illuminate\Queue\InteractsWithQueue;
@@ -32,6 +33,11 @@ use Illuminate\Support\Facades\Mail;
use Symfony\Component\Mailer\Exception\TransportException;
use Exception;
use function Safe\json_encode;
use function Safe\file_put_contents;
use function Safe\json_decode;
use function Safe\file_get_contents;
/**
* Class MailError.
*/
@@ -48,7 +54,7 @@ class MailError extends Job implements ShouldQueue
$debug = $this->exception;
unset($debug['stackTrace'], $debug['headers']);
app('log')->error(sprintf('Exception is: %s', \Safe\json_encode($debug)));
app('log')->error(sprintf('Exception is: %s', json_encode($debug)));
}
/**
@@ -119,11 +125,11 @@ class MailError extends Job implements ShouldQueue
if (!file_exists($file)) {
Log::debug(sprintf('Wrote new file in "%s"', $file));
\Safe\file_put_contents($file, \Safe\json_encode($limits, JSON_PRETTY_PRINT));
file_put_contents($file, json_encode($limits, JSON_PRETTY_PRINT));
}
if (file_exists($file)) {
Log::debug(sprintf('Read file in "%s"', $file));
$limits = \Safe\json_decode((string) \Safe\file_get_contents($file), true);
$limits = json_decode((string) file_get_contents($file), true);
}
// limit reached?
foreach ($types as $type => $info) {
@@ -131,15 +137,15 @@ class MailError extends Job implements ShouldQueue
if (!array_key_exists($type, $limits)) {
Log::debug(sprintf('Limit "%s" reset to zero, did not exist yet.', $type));
$limits[$type] = [
'time' => time(),
'time' => Carbon::now()->getTimestamp(),
'sent' => 0,
];
}
if (time() - $limits[$type]['time'] > $info['reset']) {
Log::debug(sprintf('Time past for this limit is %d seconds, exceeding %d seconds. Reset to zero.', time() - $limits[$type]['time'], $info['reset']));
if (Carbon::now()->getTimestamp() - $limits[$type]['time'] > $info['reset']) {
Log::debug(sprintf('Time past for this limit is %d seconds, exceeding %d seconds. Reset to zero.', Carbon::now()->getTimestamp() - $limits[$type]['time'], $info['reset']));
$limits[$type] = [
'time' => time(),
'time' => Carbon::now()->getTimestamp(),
'sent' => 0,
];
}
@@ -151,7 +157,7 @@ class MailError extends Job implements ShouldQueue
}
++$limits[$type]['sent'];
}
\Safe\file_put_contents($file, \Safe\json_encode($limits, JSON_PRETTY_PRINT));
file_put_contents($file, json_encode($limits, JSON_PRETTY_PRINT));
Log::debug('No limits reached, return FALSE.');
return false;

View File

@@ -55,7 +55,7 @@ class WarnAboutBills implements ShouldQueue
$newDate->startOfDay();
$this->date = $newDate;
if (null !== $date) {
if ($date instanceof Carbon) {
$newDate = clone $date;
$newDate->startOfDay();
$this->date = $newDate;