Code cleanup

This commit is contained in:
James Cole 2023-11-03 05:52:35 +01:00
parent dedc1e8131
commit 101bcc250e
No known key found for this signature in database
GPG Key ID: B49A324B7EAD6D80
4 changed files with 29 additions and 25 deletions

View File

@ -29,10 +29,10 @@ use FireflyIII\Exceptions\IntervalException;
use FireflyIII\Helpers\Fiscal\FiscalHelperInterface;
use FireflyIII\Support\Calendar\Calculator;
use FireflyIII\Support\Calendar\Periodicity;
use Illuminate\Support\Facades\Log;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Throwable;
use Illuminate\Support\Facades\Log;
/**
* Class Navigation.
@ -89,10 +89,10 @@ class Navigation
if (!array_key_exists($repeatFreq, $functionMap)) {
Log::error(sprintf(
'The periodicity %s is unknown. Choose one of available periodicity: %s',
$repeatFreq,
join(', ', array_keys($functionMap))
));
'The periodicity %s is unknown. Choose one of available periodicity: %s',
$repeatFreq,
join(', ', array_keys($functionMap))
));
return $theDate;
}
@ -352,12 +352,12 @@ class Navigation
public function diffInPeriods(string $period, int $skip, Carbon $beginning, Carbon $end): int
{
Log::debug(sprintf(
'diffInPeriods: %s (skip: %d), between %s and %s.',
$period,
$skip,
$beginning->format('Y-m-d'),
$end->format('Y-m-d')
));
'diffInPeriods: %s (skip: %d), between %s and %s.',
$period,
$skip,
$beginning->format('Y-m-d'),
$end->format('Y-m-d')
));
$map = [
'daily' => 'floatDiffInDays',
'weekly' => 'floatDiffInWeeks',
@ -372,32 +372,33 @@ class Navigation
}
$func = $map[$period];
// first do the diff
$diff = $beginning->$func($end);
$floatDiff = $beginning->$func($end);
// then correct for quarterly or half-year
if ('quarterly' === $period) {
Log::debug(sprintf('Q: Corrected %f to %f', $diff, $diff / 3));
$diff = $diff / 3;
Log::debug(sprintf('Q: Corrected %f to %f', $floatDiff, $floatDiff / 3));
$floatDiff = $floatDiff / 3;
}
if ('half-year' === $period) {
Log::debug(sprintf('H: Corrected %f to %f', $diff, $diff / 6));
$diff = $diff / 6;
Log::debug(sprintf('H: Corrected %f to %f', $floatDiff, $floatDiff / 6));
$floatDiff = $floatDiff / 6;
}
// then do ceil()
$diff = ceil($diff);
$diff = ceil($floatDiff);
Log::debug(sprintf('Diff is %f (%d)', $beginning->$func($end), $diff));
Log::debug(sprintf('Diff is %f periods (%d rounded up)', $floatDiff, $diff));
if ($skip > 0) {
$parameter = $skip + 1;
$diff = ceil($diff / $parameter) * $parameter;
Log::debug(sprintf(
'diffInPeriods: skip is %d, so param is %d, and diff becomes %d',
$skip,
$parameter,
$diff
));
'diffInPeriods: skip is %d, so param is %d, and diff becomes %d',
$skip,
$parameter,
$diff
));
}
return (int)$diff;

View File

@ -360,7 +360,7 @@ class BillTransformer extends AbstractTransformer
app('log')->debug(sprintf('Steps is %d, because addPeriod already adds 1.', $steps));
$result = app('navigation')->addPeriod($start, $bill->repeat_freq, $steps);
}
app('log')->debug(sprintf('Number of steps is %d, result is %s', $steps, $result->format('Y-m-d')));
app('log')->debug(sprintf('Number of steps is %d, added to %s, result is %s', $steps, $start->format('Y-m-d'), $result->format('Y-m-d')));
return $result;
}
}

View File

@ -38,7 +38,7 @@ class NavigationCustomEndOfPeriodTest extends TestCase
public function testGivenADateAndCustomFrequencyWhenCalculateTheDateThenReturnsTheEndOfMonthSuccessful()
{
$from = Carbon::parse('2023-08-05');
$expected = Carbon::parse('2023-09-04');
$expected = Carbon::parse('2023-09-03');
$navigation = new Navigation();
$period = $navigation->endOfPeriod($from, 'custom');

View File

@ -138,6 +138,9 @@ class NavigationAddPeriodTest extends TestCase
'3M' => ['skip' => 1, 'frequency' => '3M', 'from' => Carbon::now(), 'expected' => Carbon::now()->addMonthsNoOverflow(6)],
'quarter' => ['skip' => 1, 'frequency' => 'quarter', 'from' => Carbon::now(), 'expected' => Carbon::now()->addMonthsNoOverflow(6)],
'quarterly' => ['skip' => 1, 'frequency' => 'quarterly', 'from' => Carbon::now(), 'expected' => Carbon::now()->addMonthsNoOverflow(6)],
'quarter_2' => ['skip' => 2, 'frequency' => 'quarter', 'from' => Carbon::now(), 'expected' => Carbon::now()->addMonthsNoOverflow(9)],
'quarterly_2' => ['skip' => 2, 'frequency' => 'quarterly', 'from' => Carbon::now(), 'expected' => Carbon::now()->addMonthsNoOverflow(9)],
'quarter_3' => ['skip' => 2, 'frequency' => 'quarter', 'from' => Carbon::parse('2023-01-01'), 'expected' => Carbon::parse('2023-10-01')],
'6M' => ['skip' => 1, 'frequency' => '6M', 'from' => Carbon::now(), 'expected' => Carbon::now()->addMonthsNoOverflow(12)],
'half-year' => ['skip' => 1, 'frequency' => 'half-year', 'from' => Carbon::now(), 'expected' => Carbon::now()->addMonthsNoOverflow(12)],
'year' => ['skip' => 1, 'frequency' => 'year', 'from' => Carbon::now(), 'expected' => Carbon::now()->addYears(2)],