Skip to content

Commit 07a42e0

Browse files
Ayoub-Mabroukroot
andauthored
refactor: simplify null coalescing and optional chaining in getOverlapping (#265)
* refactor(IslamicCalendar): simplify null coalescing and optional chaining in getOverlapping Replaced explicit null check and exception throw with null coalescing + throw expression. Also added optional chaining to ensure safer date calculation using CarbonImmutable. * refactor(IndianCalender): simplify null coalescing and optional chaining in getOverlapping Replaced explicit null check and exception throw with null coalescing + throw expression. Also added optional chaining to ensure safer date calculation using CarbonImmutable. --------- Co-authored-by: root <root@localhost>
1 parent 19a720c commit 07a42e0

File tree

2 files changed

+6
-22
lines changed

2 files changed

+6
-22
lines changed

src/Calendars/IndianCalender.php

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -110,23 +110,15 @@ protected function getOverlapping(array $collection, int $year, int $totalDays):
110110
return null;
111111
}
112112

113-
$date = $collection[$year - 1] ?? null;
114-
115-
if ($date === null) {
116-
throw InvalidYear::range($this->countryCode(), 1970, 2037);
117-
}
113+
$date = $collection[$year - 1] ?? throw InvalidYear::range($this->countryCode(), 1970, 2037);
118114

119115
if (is_array($date)) {
120116
$date = end($date);
121117
}
122118

123119
$start = CarbonImmutable::createFromFormat('Y-m-d', "{$year}-{$date}")?->startOfDay();
124-
$end = $start->addDays($totalDays - 1)->startOfDay();
125-
126-
if ($end->year !== $year) {
127-
return (string) $date;
128-
}
120+
$end = $start?->addDays($totalDays - 1)->startOfDay();
129121

130-
return null;
122+
return ($end?->year !== $year) ? (string) $date : null;
131123
}
132124
}

src/Calendars/IslamicCalendar.php

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -124,24 +124,16 @@ protected function getOverlapping(array $collection, int $year, int $totalDays):
124124
return null;
125125
}
126126

127-
$date = $collection[$year - 1] ?? null;
128-
129-
if ($date === null) {
130-
throw InvalidYear::range($this->countryCode(), 1970, 2037);
131-
}
127+
$date = $collection[$year - 1] ?? throw InvalidYear::range($this->countryCode(), 1970, 2037);
132128

133129
if (is_array($date)) {
134130
$date = end($date);
135131
}
136132

137133
$start = CarbonImmutable::createFromFormat('Y-m-d', "{$year}-{$date}")?->startOfDay();
138-
$end = $start->addDays($totalDays - 1)->startOfDay();
139-
140-
if ($end->year !== $year) {
141-
return (string) $date;
142-
}
134+
$end = $start?->addDays($totalDays - 1)->startOfDay();
143135

144-
return null;
136+
return ($end?->year !== $year) ? (string) $date : null;
145137
}
146138

147139
public function setIslamicCalendarTimezone(string $islamicCalendarTimezone): static

0 commit comments

Comments
 (0)