BufferIterator   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 157
Duplicated Lines 23.57 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 93.33%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 1
dl 37
loc 157
ccs 42
cts 45
cp 0.9333
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __destruct() 0 4 1
A rewind() 0 4 1
A valid() 0 4 1
A key() 0 4 1
A current() 0 4 1
A next() 0 4 1
A prev() 0 4 1
A seek() 0 10 2
A insert() 9 9 2
A replace() 13 13 2
A remove() 15 15 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Dazzle\Util\Buffer;
4
5
use Dazzle\Throwable\Exception\Runtime\OutOfBoundsException;
6
use SeekableIterator;
7
8
class BufferIterator implements SeekableIterator
9
{
10
    /**
11
     * @var BufferInterface
12
     */
13
    private $buffer;
14
    
15
    /**
16
     * @var int
17
     */
18
    private $current = 0;
19
20
    /**
21
     * @param BufferInterface $buffer
22
     */
23 9
    public function __construct(BufferInterface $buffer)
24
    {
25 9
        $this->buffer = $buffer;
26 9
    }
27
28
    /**
29
     *
30
     */
31 1
    public function __destruct()
32
    {
33 1
        unset($this->buffer);
34 1
    }
35
    
36
    /**
37
     * Rewind the iterator to the beginning of the buffer.
38
     */
39 3
    public function rewind()
40
    {
41 3
        $this->current = 0;
42 3
    }
43
    
44
    /**
45
     * Determine if the iterator is valid.
46
     *
47
     * @return bool
48
     */
49 6
    public function valid()
50
    {
51 6
        return isset($this->buffer[$this->current]);
52
    }
53
    
54
    /**
55
     * Return the current position (key) of the iterator.
56
     *
57
     * @return int
58
     */
59 2
    public function key()
60
    {
61 2
        return $this->current;
62
    }
63
    
64
    /**
65
     * Return the current character in the buffer at the iterator position.
66
     *
67
     * @return string
68
     */
69 1
    public function current()
70
    {
71 1
        return $this->buffer[$this->current];
72
    }
73
    
74
    /**
75
     * Move to the next character in the buffer.
76
     */
77 5
    public function next()
78
    {
79 5
        ++$this->current;
80 5
    }
81
    
82
    /**
83
     * Move to the previous character in the buffer.
84
     */
85
    public function prev()
86
    {
87
        --$this->current;
88
    }
89
    
90
    /**
91
     * Move to the given position in the buffer.
92
     *
93
     * @param int $position
94
     */
95 2
    public function seek($position)
96
    {
97 2
        $position = (int) $position;
98 2
        if (0 > $position)
99
        {
100 1
            $position = 0;
101
        }
102
        
103 2
        $this->current = $position;
104 2
    }
105
    
106
    /**
107
     * Insert the given string into the buffer at the current iterator position.
108
     *
109
     * @param string $data
110
     * @throws OutOfBoundsException
111
     */
112 2 View Code Duplication
    public function insert($data)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
113
    {
114 2
        if (!$this->valid())
115
        {
116 1
            throw new OutOfBoundsException('The iterator is not valid!');
117
        }
118
        
119 1
        $this->buffer[$this->current] = $data . $this->buffer[$this->current];
120 1
    }
121
    
122
    /**
123
     * Replace the byte at the current iterator position with the given string.
124
     *
125
     * @param string $data
126
     * @return string
127
     * @throws OutOfBoundsException
128
     */
129 2 View Code Duplication
    public function replace($data)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
130
    {
131 2
        if (!$this->valid())
132
        {
133 1
            throw new OutOfBoundsException('The iterator is not valid!');
134
        }
135
        
136 1
        $temp = $this->buffer[$this->current];
137
        
138 1
        $this->buffer[$this->current] = $data;
139
        
140 1
        return $temp;
141
    }
142
    
143
    /**
144
     * Remove the byte at the current iterator position and moves the iterator to the previous character.
145
     *
146
     * @return string
147
     * @throws OutOfBoundsException
148
     */
149 2 View Code Duplication
    public function remove()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
150
    {
151 2
        if (!$this->valid())
152
        {
153 1
            throw new OutOfBoundsException('The iterator is not valid!');
154
        }
155
        
156 1
        $temp = $this->buffer[$this->current];
157
        
158 1
        unset($this->buffer[$this->current]);
159
        
160 1
        --$this->current;
161
162 1
        return $temp;
163
    }
164
}
165