Skip to content

Commit 5fb76c8

Browse files
authored
Resolve Phpstan Messages in Writer Xls (PHPOffice#3343)
* Resolve Phpstan Messages in Writer Xls Reduce number of Phpstan messages by addressing their issues. This change does not touch Worksheet, and does not eliminate all Phpstan problems in the modules it does touch. * Scrutinizer Yet another false positive.
1 parent 14fd9bd commit 5fb76c8

File tree

5 files changed

+29
-137
lines changed

5 files changed

+29
-137
lines changed

phpstan-baseline.neon

Lines changed: 0 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -595,71 +595,6 @@ parameters:
595595
count: 1
596596
path: src/PhpSpreadsheet/Shared/Trend/Trend.php
597597

598-
-
599-
message: "#^Cannot use array destructuring on array\\|false\\.$#"
600-
count: 1
601-
path: src/PhpSpreadsheet/Writer/Xls.php
602-
603-
-
604-
message: "#^Offset 'endCoordinates' does not exist on array\\|null\\.$#"
605-
count: 1
606-
path: src/PhpSpreadsheet/Writer/Xls.php
607-
608-
-
609-
message: "#^Offset 'endOffsetX' does not exist on array\\|null\\.$#"
610-
count: 1
611-
path: src/PhpSpreadsheet/Writer/Xls.php
612-
613-
-
614-
message: "#^Offset 'endOffsetY' does not exist on array\\|null\\.$#"
615-
count: 1
616-
path: src/PhpSpreadsheet/Writer/Xls.php
617-
618-
-
619-
message: "#^Offset 'startCoordinates' does not exist on array\\|null\\.$#"
620-
count: 1
621-
path: src/PhpSpreadsheet/Writer/Xls.php
622-
623-
-
624-
message: "#^Offset 'startOffsetX' does not exist on array\\|null\\.$#"
625-
count: 1
626-
path: src/PhpSpreadsheet/Writer/Xls.php
627-
628-
-
629-
message: "#^Offset 'startOffsetY' does not exist on array\\|null\\.$#"
630-
count: 1
631-
path: src/PhpSpreadsheet/Writer/Xls.php
632-
633-
-
634-
message: "#^Parameter \\#1 \\$blipType of method PhpOffice\\\\PhpSpreadsheet\\\\Shared\\\\Escher\\\\DggContainer\\\\BstoreContainer\\\\BSE\\:\\:setBlipType\\(\\) expects int, int\\|null given\\.$#"
635-
count: 1
636-
path: src/PhpSpreadsheet/Writer/Xls.php
637-
638-
-
639-
message: "#^Parameter \\#1 \\$data of method PhpOffice\\\\PhpSpreadsheet\\\\Shared\\\\Escher\\\\DggContainer\\\\BstoreContainer\\\\BSE\\\\Blip\\:\\:setData\\(\\) expects string, string\\|false given\\.$#"
640-
count: 1
641-
path: src/PhpSpreadsheet/Writer/Xls.php
642-
643-
-
644-
message: "#^Property PhpOffice\\\\PhpSpreadsheet\\\\Writer\\\\Xls\\:\\:\\$documentSummaryInformation \\(string\\) in isset\\(\\) is not nullable\\.$#"
645-
count: 1
646-
path: src/PhpSpreadsheet/Writer/Xls.php
647-
648-
-
649-
message: "#^Property PhpOffice\\\\PhpSpreadsheet\\\\Writer\\\\Xls\\:\\:\\$summaryInformation \\(string\\) in isset\\(\\) is not nullable\\.$#"
650-
count: 1
651-
path: src/PhpSpreadsheet/Writer/Xls.php
652-
653-
-
654-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Writer\\\\Xls\\\\BIFFwriter\\:\\:writeEof\\(\\) has no return type specified\\.$#"
655-
count: 1
656-
path: src/PhpSpreadsheet/Writer/Xls/BIFFwriter.php
657-
658-
-
659-
message: "#^Static property PhpOffice\\\\PhpSpreadsheet\\\\Writer\\\\Xls\\\\BIFFwriter\\:\\:\\$byteOrder \\(int\\) in isset\\(\\) is not nullable\\.$#"
660-
count: 1
661-
path: src/PhpSpreadsheet/Writer/Xls/BIFFwriter.php
662-
663598
-
664599
message: "#^Elseif condition is always true\\.$#"
665600
count: 1
@@ -680,41 +615,6 @@ parameters:
680615
count: 1
681616
path: src/PhpSpreadsheet/Writer/Xls/Escher.php
682617

683-
-
684-
message: "#^Cannot access offset 'encoding' on array\\|false\\.$#"
685-
count: 1
686-
path: src/PhpSpreadsheet/Writer/Xls/Workbook.php
687-
688-
-
689-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Writer\\\\Xls\\\\Workbook\\:\\:writeAllDefinedNamesBiff8\\(\\) has no return type specified\\.$#"
690-
count: 1
691-
path: src/PhpSpreadsheet/Writer/Xls/Workbook.php
692-
693-
-
694-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Writer\\\\Xls\\\\Workbook\\:\\:writeExternalsheetBiff8\\(\\) has no return type specified\\.$#"
695-
count: 1
696-
path: src/PhpSpreadsheet/Writer/Xls/Workbook.php
697-
698-
-
699-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Writer\\\\Xls\\\\Workbook\\:\\:writeMsoDrawingGroup\\(\\) has no return type specified\\.$#"
700-
count: 1
701-
path: src/PhpSpreadsheet/Writer/Xls/Workbook.php
702-
703-
-
704-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Writer\\\\Xls\\\\Workbook\\:\\:writeSupbookInternal\\(\\) has no return type specified\\.$#"
705-
count: 1
706-
path: src/PhpSpreadsheet/Writer/Xls/Workbook.php
707-
708-
-
709-
message: "#^Property PhpOffice\\\\PhpSpreadsheet\\\\Writer\\\\Xls\\\\Workbook\\:\\:\\$biffSize is never read, only written\\.$#"
710-
count: 1
711-
path: src/PhpSpreadsheet/Writer/Xls/Workbook.php
712-
713-
-
714-
message: "#^Property PhpOffice\\\\PhpSpreadsheet\\\\Writer\\\\Xls\\\\Workbook\\:\\:\\$colors has no type specified\\.$#"
715-
count: 1
716-
path: src/PhpSpreadsheet/Writer/Xls/Workbook.php
717-
718618
-
719619
message: "#^Cannot access offset 'comp' on array\\|false\\.$#"
720620
count: 1
@@ -814,13 +714,3 @@ parameters:
814714
message: "#^Property PhpOffice\\\\PhpSpreadsheet\\\\Writer\\\\Xls\\\\Worksheet\\:\\:\\$xlsStringMaxLength is never read, only written\\.$#"
815715
count: 1
816716
path: src/PhpSpreadsheet/Writer/Xls/Worksheet.php
817-
818-
-
819-
message: "#^Parameter \\#1 \\$textRotation of static method PhpOffice\\\\PhpSpreadsheet\\\\Writer\\\\Xls\\\\Xf\\:\\:mapTextRotation\\(\\) expects int, int\\|null given\\.$#"
820-
count: 1
821-
path: src/PhpSpreadsheet/Writer/Xls/Xf.php
822-
823-
-
824-
message: "#^Property PhpOffice\\\\PhpSpreadsheet\\\\Writer\\\\Xls\\\\Xf\\:\\:\\$diag is never read, only written\\.$#"
825-
count: 1
826-
path: src/PhpSpreadsheet/Writer/Xls/Xf.php

src/PhpSpreadsheet/Writer/Xls.php

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -201,14 +201,14 @@ public function save($filename, int $flags = 0): void
201201

202202
$this->documentSummaryInformation = $this->writeDocumentSummaryInformation();
203203
// initialize OLE Document Summary Information
204-
if (isset($this->documentSummaryInformation) && !empty($this->documentSummaryInformation)) {
204+
if (!empty($this->documentSummaryInformation)) {
205205
$OLE_DocumentSummaryInformation = new File(OLE::ascToUcs(chr(5) . 'DocumentSummaryInformation'));
206206
$OLE_DocumentSummaryInformation->append($this->documentSummaryInformation);
207207
}
208208

209209
$this->summaryInformation = $this->writeSummaryInformation();
210210
// initialize OLE Summary Information
211-
if (isset($this->summaryInformation) && !empty($this->summaryInformation)) {
211+
if (!empty($this->summaryInformation)) {
212212
$OLE_SummaryInformation = new File(OLE::ascToUcs(chr(5) . 'SummaryInformation'));
213213
$OLE_SummaryInformation->append($this->summaryInformation);
214214
}
@@ -317,14 +317,16 @@ private function buildWorksheetEschers(): void
317317

318318
$twoAnchor = \PhpOffice\PhpSpreadsheet\Shared\Xls::oneAnchor2twoAnchor($sheet, $coordinates, $offsetX, $offsetY, $width, $height);
319319

320-
$spContainer->setStartCoordinates($twoAnchor['startCoordinates']);
321-
$spContainer->setStartOffsetX($twoAnchor['startOffsetX']);
322-
$spContainer->setStartOffsetY($twoAnchor['startOffsetY']);
323-
$spContainer->setEndCoordinates($twoAnchor['endCoordinates']);
324-
$spContainer->setEndOffsetX($twoAnchor['endOffsetX']);
325-
$spContainer->setEndOffsetY($twoAnchor['endOffsetY']);
320+
if (is_array($twoAnchor)) {
321+
$spContainer->setStartCoordinates($twoAnchor['startCoordinates']);
322+
$spContainer->setStartOffsetX($twoAnchor['startOffsetX']);
323+
$spContainer->setStartOffsetY($twoAnchor['startOffsetY']);
324+
$spContainer->setEndCoordinates($twoAnchor['endCoordinates']);
325+
$spContainer->setEndOffsetX($twoAnchor['endOffsetX']);
326+
$spContainer->setEndOffsetY($twoAnchor['endOffsetY']);
326327

327-
$spgrContainer->addChild($spContainer);
328+
$spgrContainer->addChild($spContainer);
329+
}
328330
}
329331

330332
// AutoFilters
@@ -416,7 +418,7 @@ private function processMemoryDrawing(BstoreContainer &$bstoreContainer, MemoryD
416418
ob_end_clean();
417419

418420
$blip = new Blip();
419-
$blip->setData($blipData);
421+
$blip->setData("$blipData");
420422

421423
$BSE = new BSE();
422424
$BSE->setBlipType($blipType);
@@ -427,11 +429,12 @@ private function processMemoryDrawing(BstoreContainer &$bstoreContainer, MemoryD
427429

428430
private function processDrawing(BstoreContainer &$bstoreContainer, Drawing $drawing): void
429431
{
430-
$blipType = null;
432+
$blipType = 0;
431433
$blipData = '';
432434
$filename = $drawing->getPath();
433435

434-
[$imagesx, $imagesy, $imageFormat] = getimagesize($filename);
436+
$imageSize = getimagesize($filename);
437+
$imageFormat = empty($imageSize) ? 0 : ($imageSize[2] ?? 0);
435438

436439
switch ($imageFormat) {
437440
case 1: // GIF, not supported by BIFF8, we convert to PNG

src/PhpSpreadsheet/Writer/Xls/BIFFwriter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class BIFFwriter
4242
/**
4343
* The byte order of this architecture. 0 => little endian, 1 => big endian.
4444
*
45-
* @var int
45+
* @var ?int
4646
*/
4747
private static $byteOrder;
4848

@@ -175,7 +175,7 @@ protected function storeEof(): void
175175
/**
176176
* Writes Excel EOF record to indicate the end of a BIFF stream.
177177
*/
178-
public function writeEof()
178+
public function writeEof(): string
179179
{
180180
$record = 0x000A; // Record identifier
181181
$length = 0x0000; // Number of bytes to follow

src/PhpSpreadsheet/Writer/Xls/Workbook.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,13 @@ class Workbook extends BIFFwriter
5555
private $parser;
5656

5757
/**
58-
* The BIFF file size for the workbook.
58+
* The BIFF file size for the workbook. Not currently used.
5959
*
6060
* @var int
6161
*
6262
* @see calcSheetOffsets()
6363
*/
64-
private $biffSize;
64+
private $biffSize; // @phpstan-ignore-line
6565

6666
/**
6767
* XF Writers.
@@ -163,6 +163,8 @@ class Workbook extends BIFFwriter
163163

164164
/**
165165
* Color cache.
166+
*
167+
* @var array
166168
*/
167169
private $colors;
168170

@@ -581,7 +583,7 @@ private function parseDefinedNameValue(DefinedName $definedName): string
581583
* Writes all the DEFINEDNAME records (BIFF8).
582584
* So far this is only used for repeating rows/columns (print titles) and print areas.
583585
*/
584-
private function writeAllDefinedNamesBiff8()
586+
private function writeAllDefinedNamesBiff8(): string
585587
{
586588
$chunk = '';
587589

@@ -886,7 +888,7 @@ private function writeBoundSheet(\PhpOffice\PhpSpreadsheet\Worksheet\Worksheet $
886888
/**
887889
* Write Internal SUPBOOK record.
888890
*/
889-
private function writeSupbookInternal()
891+
private function writeSupbookInternal(): string
890892
{
891893
$record = 0x01AE; // Record identifier
892894
$length = 0x0004; // Bytes to follow
@@ -901,7 +903,7 @@ private function writeSupbookInternal()
901903
* Writes the Excel BIFF EXTERNSHEET record. These references are used by
902904
* formulas.
903905
*/
904-
private function writeExternalsheetBiff8()
906+
private function writeExternalsheetBiff8(): string
905907
{
906908
$totalReferences = count($this->parser->references);
907909
$record = 0x0017; // Record identifier
@@ -1060,7 +1062,7 @@ private function writeSharedStringsTable()
10601062
$headerinfo = unpack('vlength/Cencoding', $string);
10611063

10621064
// currently, this is always 1 = uncompressed
1063-
$encoding = $headerinfo['encoding'];
1065+
$encoding = $headerinfo['encoding'] ?? 1;
10641066

10651067
// initialize finished writing current $string
10661068
$finished = false;
@@ -1155,7 +1157,7 @@ private function writeSharedStringsTable()
11551157
/**
11561158
* Writes the MSODRAWINGGROUP record if needed. Possibly split using CONTINUE records.
11571159
*/
1158-
private function writeMsoDrawingGroup()
1160+
private function writeMsoDrawingGroup(): string
11591161
{
11601162
// write the Escher stream if necessary
11611163
if (isset($this->escher)) {

src/PhpSpreadsheet/Writer/Xls/Xf.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,7 @@ class Xf
116116
*/
117117
private $rightBorderColor;
118118

119-
/**
120-
* @var int
121-
*/
122-
private $diag;
119+
//private $diag; // theoretically int, not yet implemented
123120

124121
/**
125122
* @var int
@@ -148,7 +145,7 @@ public function __construct(Style $style)
148145
$this->foregroundColor = 0x40;
149146
$this->backgroundColor = 0x41;
150147

151-
$this->diag = 0;
148+
//$this->diag = 0;
152149

153150
$this->bottomBorderColor = 0x40;
154151
$this->topBorderColor = 0x40;
@@ -254,7 +251,7 @@ public function writeXf()
254251
$biff8_options |= (int) $this->style->getAlignment()->getShrinkToFit() << 4;
255252

256253
$data = pack('vvvC', $ifnt, $ifmt, $style, $align);
257-
$data .= pack('CCC', self::mapTextRotation($this->style->getAlignment()->getTextRotation()), $biff8_options, $used_attrib);
254+
$data .= pack('CCC', self::mapTextRotation((int) $this->style->getAlignment()->getTextRotation()), $biff8_options, $used_attrib);
258255
$data .= pack('VVv', $border1, $border2, $icv);
259256

260257
return $header . $data;

0 commit comments

Comments
 (0)