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 |
||
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) |
|
27 | |||
28 | /** |
||
29 | * |
||
30 | */ |
||
31 | 1 | public function __destruct() |
|
35 | |||
36 | /** |
||
37 | * Rewind the iterator to the beginning of the buffer. |
||
38 | */ |
||
39 | 3 | public function rewind() |
|
43 | |||
44 | /** |
||
45 | * Determine if the iterator is valid. |
||
46 | * |
||
47 | * @return bool |
||
48 | */ |
||
49 | 6 | public function valid() |
|
53 | |||
54 | /** |
||
55 | * Return the current position (key) of the iterator. |
||
56 | * |
||
57 | * @return int |
||
58 | */ |
||
59 | 2 | public function key() |
|
63 | |||
64 | /** |
||
65 | * Return the current character in the buffer at the iterator position. |
||
66 | * |
||
67 | * @return string |
||
68 | */ |
||
69 | 1 | public function current() |
|
73 | |||
74 | /** |
||
75 | * Move to the next character in the buffer. |
||
76 | */ |
||
77 | 5 | public function next() |
|
81 | |||
82 | /** |
||
83 | * Move to the previous character in the buffer. |
||
84 | */ |
||
85 | public function prev() |
||
89 | |||
90 | /** |
||
91 | * Move to the given position in the buffer. |
||
92 | * |
||
93 | * @param int $position |
||
94 | */ |
||
95 | 2 | public function seek($position) |
|
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) |
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) |
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() |
164 | } |
||
165 |
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.