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 ColumnCellIterator extends CellIterator implements \Iterator |
||
28 | { |
||
29 | /** |
||
30 | * Column index. |
||
31 | * |
||
32 | * @var string |
||
33 | */ |
||
34 | protected $columnIndex; |
||
35 | |||
36 | /** |
||
37 | * Start position. |
||
38 | * |
||
39 | * @var int |
||
40 | */ |
||
41 | protected $startRow = 1; |
||
42 | |||
43 | /** |
||
44 | * End position. |
||
45 | * |
||
46 | * @var int |
||
47 | */ |
||
48 | protected $endRow = 1; |
||
49 | |||
50 | /** |
||
51 | * Create a new row iterator. |
||
52 | * |
||
53 | * @param \PhpOffice\PhpSpreadsheet\Worksheet $subject The worksheet to iterate over |
||
54 | * @param string $columnIndex The column that we want to iterate |
||
55 | * @param int $startRow The row number at which to start iterating |
||
56 | * @param int $endRow Optionally, the row number at which to stop iterating |
||
57 | */ |
||
58 | 6 | View Code Duplication | public function __construct(\PhpOffice\PhpSpreadsheet\Worksheet $subject = null, $columnIndex = 'A', $startRow = 1, $endRow = null) |
|
|||
59 | { |
||
60 | // Set subject |
||
61 | 6 | $this->subject = $subject; |
|
62 | 6 | $this->columnIndex = \PhpOffice\PhpSpreadsheet\Cell::columnIndexFromString($columnIndex) - 1; |
|
63 | 6 | $this->resetEnd($endRow); |
|
64 | 6 | $this->resetStart($startRow); |
|
65 | 6 | } |
|
66 | |||
67 | /** |
||
68 | * Destructor. |
||
69 | */ |
||
70 | 6 | public function __destruct() |
|
74 | |||
75 | /** |
||
76 | * (Re)Set the start row and the current row pointer. |
||
77 | * |
||
78 | * @param int $startRow The row number at which to start iterating |
||
79 | * |
||
80 | * @throws \PhpOffice\PhpSpreadsheet\Exception |
||
81 | * |
||
82 | * @return ColumnCellIterator |
||
83 | */ |
||
84 | 6 | public function resetStart($startRow = 1) |
|
92 | |||
93 | /** |
||
94 | * (Re)Set the end row. |
||
95 | * |
||
96 | * @param int $endRow The row number at which to stop iterating |
||
97 | * |
||
98 | * @throws \PhpOffice\PhpSpreadsheet\Exception |
||
99 | * |
||
100 | * @return ColumnCellIterator |
||
101 | */ |
||
102 | 6 | public function resetEnd($endRow = null) |
|
109 | |||
110 | /** |
||
111 | * Set the row pointer to the selected row. |
||
112 | * |
||
113 | * @param int $row The row number to set the current pointer at |
||
114 | * |
||
115 | * @throws \PhpOffice\PhpSpreadsheet\Exception |
||
116 | * |
||
117 | * @return ColumnCellIterator |
||
118 | */ |
||
119 | 6 | View Code Duplication | public function seek($row = 1) |
130 | |||
131 | /** |
||
132 | * Rewind the iterator to the starting row. |
||
133 | */ |
||
134 | 2 | public function rewind() |
|
138 | |||
139 | /** |
||
140 | * Return the current cell in this worksheet column. |
||
141 | * |
||
142 | * @return null|\PhpOffice\PhpSpreadsheet\Cell |
||
143 | */ |
||
144 | 2 | public function current() |
|
148 | |||
149 | /** |
||
150 | * Return the current iterator key. |
||
151 | * |
||
152 | * @return int |
||
153 | */ |
||
154 | 3 | public function key() |
|
158 | |||
159 | /** |
||
160 | * Set the iterator to its next value. |
||
161 | */ |
||
162 | 2 | View Code Duplication | public function next() |
163 | { |
||
164 | do { |
||
165 | 2 | ++$this->position; |
|
166 | 2 | } while (($this->onlyExistingCells) && |
|
167 | (!$this->subject->cellExistsByColumnAndRow($this->columnIndex, $this->position)) && |
||
168 | ($this->position <= $this->endRow)); |
||
169 | 2 | } |
|
170 | |||
171 | /** |
||
172 | * Set the iterator to its previous value. |
||
173 | */ |
||
174 | 2 | public function prev() |
|
175 | { |
||
176 | 2 | if ($this->position <= $this->startRow) { |
|
177 | 1 | throw new \PhpOffice\PhpSpreadsheet\Exception("Row is already at the beginning of range ({$this->startRow} - {$this->endRow})"); |
|
178 | } |
||
179 | |||
180 | do { |
||
181 | 1 | --$this->position; |
|
182 | 1 | } while (($this->onlyExistingCells) && |
|
183 | (!$this->subject->cellExistsByColumnAndRow($this->columnIndex, $this->position)) && |
||
184 | ($this->position >= $this->startRow)); |
||
185 | 1 | } |
|
186 | |||
187 | /** |
||
188 | * Indicate if more rows exist in the worksheet range of rows that we're iterating. |
||
189 | * |
||
190 | * @return bool |
||
191 | */ |
||
192 | 2 | public function valid() |
|
196 | |||
197 | /** |
||
198 | * Validate start/end values for "IterateOnlyExistingCells" mode, and adjust if necessary. |
||
199 | * |
||
200 | * @throws \PhpOffice\PhpSpreadsheet\Exception |
||
201 | */ |
||
202 | 6 | View Code Duplication | protected function adjustForExistingOnlyRange() |
221 | } |
||
222 |
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.