-
Notifications
You must be signed in to change notification settings - Fork 266
Use match
instead of switch
when a simple value is returned
#1393
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -90,77 +90,32 @@ public static function anyOf(string ...$types): Constraint | |
|
||
private function doMatches($other): bool | ||
{ | ||
switch ($this->type) { | ||
case 'double': | ||
return is_float($other); | ||
|
||
case 'string': | ||
return is_string($other); | ||
|
||
case 'object': | ||
return self::isObject($other); | ||
|
||
case 'array': | ||
return self::isArray($other); | ||
|
||
case 'binData': | ||
return $other instanceof BinaryInterface; | ||
|
||
case 'undefined': | ||
return $other instanceof Undefined; | ||
|
||
case 'objectId': | ||
return $other instanceof ObjectIdInterface; | ||
|
||
case 'bool': | ||
return is_bool($other); | ||
|
||
case 'date': | ||
return $other instanceof UTCDateTimeInterface; | ||
|
||
case 'null': | ||
return $other === null; | ||
|
||
case 'regex': | ||
return $other instanceof RegexInterface; | ||
|
||
case 'dbPointer': | ||
return $other instanceof DBPointer; | ||
|
||
case 'javascript': | ||
return $other instanceof JavascriptInterface && $other->getScope() === null; | ||
|
||
case 'symbol': | ||
return $other instanceof Symbol; | ||
|
||
case 'javascriptWithScope': | ||
return $other instanceof JavascriptInterface && $other->getScope() !== null; | ||
|
||
case 'int': | ||
return is_int($other); | ||
|
||
case 'timestamp': | ||
return $other instanceof TimestampInterface; | ||
|
||
case 'long': | ||
return is_int($other) || $other instanceof Int64; | ||
|
||
case 'decimal': | ||
return $other instanceof Decimal128Interface; | ||
|
||
case 'minKey': | ||
return $other instanceof MinKeyInterface; | ||
|
||
case 'maxKey': | ||
return $other instanceof MaxKeyInterface; | ||
|
||
case 'number': | ||
return is_int($other) || $other instanceof Int64 || is_float($other) || $other instanceof Decimal128Interface; | ||
|
||
default: | ||
// This should already have been caught in the constructor | ||
throw new LogicException('Unsupported type: ' . $this->type); | ||
} | ||
return match ($this->type) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This particular instance highlights just how much more concise the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All the |
||
'double' => is_float($other), | ||
'string' => is_string($other), | ||
'object' => self::isObject($other), | ||
'array' => self::isArray($other), | ||
'binData' => $other instanceof BinaryInterface, | ||
'undefined' => $other instanceof Undefined, | ||
'objectId' => $other instanceof ObjectIdInterface, | ||
'bool' => is_bool($other), | ||
'date' => $other instanceof UTCDateTimeInterface, | ||
'null' => $other === null, | ||
'regex' => $other instanceof RegexInterface, | ||
'dbPointer' => $other instanceof DBPointer, | ||
'javascript' => $other instanceof JavascriptInterface && $other->getScope() === null, | ||
'symbol' => $other instanceof Symbol, | ||
'javascriptWithScope' => $other instanceof JavascriptInterface && $other->getScope() !== null, | ||
'int' => is_int($other), | ||
'timestamp' => $other instanceof TimestampInterface, | ||
'long' => is_int($other) || $other instanceof Int64, | ||
'decimal' => $other instanceof Decimal128Interface, | ||
'minKey' => $other instanceof MinKeyInterface, | ||
'maxKey' => $other instanceof MaxKeyInterface, | ||
'number' => is_int($other) || $other instanceof Int64 || is_float($other) || $other instanceof Decimal128Interface, | ||
// This should already have been caught in the constructor | ||
default => throw new LogicException('Unsupported type: ' . $this->type), | ||
}; | ||
} | ||
|
||
private function doToString(): string | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Noted that this assert was moved here, it was previously handled in the
switch
statement.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only
switch
that I converted manually.