Buffer::size()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace ButterAMQP;
4
5
/**
6
 * Simple reading buffered data stream provide simple API to read data byte by byte.
7
 */
8
class Buffer
9
{
10
    /**
11
     * @var int
12
     */
13
    private $offset = 0;
14
15
    /**
16
     * @var int
17
     */
18
    private $length;
19
20
    /**
21
     * @var string
22
     */
23
    private $data;
24
25
    /**
26
     * @param string $data
27
     * @param int    $offset
28
     */
29 66
    public function __construct($data = '', $offset = 0)
30
    {
31 66
        $this->data = $data;
32 66
        $this->offset = $offset;
33 66
        $this->length = strlen($data);
34 66
    }
35
36
    /**
37
     * Return $length bytes and move offset.
38
     *
39
     * @param int $length
40
     *
41
     * @return string
42
     */
43 63
    public function read($length)
44
    {
45 63
        $buffer = substr($this->data, $this->offset, $length);
46 63
        $this->offset += $length;
47
48 63
        return $buffer;
49
    }
50
51
    /**
52
     * Return buffer total size.
53
     *
54
     * @return int
55
     */
56 1
    public function size()
57
    {
58 1
        return $this->length;
59
    }
60
61
    /**
62
     * Check if offset reached end of the buffer.
63
     *
64
     * @return string
65
     */
66 25
    public function eof()
67
    {
68 25
        return $this->offset >= $this->length;
69
    }
70
}
71