Skip to content

Commit

Permalink
Use match instead of switch when a simple value is returned (#1393)
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN committed Sep 13, 2024
1 parent dae397d commit da7b9dc
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 693 deletions.
9 changes: 5 additions & 4 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use Rector\Config\RectorConfig;
use Rector\DeadCode\Rector\ClassLike\RemoveAnnotationRector;
use Rector\Php70\Rector\StmtsAwareInterface\IfIssetToCoalescingRector;
use Rector\Php73\Rector\FuncCall\JsonThrowOnErrorRector;
use Rector\Php80\Rector\Switch_\ChangeSwitchToMatchRector;
use Rector\Set\ValueObject\LevelSetList;

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

$rectorConfig->rule(ChangeSwitchToMatchRector::class);

// phpcs:disable Squiz.Arrays.ArrayDeclaration.KeySpecified
$rectorConfig->skip([
// Do not use ternaries extensively
IfIssetToCoalescingRector::class,
// Not necessary in documentation examples
JsonThrowOnErrorRector::class => [
__DIR__ . '/tests/DocumentationExamplesTest.php',
ChangeSwitchToMatchRector::class => [
__DIR__ . '/tests/SpecTests/Operation.php',
],
]);
// phpcs:enable
Expand Down
33 changes: 11 additions & 22 deletions src/Builder/Encoder/OperatorEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,27 +38,14 @@ public function encode(mixed $value): stdClass
throw UnsupportedValueException::invalidEncodableValue($value);
}

switch ($value::ENCODE) {
case Encode::Single:
return $this->encodeAsSingle($value);

case Encode::Array:
return $this->encodeAsArray($value);

case Encode::Object:
case Encode::FlatObject:
return $this->encodeAsObject($value);

case Encode::DollarObject:
return $this->encodeAsDollarObject($value);

case Encode::Group:
assert($value instanceof GroupStage);

return $this->encodeAsGroup($value);
}

throw new LogicException(sprintf('Class "%s" does not have a valid ENCODE constant.', $value::class));
return match ($value::ENCODE) {
Encode::Single => $this->encodeAsSingle($value),
Encode::Array => $this->encodeAsArray($value),
Encode::Object, Encode::FlatObject => $this->encodeAsObject($value),
Encode::DollarObject => $this->encodeAsDollarObject($value),
Encode::Group => $this->encodeAsGroup($value),
default => throw new LogicException(sprintf('Class "%s" does not have a valid ENCODE constant.', $value::class)),
};
}

/**
Expand Down Expand Up @@ -111,8 +98,10 @@ private function encodeAsDollarObject(OperatorInterface $value): stdClass
/**
* $group stage have a specific encoding because the _id argument is required and others are variadic
*/
private function encodeAsGroup(GroupStage $value): stdClass
private function encodeAsGroup(OperatorInterface $value): stdClass
{
assert($value instanceof GroupStage);

$result = new stdClass();
$result->_id = $this->recursiveEncode($value->_id);

Expand Down
Loading

0 comments on commit da7b9dc

Please sign in to comment.