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 |
||
27 | class RowCellIterator extends CellIterator implements \Iterator |
||
28 | { |
||
29 | /** |
||
30 | * Row index. |
||
31 | * |
||
32 | * @var int |
||
33 | */ |
||
34 | protected $rowIndex; |
||
35 | |||
36 | /** |
||
37 | * Start position. |
||
38 | * |
||
39 | * @var int |
||
40 | */ |
||
41 | protected $startColumn = 0; |
||
42 | |||
43 | /** |
||
44 | * End position. |
||
45 | * |
||
46 | * @var int |
||
47 | */ |
||
48 | protected $endColumn = 0; |
||
49 | |||
50 | /** |
||
51 | * Create a new column iterator. |
||
52 | * |
||
53 | * @param \PhpOffice\PhpSpreadsheet\Worksheet $subject The worksheet to iterate over |
||
54 | * @param int $rowIndex The row that we want to iterate |
||
55 | * @param string $startColumn The column address at which to start iterating |
||
56 | * @param string $endColumn Optionally, the column address at which to stop iterating |
||
57 | */ |
||
58 | 9 | View Code Duplication | public function __construct(\PhpOffice\PhpSpreadsheet\Worksheet $subject = null, $rowIndex = 1, $startColumn = 'A', $endColumn = null) |
66 | |||
67 | /** |
||
68 | * Destructor. |
||
69 | */ |
||
70 | 9 | public function __destruct() |
|
74 | |||
75 | /** |
||
76 | * (Re)Set the start column and the current column pointer. |
||
77 | * |
||
78 | * @param int $startColumn The column address at which to start iterating |
||
79 | * |
||
80 | * @throws \PhpOffice\PhpSpreadsheet\Exception |
||
81 | * |
||
82 | * @return RowCellIterator |
||
83 | */ |
||
84 | 9 | public function resetStart($startColumn = 'A') |
|
93 | |||
94 | /** |
||
95 | * (Re)Set the end column. |
||
96 | * |
||
97 | * @param string $endColumn The column address at which to stop iterating |
||
98 | * |
||
99 | * @throws \PhpOffice\PhpSpreadsheet\Exception |
||
100 | * |
||
101 | * @return RowCellIterator |
||
102 | */ |
||
103 | 9 | View Code Duplication | public function resetEnd($endColumn = null) |
111 | |||
112 | /** |
||
113 | * Set the column pointer to the selected column. |
||
114 | * |
||
115 | * @param string $column The column address to set the current pointer at |
||
116 | * |
||
117 | * @throws \PhpOffice\PhpSpreadsheet\Exception |
||
118 | * |
||
119 | * @return RowCellIterator |
||
120 | */ |
||
121 | 9 | View Code Duplication | public function seek($column = 'A') |
133 | |||
134 | /** |
||
135 | * Rewind the iterator to the starting column. |
||
136 | */ |
||
137 | 3 | public function rewind() |
|
141 | |||
142 | /** |
||
143 | * Return the current cell in this worksheet row. |
||
144 | * |
||
145 | * @return \PhpOffice\PhpSpreadsheet\Cell |
||
146 | */ |
||
147 | 5 | public function current() |
|
151 | |||
152 | /** |
||
153 | * Return the current iterator key. |
||
154 | * |
||
155 | * @return string |
||
156 | */ |
||
157 | 3 | public function key() |
|
161 | |||
162 | /** |
||
163 | * Set the iterator to its next value. |
||
164 | */ |
||
165 | 5 | View Code Duplication | public function next() |
166 | { |
||
167 | do { |
||
168 | 5 | ++$this->position; |
|
169 | 5 | } while (($this->onlyExistingCells) && |
|
170 | (!$this->subject->cellExistsByColumnAndRow($this->position, $this->rowIndex)) && |
||
171 | ($this->position <= $this->endColumn)); |
||
172 | 5 | } |
|
173 | |||
174 | /** |
||
175 | * Set the iterator to its previous value. |
||
176 | * |
||
177 | * @throws \PhpOffice\PhpSpreadsheet\Exception |
||
178 | */ |
||
179 | 2 | public function prev() |
|
180 | { |
||
181 | 2 | View Code Duplication | if ($this->position <= $this->startColumn) { |
182 | 1 | throw new \PhpOffice\PhpSpreadsheet\Exception( |
|
183 | 'Column is already at the beginning of range (' . |
||
184 | 1 | \PhpOffice\PhpSpreadsheet\Cell::stringFromColumnIndex($this->endColumn) . ' - ' . |
|
185 | 1 | \PhpOffice\PhpSpreadsheet\Cell::stringFromColumnIndex($this->endColumn) . ')' |
|
186 | ); |
||
187 | } |
||
188 | |||
189 | do { |
||
190 | 1 | --$this->position; |
|
191 | 1 | } while (($this->onlyExistingCells) && |
|
192 | (!$this->subject->cellExistsByColumnAndRow($this->position, $this->rowIndex)) && |
||
193 | ($this->position >= $this->startColumn)); |
||
194 | 1 | } |
|
195 | |||
196 | /** |
||
197 | * Indicate if more columns exist in the worksheet range of columns that we're iterating. |
||
198 | * |
||
199 | * @return bool |
||
200 | */ |
||
201 | 5 | public function valid() |
|
205 | |||
206 | /** |
||
207 | * Validate start/end values for "IterateOnlyExistingCells" mode, and adjust if necessary. |
||
208 | * |
||
209 | * @throws \PhpOffice\PhpSpreadsheet\Exception |
||
210 | */ |
||
211 | 9 | View Code Duplication | protected function adjustForExistingOnlyRange() |
230 | } |
||
231 |
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.