diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 4bfcedd0..cf486a4f 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 bb6b513b..9ab97a8c 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 841c5c0d..a6ab1d72 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 d6f8af4e..37a6109e 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 09540598..290a34f1 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 34f3b37f..0e9fecb5 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 98e10eb1..c8229257 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(); } }