Skip to content

Commit

Permalink
handle new phpstan issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Nielsvanpach committed Mar 22, 2024
1 parent 97c3c1a commit 094be29
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 9 deletions.
2 changes: 2 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -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#'
4 changes: 2 additions & 2 deletions src/Calendars/IslamicCalendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) {
Expand Down
5 changes: 5 additions & 0 deletions src/Countries/Country.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
6 changes: 6 additions & 0 deletions src/Countries/Egypt.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Countries/Greece.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
2 changes: 1 addition & 1 deletion src/Countries/Netherlands.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ protected function allHolidays(int $year): array
/** @return array<string, CarbonImmutable> */
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();
Expand Down
14 changes: 9 additions & 5 deletions src/Countries/Panama.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Spatie\Holidays\Countries;

use Carbon\CarbonImmutable;
use Carbon\Exceptions\InvalidFormatException;

class Panama extends Country
{
Expand Down Expand Up @@ -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();
}
}

Expand Down

0 comments on commit 094be29

Please sign in to comment.