Skip to content

Commit f4c1519

Browse files
committed
Rector ChangeSwitchToMatchRector
1 parent de658b4 commit f4c1519

File tree

24 files changed

+371
-931
lines changed

24 files changed

+371
-931
lines changed

src/PhpSpreadsheet/Calculation/BinaryComparison.php

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -73,28 +73,15 @@ public static function compare($operand1, $operand2, string $operator): bool
7373
*/
7474
private static function evaluateComparison($operand1, $operand2, string $operator, bool $useLowercaseFirstComparison): bool
7575
{
76-
switch ($operator) {
77-
// Equality
78-
case '=':
79-
return self::equal($operand1, $operand2);
80-
// Greater than
81-
case '>':
82-
return self::greaterThan($operand1, $operand2, $useLowercaseFirstComparison);
83-
// Less than
84-
case '<':
85-
return self::lessThan($operand1, $operand2, $useLowercaseFirstComparison);
86-
// Greater than or equal
87-
case '>=':
88-
return self::greaterThanOrEqual($operand1, $operand2, $useLowercaseFirstComparison);
89-
// Less than or equal
90-
case '<=':
91-
return self::lessThanOrEqual($operand1, $operand2, $useLowercaseFirstComparison);
92-
// Inequality
93-
case '<>':
94-
return self::notEqual($operand1, $operand2);
95-
default:
96-
throw new Exception('Unsupported binary comparison operator');
97-
}
76+
return match ($operator) {
77+
'=' => self::equal($operand1, $operand2),
78+
'>' => self::greaterThan($operand1, $operand2, $useLowercaseFirstComparison),
79+
'<' => self::lessThan($operand1, $operand2, $useLowercaseFirstComparison),
80+
'>=' => self::greaterThanOrEqual($operand1, $operand2, $useLowercaseFirstComparison),
81+
'<=' => self::lessThanOrEqual($operand1, $operand2, $useLowercaseFirstComparison),
82+
'<>' => self::notEqual($operand1, $operand2),
83+
default => throw new Exception('Unsupported binary comparison operator'),
84+
};
9885
}
9986

10087
/**

src/PhpSpreadsheet/Calculation/DateTimeExcel/YearFrac.php

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,20 +61,14 @@ public static function fraction($startDate, $endDate, $method = 0): array|string
6161
return $e->getMessage();
6262
}
6363

64-
switch ($method) {
65-
case 0:
66-
return Functions::scalar(Days360::between($startDate, $endDate)) / 360;
67-
case 1:
68-
return self::method1($startDate, $endDate);
69-
case 2:
70-
return Functions::scalar(Difference::interval($startDate, $endDate)) / 360;
71-
case 3:
72-
return Functions::scalar(Difference::interval($startDate, $endDate)) / 365;
73-
case 4:
74-
return Functions::scalar(Days360::between($startDate, $endDate, true)) / 360;
75-
}
76-
77-
return ExcelError::NAN();
64+
return match ($method) {
65+
0 => Functions::scalar(Days360::between($startDate, $endDate)) / 360,
66+
1 => self::method1($startDate, $endDate),
67+
2 => Functions::scalar(Difference::interval($startDate, $endDate)) / 360,
68+
3 => Functions::scalar(Difference::interval($startDate, $endDate)) / 365,
69+
4 => Functions::scalar(Days360::between($startDate, $endDate, true)) / 360,
70+
default => ExcelError::NAN(),
71+
};
7872
}
7973

8074
/**

src/PhpSpreadsheet/Calculation/Engine/Operands/StructuredReference.php

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -274,32 +274,22 @@ private function fullData(int $startRow, int $endRow): string
274274

275275
private function getMinimumRow(string $reference): int
276276
{
277-
switch ($reference) {
278-
case self::ITEM_SPECIFIER_ALL:
279-
case self::ITEM_SPECIFIER_HEADERS:
280-
return $this->headersRow ?? $this->firstDataRow;
281-
case self::ITEM_SPECIFIER_DATA:
282-
return $this->firstDataRow;
283-
case self::ITEM_SPECIFIER_TOTALS:
284-
return $this->totalsRow ?? $this->lastDataRow;
285-
}
286-
287-
return $this->headersRow ?? $this->firstDataRow;
277+
return match ($reference) {
278+
self::ITEM_SPECIFIER_ALL, self::ITEM_SPECIFIER_HEADERS => $this->headersRow ?? $this->firstDataRow,
279+
self::ITEM_SPECIFIER_DATA => $this->firstDataRow,
280+
self::ITEM_SPECIFIER_TOTALS => $this->totalsRow ?? $this->lastDataRow,
281+
default => $this->headersRow ?? $this->firstDataRow,
282+
};
288283
}
289284

290285
private function getMaximumRow(string $reference): int
291286
{
292-
switch ($reference) {
293-
case self::ITEM_SPECIFIER_HEADERS:
294-
return $this->headersRow ?? $this->firstDataRow;
295-
case self::ITEM_SPECIFIER_DATA:
296-
return $this->lastDataRow;
297-
case self::ITEM_SPECIFIER_ALL:
298-
case self::ITEM_SPECIFIER_TOTALS:
299-
return $this->totalsRow ?? $this->lastDataRow;
300-
}
301-
302-
return $this->totalsRow ?? $this->lastDataRow;
287+
return match ($reference) {
288+
self::ITEM_SPECIFIER_HEADERS => $this->headersRow ?? $this->firstDataRow,
289+
self::ITEM_SPECIFIER_DATA => $this->lastDataRow,
290+
self::ITEM_SPECIFIER_ALL, self::ITEM_SPECIFIER_TOTALS => $this->totalsRow ?? $this->lastDataRow,
291+
default => $this->totalsRow ?? $this->lastDataRow,
292+
};
303293
}
304294

305295
public function value(): string

src/PhpSpreadsheet/Calculation/Engineering/BesselI.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,11 @@ public static function BESSELI($x, $ord): array|string|float
5959

6060
private static function calculate(float $x, int $ord): float
6161
{
62-
// special cases
63-
switch ($ord) {
64-
case 0:
65-
return self::besselI0($x);
66-
case 1:
67-
return self::besselI1($x);
68-
}
69-
70-
return self::besselI2($x, $ord);
62+
return match ($ord) {
63+
0 => self::besselI0($x),
64+
1 => self::besselI1($x),
65+
default => self::besselI2($x, $ord),
66+
};
7167
}
7268

7369
private static function besselI0(float $x): float

src/PhpSpreadsheet/Calculation/Engineering/BesselJ.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,11 @@ public static function BESSELJ($x, $ord): array|string|float
5858

5959
private static function calculate(float $x, int $ord): float
6060
{
61-
// special cases
62-
switch ($ord) {
63-
case 0:
64-
return self::besselJ0($x);
65-
case 1:
66-
return self::besselJ1($x);
67-
}
68-
69-
return self::besselJ2($x, $ord);
61+
return match ($ord) {
62+
0 => self::besselJ0($x),
63+
1 => self::besselJ1($x),
64+
default => self::besselJ2($x, $ord),
65+
};
7066
}
7167

7268
private static function besselJ0(float $x): float

src/PhpSpreadsheet/Calculation/Engineering/BesselK.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,11 @@ public static function BESSELK($x, $ord): array|string|float
5656

5757
private static function calculate(float $x, int $ord): float
5858
{
59-
// special cases
60-
switch ($ord) {
61-
case 0:
62-
return self::besselK0($x);
63-
case 1:
64-
return self::besselK1($x);
65-
}
66-
67-
return self::besselK2($x, $ord);
59+
return match ($ord) {
60+
0 => self::besselK0($x),
61+
1 => self::besselK1($x),
62+
default => self::besselK2($x, $ord),
63+
};
6864
}
6965

7066
/**

src/PhpSpreadsheet/Calculation/Engineering/BesselY.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,11 @@ public static function BESSELY($x, $ord): array|string|float
5555

5656
private static function calculate(float $x, int $ord): float
5757
{
58-
// special cases
59-
switch ($ord) {
60-
case 0:
61-
return self::besselY0($x);
62-
case 1:
63-
return self::besselY1($x);
64-
}
65-
66-
return self::besselY2($x, $ord);
58+
return match ($ord) {
59+
0 => self::besselY0($x),
60+
1 => self::besselY1($x),
61+
default => self::besselY2($x, $ord),
62+
};
6763
}
6864

6965
/**

src/PhpSpreadsheet/Calculation/Engineering/ConvertUOM.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -674,15 +674,11 @@ protected static function convertTemperature(string $fromUOM, string $toUOM, $va
674674

675675
private static function resolveTemperatureSynonyms(string $uom): string
676676
{
677-
switch ($uom) {
678-
case 'fah':
679-
return 'F';
680-
case 'cel':
681-
return 'C';
682-
case 'kel':
683-
return 'K';
684-
}
685-
686-
return $uom;
677+
return match ($uom) {
678+
'fah' => 'F',
679+
'cel' => 'C',
680+
'kel' => 'K',
681+
default => $uom,
682+
};
687683
}
688684
}

src/PhpSpreadsheet/Calculation/Financial/Amortization.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,10 @@ public static function AMORDEGRC(
8989
$fRest -= $fNRate;
9090

9191
if ($fRest < 0.0) {
92-
switch ($period - $n) {
93-
case 0:
94-
case 1:
95-
return round($cost * 0.5, 0);
96-
default:
97-
return 0.0;
98-
}
92+
return match ($period - $n) {
93+
0, 1 => round($cost * 0.5, 0),
94+
default => 0.0,
95+
};
9996
}
10097
$cost -= $fNRate;
10198
}

src/PhpSpreadsheet/Calculation/LookupRef/ExcelMatch.php

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -64,19 +64,11 @@ public static function MATCH($lookupValue, $lookupArray, $matchType = self::MATC
6464
}
6565

6666
$valueKey = null;
67-
switch ($matchType) {
68-
case self::MATCHTYPE_LARGEST_VALUE:
69-
$valueKey = self::matchLargestValue($lookupArray, $lookupValue, $keySet);
70-
71-
break;
72-
case self::MATCHTYPE_FIRST_VALUE:
73-
$valueKey = self::matchFirstValue($lookupArray, $lookupValue);
74-
75-
break;
76-
case self::MATCHTYPE_SMALLEST_VALUE:
77-
default:
78-
$valueKey = self::matchSmallestValue($lookupArray, $lookupValue);
79-
}
67+
$valueKey = match ($matchType) {
68+
self::MATCHTYPE_LARGEST_VALUE => self::matchLargestValue($lookupArray, $lookupValue, $keySet),
69+
self::MATCHTYPE_FIRST_VALUE => self::matchFirstValue($lookupArray, $lookupValue),
70+
default => self::matchSmallestValue($lookupArray, $lookupValue),
71+
};
8072

8173
if ($valueKey !== null) {
8274
return ++$valueKey;

0 commit comments

Comments
 (0)