Completed
Push — develop ( 782b4e...557e80 )
by Adrien
43:38
created

Stack   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 79.17%

Importance

Changes 0
Metric Value
dl 0
loc 87
ccs 19
cts 24
cp 0.7917
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A count() 0 4 1
A pop() 0 8 2
A last() 0 8 2
A clear() 0 5 1
A push() 0 14 3
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Calculation\Token;
4
5
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
6
7
class Stack
8
{
9
    /**
10
     * The parser stack for formulae.
11
     *
12
     * @var mixed[]
13
     */
14
    private $stack = [];
15
16
    /**
17
     * Count of entries in the parser stack.
18
     *
19
     * @var int
20
     */
21
    private $count = 0;
22
23
    /**
24
     * Return the number of entries on the stack.
25
     *
26
     * @return int
27
     */
28 117
    public function count()
29
    {
30 117
        return $this->count;
31
    }
32
33
    /**
34
     * Push a new entry onto the stack.
35
     *
36
     * @param mixed $type
37
     * @param mixed $value
38
     * @param mixed $reference
39
     */
40 117
    public function push($type, $value, $reference = null)
41
    {
42 117
        $this->stack[$this->count++] = [
43 117
            'type' => $type,
44 117
            'value' => $value,
45 117
            'reference' => $reference,
46
        ];
47 117
        if ($type == 'Function') {
48 45
            $localeFunction = Calculation::localeFunc($value);
49 45
            if ($localeFunction != $value) {
50
                $this->stack[($this->count - 1)]['localeValue'] = $localeFunction;
51
            }
52
        }
53 117
    }
54
55
    /**
56
     * Pop the last entry from the stack.
57
     *
58
     * @return mixed
59
     */
60 117
    public function pop()
61
    {
62 117
        if ($this->count > 0) {
63 117
            return $this->stack[--$this->count];
64
        }
65
66 117
        return null;
67
    }
68
69
    /**
70
     * Return an entry from the stack without removing it.
71
     *
72
     * @param int $n number indicating how far back in the stack we want to look
73
     *
74
     * @return mixed
75
     */
76 117
    public function last($n = 1)
77
    {
78 117
        if ($this->count - $n < 0) {
79 111
            return null;
80
        }
81
82 116
        return $this->stack[$this->count - $n];
83
    }
84
85
    /**
86
     * Clear the stack.
87
     */
88
    public function clear()
89
    {
90
        $this->stack = [];
91
        $this->count = 0;
92
    }
93
}
94