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 CacheBase 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 CacheBase, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | abstract class CacheBase |
||
28 | { |
||
29 | /** |
||
30 | * Parent worksheet. |
||
31 | * |
||
32 | * @var \PhpOffice\PhpSpreadsheet\Worksheet |
||
33 | */ |
||
34 | protected $parent; |
||
35 | |||
36 | /** |
||
37 | * The currently active Cell. |
||
38 | * |
||
39 | * @var \PhpOffice\PhpSpreadsheet\Cell |
||
40 | */ |
||
41 | protected $currentObject = null; |
||
42 | |||
43 | /** |
||
44 | * Coordinate address of the currently active Cell. |
||
45 | * |
||
46 | * @var string |
||
47 | */ |
||
48 | protected $currentObjectID = null; |
||
49 | |||
50 | /** |
||
51 | * Flag indicating whether the currently active Cell requires saving. |
||
52 | * |
||
53 | * @var bool |
||
54 | */ |
||
55 | protected $currentCellIsDirty = true; |
||
56 | |||
57 | /** |
||
58 | * An array of cells or cell pointers for the worksheet cells held in this cache, |
||
59 | * and indexed by their coordinate address within the worksheet. |
||
60 | * |
||
61 | * @var array of mixed |
||
62 | */ |
||
63 | protected $cellCache = []; |
||
64 | |||
65 | /** |
||
66 | * Initialise this new cell collection. |
||
67 | * |
||
68 | * @param \PhpOffice\PhpSpreadsheet\Worksheet $parent The worksheet for this cell collection |
||
69 | */ |
||
70 | 81 | public function __construct(\PhpOffice\PhpSpreadsheet\Worksheet $parent) |
|
71 | { |
||
72 | // Set our parent worksheet. |
||
73 | // This is maintained within the cache controller to facilitate re-attaching it to \PhpOffice\PhpSpreadsheet\Cell objects when |
||
74 | // they are woken from a serialized state |
||
75 | 81 | $this->parent = $parent; |
|
76 | 81 | } |
|
77 | |||
78 | /** |
||
79 | * Return the parent worksheet for this cell collection. |
||
80 | * |
||
81 | * @return \PhpOffice\PhpSpreadsheet\Worksheet |
||
82 | */ |
||
83 | 46 | public function getParent() |
|
87 | |||
88 | /** |
||
89 | * Is a value set in the current \PhpOffice\PhpSpreadsheet\CachedObjectStorage\ICache for an indexed cell? |
||
90 | * |
||
91 | * @param string $pCoord Coordinate address of the cell to check |
||
92 | * |
||
93 | * @return bool |
||
94 | */ |
||
95 | 73 | public function isDataSet($pCoord) |
|
103 | |||
104 | /** |
||
105 | * Move a cell object from one address to another. |
||
106 | * |
||
107 | * @param string $fromAddress Current address of the cell to move |
||
108 | * @param string $toAddress Destination address of the cell to move |
||
109 | * |
||
110 | * @return bool |
||
111 | */ |
||
112 | public function moveCell($fromAddress, $toAddress) |
||
125 | |||
126 | /** |
||
127 | * Add or Update a cell in cache. |
||
128 | * |
||
129 | * @param \PhpOffice\PhpSpreadsheet\Cell $cell Cell to update |
||
130 | * |
||
131 | * @throws \PhpOffice\PhpSpreadsheet\Exception |
||
132 | * |
||
133 | * @return \PhpOffice\PhpSpreadsheet\Cell |
||
134 | */ |
||
135 | 72 | public function updateCacheData(\PhpOffice\PhpSpreadsheet\Cell $cell) |
|
139 | |||
140 | /** |
||
141 | * Delete a cell in cache identified by coordinate address. |
||
142 | * |
||
143 | * @param string $pCoord Coordinate address of the cell to delete |
||
144 | * |
||
145 | * @throws \PhpOffice\PhpSpreadsheet\Exception |
||
146 | */ |
||
147 | 15 | public function deleteCacheData($pCoord) |
|
160 | |||
161 | /** |
||
162 | * Get a list of all cell addresses currently held in cache. |
||
163 | * |
||
164 | * @return string[] |
||
165 | */ |
||
166 | 64 | public function getCellList() |
|
170 | |||
171 | /** |
||
172 | * Sort the list of all cell addresses currently held in cache by row and column. |
||
173 | * |
||
174 | * @return string[] |
||
175 | */ |
||
176 | 63 | public function getSortedCellList() |
|
187 | |||
188 | /** |
||
189 | * Get highest worksheet column and highest row that have cell records. |
||
190 | * |
||
191 | * @return array Highest column name and highest row number |
||
192 | */ |
||
193 | 62 | public function getHighestRowAndColumn() |
|
214 | |||
215 | /** |
||
216 | * Return the cell address of the currently active cell object. |
||
217 | * |
||
218 | * @return string |
||
219 | */ |
||
220 | 73 | public function getCurrentAddress() |
|
224 | |||
225 | /** |
||
226 | * Return the column address of the currently active cell object. |
||
227 | * |
||
228 | * @return string |
||
229 | */ |
||
230 | 47 | public function getCurrentColumn() |
|
236 | |||
237 | /** |
||
238 | * Return the row address of the currently active cell object. |
||
239 | * |
||
240 | * @return int |
||
241 | */ |
||
242 | 45 | public function getCurrentRow() |
|
248 | |||
249 | /** |
||
250 | * Get highest worksheet column. |
||
251 | * |
||
252 | * @param string $row Return the highest column for the specified row, |
||
253 | * or the highest column of any row if no row number is passed |
||
254 | * |
||
255 | * @return string Highest column name |
||
256 | */ |
||
257 | 13 | public function getHighestColumn($row = null) |
|
276 | |||
277 | /** |
||
278 | * Get highest worksheet row. |
||
279 | * |
||
280 | * @param string $column Return the highest row for the specified column, |
||
281 | * or the highest row of any column if no column letter is passed |
||
282 | * |
||
283 | * @return int Highest row number |
||
284 | */ |
||
285 | 15 | public function getHighestRow($column = null) |
|
304 | |||
305 | /** |
||
306 | * Generate a unique ID for cache referencing. |
||
307 | * |
||
308 | * @return string Unique Reference |
||
309 | */ |
||
310 | 1 | protected function getUniqueID() |
|
320 | |||
321 | /** |
||
322 | * Clone the cell collection. |
||
323 | * |
||
324 | * @param \PhpOffice\PhpSpreadsheet\Worksheet $parent The new worksheet that we're copying to |
||
325 | */ |
||
326 | 1 | public function copyCellCollection(\PhpOffice\PhpSpreadsheet\Worksheet $parent) |
|
336 | |||
337 | /** |
||
338 | * Remove a row, deleting all cells in that row. |
||
339 | * |
||
340 | * @param string $row Row number to remove |
||
341 | */ |
||
342 | 15 | View Code Duplication | public function removeRow($row) |
343 | { |
||
344 | 15 | foreach ($this->getCellList() as $coord) { |
|
345 | 15 | sscanf($coord, '%[A-Z]%d', $c, $r); |
|
346 | 15 | if ($r == $row) { |
|
347 | $this->deleteCacheData($coord); |
||
348 | } |
||
349 | } |
||
350 | 15 | } |
|
351 | |||
352 | /** |
||
353 | * Remove a column, deleting all cells in that column. |
||
354 | * |
||
355 | * @param string $column Column ID to remove |
||
356 | */ |
||
357 | 12 | View Code Duplication | public function removeColumn($column) |
366 | |||
367 | /** |
||
368 | * Identify whether the caching method is currently available |
||
369 | * Some methods are dependent on the availability of certain extensions being enabled in the PHP build. |
||
370 | * |
||
371 | * @return bool |
||
372 | */ |
||
373 | 67 | public static function cacheMethodIsAvailable() |
|
377 | } |
||
378 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.