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