Complex classes like PHPExcel_Writer_Excel2007 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 PHPExcel_Writer_Excel2007, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
36 | class PHPExcel_Writer_Excel2007 implements PHPExcel_Writer_IWriter |
||
37 | { |
||
38 | /** |
||
39 | * Write charts that are defined in the workbook? |
||
40 | * Identifies whether the Writer should write definitions for any charts that exist in the PHPExcel object; |
||
41 | * |
||
42 | * @var boolean |
||
43 | */ |
||
44 | private $_includeCharts = false; |
||
45 | |||
46 | /** |
||
47 | * Pre-calculate formulas |
||
48 | * |
||
49 | * @var boolean |
||
50 | */ |
||
51 | private $_preCalculateFormulas = true; |
||
52 | |||
53 | /** |
||
54 | * Office2003 compatibility |
||
55 | * |
||
56 | * @var boolean |
||
57 | */ |
||
58 | private $_office2003compatibility = false; |
||
59 | |||
60 | /** |
||
61 | * Private writer parts |
||
62 | * |
||
63 | * @var PHPExcel_Writer_Excel2007_WriterPart[] |
||
64 | */ |
||
65 | private $_writerParts = array(); |
||
66 | |||
67 | /** |
||
68 | * Private PHPExcel |
||
69 | * |
||
70 | * @var PHPExcel |
||
71 | */ |
||
72 | private $_spreadSheet; |
||
73 | |||
74 | /** |
||
75 | * Private string table |
||
76 | * |
||
77 | * @var string[] |
||
78 | */ |
||
79 | private $_stringTable = array(); |
||
80 | |||
81 | /** |
||
82 | * Private unique PHPExcel_Style_Conditional HashTable |
||
83 | * |
||
84 | * @var PHPExcel_HashTable |
||
85 | */ |
||
86 | private $_stylesConditionalHashTable; |
||
87 | |||
88 | /** |
||
89 | * Private unique PHPExcel_Style_Fill HashTable |
||
90 | * |
||
91 | * @var PHPExcel_HashTable |
||
92 | */ |
||
93 | private $_fillHashTable; |
||
94 | |||
95 | /** |
||
96 | * Private unique PHPExcel_Style_Font HashTable |
||
97 | * |
||
98 | * @var PHPExcel_HashTable |
||
99 | */ |
||
100 | private $_fontHashTable; |
||
101 | |||
102 | /** |
||
103 | * Private unique PHPExcel_Style_Borders HashTable |
||
104 | * |
||
105 | * @var PHPExcel_HashTable |
||
106 | */ |
||
107 | private $_bordersHashTable ; |
||
108 | |||
109 | /** |
||
110 | * Private unique PHPExcel_Style_NumberFormat HashTable |
||
111 | * |
||
112 | * @var PHPExcel_HashTable |
||
113 | */ |
||
114 | private $_numFmtHashTable; |
||
115 | |||
116 | /** |
||
117 | * Private unique PHPExcel_Worksheet_BaseDrawing HashTable |
||
118 | * |
||
119 | * @var PHPExcel_HashTable |
||
120 | */ |
||
121 | private $_drawingHashTable; |
||
122 | |||
123 | /** |
||
124 | * Use disk caching where possible? |
||
125 | * |
||
126 | * @var boolean |
||
127 | */ |
||
128 | private $_useDiskCaching = false; |
||
129 | |||
130 | /** |
||
131 | * Disk caching directory |
||
132 | * |
||
133 | * @var string |
||
134 | */ |
||
135 | private $_diskCachingDirectory = './'; |
||
136 | |||
137 | /** |
||
138 | * Create a new PHPExcel_Writer_Excel2007 |
||
139 | * |
||
140 | * @param PHPExcel $pPHPExcel |
||
141 | */ |
||
142 | public function __construct(PHPExcel $pPHPExcel = null) |
||
175 | |||
176 | /** |
||
177 | * Get writer part |
||
178 | * |
||
179 | * @param string $pPartName Writer part name |
||
180 | * @return PHPExcel_Writer_Excel2007_WriterPart |
||
181 | */ |
||
182 | public function getWriterPart($pPartName = '') { |
||
189 | |||
190 | /** |
||
191 | * Save PHPExcel to file |
||
192 | * |
||
193 | * @param string $pFilename |
||
194 | * @throws Exception |
||
195 | */ |
||
196 | public function save($pFilename = null) |
||
392 | |||
393 | /** |
||
394 | * Get PHPExcel object |
||
395 | * |
||
396 | * @return PHPExcel |
||
397 | * @throws Exception |
||
398 | */ |
||
399 | public function getPHPExcel() { |
||
406 | |||
407 | /** |
||
408 | * Set PHPExcel object |
||
409 | * |
||
410 | * @param PHPExcel $pPHPExcel PHPExcel object |
||
411 | * @throws Exception |
||
412 | * @return PHPExcel_Writer_Excel2007 |
||
413 | */ |
||
414 | public function setPHPExcel(PHPExcel $pPHPExcel = null) { |
||
418 | |||
419 | /** |
||
420 | * Get string table |
||
421 | * |
||
422 | * @return string[] |
||
423 | */ |
||
424 | public function getStringTable() { |
||
427 | |||
428 | /** |
||
429 | * Get PHPExcel_Style_Conditional HashTable |
||
430 | * |
||
431 | * @return PHPExcel_HashTable |
||
432 | */ |
||
433 | public function getStylesConditionalHashTable() { |
||
436 | |||
437 | /** |
||
438 | * Get PHPExcel_Style_Fill HashTable |
||
439 | * |
||
440 | * @return PHPExcel_HashTable |
||
441 | */ |
||
442 | public function getFillHashTable() { |
||
445 | |||
446 | /** |
||
447 | * Get PHPExcel_Style_Font HashTable |
||
448 | * |
||
449 | * @return PHPExcel_HashTable |
||
450 | */ |
||
451 | public function getFontHashTable() { |
||
454 | |||
455 | /** |
||
456 | * Get PHPExcel_Style_Borders HashTable |
||
457 | * |
||
458 | * @return PHPExcel_HashTable |
||
459 | */ |
||
460 | public function getBordersHashTable() { |
||
463 | |||
464 | /** |
||
465 | * Get PHPExcel_Style_NumberFormat HashTable |
||
466 | * |
||
467 | * @return PHPExcel_HashTable |
||
468 | */ |
||
469 | public function getNumFmtHashTable() { |
||
472 | |||
473 | /** |
||
474 | * Get PHPExcel_Worksheet_BaseDrawing HashTable |
||
475 | * |
||
476 | * @return PHPExcel_HashTable |
||
477 | */ |
||
478 | public function getDrawingHashTable() { |
||
481 | |||
482 | /** |
||
483 | * Write charts in workbook? |
||
484 | * If this is true, then the Writer will write definitions for any charts that exist in the PHPExcel object. |
||
485 | * If false (the default) it will ignore any charts defined in the PHPExcel object. |
||
486 | * |
||
487 | * @return boolean |
||
488 | */ |
||
489 | public function getIncludeCharts() { |
||
492 | |||
493 | /** |
||
494 | * Set write charts in workbook |
||
495 | * Set to true, to advise the Writer to include any charts that exist in the PHPExcel object. |
||
496 | * Set to false (the default) to ignore charts. |
||
497 | * |
||
498 | * @param boolean $pValue |
||
499 | * |
||
500 | * @return PHPExcel_Writer_Excel2007 |
||
501 | */ |
||
502 | public function setIncludeCharts($pValue = false) { |
||
506 | |||
507 | /** |
||
508 | * Get Pre-Calculate Formulas |
||
509 | * |
||
510 | * @return boolean |
||
511 | */ |
||
512 | public function getPreCalculateFormulas() { |
||
515 | |||
516 | /** |
||
517 | * Set Pre-Calculate Formulas |
||
518 | * |
||
519 | * @param boolean $pValue Pre-Calculate Formulas? |
||
520 | */ |
||
521 | public function setPreCalculateFormulas($pValue = true) { |
||
524 | |||
525 | /** |
||
526 | * Get Office2003 compatibility |
||
527 | * |
||
528 | * @return boolean |
||
529 | */ |
||
530 | public function getOffice2003Compatibility() { |
||
533 | |||
534 | /** |
||
535 | * Set Pre-Calculate Formulas |
||
536 | * |
||
537 | * @param boolean $pValue Office2003 compatibility? |
||
538 | * @return PHPExcel_Writer_Excel2007 |
||
539 | */ |
||
540 | public function setOffice2003Compatibility($pValue = false) { |
||
544 | |||
545 | /** |
||
546 | * Get use disk caching where possible? |
||
547 | * |
||
548 | * @return boolean |
||
549 | */ |
||
550 | public function getUseDiskCaching() { |
||
553 | |||
554 | /** |
||
555 | * Set use disk caching where possible? |
||
556 | * |
||
557 | * @param boolean $pValue |
||
558 | * @param string $pDirectory Disk caching directory |
||
559 | * @throws Exception Exception when directory does not exist |
||
560 | * @return PHPExcel_Writer_Excel2007 |
||
561 | */ |
||
562 | public function setUseDiskCaching($pValue = false, $pDirectory = null) { |
||
574 | |||
575 | /** |
||
576 | * Get disk caching directory |
||
577 | * |
||
578 | * @return string |
||
579 | */ |
||
580 | public function getDiskCachingDirectory() { |
||
583 | } |
||
584 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: