From 61bae63e512713e50be3990cf2c5fa9e65c6a830 Mon Sep 17 00:00:00 2001 From: Jeremy Nikolic Date: Thu, 2 Dec 2021 09:27:40 +0100 Subject: [PATCH] Fix code style --- composer.json | 2 +- src/Calendars/YasumiUkCalendar.php | 10 +- src/UkTaxAllowanceCalculator.php | 22 +-- tests/Calendars/YasumiUkCalendarTest.php | 12 +- tests/TestCase.php | 3 +- tests/UkTaxAllowanceCalculatorTest.php | 200 +++++++++++------------ 6 files changed, 125 insertions(+), 124 deletions(-) diff --git a/composer.json b/composer.json index e8148df..ebe265c 100644 --- a/composer.json +++ b/composer.json @@ -31,6 +31,6 @@ "phpcbf": "vendor/bin/phpcbf", "rector-ci": "vendor/bin/rector process --dry-run --ansi", "rector": "vendor/bin/rector process --ansi", - "pest:test": "pest --colors=always --stop-on-failure" + "pest:test": "pest --colors=always" } } diff --git a/src/Calendars/YasumiUkCalendar.php b/src/Calendars/YasumiUkCalendar.php index 9425d84..5b1d0eb 100644 --- a/src/Calendars/YasumiUkCalendar.php +++ b/src/Calendars/YasumiUkCalendar.php @@ -16,21 +16,21 @@ class YasumiUkCalendar implements UkCalendar public function isWeekendDay(Carbon $date): bool { return $this->getCalendar($date) - ->isWeekendDay($date); + ->isWeekendDay($date); } public function isHoliday(Carbon $date): bool { return $this->getCalendar($date) - ->isHoliday($date); + ->isHoliday($date); } public function isWorkingDay(Carbon $date): bool { $ukCalendar = $this->getCalendar($date); - return ! $ukCalendar->isWeekendDay($date) - && ! $ukCalendar->isHoliday($date); + return !$ukCalendar->isWeekendDay($date) + && !$ukCalendar->isHoliday($date); } public function closestFuturWorkingDay(Carbon $date): Carbon @@ -47,7 +47,7 @@ protected function closestWorkingDay(Carbon $date, int $direction): Carbon { $workingDay = $date->copy(); - while (! $this->isWorkingDay($workingDay)) { + while (!$this->isWorkingDay($workingDay)) { $workingDay->addDays($direction); } diff --git a/src/UkTaxAllowanceCalculator.php b/src/UkTaxAllowanceCalculator.php index 8cd8bf3..9f29744 100644 --- a/src/UkTaxAllowanceCalculator.php +++ b/src/UkTaxAllowanceCalculator.php @@ -24,14 +24,14 @@ public function weeklyEndDatesBetween(Carbon $start, Carbon $end): Collection { $insideDates = collect( CarbonInterval::week() - ->toPeriod($start, $end) + ->toPeriod($start, $end) ) ->map(fn(Carbon $date) => $date->endOfWeek()) ->reject(fn(Carbon $date) => $date->isAfter($end)); $sundaysHovered = CarbonInterval::day() - ->toPeriod($start, $end, CarbonPeriod::EXCLUDE_END_DATE) - ->filter(fn(Carbon $carbon) => $carbon->isSunday()); + ->toPeriod($start, $end, CarbonPeriod::EXCLUDE_END_DATE) + ->filter(fn(Carbon $carbon) => $carbon->isSunday()); if ($sundaysHovered->count() >= $insideDates->count()) { $insideDates->push($end->copy()); @@ -51,15 +51,15 @@ public function monthlyEndDatesBetween(Carbon $start, Carbon $end): Collection $dateEnd = $this->calendar->closestPastWorkingDay($end); if ($dateStart->isSameMonth($dateEnd)) { - return collect([ $dateEnd->copy() ]); + return collect([$dateEnd->copy()]); } $months = collect( CarbonInterval::day() - ->toPeriod($dateStart, $dateEnd) - ->filter( - fn(Carbon $day) => $day->isSameDay($this->lastWorkingDayOfTheMonth($day)) - ) + ->toPeriod($dateStart, $dateEnd) + ->filter( + fn(Carbon $day) => $day->isSameDay($this->lastWorkingDayOfTheMonth($day)) + ) ); if ($dateEnd->isBefore($this->lastWorkingDayOfTheMonth($dateEnd))) { @@ -78,7 +78,7 @@ public function monthly(Carbon $start, Carbon $end): int { // This counts the last months working days included in the period return $this->monthlyEndDatesBetween($start, $end) - ->count(); + ->count(); } public function lastWorkingDayOfTheMonth(Carbon $day): Carbon @@ -91,7 +91,7 @@ public function lastWorkingDayOfTheMonth(Carbon $day): Carbon protected function isWorkingDay(Carbon $carbon): bool { - return ! $this->calendar->isWeekendDay($carbon) - && ! $this->calendar->isHoliday($carbon); + return !$this->calendar->isWeekendDay($carbon) + && !$this->calendar->isHoliday($carbon); } } diff --git a/tests/Calendars/YasumiUkCalendarTest.php b/tests/Calendars/YasumiUkCalendarTest.php index 4ae0550..a174031 100644 --- a/tests/Calendars/YasumiUkCalendarTest.php +++ b/tests/Calendars/YasumiUkCalendarTest.php @@ -15,9 +15,9 @@ expect($closestPastWorkingDay->toDateString())->toEqual($expectedClosestPastWorkingDay); })->with([ - [ '2021-04-02', '2021-04-01' ], - [ '2021-05-29', '2021-05-28' ], - ]); + ['2021-04-02', '2021-04-01'], + ['2021-05-29', '2021-05-28'], +]); it('returns correct closest future working day', function ($day, $expectedClosestFutureWorkingDay) { $day = Carbon::createFromFormat('Y-m-d', $day); @@ -25,6 +25,6 @@ expect($closestFutureWorkingDay->toDateString())->toEqual($expectedClosestFutureWorkingDay); })->with([ - [ '2021-04-02', '2021-04-06' ], - [ '2021-05-29', '2021-06-01' ], - ]); + ['2021-04-02', '2021-04-06'], + ['2021-05-29', '2021-06-01'], +]); diff --git a/tests/TestCase.php b/tests/TestCase.php index 3c9be1f..e9cf615 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -7,7 +7,8 @@ class TestCase extends OrchestraTestCase { - protected function getPackageProviders($app): array { + protected function getPackageProviders($app): array + { return [UkTaxAllowanceServiceProvider::class]; } } diff --git a/tests/UkTaxAllowanceCalculatorTest.php b/tests/UkTaxAllowanceCalculatorTest.php index 92a7f78..3167a10 100644 --- a/tests/UkTaxAllowanceCalculatorTest.php +++ b/tests/UkTaxAllowanceCalculatorTest.php @@ -10,9 +10,9 @@ it('returns valid week end dates', function ($start, $end, $endDates) { $start = Carbon::createFromFormat('Y-m-d', $start) - ->startOfDay(); + ->startOfDay(); $end = Carbon::createFromFormat('Y-m-d', $end) - ->startOfDay(); + ->startOfDay(); $weeklyEndDates = collect($this->taxAllowanceCalculator->weeklyEndDatesBetween($start, $end)) ->map(fn(Carbon $carbon) => $carbon->toDateString()) @@ -20,123 +20,123 @@ expect($weeklyEndDates)->toEqual($endDates); })->with([ - // Start | End | Expected list of end dates - [ '2021-04-01', '2021-04-03', [ '2021-04-03' ] ], - // less than a week - [ '2021-04-03', '2021-04-06', [ '2021-04-04', '2021-04-06' ] ], - // less than a week over two weeks - [ '2021-04-05', '2021-04-05', [ '2021-04-05' ] ], - // one monday - [ '2021-04-04', '2021-04-04', [ '2021-04-04' ] ], - // one sunday - [ '2021-04-01', '2021-04-04', [ '2021-04-04' ] ], - // less than a week ending on a sunday - [ '2021-04-05', '2021-04-10', [ '2021-04-10' ] ], - // one perfect week - [ '2021-04-01', '2021-04-07', [ '2021-04-04', '2021-04-07' ] ], - // 7 days over two weeks - [ '2021-04-05', '2021-04-25', [ '2021-04-11', '2021-04-18', '2021-04-25' ] ], - // 3 perfect weeks - [ '2021-04-06', '2021-04-27', [ '2021-04-11', '2021-04-18', '2021-04-25', '2021-04-27' ] ], - // 21 days over 4 weeks - [ '2021-04-04', '2021-04-27', [ '2021-04-04', '2021-04-11', '2021-04-18', '2021-04-25', '2021-04-27' ] ], - // 23 days over 5 weeks and starting on a sunday (why not ?) - [ - '2021-04-01', - '2021-05-31', - [ - '2021-04-04', - '2021-04-11', - '2021-04-18', - '2021-04-25', - '2021-05-02', - '2021-05-09', - '2021-05-16', - '2021-05-23', - '2021-05-30', - '2021-05-31', - ], - ], - // Full April to May - ]); + // Start | End | Expected list of end dates + ['2021-04-01', '2021-04-03', ['2021-04-03']], + // less than a week + ['2021-04-03', '2021-04-06', ['2021-04-04', '2021-04-06']], + // less than a week over two weeks + ['2021-04-05', '2021-04-05', ['2021-04-05']], + // one monday + ['2021-04-04', '2021-04-04', ['2021-04-04']], + // one sunday + ['2021-04-01', '2021-04-04', ['2021-04-04']], + // less than a week ending on a sunday + ['2021-04-05', '2021-04-10', ['2021-04-10']], + // one perfect week + ['2021-04-01', '2021-04-07', ['2021-04-04', '2021-04-07']], + // 7 days over two weeks + ['2021-04-05', '2021-04-25', ['2021-04-11', '2021-04-18', '2021-04-25']], + // 3 perfect weeks + ['2021-04-06', '2021-04-27', ['2021-04-11', '2021-04-18', '2021-04-25', '2021-04-27']], + // 21 days over 4 weeks + ['2021-04-04', '2021-04-27', ['2021-04-04', '2021-04-11', '2021-04-18', '2021-04-25', '2021-04-27']], + // 23 days over 5 weeks and starting on a sunday (why not ?) + [ + '2021-04-01', + '2021-05-31', + [ + '2021-04-04', + '2021-04-11', + '2021-04-18', + '2021-04-25', + '2021-05-02', + '2021-05-09', + '2021-05-16', + '2021-05-23', + '2021-05-30', + '2021-05-31', + ], + ], + // Full April to May +]); it('returns valid weekly allowance intervals', function ($start, $end, $allowance) { $start = Carbon::createFromFormat('Y-m-d', $start) - ->startOfDay(); + ->startOfDay(); $end = Carbon::createFromFormat('Y-m-d', $end) - ->startOfDay(); + ->startOfDay(); expect($this->taxAllowanceCalculator->weekly($start, $end))->toEqual($allowance); })->with([ - // Start date | End date | Expected allowance count - [ '2021-04-01', '2021-04-03', 1 ], // less than a week - [ '2021-04-03', '2021-04-06', 2 ], // less than a week over two weeks - [ '2021-04-05', '2021-04-05', 1 ], // one monday - [ '2021-04-04', '2021-04-04', 1 ], // one sunday - [ '2021-04-01', '2021-04-04', 1 ], // less than a week ending on a sunday - [ '2021-04-05', '2021-04-10', 1 ], // one perfect week - [ '2021-04-01', '2021-04-07', 2 ], // 7 days over two weeks - [ '2021-04-05', '2021-04-25', 3 ], // 3 perfect weeks - [ '2021-04-06', '2021-04-27', 4 ], // 21 days over 4 weeks - [ '2021-04-04', '2021-04-27', 5 ], // 23 days over 5 weeks and starting on a sunday (why not ?) - [ '2021-04-01', '2021-05-31', 10 ], // Full April to May - ]); + // Start date | End date | Expected allowance count + ['2021-04-01', '2021-04-03', 1], // less than a week + ['2021-04-03', '2021-04-06', 2], // less than a week over two weeks + ['2021-04-05', '2021-04-05', 1], // one monday + ['2021-04-04', '2021-04-04', 1], // one sunday + ['2021-04-01', '2021-04-04', 1], // less than a week ending on a sunday + ['2021-04-05', '2021-04-10', 1], // one perfect week + ['2021-04-01', '2021-04-07', 2], // 7 days over two weeks + ['2021-04-05', '2021-04-25', 3], // 3 perfect weeks + ['2021-04-06', '2021-04-27', 4], // 21 days over 4 weeks + ['2021-04-04', '2021-04-27', 5], // 23 days over 5 weeks and starting on a sunday (why not ?) + ['2021-04-01', '2021-05-31', 10], // Full April to May +]); it('returns valid month end dates', function ($start, $end, $endDates) { $start = Carbon::createFromFormat('Y-m-d', $start) - ->startOfDay(); + ->startOfDay(); $end = Carbon::createFromFormat('Y-m-d', $end) - ->startOfDay(); + ->startOfDay(); $monthEndDates = $this->taxAllowanceCalculator->monthlyEndDatesBetween($start, $end) - ->map(fn(Carbon $carbon) => $carbon->toDateString()) - ->all(); + ->map(fn(Carbon $carbon) => $carbon->toDateString()) + ->all(); expect($monthEndDates)->toEqual($endDates); })->with([ - // Start | End | Expected list of end dates - [ '2021-04-01', '2021-04-03', [ '2021-04-01' ] ], - [ '2021-04-01', '2021-04-30', [ '2021-04-30' ] ], - [ '2021-04-01', '2021-05-10', [ '2021-04-30', '2021-05-10' ] ], - [ - '2021-04-01', - '2021-09-02', - [ '2021-04-30', '2021-05-28', '2021-06-30', '2021-07-30', '2021-08-31', '2021-09-02' ], - ], - [ '2021-04-30', '2021-06-02', [ '2021-04-30', '2021-05-28', '2021-06-02' ] ], - [ '2021-04-25', '2021-05-05', [ '2021-04-30', '2021-05-05' ] ], - [ '2021-04-05', '2021-05-02', [ '2021-04-30' ] ], - [ '2021-04-01', '2021-08-01', [ '2021-04-30', '2021-05-28', '2021-06-30', '2021-07-30' ] ], - [ '2021-07-31', '2021-08-01', [] ], - [ '2021-05-31', '2021-06-10', [ '2021-06-10' ] ], - ]); + // Start | End | Expected list of end dates + ['2021-04-01', '2021-04-03', ['2021-04-01']], + ['2021-04-01', '2021-04-30', ['2021-04-30']], + ['2021-04-01', '2021-05-10', ['2021-04-30', '2021-05-10']], + [ + '2021-04-01', + '2021-09-02', + ['2021-04-30', '2021-05-28', '2021-06-30', '2021-07-30', '2021-08-31', '2021-09-02'], + ], + ['2021-04-30', '2021-06-02', ['2021-04-30', '2021-05-28', '2021-06-02']], + ['2021-04-25', '2021-05-05', ['2021-04-30', '2021-05-05']], + ['2021-04-05', '2021-05-02', ['2021-04-30']], + ['2021-04-01', '2021-08-01', ['2021-04-30', '2021-05-28', '2021-06-30', '2021-07-30']], + ['2021-07-31', '2021-08-01', []], + ['2021-05-31', '2021-06-10', ['2021-06-10']], +]); it('returns valid monthly allowance intervals', function ($start, $end, $allowance) { $start = Carbon::createFromFormat('Y-m-d', $start) - ->startOfDay(); + ->startOfDay(); $end = Carbon::createFromFormat('Y-m-d', $end) - ->startOfDay(); + ->startOfDay(); expect($this->taxAllowanceCalculator->monthly($start, $end))->toEqual($allowance); })->with([ - // Start date | End date | Expected allowance count - [ '2021-04-01', '2021-04-03', 1 ], - // less than a month - [ '2021-04-01', '2021-04-30', 1 ], - // one perfect month - [ '2021-04-01', '2021-05-10', 2 ], - // over two month, start on first and ending fine - [ '2021-04-01', '2021-09-02', 6 ], - // over 6 months - [ '2021-04-30', '2021-06-02', 3 ], - // over 6 months - [ '2021-04-25', '2021-05-05', 2 ], - // less than a month length over two months - [ '2021-04-05', '2021-05-02', 1 ], - // over two months, starting fine but ending on the first days of next month which are weekends - [ '2021-04-01', '2021-08-01', 4 ], - // over 5 month but ending on first day being a non-working day, so 4 allowance - [ '2021-07-31', '2021-08-01', 0 ], - // over 2 month but starting and ending on the same weekend..., so 0 allowance - [ '2021-05-31', '2021-06-10', 1 ], - // over 2 month but starting on last day being a non-working day, so 1 allowance - ]); + // Start date | End date | Expected allowance count + ['2021-04-01', '2021-04-03', 1], + // less than a month + ['2021-04-01', '2021-04-30', 1], + // one perfect month + ['2021-04-01', '2021-05-10', 2], + // over two month, start on first and ending fine + ['2021-04-01', '2021-09-02', 6], + // over 6 months + ['2021-04-30', '2021-06-02', 3], + // over 6 months + ['2021-04-25', '2021-05-05', 2], + // less than a month length over two months + ['2021-04-05', '2021-05-02', 1], + // over two months, starting fine but ending on the first days of next month which are weekends + ['2021-04-01', '2021-08-01', 4], + // over 5 month but ending on first day being a non-working day, so 4 allowance + ['2021-07-31', '2021-08-01', 0], + // over 2 month but starting and ending on the same weekend..., so 0 allowance + ['2021-05-31', '2021-06-10', 1], + // over 2 month but starting on last day being a non-working day, so 1 allowance +]);