From 27d3f09a90450ab7069111f32dabc7b5211609ba Mon Sep 17 00:00:00 2001 From: oleibman <10341515+oleibman@users.noreply.github.com> Date: Sun, 10 Dec 2023 07:44:30 -0800 Subject: [PATCH] Reader Xls Dead Code (#3756) * Reader Xls Dead Code Recent changes left Xls Reader with a ton of Scrutinizer messages. I have filtered away the false positives. Most of what is left is dead code, and this PR addresses those. Most of those are used to parse the BIFF data, but we either don't need to implement, or haven't yet implemented, those particular parsed data items. I think it is more useful to comment those out for documetary purposes rather than deleting them. * Missed a Few --- src/PhpSpreadsheet/Reader/Xls.php | 124 ++++++++++++++---------------- 1 file changed, 57 insertions(+), 67 deletions(-) diff --git a/src/PhpSpreadsheet/Reader/Xls.php b/src/PhpSpreadsheet/Reader/Xls.php index 4b4a8e4844..b18fd09a12 100644 --- a/src/PhpSpreadsheet/Reader/Xls.php +++ b/src/PhpSpreadsheet/Reader/Xls.php @@ -1062,7 +1062,7 @@ protected function loadSpreadsheetFromFile(string $filename): Spreadsheet case 0x19: // Note if (isset($this->cellNotes[$obj['idObjID']])) { - $cellNote = $this->cellNotes[$obj['idObjID']]; + //$cellNote = $this->cellNotes[$obj['idObjID']]; if (isset($this->textObjects[$obj['idObjID']])) { $textObject = $this->textObjects[$obj['idObjID']]; @@ -1173,6 +1173,7 @@ protected function loadSpreadsheetFromFile(string $filename): Spreadsheet $ranges = explode(',', $definedName['formula']); // FIXME: what if sheetname contains comma? $extractedRanges = []; + $sheetName = ''; /** @var non-empty-string $range */ foreach ($ranges as $range) { // $range should look like one of these @@ -1347,7 +1348,7 @@ private function readSummaryInformation(): void // offset: 6; size: 2; OS indicator // offset: 8; size: 16 // offset: 24; size: 4; section count - $secCount = self::getInt4d($this->summaryInformation, 24); + //$secCount = self::getInt4d($this->summaryInformation, 24); // offset: 28; size: 16; first section's class id: e0 85 9f f2 f9 4f 68 10 ab 91 08 00 2b 27 b3 d9 // offset: 44; size: 4 @@ -1355,7 +1356,7 @@ private function readSummaryInformation(): void // section header // offset: $secOffset; size: 4; section length - $secLength = self::getInt4d($this->summaryInformation, $secOffset); + //$secLength = self::getInt4d($this->summaryInformation, $secOffset); // offset: $secOffset+4; size: 4; property count $countProperties = self::getInt4d($this->summaryInformation, $secOffset + 4); @@ -1494,7 +1495,7 @@ private function readDocumentSummaryInformation(): void // offset: 6; size: 2; OS indicator // offset: 8; size: 16 // offset: 24; size: 4; section count - $secCount = self::getInt4d($this->documentSummaryInformation, 24); + //$secCount = self::getInt4d($this->documentSummaryInformation, 24); // offset: 28; size: 16; first section's class id: 02 d5 cd d5 9c 2e 1b 10 93 97 08 00 2b 2c f9 ae // offset: 44; size: 4; first section offset @@ -1502,7 +1503,7 @@ private function readDocumentSummaryInformation(): void // section header // offset: $secOffset; size: 4; section length - $secLength = self::getInt4d($this->documentSummaryInformation, $secOffset); + //$secLength = self::getInt4d($this->documentSummaryInformation, $secOffset); // offset: $secOffset+4; size: 4; property count $countProperties = self::getInt4d($this->documentSummaryInformation, $secOffset + 4); @@ -1660,14 +1661,14 @@ private function readNote(): void // If the address row is -1 and the column is 0, (which translates as $B$65536) then this is a continuation // note from the previous cell annotation. We're not yet handling this, so annotations longer than the // max 2048 bytes will probably throw a wobbly. - $row = self::getUInt2d($recordData, 0); + //$row = self::getUInt2d($recordData, 0); $extension = true; $arrayKeys = array_keys($this->phpSheet->getComments()); $cellAddress = array_pop($arrayKeys); } $cellAddress = str_replace('$', '', (string) $cellAddress); - $noteLength = self::getUInt2d($recordData, 4); + //$noteLength = self::getUInt2d($recordData, 4); $noteText = trim(substr($recordData, 6)); if ($extension) { @@ -1706,7 +1707,7 @@ private function readTextObject(): void // followed by the continuation records containing the actual text and formatting $grbitOpts = self::getUInt2d($recordData, 0); $rot = self::getUInt2d($recordData, 2); - $cchText = self::getUInt2d($recordData, 10); + //$cchText = self::getUInt2d($recordData, 10); $cbRuns = self::getUInt2d($recordData, 12); $text = $this->getSplicedRecordData(); @@ -2224,12 +2225,12 @@ private function readXf(): void $diagonalUp = ((int) 0x80000000 & self::getInt4d($recordData, 10)) >> 31 ? true : false; if ($diagonalUp === false) { - if ($diagonalDown == false) { + if ($diagonalDown === false) { $objStyle->getBorders()->setDiagonalDirection(Borders::DIAGONAL_NONE); } else { $objStyle->getBorders()->setDiagonalDirection(Borders::DIAGONAL_DOWN); } - } elseif ($diagonalDown == false) { + } elseif ($diagonalDown === false) { $objStyle->getBorders()->setDiagonalDirection(Borders::DIAGONAL_UP); } else { $objStyle->getBorders()->setDiagonalDirection(Borders::DIAGONAL_BOTH); @@ -2368,7 +2369,7 @@ private function readXfExt(): void // offset: 16; size: 2; not used // offset: 18; size: 2; number of extension properties that follow - $cexts = self::getUInt2d($recordData, 18); + //$cexts = self::getUInt2d($recordData, 18); // start reading the actual extension data $offset = 20; @@ -2534,7 +2535,7 @@ private function readStyle(): void $ixfe = self::getUInt2d($recordData, 0); // bit: 11-0; mask 0x0FFF; index to XF record - $xfIndex = (0x0FFF & $ixfe) >> 0; + //$xfIndex = (0x0FFF & $ixfe) >> 0; // bit: 15; mask 0x8000; 0 = user-defined style, 1 = built-in style $isBuiltIn = (bool) ((0x8000 & $ixfe) >> 15); @@ -2707,7 +2708,7 @@ private function readExternName(): void // external sheet references provided for named cells if ($this->version == self::XLS_BIFF8) { // offset: 0; size: 2; options - $options = self::getUInt2d($recordData, 0); + //$options = self::getUInt2d($recordData, 0); // offset: 2; size: 2; @@ -2822,7 +2823,7 @@ private function readDefinedName(): void */ private function readMsoDrawingGroup(): void { - $length = self::getUInt2d($this->data, $this->pos + 2); + //$length = self::getUInt2d($this->data, $this->pos + 2); // get spliced record data $splicedRecordData = $this->getSplicedRecordData(); @@ -3115,7 +3116,7 @@ private function readHorizontalPageBreaks(): void for ($i = 0; $i < $nm; ++$i) { $r = self::getUInt2d($recordData, 2 + 6 * $i); $cf = self::getUInt2d($recordData, 2 + 6 * $i + 2); - $cl = self::getUInt2d($recordData, 2 + 6 * $i + 4); + //$cl = self::getUInt2d($recordData, 2 + 6 * $i + 4); // not sure why two column indexes are necessary? $this->phpSheet->setBreak([$cf + 1, $r], Worksheet::BREAK_ROW); @@ -3662,7 +3663,7 @@ private function readLabelSst(): void $column = self::getUInt2d($recordData, 2); $columnString = Coordinate::stringFromColumnIndex($column + 1); - $emptyCell = true; + $cell = null; // Read cell? if (($this->getReadFilter() !== null) && $this->getReadFilter()->readCell($columnString, $row + 1, $this->phpSheet->getTitle())) { // offset: 4; size: 2; index to XF record @@ -3709,17 +3710,15 @@ private function readLabelSst(): void if ($this->readEmptyCells || trim($richText->getPlainText()) !== '') { $cell = $this->phpSheet->getCell($columnString . ($row + 1)); $cell->setValueExplicit($richText, DataType::TYPE_STRING); - $emptyCell = false; } } else { if ($this->readEmptyCells || trim($this->sst[$index]['value']) !== '') { $cell = $this->phpSheet->getCell($columnString . ($row + 1)); $cell->setValueExplicit($this->sst[$index]['value'], DataType::TYPE_STRING); - $emptyCell = false; } } - if (!$this->readDataOnly && !$emptyCell && isset($this->mapCellXfIndex[$xfIndex])) { + if (!$this->readDataOnly && $cell !== null && isset($this->mapCellXfIndex[$xfIndex])) { // add style information $cell->setXfIndex($this->mapCellXfIndex[$xfIndex]); } @@ -3969,13 +3968,13 @@ private function readSharedFmla(): void $this->pos += 4 + $length; // offset: 0, size: 6; cell range address of the area used by the shared formula, not used for anything - $cellRange = substr($recordData, 0, 6); - $cellRange = $this->readBIFF5CellRangeAddressFixed($cellRange); // note: even BIFF8 uses BIFF5 syntax + //$cellRange = substr($recordData, 0, 6); + //$cellRange = $this->readBIFF5CellRangeAddressFixed($cellRange); // note: even BIFF8 uses BIFF5 syntax // offset: 6, size: 1; not used // offset: 7, size: 1; number of existing FORMULA records for this shared formula - $no = ord($recordData[7]); + //$no = ord($recordData[7]); // offset: 8, size: var; Binary token array of the shared formula $formula = substr($recordData, 8); @@ -4196,7 +4195,7 @@ private function readBlank(): void */ private function readMsoDrawing(): void { - $length = self::getUInt2d($this->data, $this->pos + 2); + //$length = self::getUInt2d($this->data, $this->pos + 2); // get spliced record data $splicedRecordData = $this->getSplicedRecordData(); @@ -4260,10 +4259,10 @@ private function readWindow2(): void $options = self::getUInt2d($recordData, 0); // offset: 2; size: 2; index to first visible row - $firstVisibleRow = self::getUInt2d($recordData, 2); + //$firstVisibleRow = self::getUInt2d($recordData, 2); // offset: 4; size: 2; index to first visible colum - $firstVisibleColumn = self::getUInt2d($recordData, 4); + //$firstVisibleColumn = self::getUInt2d($recordData, 4); $zoomscaleInPageBreakPreview = 0; $zoomscaleInNormalView = 0; if ($this->version === self::XLS_BIFF8) { @@ -4343,10 +4342,10 @@ private function readPageLayoutView(): void // offset: 0; size: 2; rt //->ignore - $rt = self::getUInt2d($recordData, 0); + //$rt = self::getUInt2d($recordData, 0); // offset: 2; size: 2; grbitfr //->ignore - $grbitFrt = self::getUInt2d($recordData, 2); + //$grbitFrt = self::getUInt2d($recordData, 2); // offset: 4; size: 8; reserved //->ignore @@ -4357,8 +4356,8 @@ private function readPageLayoutView(): void // decomprise grbit $fPageLayoutView = $grbit & 0x01; - $fRulerVisible = ($grbit >> 1) & 0x01; //no support - $fWhitespaceHidden = ($grbit >> 3) & 0x01; //no support + //$fRulerVisible = ($grbit >> 1) & 0x01; //no support + //$fWhitespaceHidden = ($grbit >> 3) & 0x01; //no support if ($fPageLayoutView === 1) { $this->phpSheet->getSheetView()->setView(SheetView::SHEETVIEW_PAGE_LAYOUT); @@ -4435,17 +4434,17 @@ private function readSelection(): void if (!$this->readDataOnly) { // offset: 0; size: 1; pane identifier - $paneId = ord($recordData[0]); + //$paneId = ord($recordData[0]); // offset: 1; size: 2; index to row of the active cell - $r = self::getUInt2d($recordData, 1); + //$r = self::getUInt2d($recordData, 1); // offset: 3; size: 2; index to column of the active cell - $c = self::getUInt2d($recordData, 3); + //$c = self::getUInt2d($recordData, 3); // offset: 5; size: 2; index into the following cell range list to the // entry that contains the active cell - $index = self::getUInt2d($recordData, 5); + //$index = self::getUInt2d($recordData, 5); // offset: 7; size: var; cell range address list containing all selected cell ranges $data = substr($recordData, 7); @@ -4551,7 +4550,7 @@ private function readHyperLink(): void $isFileLinkOrUrl = (0x00000001 & self::getUInt2d($recordData, 28)) >> 0; // bit: 1; mask: 0x00000002; 0 = relative path, 1 = absolute path or URL - $isAbsPathOrUrl = (0x00000001 & self::getUInt2d($recordData, 28)) >> 1; + //$isAbsPathOrUrl = (0x00000001 & self::getUInt2d($recordData, 28)) >> 1; // bit: 2 (and 4); mask: 0x00000014; 0 = no description $hasDesc = (0x00000014 & self::getUInt2d($recordData, 28)) >> 2; @@ -4572,7 +4571,7 @@ private function readHyperLink(): void // offset: 32; size: var; character count of description text $dl = self::getInt4d($recordData, 32); // offset: 36; size: var; character array of description text, no Unicode string header, always 16-bit characters, zero terminated - $desc = self::encodeUTF16(substr($recordData, 36, 2 * ($dl - 1)), false); + //$desc = self::encodeUTF16(substr($recordData, 36, 2 * ($dl - 1)), false); $offset += 4 + 2 * $dl; } if ($hasFrame) { @@ -4645,6 +4644,7 @@ private function readHyperLink(): void $sz = self::getInt4d($recordData, $offset); $offset += 4; + $extendedFilePath = ''; // only present if $sz > 0 if ($sz > 0) { // offset: var; size: 4; size of the character array of the extended file path and name @@ -4702,7 +4702,7 @@ private function readHyperLink(): void private function readDataValidations(): void { $length = self::getUInt2d($this->data, $this->pos + 2); - $recordData = $this->readRecordData($this->data, $this->pos + 4, $length); + //$recordData = $this->readRecordData($this->data, $this->pos + 4, $length); // move stream pointer forward to next record $this->pos += 4 + $length; @@ -4736,7 +4736,7 @@ private function readDataValidation(): void // bit: 7; mask: 0x00000080; 1= formula is explicit (only applies to list) // I have only seen cases where this is 1 - $explicitFormula = (0x00000080 & $options) >> 7; + //$explicitFormula = (0x00000080 & $options) >> 7; // bit: 8; mask: 0x00000100; 1= empty cells allowed $allowBlank = (0x00000100 & $options) >> 8; @@ -4856,9 +4856,6 @@ private function readSheetLayout(): void // move stream pointer to next record $this->pos += 4 + $length; - // local pointer in record data - $offset = 0; - if (!$this->readDataOnly) { // offset: 0; size: 2; repeated record identifier 0x0862 @@ -5033,7 +5030,7 @@ private function readRangeProtection(): void } // offset: var; size: var; variable length of feature specific data - $rgbFeat = substr($recordData, $offset); + //$rgbFeat = substr($recordData, $offset); $offset += 4; // offset: var; size: 4; the encrypted password (only 16-bit although field is 32-bit) @@ -5113,7 +5110,7 @@ private function getSplicedRecordData(): array ++$i; // offset: 0; size: 2; identifier - $identifier = self::getUInt2d($this->data, $this->pos); + //$identifier = self::getUInt2d($this->data, $this->pos); // offset: 2; size: 2; length $length = self::getUInt2d($this->data, $this->pos + 2); $data .= $this->readRecordData($this->data, $this->pos + 4, $length); @@ -7189,8 +7186,6 @@ private function readByteStringLong(string $subData): array */ private static function readUnicodeStringShort($subData) { - $value = ''; - // offset: 0: size: 1; length of the string (character count) $characterCount = ord($subData[0]); @@ -7213,8 +7208,6 @@ private static function readUnicodeStringShort($subData) */ private static function readUnicodeStringLong($subData) { - $value = ''; - // offset: 0: size: 2; length of the string (character count) $characterCount = self::getUInt2d($subData, 0); @@ -7236,17 +7229,15 @@ private static function readUnicodeStringLong($subData) */ private static function readUnicodeString($subData, $characterCount): array { - $value = ''; - // offset: 0: size: 1; option flags // bit: 0; mask: 0x01; character compression (0 = compressed 8-bit, 1 = uncompressed 16-bit) $isCompressed = !((0x01 & ord($subData[0])) >> 0); // bit: 2; mask: 0x04; Asian phonetic settings - $hasAsian = (0x04) & ord($subData[0]) >> 2; + //$hasAsian = (0x04) & ord($subData[0]) >> 2; // bit: 3; mask: 0x08; Rich-Text settings - $hasRichText = (0x08) & ord($subData[0]) >> 3; + //$hasRichText = (0x08) & ord($subData[0]) >> 3; // offset: 1: size: var; character array // this offset assumes richtext and Asian phonetic settings are off which is generally wrong @@ -7498,7 +7489,7 @@ private function readCFRule(array $cellRangeAddresses): void $options = self::getInt4d($recordData, 6); $style = new Style(false, true); // non-supervisor, conditional - $this->getCFStyleOptions($options, $style); + //$this->getCFStyleOptions($options, $style); $hasFontRecord = (bool) ((0x04000000 & $options) >> 26); $hasAlignmentRecord = (bool) ((0x08000000 & $options) >> 27); @@ -7515,14 +7506,14 @@ private function readCFRule(array $cellRangeAddresses): void } if ($hasAlignmentRecord === true) { - $alignmentStyle = substr($recordData, $offset, 8); - $this->getCFAlignmentStyle($alignmentStyle, $style); + //$alignmentStyle = substr($recordData, $offset, 8); + //$this->getCFAlignmentStyle($alignmentStyle, $style); $offset += 8; } if ($hasBorderRecord === true) { - $borderStyle = substr($recordData, $offset, 8); - $this->getCFBorderStyle($borderStyle, $style); + //$borderStyle = substr($recordData, $offset, 8); + //$this->getCFBorderStyle($borderStyle, $style); $offset += 8; } @@ -7533,8 +7524,8 @@ private function readCFRule(array $cellRangeAddresses): void } if ($hasProtectionRecord === true) { - $protectionStyle = substr($recordData, $offset, 4); - $this->getCFProtectionStyle($protectionStyle, $style); + //$protectionStyle = substr($recordData, $offset, 4); + //$this->getCFProtectionStyle($protectionStyle, $style); $offset += 2; } @@ -7560,9 +7551,9 @@ private function readCFRule(array $cellRangeAddresses): void $this->setCFRules($cellRangeAddresses, $type, $operator, $formula1, $formula2, $style); } - private function getCFStyleOptions(int $options, Style $style): void + /*private function getCFStyleOptions(int $options, Style $style): void { - } + }*/ private function getCFFontStyle(string $options, Style $style): void { @@ -7581,13 +7572,13 @@ private function getCFFontStyle(string $options, Style $style): void } } - private function getCFAlignmentStyle(string $options, Style $style): void + /*private function getCFAlignmentStyle(string $options, Style $style): void { - } + }*/ - private function getCFBorderStyle(string $options, Style $style): void + /*private function getCFBorderStyle(string $options, Style $style): void { - } + }*/ private function getCFFillStyle(string $options, Style $style): void { @@ -7612,9 +7603,9 @@ private function getCFFillStyle(string $options, Style $style): void } } - private function getCFProtectionStyle(string $options, Style $style): void + /*private function getCFProtectionStyle(string $options, Style $style): void { - } + }*/ private function readCFFormula(string $recordData, int $offset, int $size): float|int|string|null { @@ -7629,9 +7620,8 @@ private function readCFFormula(string $recordData, int $offset, int $size): floa return $formula; } catch (PhpSpreadsheetException) { + return null; } - - return null; } /**