Skip to content

Commit

Permalink
Use match instead of switch
Browse files Browse the repository at this point in the history
  • Loading branch information
BenMorel committed Oct 11, 2023
1 parent b51ec8a commit cc2a1da
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 32 deletions.
30 changes: 10 additions & 20 deletions src/Internal/Calculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -571,27 +571,17 @@ private function bitwise(string $operator, string $a, string $b) : string
$bBin = $this->twosComplement($bBin);
}

switch ($operator) {
case 'and':
$value = $aBin & $bBin;
$negative = ($aNeg and $bNeg);
break;

case 'or':
$value = $aBin | $bBin;
$negative = ($aNeg or $bNeg);
break;

case 'xor':
$value = $aBin ^ $bBin;
$negative = ($aNeg xor $bNeg);
break;
$value = match ($operator) {
'and' => $aBin & $bBin,
'or' => $aBin | $bBin,
'xor' => $aBin ^ $bBin,
};

// @codeCoverageIgnoreStart
default:
throw new \InvalidArgumentException('Invalid bitwise operator.');
// @codeCoverageIgnoreEnd
}
$negative = match ($operator) {
'and' => $aNeg and $bNeg,
'or' => $aNeg or $bNeg,
'xor' => $aNeg xor $bNeg,
};

if ($negative) {
$value = $this->twosComplement($value);
Expand Down
17 changes: 5 additions & 12 deletions src/Internal/Calculator/NativeCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,11 @@ class NativeCalculator extends Calculator
*/
public function __construct()
{
switch (PHP_INT_SIZE) {
case 4:
$this->maxDigits = 9;
break;

case 8:
$this->maxDigits = 18;
break;

default:
throw new \RuntimeException('The platform is not 32-bit or 64-bit as expected.');
}
$this->maxDigits = match (PHP_INT_SIZE) {
4 => 9,
8 => 18,
default => throw new \RuntimeException('The platform is not 32-bit or 64-bit as expected.')
};
}

public function add(string $a, string $b) : string
Expand Down

0 comments on commit cc2a1da

Please sign in to comment.