Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Cell 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 Cell, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | class Cell |
||
28 | { |
||
29 | /** |
||
30 | * Default range variable constant. |
||
31 | * |
||
32 | * @var string |
||
33 | */ |
||
34 | const DEFAULT_RANGE = 'A1:A1'; |
||
35 | |||
36 | /** |
||
37 | * Value binder to use. |
||
38 | * |
||
39 | * @var Cell\IValueBinder |
||
40 | */ |
||
41 | private static $valueBinder; |
||
42 | |||
43 | /** |
||
44 | * Value of the cell. |
||
45 | * |
||
46 | * @var mixed |
||
47 | */ |
||
48 | private $value; |
||
49 | |||
50 | /** |
||
51 | * Calculated value of the cell (used for caching) |
||
52 | * This returns the value last calculated by MS Excel or whichever spreadsheet program was used to |
||
53 | * create the original spreadsheet file. |
||
54 | * Note that this value is not guaranteed to reflect the actual calculated value because it is |
||
55 | * possible that auto-calculation was disabled in the original spreadsheet, and underlying data |
||
56 | * values used by the formula have changed since it was last calculated. |
||
57 | * |
||
58 | * @var mixed |
||
59 | */ |
||
60 | private $calculatedValue; |
||
61 | |||
62 | /** |
||
63 | * Type of the cell data. |
||
64 | * |
||
65 | * @var string |
||
66 | */ |
||
67 | private $dataType; |
||
68 | |||
69 | /** |
||
70 | * Parent worksheet. |
||
71 | * |
||
72 | * @var CachedObjectStorage\CacheBase |
||
73 | */ |
||
74 | private $parent; |
||
75 | |||
76 | /** |
||
77 | * Index to cellXf. |
||
78 | * |
||
79 | * @var int |
||
80 | */ |
||
81 | private $xfIndex = 0; |
||
82 | |||
83 | /** |
||
84 | * Attributes of the formula. |
||
85 | */ |
||
86 | private $formulaAttributes; |
||
87 | |||
88 | /** |
||
89 | * Send notification to the cache controller. |
||
90 | **/ |
||
91 | 73 | public function notifyCacheController() |
|
97 | |||
98 | 17 | public function detach() |
|
102 | |||
103 | 37 | public function attach(CachedObjectStorage\CacheBase $parent) |
|
107 | |||
108 | /** |
||
109 | * Create a new Cell. |
||
110 | * |
||
111 | * @param mixed $pValue |
||
112 | * @param string $pDataType |
||
113 | * @param Worksheet $pSheet |
||
114 | * |
||
115 | * @throws Exception |
||
116 | */ |
||
117 | 73 | public function __construct($pValue = null, $pDataType = null, Worksheet $pSheet = null) |
|
135 | |||
136 | /** |
||
137 | * Get cell coordinate column. |
||
138 | * |
||
139 | * @return string |
||
140 | */ |
||
141 | 45 | public function getColumn() |
|
145 | |||
146 | /** |
||
147 | * Get cell coordinate row. |
||
148 | * |
||
149 | * @return int |
||
150 | */ |
||
151 | 45 | public function getRow() |
|
155 | |||
156 | /** |
||
157 | * Get cell coordinate. |
||
158 | * |
||
159 | * @return string |
||
160 | */ |
||
161 | 73 | public function getCoordinate() |
|
165 | |||
166 | /** |
||
167 | * Get cell value. |
||
168 | * |
||
169 | * @return mixed |
||
170 | */ |
||
171 | 72 | public function getValue() |
|
175 | |||
176 | /** |
||
177 | * Get cell value with formatting. |
||
178 | * |
||
179 | * @return string |
||
180 | */ |
||
181 | 1 | public function getFormattedValue() |
|
189 | |||
190 | /** |
||
191 | * Set cell value. |
||
192 | * |
||
193 | * Sets the value for a cell, automatically determining the datatype using the value binder |
||
194 | * |
||
195 | * @param mixed $pValue Value |
||
196 | * |
||
197 | * @throws Exception |
||
198 | * |
||
199 | * @return Cell |
||
200 | */ |
||
201 | 55 | public function setValue($pValue = null) |
|
209 | |||
210 | /** |
||
211 | * Set the value for a cell, with the explicit data type passed to the method (bypassing any use of the value binder). |
||
212 | * |
||
213 | * @param mixed $pValue Value |
||
214 | * @param string $pDataType Explicit data type |
||
215 | * |
||
216 | * @throws Exception |
||
217 | * |
||
218 | * @return Cell |
||
219 | */ |
||
220 | 70 | public function setValueExplicit($pValue = null, $pDataType = Cell\DataType::TYPE_STRING) |
|
258 | |||
259 | /** |
||
260 | * Get calculated cell value. |
||
261 | * |
||
262 | * @deprecated Since version 1.7.8 for planned changes to cell for array formula handling |
||
263 | * |
||
264 | * @param bool $resetLog Whether the calculation engine logger should be reset or not |
||
265 | * |
||
266 | * @throws Exception |
||
267 | * |
||
268 | * @return mixed |
||
269 | */ |
||
270 | 42 | public function getCalculatedValue($resetLog = true) |
|
271 | { |
||
272 | 42 | if ($this->dataType == Cell\DataType::TYPE_FORMULA) { |
|
273 | try { |
||
274 | 25 | $result = Calculation::getInstance( |
|
275 | 25 | $this->getWorksheet()->getParent() |
|
276 | 25 | )->calculateCellValue($this, $resetLog); |
|
277 | // We don't yet handle array returns |
||
278 | 25 | if (is_array($result)) { |
|
279 | 25 | while (is_array($result)) { |
|
280 | 6 | $result = array_pop($result); |
|
281 | } |
||
282 | } |
||
283 | } catch (Exception $ex) { |
||
284 | if (($ex->getMessage() === 'Unable to access External Workbook') && ($this->calculatedValue !== null)) { |
||
285 | return $this->calculatedValue; // Fallback for calculations referencing external files. |
||
286 | } |
||
287 | $result = '#N/A'; |
||
288 | throw new Calculation\Exception( |
||
289 | $this->getWorksheet()->getTitle() . '!' . $this->getCoordinate() . ' -> ' . $ex->getMessage() |
||
290 | ); |
||
291 | } |
||
292 | |||
293 | 25 | if ($result === '#Not Yet Implemented') { |
|
294 | return $this->calculatedValue; // Fallback if calculation engine does not support the formula. |
||
295 | } |
||
296 | |||
297 | 25 | return $result; |
|
298 | 42 | } elseif ($this->value instanceof RichText) { |
|
299 | 2 | return $this->value->getPlainText(); |
|
300 | } |
||
301 | |||
302 | 42 | return $this->value; |
|
303 | } |
||
304 | |||
305 | /** |
||
306 | * Set old calculated value (cached). |
||
307 | * |
||
308 | * @param mixed $pValue Value |
||
309 | * |
||
310 | * @return Cell |
||
311 | */ |
||
312 | 8 | public function setCalculatedValue($pValue = null) |
|
320 | |||
321 | /** |
||
322 | * Get old calculated value (cached) |
||
323 | * This returns the value last calculated by MS Excel or whichever spreadsheet program was used to |
||
324 | * create the original spreadsheet file. |
||
325 | * Note that this value is not guaranteed to refelect the actual calculated value because it is |
||
326 | * possible that auto-calculation was disabled in the original spreadsheet, and underlying data |
||
327 | * values used by the formula have changed since it was last calculated. |
||
328 | * |
||
329 | * @return mixed |
||
330 | */ |
||
331 | public function getOldCalculatedValue() |
||
335 | |||
336 | /** |
||
337 | * Get cell data type. |
||
338 | * |
||
339 | * @return string |
||
340 | */ |
||
341 | 62 | public function getDataType() |
|
345 | |||
346 | /** |
||
347 | * Set cell data type. |
||
348 | * |
||
349 | * @param string $pDataType |
||
350 | * |
||
351 | * @return Cell |
||
352 | */ |
||
353 | public function setDataType($pDataType = Cell\DataType::TYPE_STRING) |
||
362 | |||
363 | /** |
||
364 | * Identify if the cell contains a formula. |
||
365 | * |
||
366 | * @return bool |
||
367 | */ |
||
368 | public function isFormula() |
||
372 | |||
373 | /** |
||
374 | * Does this cell contain Data validation rules? |
||
375 | * |
||
376 | * @throws Exception |
||
377 | * |
||
378 | * @return bool |
||
379 | */ |
||
380 | public function hasDataValidation() |
||
388 | |||
389 | /** |
||
390 | * Get Data validation rules. |
||
391 | * |
||
392 | * @throws Exception |
||
393 | * |
||
394 | * @return Cell\DataValidation |
||
395 | */ |
||
396 | 2 | public function getDataValidation() |
|
404 | |||
405 | /** |
||
406 | * Set Data validation rules. |
||
407 | * |
||
408 | * @param Cell\DataValidation $pDataValidation |
||
409 | * |
||
410 | * @throws Exception |
||
411 | * |
||
412 | * @return Cell |
||
413 | */ |
||
414 | View Code Duplication | public function setDataValidation(Cell\DataValidation $pDataValidation = null) |
|
424 | |||
425 | /** |
||
426 | * Does this cell contain a Hyperlink? |
||
427 | * |
||
428 | * @throws Exception |
||
429 | * |
||
430 | * @return bool |
||
431 | */ |
||
432 | public function hasHyperlink() |
||
440 | |||
441 | /** |
||
442 | * Get Hyperlink. |
||
443 | * |
||
444 | * @throws Exception |
||
445 | * |
||
446 | * @return Cell\Hyperlink |
||
447 | */ |
||
448 | 14 | public function getHyperlink() |
|
456 | |||
457 | /** |
||
458 | * Set Hyperlink. |
||
459 | * |
||
460 | * @param Cell\Hyperlink $pHyperlink |
||
461 | * |
||
462 | * @throws Exception |
||
463 | * |
||
464 | * @return Cell |
||
465 | */ |
||
466 | View Code Duplication | public function setHyperlink(Cell\Hyperlink $pHyperlink = null) |
|
476 | |||
477 | /** |
||
478 | * Get parent worksheet. |
||
479 | * |
||
480 | * @return CachedObjectStorage\CacheBase |
||
481 | */ |
||
482 | 39 | public function getParent() |
|
486 | |||
487 | /** |
||
488 | * Get parent worksheet. |
||
489 | * |
||
490 | * @return Worksheet |
||
491 | */ |
||
492 | 48 | public function getWorksheet() |
|
496 | |||
497 | /** |
||
498 | * Is this cell in a merge range. |
||
499 | * |
||
500 | * @return bool |
||
501 | */ |
||
502 | public function isInMergeRange() |
||
506 | |||
507 | /** |
||
508 | * Is this cell the master (top left cell) in a merge range (that holds the actual data value). |
||
509 | * |
||
510 | * @return bool |
||
511 | */ |
||
512 | public function isMergeRangeValueCell() |
||
524 | |||
525 | /** |
||
526 | * If this cell is in a merge range, then return the range. |
||
527 | * |
||
528 | * @return string |
||
529 | */ |
||
530 | public function getMergeRange() |
||
540 | |||
541 | /** |
||
542 | * Get cell style. |
||
543 | * |
||
544 | * @return Style |
||
545 | */ |
||
546 | 1 | public function getStyle() |
|
550 | |||
551 | /** |
||
552 | * Re-bind parent. |
||
553 | * |
||
554 | * @param Worksheet $parent |
||
555 | * |
||
556 | * @return Cell |
||
557 | */ |
||
558 | public function rebindParent(Worksheet $parent) |
||
564 | |||
565 | /** |
||
566 | * Is cell in a specific range? |
||
567 | * |
||
568 | * @param string $pRange Cell range (e.g. A1:A1) |
||
569 | * |
||
570 | * @return bool |
||
571 | */ |
||
572 | public function isInRange($pRange = 'A1:A1') |
||
584 | |||
585 | /** |
||
586 | * Coordinate from string. |
||
587 | * |
||
588 | * @param string $pCoordinateString |
||
589 | * |
||
590 | * @throws Exception |
||
591 | * |
||
592 | * @return string[] Array containing column and row (indexes 0 and 1) |
||
593 | */ |
||
594 | 118 | public static function coordinateFromString($pCoordinateString = 'A1') |
|
606 | |||
607 | /** |
||
608 | * Make string row, column or cell coordinate absolute. |
||
609 | * |
||
610 | * @param string $pCoordinateString e.g. 'A' or '1' or 'A1' |
||
611 | * Note that this value can be a row or column reference as well as a cell reference |
||
612 | * |
||
613 | * @throws Exception |
||
614 | * |
||
615 | * @return string Absolute coordinate e.g. '$A' or '$1' or '$A$1' |
||
616 | */ |
||
617 | 21 | public static function absoluteReference($pCoordinateString = 'A1') |
|
642 | |||
643 | /** |
||
644 | * Make string coordinate absolute. |
||
645 | * |
||
646 | * @param string $pCoordinateString e.g. 'A1' |
||
647 | * |
||
648 | * @throws Exception |
||
649 | * |
||
650 | * @return string Absolute coordinate e.g. '$A$1' |
||
651 | */ |
||
652 | 32 | public static function absoluteCoordinate($pCoordinateString = 'A1') |
|
675 | |||
676 | /** |
||
677 | * Split range into coordinate strings. |
||
678 | * |
||
679 | * @param string $pRange e.g. 'B4:D9' or 'B4:D9,H2:O11' or 'B4' |
||
680 | * |
||
681 | * @return array Array containg one or more arrays containing one or two coordinate strings |
||
682 | * e.g. array('B4','D9') or array(array('B4','D9'),array('H2','O11')) |
||
683 | * or array('B4') |
||
684 | */ |
||
685 | 72 | public static function splitRange($pRange = 'A1:A1') |
|
700 | |||
701 | /** |
||
702 | * Build range from coordinate strings. |
||
703 | * |
||
704 | * @param array $pRange Array containg one or more arrays containing one or two coordinate strings |
||
705 | * |
||
706 | * @throws Exception |
||
707 | * |
||
708 | * @return string String representation of $pRange |
||
709 | */ |
||
710 | 20 | public static function buildRange($pRange) |
|
727 | |||
728 | /** |
||
729 | * Calculate range boundaries. |
||
730 | * |
||
731 | * @param string $pRange Cell range (e.g. A1:A1) |
||
732 | * |
||
733 | * @return array Range coordinates array(Start Cell, End Cell) |
||
734 | * where Start Cell and End Cell are arrays (Column Number, Row Number) |
||
735 | */ |
||
736 | 41 | public static function rangeBoundaries($pRange = 'A1:A1') |
|
763 | |||
764 | /** |
||
765 | * Calculate range dimension. |
||
766 | * |
||
767 | * @param string $pRange Cell range (e.g. A1:A1) |
||
768 | * |
||
769 | * @return array Range dimension (width, height) |
||
770 | */ |
||
771 | 15 | public static function rangeDimension($pRange = 'A1:A1') |
|
778 | |||
779 | /** |
||
780 | * Calculate range boundaries. |
||
781 | * |
||
782 | * @param string $pRange Cell range (e.g. A1:A1) |
||
783 | * |
||
784 | * @return array Range coordinates array(Start Cell, End Cell) |
||
785 | * where Start Cell and End Cell are arrays (Column ID, Row Number) |
||
786 | */ |
||
787 | 4 | public static function getRangeBoundaries($pRange = 'A1:A1') |
|
806 | |||
807 | /** |
||
808 | * Column index from string. |
||
809 | * |
||
810 | * @param string $pString |
||
811 | * |
||
812 | * @return int Column index (base 1 !!!) |
||
813 | */ |
||
814 | 114 | public static function columnIndexFromString($pString = 'A') |
|
853 | |||
854 | /** |
||
855 | * String from columnindex. |
||
856 | * |
||
857 | * @param int $pColumnIndex Column index (base 0 !!!) |
||
858 | * |
||
859 | * @return string |
||
860 | */ |
||
861 | 87 | public static function stringFromColumnIndex($pColumnIndex = 0) |
|
884 | |||
885 | /** |
||
886 | * Extract all cell references in range. |
||
887 | * |
||
888 | * @param string $pRange Range (e.g. A1 or A1:C10 or A1:E10 A20:E25) |
||
889 | * |
||
890 | * @return array Array containing single cell references |
||
891 | */ |
||
892 | 46 | public static function extractAllCellReferencesInRange($pRange = 'A1') |
|
948 | |||
949 | /** |
||
950 | * Compare 2 cells. |
||
951 | * |
||
952 | * @param Cell $a Cell a |
||
953 | * @param Cell $b Cell b |
||
954 | * |
||
955 | * @return int Result of comparison (always -1 or 1, never zero!) |
||
956 | */ |
||
957 | public static function compareCells(Cell $a, Cell $b) |
||
969 | |||
970 | /** |
||
971 | * Get value binder to use. |
||
972 | * |
||
973 | * @return Cell\IValueBinder |
||
974 | */ |
||
975 | 55 | public static function getValueBinder() |
|
983 | |||
984 | /** |
||
985 | * Set value binder to use. |
||
986 | * |
||
987 | * @param Cell\IValueBinder $binder |
||
988 | * |
||
989 | * @throws Exception |
||
990 | */ |
||
991 | 1 | public static function setValueBinder(Cell\IValueBinder $binder = null) |
|
999 | |||
1000 | /** |
||
1001 | * Implement PHP __clone to create a deep clone, not just a shallow copy. |
||
1002 | */ |
||
1003 | 1 | public function __clone() |
|
1014 | |||
1015 | /** |
||
1016 | * Get index to cellXf. |
||
1017 | * |
||
1018 | * @return int |
||
1019 | */ |
||
1020 | 63 | public function getXfIndex() |
|
1024 | |||
1025 | /** |
||
1026 | * Set index to cellXf. |
||
1027 | * |
||
1028 | * @param int $pValue |
||
1029 | * |
||
1030 | * @return Cell |
||
1031 | */ |
||
1032 | 63 | public function setXfIndex($pValue = 0) |
|
1038 | |||
1039 | /** |
||
1040 | * @deprecated Since version 1.7.8 for planned changes to cell for array formula handling |
||
1041 | * |
||
1042 | * @param mixed $pAttributes |
||
1043 | */ |
||
1044 | public function setFormulaAttributes($pAttributes) |
||
1045 | { |
||
1046 | $this->formulaAttributes = $pAttributes; |
||
1047 | |||
1048 | return $this; |
||
1049 | } |
||
1050 | |||
1051 | /** |
||
1052 | * @deprecated Since version 1.7.8 for planned changes to cell for array formula handling |
||
1053 | */ |
||
1054 | 18 | public function getFormulaAttributes() |
|
1055 | { |
||
1056 | 18 | return $this->formulaAttributes; |
|
1057 | } |
||
1058 | |||
1059 | /** |
||
1060 | * Convert to string. |
||
1061 | * |
||
1062 | * @return string |
||
1063 | */ |
||
1064 | public function __toString() |
||
1068 | } |
||
1069 |
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: