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 | 70 | public function __construct(\PhpOffice\PhpSpreadsheet\Worksheet $parent) |
|
77 | |||
78 | /** |
||
79 | * Return the parent worksheet for this cell collection |
||
80 | * |
||
81 | * @return \PhpOffice\PhpSpreadsheet\Worksheet |
||
82 | */ |
||
83 | 37 | public function getParent() |
|
84 | { |
||
85 | 37 | return $this->parent; |
|
86 | } |
||
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 | * @return bool |
||
93 | */ |
||
94 | 62 | public function isDataSet($pCoord) |
|
95 | { |
||
96 | 62 | if ($pCoord === $this->currentObjectID) { |
|
97 | 57 | return true; |
|
98 | } |
||
99 | // Check if the requested entry exists in the cache |
||
100 | 62 | return isset($this->cellCache[$pCoord]); |
|
101 | } |
||
102 | |||
103 | /** |
||
104 | * Move a cell object from one address to another |
||
105 | * |
||
106 | * @param string $fromAddress Current address of the cell to move |
||
107 | * @param string $toAddress Destination address of the cell to move |
||
108 | * @return bool |
||
109 | */ |
||
110 | public function moveCell($fromAddress, $toAddress) |
||
123 | |||
124 | /** |
||
125 | * Add or Update a cell in cache |
||
126 | * |
||
127 | * @param \PhpOffice\PhpSpreadsheet\Cell $cell Cell to update |
||
128 | * @throws \PhpOffice\PhpSpreadsheet\Exception |
||
129 | * @return \PhpOffice\PhpSpreadsheet\Cell |
||
130 | */ |
||
131 | 62 | public function updateCacheData(\PhpOffice\PhpSpreadsheet\Cell $cell) |
|
135 | |||
136 | /** |
||
137 | * Delete a cell in cache identified by coordinate address |
||
138 | * |
||
139 | * @param string $pCoord Coordinate address of the cell to delete |
||
140 | * @throws \PhpOffice\PhpSpreadsheet\Exception |
||
141 | */ |
||
142 | 12 | public function deleteCacheData($pCoord) |
|
143 | { |
||
144 | 12 | if ($pCoord === $this->currentObjectID && !is_null($this->currentObject)) { |
|
145 | $this->currentObject->detach(); |
||
146 | $this->currentObjectID = $this->currentObject = null; |
||
147 | } |
||
148 | |||
149 | 12 | if (is_object($this->cellCache[$pCoord])) { |
|
150 | 12 | $this->cellCache[$pCoord]->detach(); |
|
151 | 12 | unset($this->cellCache[$pCoord]); |
|
152 | } |
||
153 | 12 | $this->currentCellIsDirty = false; |
|
154 | 12 | } |
|
155 | |||
156 | /** |
||
157 | * Get a list of all cell addresses currently held in cache |
||
158 | * |
||
159 | * @return string[] |
||
160 | */ |
||
161 | 60 | public function getCellList() |
|
165 | |||
166 | /** |
||
167 | * Sort the list of all cell addresses currently held in cache by row and column |
||
168 | * |
||
169 | * @return string[] |
||
170 | */ |
||
171 | 60 | public function getSortedCellList() |
|
182 | |||
183 | /** |
||
184 | * Get highest worksheet column and highest row that have cell records |
||
185 | * |
||
186 | * @return array Highest column name and highest row number |
||
187 | */ |
||
188 | 59 | public function getHighestRowAndColumn() |
|
189 | { |
||
190 | // Lookup highest column and highest row |
||
191 | 59 | $col = ['A' => '1A']; |
|
192 | 59 | $row = [1]; |
|
193 | 59 | foreach ($this->getCellList() as $coord) { |
|
194 | 59 | sscanf($coord, '%[A-Z]%d', $c, $r); |
|
195 | 59 | $row[$r] = $r; |
|
196 | 59 | $col[$c] = strlen($c) . $c; |
|
197 | } |
||
198 | 59 | if (!empty($row)) { |
|
199 | // Determine highest column and row |
||
200 | 59 | $highestRow = max($row); |
|
201 | 59 | $highestColumn = substr(max($col), 1); |
|
202 | } |
||
203 | |||
204 | return [ |
||
205 | 59 | 'row' => $highestRow, |
|
206 | 59 | 'column' => $highestColumn, |
|
207 | ]; |
||
208 | } |
||
209 | |||
210 | /** |
||
211 | * Return the cell address of the currently active cell object |
||
212 | * |
||
213 | * @return string |
||
214 | */ |
||
215 | 62 | public function getCurrentAddress() |
|
219 | |||
220 | /** |
||
221 | * Return the column address of the currently active cell object |
||
222 | * |
||
223 | * @return string |
||
224 | */ |
||
225 | 42 | public function getCurrentColumn() |
|
226 | { |
||
227 | 42 | sscanf($this->currentObjectID, '%[A-Z]%d', $column, $row); |
|
228 | |||
229 | 42 | return $column; |
|
230 | } |
||
231 | |||
232 | /** |
||
233 | * Return the row address of the currently active cell object |
||
234 | * |
||
235 | * @return int |
||
236 | */ |
||
237 | 42 | public function getCurrentRow() |
|
238 | { |
||
239 | 42 | sscanf($this->currentObjectID, '%[A-Z]%d', $column, $row); |
|
240 | |||
241 | 42 | return (integer) $row; |
|
242 | } |
||
243 | |||
244 | /** |
||
245 | * Get highest worksheet column |
||
246 | * |
||
247 | * @param string $row Return the highest column for the specified row, |
||
248 | * or the highest column of any row if no row number is passed |
||
249 | * @return string Highest column name |
||
250 | */ |
||
251 | 10 | public function getHighestColumn($row = null) |
|
252 | { |
||
253 | 10 | if ($row == null) { |
|
254 | 10 | $colRow = $this->getHighestRowAndColumn(); |
|
255 | |||
256 | 10 | return $colRow['column']; |
|
257 | } |
||
258 | |||
259 | $columnList = [1]; |
||
260 | foreach ($this->getCellList() as $coord) { |
||
261 | sscanf($coord, '%[A-Z]%d', $c, $r); |
||
262 | if ($r != $row) { |
||
263 | continue; |
||
264 | } |
||
265 | $columnList[] = \PhpOffice\PhpSpreadsheet\Cell::columnIndexFromString($c); |
||
266 | } |
||
267 | |||
268 | return \PhpOffice\PhpSpreadsheet\Cell::stringFromColumnIndex(max($columnList) - 1); |
||
269 | } |
||
270 | |||
271 | /** |
||
272 | * Get highest worksheet row |
||
273 | * |
||
274 | * @param string $column Return the highest row for the specified column, |
||
275 | * or the highest row of any column if no column letter is passed |
||
276 | * @return int Highest row number |
||
277 | */ |
||
278 | 12 | public function getHighestRow($column = null) |
|
279 | { |
||
280 | 12 | if ($column == null) { |
|
281 | 12 | $colRow = $this->getHighestRowAndColumn(); |
|
282 | |||
283 | 12 | return $colRow['row']; |
|
284 | } |
||
285 | |||
286 | $rowList = [0]; |
||
287 | foreach ($this->getCellList() as $coord) { |
||
288 | sscanf($coord, '%[A-Z]%d', $c, $r); |
||
289 | if ($c != $column) { |
||
290 | continue; |
||
291 | } |
||
292 | $rowList[] = $r; |
||
293 | } |
||
294 | |||
295 | return max($rowList); |
||
296 | } |
||
297 | |||
298 | /** |
||
299 | * Generate a unique ID for cache referencing |
||
300 | * |
||
301 | * @return string Unique Reference |
||
302 | */ |
||
303 | 1 | protected function getUniqueID() |
|
313 | |||
314 | /** |
||
315 | * Clone the cell collection |
||
316 | * |
||
317 | * @param \PhpOffice\PhpSpreadsheet\Worksheet $parent The new worksheet that we're copying to |
||
318 | */ |
||
319 | 1 | public function copyCellCollection(\PhpOffice\PhpSpreadsheet\Worksheet $parent) |
|
320 | { |
||
321 | 1 | $this->currentCellIsDirty; |
|
322 | 1 | $this->storeData(); |
|
323 | |||
324 | 1 | $this->parent = $parent; |
|
325 | 1 | if (($this->currentObject !== null) && (is_object($this->currentObject))) { |
|
326 | $this->currentObject->attach($this); |
||
327 | } |
||
328 | 1 | } |
|
329 | |||
330 | /** |
||
331 | * Remove a row, deleting all cells in that row |
||
332 | * |
||
333 | * @param string $row Row number to remove |
||
334 | */ |
||
335 | 12 | View Code Duplication | public function removeRow($row) |
336 | { |
||
337 | 12 | foreach ($this->getCellList() as $coord) { |
|
338 | 12 | sscanf($coord, '%[A-Z]%d', $c, $r); |
|
339 | 12 | if ($r == $row) { |
|
340 | 12 | $this->deleteCacheData($coord); |
|
341 | } |
||
342 | } |
||
343 | 12 | } |
|
344 | |||
345 | /** |
||
346 | * Remove a column, deleting all cells in that column |
||
347 | * |
||
348 | * @param string $column Column ID to remove |
||
349 | */ |
||
350 | 9 | View Code Duplication | public function removeColumn($column) |
351 | { |
||
352 | 9 | foreach ($this->getCellList() as $coord) { |
|
353 | 9 | sscanf($coord, '%[A-Z]%d', $c, $r); |
|
354 | 9 | if ($c == $column) { |
|
355 | 9 | $this->deleteCacheData($coord); |
|
356 | } |
||
357 | } |
||
358 | 9 | } |
|
359 | |||
360 | /** |
||
361 | * Identify whether the caching method is currently available |
||
362 | * Some methods are dependent on the availability of certain extensions being enabled in the PHP build |
||
363 | * |
||
364 | * @return bool |
||
365 | */ |
||
366 | 63 | public static function cacheMethodIsAvailable() |
|
370 | } |
||
371 |
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.