Skip to content

Commit

Permalink
Refactoring the Calculatable Trait
Browse files Browse the repository at this point in the history
  • Loading branch information
arcanedev-maroc committed Aug 22, 2016
1 parent b8d6694 commit 053f5fe
Showing 1 changed file with 11 additions and 18 deletions.
29 changes: 11 additions & 18 deletions src/Traits/Calculatable.php
Expand Up @@ -19,24 +19,17 @@ trait Calculatable
*/
protected static function calculate($a, $operator, $b)
{
switch ($operator) {
case '+':
return $a + $b;
$operations = $operations = [
'+' => function ($a, $b) { return $a + $b; },
'-' => function ($a, $b) { return $a - $b; },
'x' => function ($a, $b) { return $a * $b; },
'*' => function ($a, $b) { return $a * $b; },
'/' => function ($a, $b) { return $a / $b; },
'^' => function ($a, $b) { return pow($a, $b); },
];

case '-':
return $a - $b;

case 'x':
case '*':
return $a * $b;

case '/':
return $a / $b;

case '^':
return pow($a, $b);
}

return $a;
return array_key_exists($operator, $operations)
? call_user_func_array($operations[$operator], [$a, $b])
: $a;
}
}

0 comments on commit 053f5fe

Please sign in to comment.