From 094be293516eb74a0ce584febeddf7203d030830 Mon Sep 17 00:00:00 2001 From: Niels Vanpachtenbeke <10651054+Nielsvanpach@users.noreply.github.com> Date: Fri, 22 Mar 2024 12:20:07 +0100 Subject: [PATCH] handle new phpstan issues --- phpstan.neon.dist | 2 ++ src/Calendars/IslamicCalendar.php | 4 ++-- src/Countries/Country.php | 5 +++++ src/Countries/Egypt.php | 6 ++++++ src/Countries/Greece.php | 2 +- src/Countries/Netherlands.php | 2 +- src/Countries/Panama.php | 14 +++++++++----- 7 files changed, 26 insertions(+), 9 deletions(-) diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 4bfcedd04..cf486a4f4 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -8,3 +8,5 @@ parameters: ignoreErrors: - '#Unsafe usage of new static#' + - '#Cannot call method startOfDay\(\) on Carbon\\CarbonImmutable\|null#' + - '#Cannot call method addDays\(\) on Carbon\\CarbonImmutable\|null#' diff --git a/src/Calendars/IslamicCalendar.php b/src/Calendars/IslamicCalendar.php index bb6b513bb..9ab97a8c6 100644 --- a/src/Calendars/IslamicCalendar.php +++ b/src/Calendars/IslamicCalendar.php @@ -72,7 +72,7 @@ protected function getHoliday(array $collection, int $year, int $totalDays): arr protected function createPeriod(string $date, int $year, int $totalDays): CarbonPeriod { - $start = CarbonImmutable::createFromFormat('Y-m-d', "{$year}-{$date}")->startOfDay(); + $start = CarbonImmutable::createFromFormat('Y-m-d', "{$year}-{$date}")?->startOfDay(); $end = $start->addDays($totalDays - 1)->startOfDay(); return CarbonPeriod::create($start, '1 day', $end); @@ -95,7 +95,7 @@ protected function getOverlapping(array $collection, int $year, int $totalDays): $date = end($date); } - $start = CarbonImmutable::createFromFormat('Y-m-d', "{$year}-{$date}")->startOfDay(); + $start = CarbonImmutable::createFromFormat('Y-m-d', "{$year}-{$date}")?->startOfDay(); $end = $start->addDays($totalDays - 1)->startOfDay(); if ($end->year !== $year) { diff --git a/src/Countries/Country.php b/src/Countries/Country.php index 841c5c0d7..a6ab1d72a 100644 --- a/src/Countries/Country.php +++ b/src/Countries/Country.php @@ -5,6 +5,7 @@ use Carbon\CarbonImmutable; use Carbon\CarbonInterface; use Carbon\CarbonPeriod; +use Carbon\Exceptions\InvalidFormatException; use Spatie\Holidays\Contracts\HasTranslations; use Spatie\Holidays\Exceptions\InvalidCountry; use Spatie\Holidays\Exceptions\InvalidYear; @@ -33,6 +34,10 @@ public function get(int $year, ?string $locale = null): array } } + if ($date === null) { + throw new InvalidFormatException("Invalid date for holiday `{$name}`"); + } + if ($this instanceof HasTranslations) { $name = $this->translate(basename(str_replace('\\', '/', static::class)), $name, $locale); } diff --git a/src/Countries/Egypt.php b/src/Countries/Egypt.php index d6f8af4ef..37a6109e7 100644 --- a/src/Countries/Egypt.php +++ b/src/Countries/Egypt.php @@ -4,6 +4,7 @@ use Carbon\CarbonImmutable; use Carbon\CarbonInterface; +use Carbon\Exceptions\InvalidFormatException; use Spatie\Holidays\Concerns\Translatable; use Spatie\Holidays\Contracts\HasTranslations; use Spatie\Holidays\Exceptions\InvalidYear; @@ -304,6 +305,11 @@ private function getIslamicHolidayDatesForYear(array $holidayDates, int $year, s $startDay = CarbonImmutable::createFromFormat('Y-m-d', sprintf('%s-%s', $year, $holidayDates[$year])); + if ($startDay === null) { + throw new InvalidFormatException("Invalid date format for holiday: {$holidayName}"); + + } + if ($duration === 1) { // For single-day holidays, use the holiday name without "Day" $dates[$holidayName] = $startDay; diff --git a/src/Countries/Greece.php b/src/Countries/Greece.php index 095405980..290a34f10 100644 --- a/src/Countries/Greece.php +++ b/src/Countries/Greece.php @@ -35,7 +35,7 @@ protected function variableHolidays(int $year): array $megaloSavvato = $orthodoxEaster->copy()->subDay(); $deuteraPasha = $orthodoxEaster->copy()->addDay(); - $protomagia = CarbonImmutable::createFromFormat('Y-m-d', "{$year}-05-01"); + $protomagia = CarbonImmutable::createFromDate($year, 5, 1); $moveProtomagia = [$megaliParaskevi, $megaloSavvato, $orthodoxEaster, $deuteraPasha]; if (in_array($protomagia, $moveProtomagia, true)) { diff --git a/src/Countries/Netherlands.php b/src/Countries/Netherlands.php index 34f3b37fc..0e9fecb54 100644 --- a/src/Countries/Netherlands.php +++ b/src/Countries/Netherlands.php @@ -24,7 +24,7 @@ protected function allHolidays(int $year): array /** @return array */ protected function variableHolidays(int $year): array { - $koningsDag = CarbonImmutable::createFromFormat('Y-m-d', "{$year}-04-27"); + $koningsDag = CarbonImmutable::createFromDate($year, 4, 27); if ($koningsDag->isSunday()) { $koningsDag = $koningsDag->subDay(); diff --git a/src/Countries/Panama.php b/src/Countries/Panama.php index 98e10eb1d..c8229257d 100644 --- a/src/Countries/Panama.php +++ b/src/Countries/Panama.php @@ -3,6 +3,7 @@ namespace Spatie\Holidays\Countries; use Carbon\CarbonImmutable; +use Carbon\Exceptions\InvalidFormatException; class Panama extends Country { @@ -80,11 +81,14 @@ protected function calculateBridgeDays(array $fixedHolidays, int $year): array foreach ($fixedHolidays as $name => $date) { $holiday = CarbonImmutable::createFromFormat('Y-m-d', "{$year}-{$date}"); - if ($holiday !== false) { - $holidays[$name] = $holiday; - if ($holiday->isSunday()) { - $holidays[$name.' (Puente)'] = $holiday->addDay(); - } + if ($holiday === null) { + throw new InvalidFormatException("Invalid date format for holiday: {$name}"); + } + + $holidays[$name] = $holiday; + + if ($holiday->isSunday()) { + $holidays[$name.' (Puente)'] = $holiday->addDay(); } }