Buffer   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 0
dl 0
loc 63
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A read() 0 7 1
A size() 0 4 1
A eof() 0 4 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