BasicCalculatorII::calculate()   B
last analyzed

Complexity

Conditions 10
Paths 14

Size

Total Lines 33
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 25
c 1
b 0
f 0
dl 0
loc 33
rs 7.6666
cc 10
nc 14
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace leetcode;
6
7
class BasicCalculatorII
8
{
9
    private static array $operators = ['+', '-', '*', '/'];
10
11
    public static function calculate(string $s): int
12
    {
13
        if (empty($s)) {
14
            return 0;
15
        }
16
        $s = str_replace(' ', '', $s);
17
        $n = strlen($s);
18
        [$stack, $num, $operator] = [[], 0, '+'];
19
        for ($i = 0; $i < $n; $i++) {
20
            $char = $s[$i];
21
            if (is_numeric($char)) {
22
                $num = $char;
23
            }
24
            if ($i === $n - 1 || in_array($char, self::$operators)) {
25
                switch ($operator) {
26
                    case '+':
27
                        array_push($stack, $num);
28
                        break;
29
                    case '-':
30
                        array_push($stack, -$num);
31
                        break;
32
                    case '*':
33
                        array_push($stack, (int)(array_pop($stack) * $num));
34
                        break;
35
                    case '/':
36
                        array_push($stack, (int)(array_pop($stack) / $num));
37
                        break;
38
                }
39
                [$num, $operator] = [0, $char];
40
            }
41
        }
42
43
        return array_sum($stack);
0 ignored issues
show
Bug Best Practice introduced by
The expression return array_sum($stack) could return the type double which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
44
    }
45
46
    public static function calculate2(string $s): int
47
    {
48
        if (empty($s)) {
49
            return 0;
50
        }
51
        $s = str_replace(' ', '', $s);
52
        $n = strlen($s);
53
        [$stack, $operator] = [[], '+'];
54
55
        for ($i = 0; $i < $n; $i++) {
56
            $char = $s[$i];
57
            if (is_numeric($char)) {
58
                if ($operator === '+') {
59
                    array_push($stack, (int)$char);
60
                }
61
                if ($operator === '-') {
62
                    array_push($stack, -(int)$char);
63
                }
64
                if ($operator === '*') {
65
                    array_push($stack, array_pop($stack) * (int)$char);
66
                }
67
                if ($operator === '/') {
68
                    array_push($stack, (int)(array_pop($stack) / $char));
69
                }
70
            } else {
71
                $operator = $char;
72
            }
73
        }
74
75
        return array_sum($stack);
0 ignored issues
show
Bug Best Practice introduced by
The expression return array_sum($stack) could return the type double which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
76
    }
77
}
78