Code cleanup.

This commit is contained in:
James Cole
2023-12-20 19:35:52 +01:00
parent c4f6366642
commit 64ec0cf62e
997 changed files with 12908 additions and 28136 deletions

View File

@@ -32,7 +32,6 @@ use FireflyIII\Support\Calendar\Periodicity;
use Illuminate\Support\Facades\Log;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Throwable;
/**
* Class Navigation.
@@ -41,21 +40,11 @@ class Navigation
{
private Calculator $calculator;
/**
* @param Calculator|null $calculator
*/
public function __construct(Calculator $calculator = null)
{
$this->calculator = $calculator instanceof Calculator ? $calculator : new Calculator();
}
/**
* @param Carbon $theDate
* @param string $repeatFreq
* @param int $skip
*
* @return Carbon
*/
public function addPeriod(Carbon $theDate, string $repeatFreq, int $skip = 0): Carbon
{
$date = clone $theDate;
@@ -93,26 +82,20 @@ class Navigation
$repeatFreq,
implode(', ', array_keys($functionMap))
));
return $theDate;
}
return $this->nextDateByInterval($date, $functionMap[$repeatFreq], $skip);
}
/**
* @param Carbon $epoch
* @param Periodicity $periodicity
* @param int $skipInterval
*
* @return Carbon
*/
public function nextDateByInterval(Carbon $epoch, Periodicity $periodicity, int $skipInterval = 0): Carbon
{
try {
return $this->calculator->nextDateByInterval($epoch, $periodicity, $skipInterval);
} catch (IntervalException $exception) {
Log::warning($exception->getMessage(), ['exception' => $exception]);
} catch (Throwable $exception) { // @phpstan-ignore-line
} catch (\Throwable $exception) { // @phpstan-ignore-line
Log::error($exception->getMessage(), ['exception' => $exception]);
}
@@ -124,14 +107,6 @@ class Navigation
return $epoch;
}
/**
* @param Carbon $start
* @param Carbon $end
* @param string $range
*
* @return array
*
*/
public function blockPeriods(Carbon $start, Carbon $end, string $range): array
{
if ($end < $start) {
@@ -158,7 +133,7 @@ class Navigation
}
// skip to the next period:
$workStart->subDay()->startOfDay();
$loopCount++;
++$loopCount;
}
// if $workEnd is still before $start, continue on a yearly basis:
$loopCount = 0;
@@ -178,19 +153,13 @@ class Navigation
}
// skip to the next period:
$workStart->subDay()->startOfDay();
$loopCount++;
++$loopCount;
}
}
return $periods;
}
/**
* @param Carbon $theDate
* @param string $repeatFreq
*
* @return Carbon
*/
public function startOfPeriod(Carbon $theDate, string $repeatFreq): Carbon
{
$date = clone $theDate;
@@ -213,7 +182,7 @@ class Navigation
];
if (array_key_exists($repeatFreq, $functionMap)) {
$function = $functionMap[$repeatFreq];
$date->$function(); // @phpstan-ignore-line
$date->{$function}(); // @phpstan-ignore-line
return $date;
}
@@ -238,7 +207,6 @@ class Navigation
return $result;
}
if ('custom' === $repeatFreq) {
return $date; // the date is already at the start.
}
@@ -247,12 +215,6 @@ class Navigation
return $theDate;
}
/**
* @param Carbon $end
* @param string $repeatFreq
*
* @return Carbon
*/
public function endOfPeriod(Carbon $end, string $repeatFreq): Carbon
{
$currentEnd = clone $end;
@@ -293,6 +255,7 @@ class Navigation
if ('custom' === $repeatFreq) {
/** @var Carbon $tStart */
$tStart = session('start', today(config('app.timezone'))->startOfMonth());
/** @var Carbon $tEnd */
$tEnd = session('end', today(config('app.timezone'))->endOfMonth());
$diffInDays = $tStart->diffInDays($tEnd);
@@ -316,7 +279,6 @@ class Navigation
}
unset($result);
if (!array_key_exists($repeatFreq, $functionMap)) {
Log::error(sprintf('Cannot do endOfPeriod for $repeat_freq "%s"', $repeatFreq));
@@ -325,7 +287,7 @@ class Navigation
$function = $functionMap[$repeatFreq];
if (array_key_exists($repeatFreq, $modifierMap)) {
$currentEnd->$function($modifierMap[$repeatFreq]); // @phpstan-ignore-line
$currentEnd->{$function}($modifierMap[$repeatFreq]); // @phpstan-ignore-line
if (in_array($repeatFreq, $subDay, true)) {
$currentEnd->subDay();
}
@@ -333,7 +295,7 @@ class Navigation
return $currentEnd;
}
$currentEnd->$function(); // @phpstan-ignore-line
$currentEnd->{$function}(); // @phpstan-ignore-line
$currentEnd->endOfDay();
if (in_array($repeatFreq, $subDay, true)) {
$currentEnd->subDay();
@@ -342,13 +304,6 @@ class Navigation
return $currentEnd;
}
/**
* @param string $period
* @param Carbon $beginning
* @param Carbon $end
*
* @return int
*/
public function diffInPeriods(string $period, int $skip, Carbon $beginning, Carbon $end): int
{
Log::debug(sprintf(
@@ -368,12 +323,12 @@ class Navigation
];
if (!array_key_exists($period, $map)) {
Log::warning(sprintf('No diffInPeriods for period "%s"', $period));
return 1;
}
$func = $map[$period];
// first do the diff
$floatDiff = $beginning->$func($end); // @phpstan-ignore-line
$floatDiff = $beginning->{$func}($end); // @phpstan-ignore-line
// then correct for quarterly or half-year
if ('quarterly' === $period) {
@@ -404,13 +359,6 @@ class Navigation
return (int)$diff;
}
/**
* @param Carbon $theCurrentEnd
* @param string $repeatFreq
* @param Carbon|null $maxDate
*
* @return Carbon
*/
public function endOfX(Carbon $theCurrentEnd, string $repeatFreq, ?Carbon $maxDate): Carbon
{
$functionMap = [
@@ -434,7 +382,7 @@ class Navigation
if (array_key_exists($repeatFreq, $functionMap)) {
$function = $functionMap[$repeatFreq];
$currentEnd->$function(); // @phpstan-ignore-line
$currentEnd->{$function}(); // @phpstan-ignore-line
}
if (null !== $maxDate && $currentEnd > $maxDate) {
@@ -448,9 +396,6 @@ class Navigation
* Returns the user's view range and if necessary, corrects the dynamic view
* range to a normal range.
*
* @param bool $correct
*
* @return string
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
@@ -464,17 +409,22 @@ class Navigation
if (!$correct) {
return $range;
}
switch ($range) {
default:
return $range;
case 'last7':
return '1W';
case 'last30':
case 'MTD':
return '1M';
case 'last90':
case 'QTD':
return '3M';
case 'last365':
case 'YTD':
return '1Y';
@@ -482,10 +432,6 @@ class Navigation
}
/**
* @param Carbon $start
* @param Carbon $end
*
* @return array
* @throws FireflyException
*/
public function listOfPeriods(Carbon $start, Carbon $end): array
@@ -512,7 +458,7 @@ class Navigation
$formatted = $begin->format($format);
$displayed = $begin->isoFormat($displayFormat);
$entries[$formatted] = $displayed;
$begin->$increment(); // @phpstan-ignore-line
$begin->{$increment}(); // @phpstan-ignore-line
}
return $entries;
@@ -521,11 +467,6 @@ class Navigation
/**
* If the date difference between start and end is less than a month, method returns "Y-m-d". If the difference is
* less than a year, method returns "Y-m". If the date difference is larger, method returns "Y".
*
* @param Carbon $start
* @param Carbon $end
*
* @return string
*/
public function preferredCarbonFormat(Carbon $start, Carbon $end): string
{
@@ -541,12 +482,6 @@ class Navigation
return $format;
}
/**
* @param Carbon $theDate
* @param string $repeatFrequency
*
* @return string
*/
public function periodShow(Carbon $theDate, string $repeatFrequency): string
{
$date = clone $theDate;
@@ -583,16 +518,12 @@ class Navigation
/**
* Same as preferredCarbonFormat but by string
*
* @param string $period
*
* @return string
*/
public function preferredCarbonFormatByPeriod(string $period): string
{
return match ($period) {
default => 'Y-m-d',
//'1D' => 'Y-m-d',
// '1D' => 'Y-m-d',
'1W' => '\WW,Y',
'1M' => 'Y-m',
'3M', '6M' => '\QQ,Y',
@@ -604,11 +535,6 @@ class Navigation
* If the date difference between start and end is less than a month, method returns trans(config.month_and_day).
* If the difference is less than a year, method returns "config.month". If the date difference is larger, method
* returns "config.year".
*
* @param Carbon $start
* @param Carbon $end
*
* @return string
*/
public function preferredCarbonLocalizedFormat(Carbon $start, Carbon $end): string
{
@@ -628,11 +554,6 @@ class Navigation
/**
* If the date difference between start and end is less than a month, method returns "endOfDay". If the difference
* is less than a year, method returns "endOfMonth". If the date difference is larger, method returns "endOfYear".
*
* @param Carbon $start
* @param Carbon $end
*
* @return string
*/
public function preferredEndOfPeriod(Carbon $start, Carbon $end): string
{
@@ -651,11 +572,6 @@ class Navigation
/**
* If the date difference between start and end is less than a month, method returns "1D". If the difference is
* less than a year, method returns "1M". If the date difference is larger, method returns "1Y".
*
* @param Carbon $start
* @param Carbon $end
*
* @return string
*/
public function preferredRangeFormat(Carbon $start, Carbon $end): string
{
@@ -674,11 +590,6 @@ class Navigation
/**
* If the date difference between start and end is less than a month, method returns "%Y-%m-%d". If the difference
* is less than a year, method returns "%Y-%m". If the date difference is larger, method returns "%Y".
*
* @param Carbon $start
* @param Carbon $end
*
* @return string
*/
public function preferredSqlFormat(Carbon $start, Carbon $end): string
{
@@ -695,12 +606,6 @@ class Navigation
}
/**
* @param Carbon $theDate
* @param string $repeatFreq
* @param int|null $subtract
*
* @return Carbon
*
* @throws FireflyException
*/
public function subtractPeriod(Carbon $theDate, string $repeatFreq, int $subtract = null): Carbon
@@ -730,7 +635,7 @@ class Navigation
];
if (array_key_exists($repeatFreq, $functionMap)) {
$function = $functionMap[$repeatFreq];
$date->$function($subtract); // @phpstan-ignore-line
$date->{$function}($subtract); // @phpstan-ignore-line
return $date;
}
@@ -746,6 +651,7 @@ class Navigation
if ('custom' === $repeatFreq) {
/** @var Carbon $tStart */
$tStart = session('start', today(config('app.timezone'))->startOfMonth());
/** @var Carbon $tEnd */
$tEnd = session('end', today(config('app.timezone'))->endOfMonth());
$diffInDays = $tStart->diffInDays($tEnd);
@@ -753,42 +659,51 @@ class Navigation
return $date;
}
switch ($repeatFreq) {
default:
break;
case 'last7':
$date->subDays(7);
return $date;
case 'last30':
$date->subDays(30);
return $date;
case 'last90':
$date->subDays(90);
return $date;
case 'last365':
$date->subDays(365);
return $date;
case 'YTD':
$date->subYear();
return $date;
case 'QTD':
$date->subQuarter();
return $date;
case 'MTD':
$date->subMonth();
return $date;
}
throw new FireflyException(sprintf('Cannot do subtractPeriod for $repeat_freq "%s"', $repeatFreq));
}
/**
* @param string $range
* @param Carbon $start
*
* @return Carbon
*
* @throws FireflyException
*/
public function updateEndDate(string $range, Carbon $start): Carbon
@@ -805,7 +720,7 @@ class Navigation
if (array_key_exists($range, $functionMap)) {
$function = $functionMap[$range];
$end->$function(); // @phpstan-ignore-line
$end->{$function}(); // @phpstan-ignore-line
return $end;
}
@@ -840,6 +755,7 @@ class Navigation
$end = today(config('app.timezone'));
$end->endOfDay();
Log::debug(sprintf('updateEndDate returns "%s"', $end->format('Y-m-d')));
return $end;
}
@@ -847,11 +763,6 @@ class Navigation
}
/**
* @param string $range
* @param Carbon $start
*
* @return Carbon
*
* @throws FireflyException
*/
public function updateStartDate(string $range, Carbon $start): Carbon
@@ -866,7 +777,7 @@ class Navigation
];
if (array_key_exists($range, $functionMap)) {
$function = $functionMap[$range];
$start->$function(); // @phpstan-ignore-line
$start->{$function}(); // @phpstan-ignore-line
return $start;
}
@@ -888,31 +799,47 @@ class Navigation
return $fiscalHelper->startOfFiscalYear($start);
}
switch ($range) {
default:
break;
case 'last7':
$start->subDays(7);
return $start;
case 'last30':
$start->subDays(30);
return $start;
case 'last90':
$start->subDays(90);
return $start;
case 'last365':
$start->subDays(365);
return $start;
case 'YTD':
$start->startOfYear();
return $start;
case 'QTD':
$start->startOfQuarter();
return $start;
case 'MTD':
$start->startOfMonth();
return $start;
}
throw new FireflyException(sprintf('updateStartDate cannot handle range "%s"', $range));
}
}