Complex classes like RowIterator 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 RowIterator, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class RowIterator implements IteratorInterface |
||
| 19 | { |
||
| 20 | /** Definition of XML nodes names used to parse data */ |
||
| 21 | const XML_NODE_DIMENSION = 'dimension'; |
||
| 22 | const XML_NODE_WORKSHEET = 'worksheet'; |
||
| 23 | const XML_NODE_ROW = 'row'; |
||
| 24 | const XML_NODE_CELL = 'c'; |
||
| 25 | |||
| 26 | /** Definition of XML attributes used to parse data */ |
||
| 27 | const XML_ATTRIBUTE_REF = 'ref'; |
||
| 28 | const XML_ATTRIBUTE_SPANS = 'spans'; |
||
| 29 | const XML_ATTRIBUTE_ROW_INDEX = 'r'; |
||
| 30 | const XML_ATTRIBUTE_CELL_INDEX = 'r'; |
||
| 31 | |||
| 32 | /** @var string Path of the XLSX file being read */ |
||
| 33 | protected $filePath; |
||
| 34 | |||
| 35 | /** @var string $sheetDataXMLFilePath Path of the sheet data XML file as in [Content_Types].xml */ |
||
| 36 | protected $sheetDataXMLFilePath; |
||
| 37 | |||
| 38 | /** @var \Box\Spout\Reader\Wrapper\XMLReader The XMLReader object that will help read sheet's XML data */ |
||
| 39 | protected $xmlReader; |
||
| 40 | |||
| 41 | /** @var Helper\CellValueFormatter Helper to format cell values */ |
||
| 42 | protected $cellValueFormatter; |
||
| 43 | |||
| 44 | /** @var Helper\StyleHelper $styleHelper Helper to work with styles */ |
||
| 45 | protected $styleHelper; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * TODO: This variable can be deleted when row indices get preserved |
||
| 49 | * @var int Number of read rows |
||
| 50 | */ |
||
| 51 | protected $numReadRows = 0; |
||
| 52 | |||
| 53 | /** @var array|null Buffer used to store the row data, while checking if there are more rows to read */ |
||
| 54 | protected $rowDataBuffer = null; |
||
| 55 | |||
| 56 | /** @var bool Indicates whether all rows have been read */ |
||
| 57 | protected $hasReachedEndOfFile = false; |
||
| 58 | |||
| 59 | /** @var int The number of columns the sheet has (0 meaning undefined) */ |
||
| 60 | protected $numColumns = 0; |
||
| 61 | |||
| 62 | /** @var bool Whether empty rows should be returned or skipped */ |
||
| 63 | protected $shouldPreserveEmptyRows; |
||
| 64 | |||
| 65 | /** @var int Last row index processed (one-based) */ |
||
| 66 | protected $lastRowIndexProcessed = 0; |
||
| 67 | |||
| 68 | /** @var int Row index to be processed next (one-based) */ |
||
| 69 | protected $nextRowIndexToBeProcessed = 0; |
||
| 70 | |||
| 71 | /** @var int Last column index processed (zero-based) */ |
||
| 72 | protected $lastColumnIndexProcessed = -1; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @param string $filePath Path of the XLSX file being read |
||
| 76 | * @param string $sheetDataXMLFilePath Path of the sheet data XML file as in [Content_Types].xml |
||
| 77 | * @param Helper\SharedStringsHelper $sharedStringsHelper Helper to work with shared strings |
||
| 78 | * @param bool $shouldFormatDates Whether date/time values should be returned as PHP objects or be formatted as strings |
||
| 79 | * @param bool $shouldPreserveEmptyRows Whether empty rows should be returned or skipped |
||
| 80 | */ |
||
| 81 | 96 | public function __construct($filePath, $sheetDataXMLFilePath, $sharedStringsHelper, $shouldFormatDates, $shouldPreserveEmptyRows) |
|
| 93 | |||
| 94 | /** |
||
| 95 | * @param string $sheetDataXMLFilePath Path of the sheet data XML file as in [Content_Types].xml |
||
| 96 | * @return string Path of the XML file containing the sheet data, |
||
| 97 | * without the leading slash. |
||
| 98 | */ |
||
| 99 | 96 | protected function normalizeSheetDataXMLFilePath($sheetDataXMLFilePath) |
|
| 103 | |||
| 104 | /** |
||
| 105 | * Rewind the Iterator to the first element. |
||
| 106 | * Initializes the XMLReader object that reads the associated sheet data. |
||
| 107 | * The XMLReader is configured to be safe from billion laughs attack. |
||
| 108 | * @link http://php.net/manual/en/iterator.rewind.php |
||
| 109 | * |
||
| 110 | * @return void |
||
| 111 | * @throws \Box\Spout\Common\Exception\IOException If the sheet data XML cannot be read |
||
| 112 | */ |
||
| 113 | 93 | public function rewind() |
|
| 131 | |||
| 132 | /** |
||
| 133 | * Checks if current position is valid |
||
| 134 | * @link http://php.net/manual/en/iterator.valid.php |
||
| 135 | * |
||
| 136 | * @return bool |
||
| 137 | */ |
||
| 138 | 90 | public function valid() |
|
| 142 | |||
| 143 | /** |
||
| 144 | * Move forward to next element. Reads data describing the next unprocessed row. |
||
| 145 | * @link http://php.net/manual/en/iterator.next.php |
||
| 146 | * |
||
| 147 | * @return void |
||
| 148 | * @throws \Box\Spout\Reader\Exception\SharedStringNotFoundException If a shared string was not found |
||
| 149 | * @throws \Box\Spout\Common\Exception\IOException If unable to read the sheet data XML |
||
| 150 | */ |
||
| 151 | 90 | public function next() |
|
| 159 | |||
| 160 | /** |
||
| 161 | * Returns whether we need data for the next row to be processed. |
||
| 162 | * We don't need to read data if: |
||
| 163 | * we have already read at least one row |
||
| 164 | * AND |
||
| 165 | * we need to preserve empty rows |
||
| 166 | * AND |
||
| 167 | * the last row that was read is not the row that need to be processed |
||
| 168 | * (i.e. if we need to return empty rows) |
||
| 169 | * |
||
| 170 | * @return bool Whether we need data for the next row to be processed. |
||
| 171 | */ |
||
| 172 | 90 | protected function doesNeedDataForNextRowToBeProcessed() |
|
| 182 | |||
| 183 | /** |
||
| 184 | * @param \Box\Spout\Reader\Wrapper\XMLReader $xmlReader XMLReader object |
||
| 185 | * @return void |
||
| 186 | * @throws \Box\Spout\Reader\Exception\SharedStringNotFoundException If a shared string was not found |
||
| 187 | * @throws \Box\Spout\Common\Exception\IOException If unable to read the sheet data XML |
||
| 188 | */ |
||
| 189 | 90 | protected function readDataForNextRow($xmlReader) |
|
| 229 | |||
| 230 | /** |
||
| 231 | * @param \Box\Spout\Reader\Wrapper\XMLReader $xmlReader XMLReader object, positioned on a "<dimension>" starting node |
||
| 232 | * @return void |
||
| 233 | */ |
||
| 234 | 45 | protected function processDimensionStartingNode($xmlReader) |
|
| 243 | |||
| 244 | /** |
||
| 245 | * @param \Box\Spout\Reader\Wrapper\XMLReader $xmlReader XMLReader object, positioned on a "<row>" starting node |
||
| 246 | * @return array |
||
| 247 | */ |
||
| 248 | 87 | protected function processRowStartingNode($xmlReader) |
|
| 266 | |||
| 267 | /** |
||
| 268 | * @param \Box\Spout\Reader\Wrapper\XMLReader $xmlReader XMLReader object, positioned on a "<cell>" starting node |
||
| 269 | * @param array $rowData Data of all cells read so far (key = cell index, value = cell value) |
||
| 270 | * @return array Original row data + data for the cell that was just read (key = cell index, value = cell value) |
||
| 271 | */ |
||
| 272 | 87 | protected function processCellStartingNode($xmlReader, $rowData) |
|
| 283 | |||
| 284 | /** |
||
| 285 | * @param array $rowData Data of all cells read so far (key = cell index, value = cell value) |
||
| 286 | * @return array |
||
| 287 | */ |
||
| 288 | 87 | protected function processRowEndingNode($rowData) |
|
| 295 | |||
| 296 | /** |
||
| 297 | * @return void |
||
| 298 | */ |
||
| 299 | 87 | protected function processWorksheetEndingNode() |
|
| 304 | |||
| 305 | /** |
||
| 306 | * @param \Box\Spout\Reader\Wrapper\XMLReader $xmlReader XMLReader object, positioned on a "<row>" node |
||
| 307 | * @return int Row index |
||
| 308 | * @throws \Box\Spout\Common\Exception\InvalidArgumentException When the given cell index is invalid |
||
| 309 | */ |
||
| 310 | 87 | protected function getRowIndex($xmlReader) |
|
| 319 | |||
| 320 | /** |
||
| 321 | * @param \Box\Spout\Reader\Wrapper\XMLReader $xmlReader XMLReader object, positioned on a "<c>" node |
||
| 322 | * @return int Column index |
||
| 323 | * @throws \Box\Spout\Common\Exception\InvalidArgumentException When the given cell index is invalid |
||
| 324 | */ |
||
| 325 | 87 | protected function getColumnIndex($xmlReader) |
|
| 334 | |||
| 335 | /** |
||
| 336 | * Returns the (unescaped) correctly marshalled, cell value associated to the given XML node. |
||
| 337 | * |
||
| 338 | * @param \DOMNode $node |
||
| 339 | * @return string|int|float|bool|\DateTime|null The value associated with the cell (null when the cell has an error) |
||
| 340 | */ |
||
| 341 | 87 | protected function getCellValue($node) |
|
| 345 | |||
| 346 | /** |
||
| 347 | * @param array $rowData |
||
| 348 | * @return bool Whether the given row is empty |
||
| 349 | */ |
||
| 350 | 84 | protected function isEmptyRow($rowData) |
|
| 354 | |||
| 355 | /** |
||
| 356 | * Return the current element, either an empty row or from the buffer. |
||
| 357 | * @link http://php.net/manual/en/iterator.current.php |
||
| 358 | * |
||
| 359 | * @return array|null |
||
| 360 | */ |
||
| 361 | 87 | public function current() |
|
| 379 | |||
| 380 | /** |
||
| 381 | * Return the key of the current element. Here, the row index. |
||
| 382 | * @link http://php.net/manual/en/iterator.key.php |
||
| 383 | * |
||
| 384 | * @return int |
||
| 385 | */ |
||
| 386 | 84 | public function key() |
|
| 395 | |||
| 396 | |||
| 397 | /** |
||
| 398 | * Cleans up what was created to iterate over the object. |
||
| 399 | * |
||
| 400 | * @return void |
||
| 401 | */ |
||
| 402 | 93 | public function end() |
|
| 406 | } |
||
| 407 |