Complex classes like Workbook often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Workbook, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class Workbook extends BIFFwriter |
||
| 12 | { |
||
| 13 | const COUNTRY_NONE = -1; |
||
| 14 | const COUNTRY_USA = 1; |
||
| 15 | |||
| 16 | const BOF_TYPE = 0x0005; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Formula parser |
||
| 20 | * @var FormulaParser |
||
| 21 | */ |
||
| 22 | protected $formulaParser; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * The active worksheet of the workbook (0 indexed) |
||
| 26 | * @var integer |
||
| 27 | */ |
||
| 28 | protected $activeSheetIndex = 0; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * 1st displayed worksheet in the workbook (0 indexed) |
||
| 32 | * @var integer |
||
| 33 | */ |
||
| 34 | protected $firstSheetIndex = 0; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Index for creating adding new formats to the workbook |
||
| 38 | * 15 style XF's and 1 cell XF |
||
| 39 | * @var integer |
||
| 40 | */ |
||
| 41 | protected $xfIndex = 16; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Flag for preventing close from being called twice. |
||
| 45 | * @var boolean |
||
| 46 | * @see close() |
||
| 47 | */ |
||
| 48 | protected $saved = false; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * The default XF format. |
||
| 52 | * @var Format |
||
| 53 | */ |
||
| 54 | protected $tmpFormat; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Array containing references to all of this workbook's worksheets |
||
| 58 | * @var Worksheet[] |
||
| 59 | */ |
||
| 60 | protected $worksheets = array(); |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Array of sheetnames for creating the EXTERNSHEET records |
||
| 64 | * @var array |
||
| 65 | */ |
||
| 66 | protected $sheetNames = array(); |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Array containing references to all of this workbook's formats |
||
| 70 | * @var Format[] |
||
| 71 | */ |
||
| 72 | protected $formats = array(); |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Array containing the colour palette |
||
| 76 | * @var array |
||
| 77 | */ |
||
| 78 | protected $palette = array(); |
||
| 79 | |||
| 80 | /** |
||
| 81 | * The default format for URLs. |
||
| 82 | * @var Format |
||
| 83 | */ |
||
| 84 | protected $urlFormat; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * The country code used for localization |
||
| 88 | * @var integer |
||
| 89 | */ |
||
| 90 | protected $countryCode = self::COUNTRY_NONE; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @var int |
||
| 94 | */ |
||
| 95 | protected $creationTimestamp; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @var SharedStringsTable |
||
| 99 | */ |
||
| 100 | protected $sst; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * |
||
| 104 | */ |
||
| 105 | public function __construct() |
||
| 117 | |||
| 118 | /** |
||
| 119 | * |
||
| 120 | */ |
||
| 121 | protected function addDefaultFormats() |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @param int $creationTime |
||
| 136 | */ |
||
| 137 | public function setCreationTimestamp($creationTime) |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Assemble worksheets into a workbook and send the BIFF data to an OLE |
||
| 144 | * storage. |
||
| 145 | * This method should always be the last one to be called on every workbook |
||
| 146 | * |
||
| 147 | * @param string $filePath File path to save |
||
| 148 | * |
||
| 149 | * @throws \Exception |
||
| 150 | */ |
||
| 151 | public function save($filePath) |
||
| 191 | |||
| 192 | protected function storeDrawings() |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Write Internal SUPBOOK record |
||
| 213 | */ |
||
| 214 | protected function storeSupbookInternal() |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Calculate the number of selected worksheet tabs and call the finalization |
||
| 221 | * methods for each worksheet |
||
| 222 | */ |
||
| 223 | protected function closeSheets() |
||
| 229 | |||
| 230 | /** |
||
| 231 | * |
||
| 232 | */ |
||
| 233 | protected function storeSheets() |
||
| 246 | |||
| 247 | /** |
||
| 248 | * @return int |
||
| 249 | */ |
||
| 250 | protected function calcSheetRecordsTotalSize() |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Returns an array of the worksheet objects in a workbook |
||
| 263 | * |
||
| 264 | * @return Worksheet[] |
||
| 265 | */ |
||
| 266 | public function getWorksheets() |
||
| 270 | |||
| 271 | /** |
||
| 272 | * @return int |
||
| 273 | */ |
||
| 274 | public function getSheetsCount() |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Set the country identifier for the workbook |
||
| 281 | * |
||
| 282 | * @param integer $code Is the international calling country code for the |
||
| 283 | * chosen country. |
||
| 284 | */ |
||
| 285 | public function setCountry($code) |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Add a new worksheet to the Excel workbook. |
||
| 292 | * If no name is given the name of the worksheet will be Sheeti$i, with |
||
| 293 | * $i in [1..]. |
||
| 294 | * |
||
| 295 | * @param string $name the optional name of the worksheet |
||
| 296 | * @throws \Exception |
||
| 297 | * @return Worksheet |
||
| 298 | */ |
||
| 299 | public function addWorksheet($name = '') |
||
| 335 | |||
| 336 | /** |
||
| 337 | * @param string $name |
||
| 338 | * |
||
| 339 | * @return string |
||
| 340 | * @throws \Exception |
||
| 341 | */ |
||
| 342 | protected function checkSheetName($name) |
||
| 351 | |||
| 352 | /** |
||
| 353 | * @param int $sheetIndex |
||
| 354 | */ |
||
| 355 | public function setActiveSheetIndex($sheetIndex) |
||
| 366 | |||
| 367 | /** |
||
| 368 | * @return int |
||
| 369 | */ |
||
| 370 | public function getActiveSheetIndex() |
||
| 374 | |||
| 375 | /** |
||
| 376 | * @return int |
||
| 377 | */ |
||
| 378 | public function getFirstSheetIndex() |
||
| 382 | |||
| 383 | /** |
||
| 384 | * @param int $firstSheetIndex |
||
| 385 | */ |
||
| 386 | public function setFirstSheetIndex($firstSheetIndex) |
||
| 390 | |||
| 391 | /** |
||
| 392 | * @param string $name |
||
| 393 | * |
||
| 394 | * @return bool |
||
| 395 | */ |
||
| 396 | public function hasSheet($name) |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Add a new format to the Excel workbook. |
||
| 403 | * Also, pass any properties to the Format constructor. |
||
| 404 | * |
||
| 405 | * @param array $properties array with properties for initializing the format. |
||
| 406 | * @return Format reference to an Excel Format |
||
| 407 | */ |
||
| 408 | public function addFormat($properties = array()) |
||
| 416 | |||
| 417 | /** |
||
| 418 | * Create new validator. |
||
| 419 | * |
||
| 420 | * @return Validator reference to a Validator |
||
| 421 | */ |
||
| 422 | public function addValidator() |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Change the RGB components of the elements in the colour palette. |
||
| 429 | * |
||
| 430 | * @param integer $index colour index |
||
| 431 | * @param integer $red red RGB value [0-255] |
||
| 432 | * @param integer $green green RGB value [0-255] |
||
| 433 | * @param integer $blue blue RGB value [0-255] |
||
| 434 | * @throws \Exception |
||
| 435 | * |
||
| 436 | * @return integer The palette index for the custom color |
||
| 437 | */ |
||
| 438 | public function setCustomColor($index, $red, $green, $blue) |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Store the workbook in an OLE container |
||
| 450 | * @param string $filePath |
||
| 451 | */ |
||
| 452 | protected function saveOleFile($filePath) |
||
| 468 | |||
| 469 | /** |
||
| 470 | * Store the Excel FONT records. |
||
| 471 | */ |
||
| 472 | protected function storeAllFonts() |
||
| 478 | |||
| 479 | /** |
||
| 480 | * @return Font[] |
||
| 481 | */ |
||
| 482 | protected function getFonts() |
||
| 513 | |||
| 514 | /** |
||
| 515 | * Store user defined numerical formats i.e. FORMAT records |
||
| 516 | */ |
||
| 517 | protected function storeAllNumFormats() |
||
| 541 | |||
| 542 | /** |
||
| 543 | * Write all XF records. |
||
| 544 | */ |
||
| 545 | protected function storeAllXfs() |
||
| 563 | |||
| 564 | /** |
||
| 565 | * Write all STYLE records. |
||
| 566 | */ |
||
| 567 | protected function storeAllStyles() |
||
| 571 | |||
| 572 | /** |
||
| 573 | * |
||
| 574 | */ |
||
| 575 | protected function storeCountry() |
||
| 581 | |||
| 582 | /** |
||
| 583 | * Write the NAME record to define the print area and the repeat rows and cols. |
||
| 584 | */ |
||
| 585 | protected function storeDefinedNames() |
||
| 590 | |||
| 591 | /** |
||
| 592 | * Create the print area NAME records |
||
| 593 | */ |
||
| 594 | protected function storePrintAreaNames() |
||
| 612 | |||
| 613 | protected function getRangeCommonHeader(Worksheet $sheet) |
||
| 617 | |||
| 618 | /** |
||
| 619 | * Create the print title NAME records |
||
| 620 | */ |
||
| 621 | protected function storePrintTitleNames() |
||
| 627 | |||
| 628 | /** |
||
| 629 | * @param Worksheet $sheet |
||
| 630 | */ |
||
| 631 | protected function storePrintTitleName(Worksheet $sheet) |
||
| 644 | |||
| 645 | /** |
||
| 646 | * @param Worksheet $sheet |
||
| 647 | * |
||
| 648 | * @return string |
||
| 649 | */ |
||
| 650 | protected function getPrintTitleData(Worksheet $sheet) |
||
| 675 | |||
| 676 | /** |
||
| 677 | * Write Excel BIFF WINDOW1 record. |
||
| 678 | */ |
||
| 679 | protected function storeWindow1() |
||
| 691 | |||
| 692 | /** |
||
| 693 | * Writes the Excel BIFF EXTERNSHEET record. These references are used by |
||
| 694 | * formulas. |
||
| 695 | */ |
||
| 696 | protected function storeExternsheet() |
||
| 700 | |||
| 701 | /** |
||
| 702 | * Write DATEMODE record to indicate the date system in use (1904 or 1900) |
||
| 703 | * Flag for 1904 date system (0 => base date is 1900, 1 => base date is 1904) |
||
| 704 | */ |
||
| 705 | protected function storeDatemode() |
||
| 709 | |||
| 710 | /** |
||
| 711 | * Write all of the workbooks strings into an indexed array. |
||
| 712 | * |
||
| 713 | * The Excel documentation says that the SST record should be followed by an |
||
| 714 | * EXTSST record. The EXTSST record is a hash table that is used to optimise |
||
| 715 | * access to SST. However, despite the documentation it doesn't seem to be |
||
| 716 | * required so we will ignore it. |
||
| 717 | */ |
||
| 718 | protected function storeSharedStringsTable() |
||
| 726 | } |
||
| 727 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: