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

View File

@ -38,7 +38,7 @@ class NavigationCustomEndOfPeriodTest extends TestCase
public function testGivenADateAndCustomFrequencyWhenCalculateTheDateThenReturnsTheEndOfMonthSuccessful() public function testGivenADateAndCustomFrequencyWhenCalculateTheDateThenReturnsTheEndOfMonthSuccessful()
{ {
$from = Carbon::parse('2023-08-05'); $from = Carbon::parse('2023-08-05');
$expected = Carbon::parse('2023-09-04'); $expected = Carbon::parse('2023-09-03');
$navigation = new Navigation(); $navigation = new Navigation();
$period = $navigation->endOfPeriod($from, 'custom'); $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)], '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)], '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)], '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)], '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)], '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)], 'year' => ['skip' => 1, 'frequency' => 'year', 'from' => Carbon::now(), 'expected' => Carbon::now()->addYears(2)],