Conditions | 37 |
Paths | 600 |
Total Lines | 173 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
172 | protected function finalizeTable() |
||
173 | { |
||
174 | |||
175 | // Add the max cols and rows to the table opening |
||
176 | if ($this->tableCalls[0][0] == 'table_open') { |
||
177 | // Adjust to num cols not num col delimeters |
||
178 | $this->tableCalls[0][1][] = $this->maxCols - 1; |
||
179 | $this->tableCalls[0][1][] = $this->maxRows; |
||
180 | $this->tableCalls[0][1][] = array_shift($this->tableCalls[0][1]); |
||
181 | } else { |
||
182 | trigger_error('First element in table call list is not table_open'); |
||
183 | } |
||
184 | |||
185 | $lastRow = 0; |
||
186 | $lastCell = 0; |
||
187 | $cellKey = array(); |
||
188 | $toDelete = array(); |
||
189 | |||
190 | // if still in tableheader, then there can be no table header |
||
191 | // as all rows can't be within <THEAD> |
||
192 | if ($this->inTableHead) { |
||
193 | $this->inTableHead = false; |
||
194 | $this->countTableHeadRows = 0; |
||
195 | } |
||
196 | |||
197 | // Look for the colspan elements and increment the colspan on the |
||
198 | // previous non-empty opening cell. Once done, delete all the cells |
||
199 | // that contain colspans |
||
200 | for ($key = 0; $key < count($this->tableCalls); ++$key) { |
||
|
|||
201 | $call = $this->tableCalls[$key]; |
||
202 | |||
203 | switch ($call[0]) { |
||
204 | case 'table_open': |
||
205 | if ($this->countTableHeadRows) { |
||
206 | array_splice($this->tableCalls, $key+1, 0, array( |
||
207 | array('tablethead_open', array(), $call[2]))); |
||
208 | } |
||
209 | break; |
||
210 | |||
211 | case 'tablerow_open': |
||
212 | $lastRow++; |
||
213 | $lastCell = 0; |
||
214 | break; |
||
215 | |||
216 | case 'tablecell_open': |
||
217 | case 'tableheader_open': |
||
218 | $lastCell++; |
||
219 | $cellKey[$lastRow][$lastCell] = $key; |
||
220 | break; |
||
221 | |||
222 | case 'table_align': |
||
223 | $prev = in_array($this->tableCalls[$key-1][0], array('tablecell_open', 'tableheader_open')); |
||
224 | $next = in_array($this->tableCalls[$key+1][0], array('tablecell_close', 'tableheader_close')); |
||
225 | // If the cell is empty, align left |
||
226 | if ($prev && $next) { |
||
227 | $this->tableCalls[$key-1][1][1] = 'left'; |
||
228 | |||
229 | // If the previous element was a cell open, align right |
||
230 | } elseif ($prev) { |
||
231 | $this->tableCalls[$key-1][1][1] = 'right'; |
||
232 | |||
233 | // If the next element is the close of an element, align either center or left |
||
234 | } elseif ($next) { |
||
235 | if ($this->tableCalls[$cellKey[$lastRow][$lastCell]][1][1] == 'right') { |
||
236 | $this->tableCalls[$cellKey[$lastRow][$lastCell]][1][1] = 'center'; |
||
237 | } else { |
||
238 | $this->tableCalls[$cellKey[$lastRow][$lastCell]][1][1] = 'left'; |
||
239 | } |
||
240 | } |
||
241 | |||
242 | // Now convert the whitespace back to cdata |
||
243 | $this->tableCalls[$key][0] = 'cdata'; |
||
244 | break; |
||
245 | |||
246 | case 'colspan': |
||
247 | $this->tableCalls[$key-1][1][0] = false; |
||
248 | |||
249 | for ($i = $key-2; $i >= $cellKey[$lastRow][1]; $i--) { |
||
250 | if ($this->tableCalls[$i][0] == 'tablecell_open' || |
||
251 | $this->tableCalls[$i][0] == 'tableheader_open' |
||
252 | ) { |
||
253 | if (false !== $this->tableCalls[$i][1][0]) { |
||
254 | $this->tableCalls[$i][1][0]++; |
||
255 | break; |
||
256 | } |
||
257 | } |
||
258 | } |
||
259 | |||
260 | $toDelete[] = $key-1; |
||
261 | $toDelete[] = $key; |
||
262 | $toDelete[] = $key+1; |
||
263 | break; |
||
264 | |||
265 | case 'rowspan': |
||
266 | if ($this->tableCalls[$key-1][0] == 'cdata') { |
||
267 | // ignore rowspan if previous call was cdata (text mixed with :::) |
||
268 | // we don't have to check next call as that wont match regex |
||
269 | $this->tableCalls[$key][0] = 'cdata'; |
||
270 | } else { |
||
271 | $spanning_cell = null; |
||
272 | |||
273 | // can't cross thead/tbody boundary |
||
274 | if (!$this->countTableHeadRows || ($lastRow-1 != $this->countTableHeadRows)) { |
||
275 | for ($i = $lastRow-1; $i > 0; $i--) { |
||
276 | if ($this->tableCalls[$cellKey[$i][$lastCell]][0] == 'tablecell_open' || |
||
277 | $this->tableCalls[$cellKey[$i][$lastCell]][0] == 'tableheader_open' |
||
278 | ) { |
||
279 | if ($this->tableCalls[$cellKey[$i][$lastCell]][1][2] >= $lastRow - $i) { |
||
280 | $spanning_cell = $i; |
||
281 | break; |
||
282 | } |
||
283 | } |
||
284 | } |
||
285 | } |
||
286 | if (is_null($spanning_cell)) { |
||
287 | // No spanning cell found, so convert this cell to |
||
288 | // an empty one to avoid broken tables |
||
289 | $this->tableCalls[$key][0] = 'cdata'; |
||
290 | $this->tableCalls[$key][1][0] = ''; |
||
291 | break; |
||
292 | } |
||
293 | $this->tableCalls[$cellKey[$spanning_cell][$lastCell]][1][2]++; |
||
294 | |||
295 | $this->tableCalls[$key-1][1][2] = false; |
||
296 | |||
297 | $toDelete[] = $key-1; |
||
298 | $toDelete[] = $key; |
||
299 | $toDelete[] = $key+1; |
||
300 | } |
||
301 | break; |
||
302 | |||
303 | case 'tablerow_close': |
||
304 | // Fix broken tables by adding missing cells |
||
305 | $moreCalls = array(); |
||
306 | while (++$lastCell < $this->maxCols) { |
||
307 | $moreCalls[] = array('tablecell_open', array(1, null, 1), $call[2]); |
||
308 | $moreCalls[] = array('cdata', array(''), $call[2]); |
||
309 | $moreCalls[] = array('tablecell_close', array(), $call[2]); |
||
310 | } |
||
311 | $moreCallsLength = count($moreCalls); |
||
312 | if ($moreCallsLength) { |
||
313 | array_splice($this->tableCalls, $key, 0, $moreCalls); |
||
314 | $key += $moreCallsLength; |
||
315 | } |
||
316 | |||
317 | if ($this->countTableHeadRows == $lastRow) { |
||
318 | array_splice($this->tableCalls, $key+1, 0, array( |
||
319 | array('tablethead_close', array(), $call[2]))); |
||
320 | } |
||
321 | break; |
||
322 | } |
||
323 | } |
||
324 | |||
325 | // condense cdata |
||
326 | $cnt = count($this->tableCalls); |
||
327 | for ($key = 0; $key < $cnt; $key++) { |
||
328 | if ($this->tableCalls[$key][0] == 'cdata') { |
||
329 | $ckey = $key; |
||
330 | $key++; |
||
331 | while ($this->tableCalls[$key][0] == 'cdata') { |
||
332 | $this->tableCalls[$ckey][1][0] .= $this->tableCalls[$key][1][0]; |
||
333 | $toDelete[] = $key; |
||
334 | $key++; |
||
335 | } |
||
336 | continue; |
||
337 | } |
||
338 | } |
||
339 | |||
340 | foreach ($toDelete as $delete) { |
||
341 | unset($this->tableCalls[$delete]); |
||
342 | } |
||
343 | $this->tableCalls = array_values($this->tableCalls); |
||
344 | } |
||
345 | } |
||
346 |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: