Stack   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 142
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 95%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 17
c 1
b 0
f 1
lcom 1
cbo 2
dl 0
loc 142
ccs 38
cts 40
cp 0.95
rs 10

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A takeReference() 0 8 2
A offsetExists() 0 4 1
A offsetGet() 0 4 2
A offsetSet() 0 6 1
A offsetUnset() 0 4 1
A current() 0 4 1
A next() 0 4 1
A key() 0 4 1
A valid() 0 4 1
A rewind() 0 4 1
A top() 0 4 1
A pop() 0 4 1
A shift() 0 4 1
A count() 0 4 1
1
<?php
2
/**
3
 * Part of twLib
4
 * http://www.thomaswhiston.com
5
 * [email protected]
6
 * Created by PhpStorm.
7
 * User: Thomas Whiston
8
 * Date: 03/02/2016
9
 * Time: 20:44
10
 */
11
namespace twhiston\twLib\Reference;
12
13
use twhiston\twLib\Exception\TwLibException;
14
15
/**
16
 * Class Stack
17
 * Basic reference stack
18
 * add items to the stack with the takeReference function
19
 * Though this class has array access it will throw an exception if you attempt to add anything by reference as its impossible
20
 * @package twhiston\twLib\Reference
21
 */
22
class Stack implements \ArrayAccess, \Iterator, \Countable
23
{
24
25
    /**
26
     * @var Reference[]
27
     */
28
    private $stack;
29
30
    /**
31
     * @var int
32
     */
33
    private $pos;
34
35
    /**
36
     * Stack constructor.
37
     */
38 4
    public function __construct()
39
    {
40 4
        $this->pos = 0;
41 4
    }
42
43
    /**
44
     * @param $data
45
     */
46 4
    public function takeReference(&$data)
47
    {
48 4
        if ($data instanceof Reference) {
49 1
            $this->stack[] = $data;
50 1
        } else {
51 4
            $this->stack[] = new Reference($data);
52
        }
53 4
    }
54
55
    /**
56
     * @inheritDoc
57
     */
58
    public function offsetExists($offset)
59
    {
60
        return isset($this->stack[$offset]);
61
    }
62
63
    /**
64
     * @inheritDoc
65
     */
66 1
    public function offsetGet($offset)
67
    {
68 1
        return isset($this->stack[$offset]) ? $this->stack[$offset] : null;
69
    }
70
71
    /**
72
     * @inheritDoc
73
     */
74 1
    public function offsetSet($offset, $value)
75
    {
76 1
        throw new TwLibException(
77
            'Do not set stack references with the array operator'
78 1
        );
79
    }
80
81
    /**
82
     * @inheritDoc
83
     */
84 1
    public function offsetUnset($offset)
85
    {
86 1
        unset($this->stack[$offset]);
87 1
    }
88
89
90
    /**
91
     * @inheritDoc
92
     */
93 1
    public function current()
94
    {
95 1
        $this->stack[$this->pos];
96 1
    }
97
98
    /**
99
     * @inheritDoc
100
     */
101 1
    public function next()
102
    {
103 1
        ++$this->pos;
104 1
    }
105
106
    /**
107
     * @inheritDoc
108
     */
109 1
    public function key()
110
    {
111 1
        return $this->pos;
112
    }
113
114
    /**
115
     * @inheritDoc
116
     */
117 1
    public function valid()
118
    {
119 1
        return isset($this->stack[$this->pos]);
120
    }
121
122
    /**
123
     * @inheritDoc
124
     */
125 1
    public function rewind()
126
    {
127 1
        $this->pos = 0;
128 1
    }
129
130
    /**
131
     * @return \twhiston\twLib\Reference\Reference
132
     */
133 2
    public function &top()
134
    {
135 2
        return $this->stack[count($this->stack) - 1];
136
    }
137
138
    /**
139
     * Remove the Reference from the top of the stack
140
     */
141 2
    public function pop()
142
    {
143 2
        return array_pop($this->stack);
144
    }
145
146
    /**
147
     * Remove the Reference from the bottom of the stack
148
     */
149 1
    public function shift()
150
    {
151 1
        return array_shift($this->stack);
152
    }
153
154
    /**
155
     * @inheritDoc
156
     */
157 2
    public function count()
158
    {
159 2
        return count($this->stack);
160
    }
161
162
163
}