Total Complexity | 10 |
Total Lines | 51 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | <?php |
||
13 | class DefaultOperation implements Operation |
||
14 | { |
||
15 | protected const OPERATION_PRIORITIES = [ |
||
16 | 0 => ['+', '-'], |
||
17 | 1 => ['*', '/'], |
||
18 | ]; |
||
19 | |||
20 | protected $operation; |
||
21 | |||
22 | public function __construct(string $operation) |
||
23 | { |
||
24 | $this->operation = $operation; |
||
25 | } |
||
26 | |||
27 | /** |
||
28 | * @param float $leftOperand |
||
29 | * @param float $rightOperand |
||
30 | * @return float|int |
||
31 | * @throws DivisionByZeroException |
||
32 | */ |
||
33 | public function execute(float $leftOperand, float $rightOperand) |
||
34 | { |
||
35 | $result = 0; |
||
36 | switch ($this->operation) { |
||
37 | case '+': |
||
38 | $result = $leftOperand + $rightOperand; |
||
39 | break; |
||
40 | case '-': |
||
41 | $result = $leftOperand - $rightOperand; |
||
42 | break; |
||
43 | case '*': |
||
44 | $result = $leftOperand * $rightOperand; |
||
45 | break; |
||
46 | case '/': |
||
47 | if (empty($rightOperand)) { |
||
48 | throw new DivisionByZeroException('Division by zero'); |
||
49 | } |
||
50 | $result = $leftOperand / $rightOperand; |
||
51 | break; |
||
52 | } |
||
53 | return $result; |
||
54 | } |
||
55 | |||
56 | public function getPriority(): int |
||
64 | } |
||
65 | } |
||
66 |