| Conditions | 12 |
| Paths | 24 |
| Total Lines | 66 |
| Code Lines | 35 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 1 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 100 | public function next() |
||
| 101 | { |
||
| 102 | $rowData = []; |
||
| 103 | $cellValue = null; |
||
| 104 | $numColumnsRepeated = 1; |
||
| 105 | $numCellsRead = 0; |
||
| 106 | $hasAlreadyReadOneCell = false; |
||
| 107 | |||
| 108 | try { |
||
| 109 | while ($this->xmlReader->read()) { |
||
| 110 | if ($this->xmlReader->isPositionedOnStartingNode(self::XML_NODE_CELL)) { |
||
| 111 | // Start of a cell description |
||
| 112 | $currentNumColumnsRepeated = $this->getNumColumnsRepeatedForCurrentNode(); |
||
| 113 | |||
| 114 | $node = $this->xmlReader->expand(); |
||
| 115 | $currentCellValue = $this->getCellValue($node); |
||
| 116 | |||
| 117 | // process cell N only after having read cell N+1 (see below why) |
||
| 118 | if ($hasAlreadyReadOneCell) { |
||
| 119 | for ($i = 0; $i < $numColumnsRepeated; $i++) { |
||
| 120 | $rowData[] = $cellValue; |
||
| 121 | } |
||
| 122 | } |
||
| 123 | |||
| 124 | $cellValue = $currentCellValue; |
||
| 125 | $numColumnsRepeated = $currentNumColumnsRepeated; |
||
| 126 | |||
| 127 | $numCellsRead++; |
||
| 128 | $hasAlreadyReadOneCell = true; |
||
| 129 | |||
| 130 | } else if ($this->xmlReader->isPositionedOnEndingNode(self::XML_NODE_ROW)) { |
||
| 131 | // End of the row description |
||
| 132 | $isEmptyRow = ($numCellsRead <= 1 && $this->isEmptyCellValue($cellValue)); |
||
| 133 | if ($isEmptyRow) { |
||
| 134 | // skip empty rows |
||
| 135 | $this->next(); |
||
| 136 | return; |
||
| 137 | } |
||
| 138 | |||
| 139 | // Only add the value if the last read cell is not a trailing empty cell repeater in Excel. |
||
| 140 | // The current count of read columns is determined by counting the values in $rowData. |
||
| 141 | // This is to avoid creating a lot of empty cells, as Excel adds a last empty "<table:table-cell>" |
||
| 142 | // with a number-columns-repeated value equals to the number of (supported columns - used columns). |
||
| 143 | // In Excel, the number of supported columns is 16384, but we don't want to returns rows with |
||
| 144 | // always 16384 cells. |
||
| 145 | if ((count($rowData) + $numColumnsRepeated) !== self::MAX_COLUMNS_EXCEL) { |
||
| 146 | for ($i = 0; $i < $numColumnsRepeated; $i++) { |
||
| 147 | $rowData[] = $cellValue; |
||
| 148 | } |
||
| 149 | $this->numReadRows++; |
||
| 150 | } |
||
| 151 | break; |
||
| 152 | |||
| 153 | } else if ($this->xmlReader->isPositionedOnEndingNode(self::XML_NODE_TABLE)) { |
||
| 154 | // The closing "</table:table>" marks the end of the file |
||
| 155 | $this->hasReachedEndOfFile = true; |
||
| 156 | break; |
||
| 157 | } |
||
| 158 | } |
||
| 159 | |||
| 160 | } catch (XMLProcessingException $exception) { |
||
| 161 | throw new IOException("The sheet's data cannot be read. [{$exception->getMessage()}]"); |
||
| 162 | } |
||
| 163 | |||
| 164 | $this->rowDataBuffer = $rowData; |
||
| 165 | } |
||
| 166 | |||
| 231 |