Buffer   A
last analyzed

Complexity

Total Complexity 34

Size/Duplication

Total Lines 279
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 96.67%

Importance

Changes 0
Metric Value
wmc 34
lcom 1
cbo 1
dl 0
loc 279
ccs 87
cts 90
cp 0.9667
rs 9.2
c 0
b 0
f 0

21 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __destruct() 0 4 1
A __toString() 0 4 1
A length() 0 4 1
A count() 0 4 1
A isEmpty() 0 4 1
A push() 0 4 1
A unshift() 0 4 1
A shift() 0 21 3
B peek() 0 27 6
A pop() 0 14 2
B remove() 0 27 4
A drain() 0 6 1
A insert() 0 4 1
A replace() 0 6 1
A search() 0 9 2
A offsetExists() 0 4 1
A offsetGet() 0 4 1
A offsetSet() 0 4 1
A offsetUnset() 0 7 2
A getIterator() 0 4 1
1
<?php
2
3
namespace Dazzle\Util\Buffer;
4
5
class Buffer implements BufferInterface
6
{
7
    /**
8
     * @var string
9
     */
10
    private $data;
11
    
12
    /**
13
     * @param string $data
14
     */
15 39
    public function __construct($data = '')
16
    {
17 39
        $this->data = (string) $data;
18 39
    }
19
20
    /**
21
     *
22
     */
23 1
    public function __destruct()
24
    {
25 1
        unset($this->data);
26 1
    }
27
28
    /**
29
     * @override
30
     * @inheritDoc
31
     */
32 25
    public function __toString()
33
    {
34 25
        return $this->data;
35
    }
36
37
    /**
38
     * @override
39
     * @inheritDoc
40
     */
41 3
    public function length()
42
    {
43 3
        return strlen($this->data);
44
    }
45
46
    /**
47
     * @override
48
     * @inheritDoc
49
     */
50 1
    public function count()
51
    {
52 1
        return $this->length();
53
    }
54
55
    /**
56
     * @override
57
     * @inheritDoc
58
     */
59 3
    public function isEmpty()
60
    {
61 3
        return '' === $this->data;
62
    }
63
64
    /**
65
     * @override
66
     * @inheritDoc
67
     */
68 2
    public function push($data)
69
    {
70 2
        $this->data .= $data;
71 2
    }
72
73
    /**
74
     * @override
75
     * @inheritDoc
76
     */
77 1
    public function unshift($data)
78
    {
79 1
        $this->data = $data . $this->data;
80 1
    }
81
82
    /**
83
     * @override
84
     * @inheritDoc
85
     */
86 2
    public function shift($length)
87
    {
88 2
        $length = (int) $length;
89 2
        if (0 >= $length)
90
        {
91 1
            return '';
92
        }
93
94 1
        if (strlen($this->data) <= $length)
95
        {
96
            $buffer = $this->data;
97
            $this->data = '';
98
            return $buffer;
99
        }
100
101 1
        $buffer = (string) substr($this->data, 0, $length);
102
103 1
        $this->data = (string) substr($this->data, $length);
104
105 1
        return $buffer;
106
    }
107
108
    /**
109
     * @override
110
     * @inheritDoc
111
     */
112 6
    public function peek($length = 0, $offset = 0)
113
    {
114 6
        $length = (int) $length;
115 6
        if (0 > $length)
116
        {
117 1
            return '';
118
        }
119 5
        else if (0 === $length)
120
        {
121 1
            return $this->data;
122
        }
123
124 4
        $offset = (int) $offset;
125 4
        if (0 > $offset)
126
        {
127 1
            $offset = 0;
128
        }
129
130 4
        if (0 === $offset && strlen($this->data) <= $length)
131
        {
132 1
            return $this->data;
133
        }
134
135 3
        $result = (string) substr($this->data, $offset, $length);
136
137 3
        return $result;
138
    }
139
140
    /**
141
     * @override
142
     * @inheritDoc
143
     */
144 2
    public function pop($length)
145
    {
146 2
        $length = (int) $length;
147 2
        if (0 >= $length)
148
        {
149 1
            return '';
150
        }
151
152 1
        $buffer = (string) substr($this->data, -$length);
153
154 1
        $this->data = (string) substr($this->data, 0, -$length);
155
156 1
        return $buffer;
157
    }
158
159
    /**
160
     * @override
161
     * @inheritDoc
162
     */
163 4
    public function remove($length, $offset = 0)
164
    {
165 4
        $length = (int) $length;
166 4
        if (0 >= $length)
167
        {
168 1
            return '';
169
        }
170
171 3
        $offset = (int) $offset;
172 3
        if (0 > $offset)
173
        {
174 1
            $offset = 0;
175
        }
176
177 3
        $buffer = (string) substr($this->data, $offset, $length);
178
179 3
        if (0 === $offset)
180
        {
181 2
            $this->data = (string) substr($this->data, $length);
182
        }
183
        else
184
        {
185 1
            $this->data = (string) (substr($this->data, 0, $offset) . substr($this->data, $offset + $length));
186
        }
187
188 3
        return $buffer;
189
    }
190
191
    /**
192
     * @override
193
     * @inheritDoc
194
     */
195 1
    public function drain()
196
    {
197 1
        $buffer = $this->data;
198 1
        $this->data = '';
199 1
        return $buffer;
200
    }
201
202
    /**
203
     * @override
204
     * @inheritDoc
205
     */
206 1
    public function insert($string, $position)
207
    {
208 1
        $this->data = substr_replace($this->data, $string, $position, 0);
209 1
    }
210
211
    /**
212
     * @override
213
     * @inheritDoc
214
     */
215 1
    public function replace($search, $replace)
216
    {
217 1
        $this->data = str_replace($search, $replace, $this->data, $count);
218
219 1
        return $count;
220
    }
221
222
    /**
223
     * @override
224
     * @inheritDoc
225
     */
226 2
    public function search($string, $reverse = false)
227
    {
228 2
        if ($reverse)
229
        {
230 1
            return strrpos($this->data, $string);
231
        }
232
233 1
        return strpos($this->data, $string);
234
    }
235
236
    /**
237
     * @override
238
     * @inheritDoc
239
     */
240 7
    public function offsetExists($index)
241
    {
242 7
        return isset($this->data[$index]);
243
    }
244
245
    /**
246
     * @override
247
     * @inheritDoc
248
     */
249 5
    public function offsetGet($index)
250
    {
251 5
        return $this->data[$index];
252
    }
253
254
    /**
255
     * @override
256
     * @inheritDoc
257
     */
258 3
    public function offsetSet($index, $data)
259
    {
260 3
        $this->data = substr_replace($this->data, $data, $index, 1);
261 3
    }
262
263
    /**
264
     * @override
265
     * @inheritDoc
266
     */
267 2
    public function offsetUnset($index)
268
    {
269 2
        if (isset($this->data[$index]))
270
        {
271 2
            $this->data = substr_replace($this->data, null, $index, 1);
272
        }
273 2
    }
274
275
    /**
276
     * @override
277
     * @inheritDoc
278
     */
279 9
    public function getIterator()
280
    {
281 9
        return new BufferIterator($this);
282
    }
283
}
284