Conditions | 61 |
Paths | > 20000 |
Total Lines | 261 |
Code Lines | 154 |
Lines | 72 |
Ratio | 27.59 % |
Changes | 3 | ||
Bugs | 1 | Features | 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 |
||
211 | if ($this->isSupervisor) { |
||
212 | $pRange = $this->getSelectedCells(); |
||
213 | |||
214 | // Uppercase coordinate |
||
215 | $pRange = strtoupper($pRange); |
||
216 | |||
217 | // Is it a cell range or a single cell? |
||
218 | View Code Duplication | if (strpos($pRange, ':') === false) { |
|
219 | $rangeA = $pRange; |
||
220 | $rangeB = $pRange; |
||
221 | } else { |
||
222 | list($rangeA, $rangeB) = explode(':', $pRange); |
||
223 | } |
||
224 | |||
225 | // Calculate range outer borders |
||
226 | $rangeStart = Cell::coordinateFromString($rangeA); |
||
227 | $rangeEnd = Cell::coordinateFromString($rangeB); |
||
228 | |||
229 | // Translate column into index |
||
230 | $rangeStart[0] = Cell::columnIndexFromString($rangeStart[0]) - 1; |
||
231 | $rangeEnd[0] = Cell::columnIndexFromString($rangeEnd[0]) - 1; |
||
232 | |||
233 | // Make sure we can loop upwards on rows and columns |
||
234 | View Code Duplication | if ($rangeStart[0] > $rangeEnd[0] && $rangeStart[1] > $rangeEnd[1]) { |
|
235 | $tmp = $rangeStart; |
||
236 | $rangeStart = $rangeEnd; |
||
237 | $rangeEnd = $tmp; |
||
238 | } |
||
239 | |||
240 | // ADVANCED MODE: |
||
241 | if ($pAdvanced && isset($pStyles['borders'])) { |
||
242 | // 'allborders' is a shorthand property for 'outline' and 'inside' and |
||
243 | // it applies to components that have not been set explicitly |
||
244 | View Code Duplication | if (isset($pStyles['borders']['allborders'])) { |
|
245 | foreach (array('outline', 'inside') as $component) { |
||
246 | if (!isset($pStyles['borders'][$component])) { |
||
247 | $pStyles['borders'][$component] = $pStyles['borders']['allborders']; |
||
248 | } |
||
249 | } |
||
250 | unset($pStyles['borders']['allborders']); // not needed any more |
||
251 | } |
||
252 | // 'outline' is a shorthand property for 'top', 'right', 'bottom', 'left' |
||
253 | // it applies to components that have not been set explicitly |
||
254 | View Code Duplication | if (isset($pStyles['borders']['outline'])) { |
|
255 | foreach (array('top', 'right', 'bottom', 'left') as $component) { |
||
256 | if (!isset($pStyles['borders'][$component])) { |
||
257 | $pStyles['borders'][$component] = $pStyles['borders']['outline']; |
||
258 | } |
||
259 | } |
||
260 | unset($pStyles['borders']['outline']); // not needed any more |
||
261 | } |
||
262 | // 'inside' is a shorthand property for 'vertical' and 'horizontal' |
||
263 | // it applies to components that have not been set explicitly |
||
264 | View Code Duplication | if (isset($pStyles['borders']['inside'])) { |
|
265 | foreach (array('vertical', 'horizontal') as $component) { |
||
266 | if (!isset($pStyles['borders'][$component])) { |
||
267 | $pStyles['borders'][$component] = $pStyles['borders']['inside']; |
||
268 | } |
||
269 | } |
||
270 | unset($pStyles['borders']['inside']); // not needed any more |
||
271 | } |
||
272 | // width and height characteristics of selection, 1, 2, or 3 (for 3 or more) |
||
273 | $xMax = min($rangeEnd[0] - $rangeStart[0] + 1, 3); |
||
274 | $yMax = min($rangeEnd[1] - $rangeStart[1] + 1, 3); |
||
275 | |||
276 | // loop through up to 3 x 3 = 9 regions |
||
277 | for ($x = 1; $x <= $xMax; ++$x) { |
||
278 | // start column index for region |
||
279 | $colStart = ($x == 3) ? |
||
280 | Cell::stringFromColumnIndex($rangeEnd[0]) |
||
281 | : Cell::stringFromColumnIndex($rangeStart[0] + $x - 1); |
||
282 | // end column index for region |
||
283 | $colEnd = ($x == 1) ? |
||
284 | Cell::stringFromColumnIndex($rangeStart[0]) |
||
285 | : Cell::stringFromColumnIndex($rangeEnd[0] - $xMax + $x); |
||
286 | |||
287 | for ($y = 1; $y <= $yMax; ++$y) { |
||
288 | // which edges are touching the region |
||
289 | $edges = array(); |
||
290 | if ($x == 1) { |
||
291 | // are we at left edge |
||
292 | $edges[] = 'left'; |
||
293 | } |
||
294 | if ($x == $xMax) { |
||
295 | // are we at right edge |
||
296 | $edges[] = 'right'; |
||
297 | } |
||
298 | if ($y == 1) { |
||
299 | // are we at top edge? |
||
300 | $edges[] = 'top'; |
||
301 | } |
||
302 | if ($y == $yMax) { |
||
303 | // are we at bottom edge? |
||
304 | $edges[] = 'bottom'; |
||
305 | } |
||
306 | |||
307 | // start row index for region |
||
308 | $rowStart = ($y == 3) ? |
||
309 | $rangeEnd[1] : $rangeStart[1] + $y - 1; |
||
310 | |||
311 | // end row index for region |
||
312 | $rowEnd = ($y == 1) ? |
||
313 | $rangeStart[1] : $rangeEnd[1] - $yMax + $y; |
||
314 | |||
315 | // build range for region |
||
316 | $range = $colStart . $rowStart . ':' . $colEnd . $rowEnd; |
||
317 | |||
318 | // retrieve relevant style array for region |
||
319 | $regionStyles = $pStyles; |
||
320 | unset($regionStyles['borders']['inside']); |
||
321 | |||
322 | // what are the inner edges of the region when looking at the selection |
||
323 | $innerEdges = array_diff(array('top', 'right', 'bottom', 'left'), $edges); |
||
324 | |||
325 | // inner edges that are not touching the region should take the 'inside' border properties if they have been set |
||
326 | foreach ($innerEdges as $innerEdge) { |
||
327 | switch ($innerEdge) { |
||
328 | case 'top': |
||
329 | View Code Duplication | case 'bottom': |
|
330 | // should pick up 'horizontal' border property if set |
||
331 | if (isset($pStyles['borders']['horizontal'])) { |
||
332 | $regionStyles['borders'][$innerEdge] = $pStyles['borders']['horizontal']; |
||
333 | } else { |
||
334 | unset($regionStyles['borders'][$innerEdge]); |
||
335 | } |
||
336 | break; |
||
337 | case 'left': |
||
338 | View Code Duplication | case 'right': |
|
339 | // should pick up 'vertical' border property if set |
||
340 | if (isset($pStyles['borders']['vertical'])) { |
||
341 | $regionStyles['borders'][$innerEdge] = $pStyles['borders']['vertical']; |
||
342 | } else { |
||
343 | unset($regionStyles['borders'][$innerEdge]); |
||
344 | } |
||
345 | break; |
||
346 | } |
||
347 | } |
||
348 | |||
349 | // apply region style to region by calling applyFromArray() in simple mode |
||
350 | $this->getActiveSheet()->getStyle($range)->applyFromArray($regionStyles, false); |
||
351 | } |
||
352 | } |
||
353 | return $this; |
||
354 | } |
||
355 | |||
356 | // SIMPLE MODE: |
||
357 | // Selection type, inspect |
||
358 | if (preg_match('/^[A-Z]+1:[A-Z]+1048576$/', $pRange)) { |
||
359 | $selectionType = 'COLUMN'; |
||
360 | } elseif (preg_match('/^A[0-9]+:XFD[0-9]+$/', $pRange)) { |
||
361 | $selectionType = 'ROW'; |
||
362 | } else { |
||
363 | $selectionType = 'CELL'; |
||
364 | } |
||
365 | |||
366 | // First loop through columns, rows, or cells to find out which styles are affected by this operation |
||
367 | switch ($selectionType) { |
||
368 | case 'COLUMN': |
||
369 | $oldXfIndexes = array(); |
||
370 | View Code Duplication | for ($col = $rangeStart[0]; $col <= $rangeEnd[0]; ++$col) { |
|
371 | $oldXfIndexes[$this->getActiveSheet()->getColumnDimensionByColumn($col)->getXfIndex()] = true; |
||
372 | } |
||
373 | break; |
||
374 | case 'ROW': |
||
375 | $oldXfIndexes = array(); |
||
376 | for ($row = $rangeStart[1]; $row <= $rangeEnd[1]; ++$row) { |
||
377 | if ($this->getActiveSheet()->getRowDimension($row)->getXfIndex() == null) { |
||
378 | $oldXfIndexes[0] = true; // row without explicit style should be formatted based on default style |
||
379 | } else { |
||
380 | $oldXfIndexes[$this->getActiveSheet()->getRowDimension($row)->getXfIndex()] = true; |
||
381 | } |
||
382 | } |
||
383 | break; |
||
384 | case 'CELL': |
||
385 | $oldXfIndexes = array(); |
||
386 | for ($col = $rangeStart[0]; $col <= $rangeEnd[0]; ++$col) { |
||
387 | View Code Duplication | for ($row = $rangeStart[1]; $row <= $rangeEnd[1]; ++$row) { |
|
388 | $oldXfIndexes[$this->getActiveSheet()->getCellByColumnAndRow($col, $row)->getXfIndex()] = true; |
||
389 | } |
||
390 | } |
||
391 | break; |
||
392 | } |
||
393 | |||
394 | // clone each of the affected styles, apply the style array, and add the new styles to the workbook |
||
395 | $workbook = $this->getActiveSheet()->getParent(); |
||
396 | foreach ($oldXfIndexes as $oldXfIndex => $dummy) { |
||
397 | $style = $workbook->getCellXfByIndex($oldXfIndex); |
||
398 | $newStyle = clone $style; |
||
399 | $newStyle->applyFromArray($pStyles); |
||
400 | |||
401 | if ($existingStyle = $workbook->getCellXfByHashCode($newStyle->getHashCode())) { |
||
402 | // there is already such cell Xf in our collection |
||
403 | $newXfIndexes[$oldXfIndex] = $existingStyle->getIndex(); |
||
404 | } else { |
||
405 | // we don't have such a cell Xf, need to add |
||
406 | $workbook->addCellXf($newStyle); |
||
407 | $newXfIndexes[$oldXfIndex] = $newStyle->getIndex(); |
||
408 | } |
||
409 | } |
||
410 | |||
411 | // Loop through columns, rows, or cells again and update the XF index |
||
412 | switch ($selectionType) { |
||
413 | View Code Duplication | case 'COLUMN': |
|
414 | for ($col = $rangeStart[0]; $col <= $rangeEnd[0]; ++$col) { |
||
415 | $columnDimension = $this->getActiveSheet()->getColumnDimensionByColumn($col); |
||
416 | $oldXfIndex = $columnDimension->getXfIndex(); |
||
417 | $columnDimension->setXfIndex($newXfIndexes[$oldXfIndex]); |
||
418 | } |
||
419 | break; |
||
420 | |||
421 | View Code Duplication | case 'ROW': |
|
422 | for ($row = $rangeStart[1]; $row <= $rangeEnd[1]; ++$row) { |
||
423 | $rowDimension = $this->getActiveSheet()->getRowDimension($row); |
||
424 | $oldXfIndex = $rowDimension->getXfIndex() === null ? |
||
425 | 0 : $rowDimension->getXfIndex(); // row without explicit style should be formatted based on default style |
||
426 | $rowDimension->setXfIndex($newXfIndexes[$oldXfIndex]); |
||
427 | } |
||
428 | break; |
||
429 | |||
430 | case 'CELL': |
||
431 | for ($col = $rangeStart[0]; $col <= $rangeEnd[0]; ++$col) { |
||
432 | for ($row = $rangeStart[1]; $row <= $rangeEnd[1]; ++$row) { |
||
433 | $cell = $this->getActiveSheet()->getCellByColumnAndRow($col, $row); |
||
434 | $oldXfIndex = $cell->getXfIndex(); |
||
435 | $cell->setXfIndex($newXfIndexes[$oldXfIndex]); |
||
436 | } |
||
437 | } |
||
438 | break; |
||
439 | } |
||
440 | } else { |
||
441 | // not a supervisor, just apply the style array directly on style object |
||
442 | if (array_key_exists('fill', $pStyles)) { |
||
443 | $this->getFill()->applyFromArray($pStyles['fill']); |
||
444 | } |
||
445 | if (array_key_exists('font', $pStyles)) { |
||
446 | $this->getFont()->applyFromArray($pStyles['font']); |
||
447 | } |
||
448 | if (array_key_exists('borders', $pStyles)) { |
||
449 | $this->getBorders()->applyFromArray($pStyles['borders']); |
||
450 | } |
||
451 | if (array_key_exists('alignment', $pStyles)) { |
||
452 | $this->getAlignment()->applyFromArray($pStyles['alignment']); |
||
453 | } |
||
454 | if (array_key_exists('numberformat', $pStyles)) { |
||
455 | $this->getNumberFormat()->applyFromArray($pStyles['numberformat']); |
||
456 | } |
||
457 | if (array_key_exists('protection', $pStyles)) { |
||
458 | $this->getProtection()->applyFromArray($pStyles['protection']); |
||
459 | } |
||
460 | if (array_key_exists('quotePrefix', $pStyles)) { |
||
461 | $this->quotePrefix = $pStyles['quotePrefix']; |
||
462 | } |
||
463 | } |
||
464 | } else { |
||
465 | throw new Exception("Invalid style array passed."); |
||
466 | } |
||
467 | return $this; |
||
468 | } |
||
469 | |||
470 | /** |
||
471 | * Get Fill |
||
472 | * |
||
643 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.