Skip to content

Commit 1e417fd

Browse files
authored
Use match() instead of switch/case. (#487)
* Use match() instead of switch/case. Also improve docblock type * Fix CS error * Remove impossible "default" case
1 parent 7d9af08 commit 1e417fd

File tree

2 files changed

+10
-13
lines changed

2 files changed

+10
-13
lines changed

src/FormattingTrait.php

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ public function toQuarter(bool $range = false): int|array
250250

251251
trigger_error(
252252
'Using toQuarter() with `$range=true` is deprecated. Use `toQuarterRange()` instead.',
253-
E_USER_DEPRECATED
253+
E_USER_DEPRECATED,
254254
);
255255

256256
return $this->toQuarterRange();
@@ -259,23 +259,20 @@ public function toQuarter(bool $range = false): int|array
259259
/**
260260
* Returns the quarter range
261261
*
262-
* @return array<string> Array with start and end date of quarter in Y-m-d format
262+
* @return array{0: string, 1: string} Array with start and end date of quarter in Y-m-d format
263263
*/
264264
public function toQuarterRange(): array
265265
{
266+
/** @var int<1, 4> $quarter */
266267
$quarter = (int)ceil((int)$this->format('m') / 3);
267268
$year = $this->format('Y');
268269

269-
switch ($quarter) {
270-
case 1:
271-
return [$year . '-01-01', $year . '-03-31'];
272-
case 2:
273-
return [$year . '-04-01', $year . '-06-30'];
274-
case 3:
275-
return [$year . '-07-01', $year . '-09-30'];
276-
default:
277-
return [$year . '-10-01', $year . '-12-31'];
278-
}
270+
return match ($quarter) {
271+
1 => [$year . '-01-01', $year . '-03-31'],
272+
2 => [$year . '-04-01', $year . '-06-30'],
273+
3 => [$year . '-07-01', $year . '-09-30'],
274+
4 => [$year . '-10-01', $year . '-12-31'],
275+
};
279276
}
280277

281278
/**

tests/TestCase/DateTime/StringsTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ public static function toQuarterRangeProvider()
205205
public function testToQuarterRange($date, $expected)
206206
{
207207
$this->assertSame($expected, (new Chronos($date))->toQuarterRange());
208-
$this->deprecated(function() use ($date, $expected) {
208+
$this->deprecated(function () use ($date, $expected) {
209209
$this->assertSame($expected, (new Chronos($date))->toQuarter(true));
210210
});
211211
}

0 commit comments

Comments
 (0)