Skip to content

Commit da7b9dc

Browse files
committed
Use match instead of switch when a simple value is returned (#1393)
1 parent dae397d commit da7b9dc

File tree

10 files changed

+91
-693
lines changed

10 files changed

+91
-693
lines changed

rector.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use Rector\Config\RectorConfig;
44
use Rector\DeadCode\Rector\ClassLike\RemoveAnnotationRector;
55
use Rector\Php70\Rector\StmtsAwareInterface\IfIssetToCoalescingRector;
6-
use Rector\Php73\Rector\FuncCall\JsonThrowOnErrorRector;
6+
use Rector\Php80\Rector\Switch_\ChangeSwitchToMatchRector;
77
use Rector\Set\ValueObject\LevelSetList;
88

99
return static function (RectorConfig $rectorConfig): void {
@@ -17,13 +17,14 @@
1717
// Modernize code
1818
$rectorConfig->sets([LevelSetList::UP_TO_PHP_74]);
1919

20+
$rectorConfig->rule(ChangeSwitchToMatchRector::class);
21+
2022
// phpcs:disable Squiz.Arrays.ArrayDeclaration.KeySpecified
2123
$rectorConfig->skip([
2224
// Do not use ternaries extensively
2325
IfIssetToCoalescingRector::class,
24-
// Not necessary in documentation examples
25-
JsonThrowOnErrorRector::class => [
26-
__DIR__ . '/tests/DocumentationExamplesTest.php',
26+
ChangeSwitchToMatchRector::class => [
27+
__DIR__ . '/tests/SpecTests/Operation.php',
2728
],
2829
]);
2930
// phpcs:enable

src/Builder/Encoder/OperatorEncoder.php

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -38,27 +38,14 @@ public function encode(mixed $value): stdClass
3838
throw UnsupportedValueException::invalidEncodableValue($value);
3939
}
4040

41-
switch ($value::ENCODE) {
42-
case Encode::Single:
43-
return $this->encodeAsSingle($value);
44-
45-
case Encode::Array:
46-
return $this->encodeAsArray($value);
47-
48-
case Encode::Object:
49-
case Encode::FlatObject:
50-
return $this->encodeAsObject($value);
51-
52-
case Encode::DollarObject:
53-
return $this->encodeAsDollarObject($value);
54-
55-
case Encode::Group:
56-
assert($value instanceof GroupStage);
57-
58-
return $this->encodeAsGroup($value);
59-
}
60-
61-
throw new LogicException(sprintf('Class "%s" does not have a valid ENCODE constant.', $value::class));
41+
return match ($value::ENCODE) {
42+
Encode::Single => $this->encodeAsSingle($value),
43+
Encode::Array => $this->encodeAsArray($value),
44+
Encode::Object, Encode::FlatObject => $this->encodeAsObject($value),
45+
Encode::DollarObject => $this->encodeAsDollarObject($value),
46+
Encode::Group => $this->encodeAsGroup($value),
47+
default => throw new LogicException(sprintf('Class "%s" does not have a valid ENCODE constant.', $value::class)),
48+
};
6249
}
6350

6451
/**
@@ -111,8 +98,10 @@ private function encodeAsDollarObject(OperatorInterface $value): stdClass
11198
/**
11299
* $group stage have a specific encoding because the _id argument is required and others are variadic
113100
*/
114-
private function encodeAsGroup(GroupStage $value): stdClass
101+
private function encodeAsGroup(OperatorInterface $value): stdClass
115102
{
103+
assert($value instanceof GroupStage);
104+
116105
$result = new stdClass();
117106
$result->_id = $this->recursiveEncode($value->_id);
118107

0 commit comments

Comments
 (0)