Skip to content

Commit 5f33ec0

Browse files
authored
Phpstan Baseline < 4000 Lines Part 3 (#3041)
The last of these changes for now. No remaining Phpstan complaints in any Writer/Xlsx. Number of lines remaining in Phpstan baseline is now below 3500.
1 parent 4f8aa80 commit 5f33ec0

File tree

6 files changed

+111
-273
lines changed

6 files changed

+111
-273
lines changed

phpstan-baseline.neon

Lines changed: 0 additions & 195 deletions
Original file line numberDiff line numberDiff line change
@@ -3474,198 +3474,3 @@ parameters:
34743474
message: "#^Property PhpOffice\\\\PhpSpreadsheet\\\\Writer\\\\Xls\\\\Xf\\:\\:\\$diag is never read, only written\\.$#"
34753475
count: 1
34763476
path: src/PhpSpreadsheet/Writer/Xls/Xf.php
3477-
3478-
-
3479-
message: "#^Argument of an invalid type array\\|null supplied for foreach, only iterables are supported\\.$#"
3480-
count: 1
3481-
path: src/PhpSpreadsheet/Writer/Xlsx.php
3482-
3483-
-
3484-
message: "#^Parameter \\#1 \\$path of function basename expects string, array\\|string\\|null given\\.$#"
3485-
count: 1
3486-
path: src/PhpSpreadsheet/Writer/Xlsx.php
3487-
3488-
-
3489-
message: "#^Parameter \\#1 \\$path of function dirname expects string, array\\|string\\|null given\\.$#"
3490-
count: 1
3491-
path: src/PhpSpreadsheet/Writer/Xlsx.php
3492-
3493-
-
3494-
message: "#^Possibly invalid array key type array\\|string\\|null\\.$#"
3495-
count: 1
3496-
path: src/PhpSpreadsheet/Writer/Xlsx.php
3497-
3498-
-
3499-
message: "#^Property PhpOffice\\\\PhpSpreadsheet\\\\Writer\\\\Xlsx\\:\\:\\$pathNames has no type specified\\.$#"
3500-
count: 1
3501-
path: src/PhpSpreadsheet/Writer/Xlsx.php
3502-
3503-
-
3504-
message: "#^Expression on left side of \\?\\? is not nullable\\.$#"
3505-
count: 1
3506-
path: src/PhpSpreadsheet/Writer/Xlsx/DefinedNames.php
3507-
3508-
-
3509-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Writer\\\\Xlsx\\\\Rels\\:\\:writeUnparsedRelationship\\(\\) has parameter \\$relationship with no type specified\\.$#"
3510-
count: 1
3511-
path: src/PhpSpreadsheet/Writer/Xlsx/Rels.php
3512-
3513-
-
3514-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Writer\\\\Xlsx\\\\Rels\\:\\:writeUnparsedRelationship\\(\\) has parameter \\$type with no type specified\\.$#"
3515-
count: 1
3516-
path: src/PhpSpreadsheet/Writer/Xlsx/Rels.php
3517-
3518-
-
3519-
message: "#^Parameter \\#2 \\$id of method PhpOffice\\\\PhpSpreadsheet\\\\Writer\\\\Xlsx\\\\Rels\\:\\:writeRelationship\\(\\) expects int, string given\\.$#"
3520-
count: 5
3521-
path: src/PhpSpreadsheet/Writer/Xlsx/Rels.php
3522-
3523-
-
3524-
message: "#^Parameter \\#4 \\$target of method PhpOffice\\\\PhpSpreadsheet\\\\Writer\\\\Xlsx\\\\Rels\\:\\:writeRelationship\\(\\) expects string, array\\|string\\|null given\\.$#"
3525-
count: 1
3526-
path: src/PhpSpreadsheet/Writer/Xlsx/Rels.php
3527-
3528-
-
3529-
message: "#^Cannot call method getBold\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\Font\\|null\\.$#"
3530-
count: 1
3531-
path: src/PhpSpreadsheet/Writer/Xlsx/StringTable.php
3532-
3533-
-
3534-
message: "#^Cannot call method getColor\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\Font\\|null\\.$#"
3535-
count: 1
3536-
path: src/PhpSpreadsheet/Writer/Xlsx/StringTable.php
3537-
3538-
-
3539-
message: "#^Cannot call method getItalic\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\Font\\|null\\.$#"
3540-
count: 1
3541-
path: src/PhpSpreadsheet/Writer/Xlsx/StringTable.php
3542-
3543-
-
3544-
message: "#^Cannot call method getName\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\Font\\|null\\.$#"
3545-
count: 1
3546-
path: src/PhpSpreadsheet/Writer/Xlsx/StringTable.php
3547-
3548-
-
3549-
message: "#^Cannot call method getSize\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\Font\\|null\\.$#"
3550-
count: 1
3551-
path: src/PhpSpreadsheet/Writer/Xlsx/StringTable.php
3552-
3553-
-
3554-
message: "#^Cannot call method getStrikethrough\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\Font\\|null\\.$#"
3555-
count: 1
3556-
path: src/PhpSpreadsheet/Writer/Xlsx/StringTable.php
3557-
3558-
-
3559-
message: "#^Cannot call method getSubscript\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\Font\\|null\\.$#"
3560-
count: 2
3561-
path: src/PhpSpreadsheet/Writer/Xlsx/StringTable.php
3562-
3563-
-
3564-
message: "#^Cannot call method getSuperscript\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\Font\\|null\\.$#"
3565-
count: 2
3566-
path: src/PhpSpreadsheet/Writer/Xlsx/StringTable.php
3567-
3568-
-
3569-
message: "#^Cannot call method getUnderline\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\Font\\|null\\.$#"
3570-
count: 1
3571-
path: src/PhpSpreadsheet/Writer/Xlsx/StringTable.php
3572-
3573-
-
3574-
message: "#^Instanceof between \\*NEVER\\* and PhpOffice\\\\PhpSpreadsheet\\\\RichText\\\\RichText will always evaluate to false\\.$#"
3575-
count: 1
3576-
path: src/PhpSpreadsheet/Writer/Xlsx/StringTable.php
3577-
3578-
-
3579-
message: "#^Instanceof between string and PhpOffice\\\\PhpSpreadsheet\\\\RichText\\\\RichText will always evaluate to false\\.$#"
3580-
count: 1
3581-
path: src/PhpSpreadsheet/Writer/Xlsx/StringTable.php
3582-
3583-
-
3584-
message: "#^Parameter \\#1 \\$text of method PhpOffice\\\\PhpSpreadsheet\\\\RichText\\\\RichText\\:\\:createTextRun\\(\\) expects string, string\\|null given\\.$#"
3585-
count: 1
3586-
path: src/PhpSpreadsheet/Writer/Xlsx/StringTable.php
3587-
3588-
-
3589-
message: "#^Parameter \\#2 \\$value of method XMLWriter\\:\\:writeAttribute\\(\\) expects string, float\\|null given\\.$#"
3590-
count: 1
3591-
path: src/PhpSpreadsheet/Writer/Xlsx/StringTable.php
3592-
3593-
-
3594-
message: "#^Parameter \\#2 \\$value of method XMLWriter\\:\\:writeAttribute\\(\\) expects string, int given\\.$#"
3595-
count: 2
3596-
path: src/PhpSpreadsheet/Writer/Xlsx/StringTable.php
3597-
3598-
-
3599-
message: "#^Parameter \\#2 \\$value of method XMLWriter\\:\\:writeAttribute\\(\\) expects string, int\\<0, max\\> given\\.$#"
3600-
count: 1
3601-
path: src/PhpSpreadsheet/Writer/Xlsx/StringTable.php
3602-
3603-
-
3604-
message: "#^Parameter \\#2 \\$value of method XMLWriter\\:\\:writeAttribute\\(\\) expects string, string\\|null given\\.$#"
3605-
count: 4
3606-
path: src/PhpSpreadsheet/Writer/Xlsx/StringTable.php
3607-
3608-
-
3609-
message: "#^Cannot call method getStyle\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\Conditional\\|null\\.$#"
3610-
count: 1
3611-
path: src/PhpSpreadsheet/Writer/Xlsx/Style.php
3612-
3613-
-
3614-
message: "#^Comparison operation \"\\<\" between int\\<min, \\-1\\> and 0 is always true\\.$#"
3615-
count: 2
3616-
path: src/PhpSpreadsheet/Writer/Xlsx/Style.php
3617-
3618-
-
3619-
message: "#^Parameter \\#2 \\$borders of method PhpOffice\\\\PhpSpreadsheet\\\\Writer\\\\Xlsx\\\\Style\\:\\:writeBorder\\(\\) expects PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\Borders, PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\Borders\\|null given\\.$#"
3620-
count: 1
3621-
path: src/PhpSpreadsheet/Writer/Xlsx/Style.php
3622-
3623-
-
3624-
message: "#^Parameter \\#2 \\$fill of method PhpOffice\\\\PhpSpreadsheet\\\\Writer\\\\Xlsx\\\\Style\\:\\:writeFill\\(\\) expects PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\Fill, PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\Fill\\|null given\\.$#"
3625-
count: 1
3626-
path: src/PhpSpreadsheet/Writer/Xlsx/Style.php
3627-
3628-
-
3629-
message: "#^Parameter \\#2 \\$font of method PhpOffice\\\\PhpSpreadsheet\\\\Writer\\\\Xlsx\\\\Style\\:\\:writeFont\\(\\) expects PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\Font, PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\Font\\|null given\\.$#"
3630-
count: 1
3631-
path: src/PhpSpreadsheet/Writer/Xlsx/Style.php
3632-
3633-
-
3634-
message: "#^Parameter \\#2 \\$numberFormat of method PhpOffice\\\\PhpSpreadsheet\\\\Writer\\\\Xlsx\\\\Style\\:\\:writeNumFmt\\(\\) expects PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\NumberFormat, PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\NumberFormat\\|null given\\.$#"
3635-
count: 1
3636-
path: src/PhpSpreadsheet/Writer/Xlsx/Style.php
3637-
3638-
-
3639-
message: "#^Parameter \\#2 \\$value of method XMLWriter\\:\\:writeAttribute\\(\\) expects string, float given\\.$#"
3640-
count: 1
3641-
path: src/PhpSpreadsheet/Writer/Xlsx/Style.php
3642-
3643-
-
3644-
message: "#^Parameter \\#2 \\$value of method XMLWriter\\:\\:writeAttribute\\(\\) expects string, int given\\.$#"
3645-
count: 22
3646-
path: src/PhpSpreadsheet/Writer/Xlsx/Style.php
3647-
3648-
-
3649-
message: "#^Parameter \\#2 \\$value of method XMLWriter\\:\\:writeAttribute\\(\\) expects string, int\\<0, max\\> given\\.$#"
3650-
count: 1
3651-
path: src/PhpSpreadsheet/Writer/Xlsx/Style.php
3652-
3653-
-
3654-
message: "#^Parameter \\#2 \\$value of method XMLWriter\\:\\:writeAttribute\\(\\) expects string, int\\<1, max\\> given\\.$#"
3655-
count: 2
3656-
path: src/PhpSpreadsheet/Writer/Xlsx/Style.php
3657-
3658-
-
3659-
message: "#^Parameter \\#2 \\$value of method XMLWriter\\:\\:writeAttribute\\(\\) expects string, int\\|null given\\.$#"
3660-
count: 1
3661-
path: src/PhpSpreadsheet/Writer/Xlsx/Style.php
3662-
3663-
-
3664-
message: "#^Parameter \\#2 \\$value of method XMLWriter\\:\\:writeAttribute\\(\\) expects string, string\\|null given\\.$#"
3665-
count: 7
3666-
path: src/PhpSpreadsheet/Writer/Xlsx/Style.php
3667-
3668-
-
3669-
message: "#^Result of \\|\\| is always true\\.$#"
3670-
count: 1
3671-
path: src/PhpSpreadsheet/Writer/Xlsx/Style.php

src/PhpSpreadsheet/Writer/Xlsx.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,12 +349,15 @@ public function save($filename, int $flags = 0): void
349349
//a custom UI in this workbook ? add it ("base" xml and additional objects (pictures) and rels)
350350
if ($this->spreadSheet->hasRibbon()) {
351351
$tmpRibbonTarget = $this->spreadSheet->getRibbonXMLData('target');
352+
$tmpRibbonTarget = is_string($tmpRibbonTarget) ? $tmpRibbonTarget : '';
352353
$zipContent[$tmpRibbonTarget] = $this->spreadSheet->getRibbonXMLData('data');
353354
if ($this->spreadSheet->hasRibbonBinObjects()) {
354355
$tmpRootPath = dirname($tmpRibbonTarget) . '/';
355356
$ribbonBinObjects = $this->spreadSheet->getRibbonBinObjects('data'); //the files to write
356-
foreach ($ribbonBinObjects as $aPath => $aContent) {
357-
$zipContent[$tmpRootPath . $aPath] = $aContent;
357+
if (is_array($ribbonBinObjects)) {
358+
foreach ($ribbonBinObjects as $aPath => $aContent) {
359+
$zipContent[$tmpRootPath . $aPath] = $aContent;
360+
}
358361
}
359362
//the rels for files
360363
$zipContent[$tmpRootPath . '_rels/' . basename($tmpRibbonTarget) . '.rels'] = $this->getWriterPartRelsRibbon()->writeRibbonRelationships($this->spreadSheet);
@@ -684,6 +687,7 @@ public function setOffice2003Compatibility($office2003compatibility)
684687
return $this;
685688
}
686689

690+
/** @var array */
687691
private $pathNames = [];
688692

689693
private function addZipFile(string $path, string $content): void

src/PhpSpreadsheet/Writer/Xlsx/DefinedNames.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ private function writeNamedRangeForAutofilter(Worksheet $worksheet, int $workshe
118118
$range[1] = Coordinate::absoluteCoordinate($range[1]);
119119
$range = implode(':', $range);
120120

121-
$this->objWriter->writeRawData('\'' . str_replace("'", "''", $worksheet->getTitle() ?? '') . '\'!' . $range);
121+
$this->objWriter->writeRawData('\'' . str_replace("'", "''", $worksheet->getTitle()) . '\'!' . $range);
122122

123123
$this->objWriter->endElement();
124124
}

src/PhpSpreadsheet/Writer/Xlsx/Rels.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,13 @@ public function writeRelationships(Spreadsheet $spreadsheet)
6767
'xl/workbook.xml'
6868
);
6969
// a custom UI in workbook ?
70+
$target = $spreadsheet->getRibbonXMLData('target');
7071
if ($spreadsheet->hasRibbon()) {
7172
$this->writeRelationShip(
7273
$objWriter,
7374
5,
7475
'http://schemas.microsoft.com/office/2006/relationships/ui/extensibility',
75-
$spreadsheet->getRibbonXMLData('target')
76+
is_string($target) ? $target : ''
7677
);
7778
}
7879

@@ -284,7 +285,7 @@ public function writeWorksheetRelationships(\PhpOffice\PhpSpreadsheet\Worksheet\
284285
return $objWriter->getData();
285286
}
286287

287-
private function writeUnparsedRelationship(\PhpOffice\PhpSpreadsheet\Worksheet\Worksheet $worksheet, XMLWriter $objWriter, $relationship, $type): void
288+
private function writeUnparsedRelationship(\PhpOffice\PhpSpreadsheet\Worksheet\Worksheet $worksheet, XMLWriter $objWriter, string $relationship, string $type): void
288289
{
289290
$unparsedLoadedData = $worksheet->getParent()->getUnparsedLoadedData();
290291
if (!isset($unparsedLoadedData['sheets'][$worksheet->getCodeName()][$relationship])) {
@@ -448,7 +449,7 @@ public function writeVMLDrawingRelationships(\PhpOffice\PhpSpreadsheet\Worksheet
448449
/**
449450
* Write Override content type.
450451
*
451-
* @param int $id Relationship ID. rId will be prepended!
452+
* @param int|string $id Relationship ID. rId will be prepended!
452453
* @param string $type Relationship type
453454
* @param string $target Relationship target
454455
* @param string $targetMode Relationship target mode

src/PhpSpreadsheet/Writer/Xlsx/StringTable.php

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function createStringTable(Worksheet $worksheet, $existingTable = null)
6666
/**
6767
* Write string table to XML format.
6868
*
69-
* @param string[] $stringTable
69+
* @param (string|RichText)[] $stringTable
7070
*
7171
* @return string XML Output
7272
*/
@@ -86,21 +86,21 @@ public function writeStringTable(array $stringTable)
8686
// String table
8787
$objWriter->startElement('sst');
8888
$objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/spreadsheetml/2006/main');
89-
$objWriter->writeAttribute('uniqueCount', count($stringTable));
89+
$objWriter->writeAttribute('uniqueCount', (string) count($stringTable));
9090

9191
// Loop through string table
9292
foreach ($stringTable as $textElement) {
9393
$objWriter->startElement('si');
9494

95-
if (!$textElement instanceof RichText) {
95+
if (!($textElement instanceof RichText)) {
9696
$textToWrite = StringHelper::controlCharacterPHP2OOXML($textElement);
9797
$objWriter->startElement('t');
9898
if ($textToWrite !== trim($textToWrite)) {
9999
$objWriter->writeAttribute('xml:space', 'preserve');
100100
}
101101
$objWriter->writeRawData($textToWrite);
102102
$objWriter->endElement();
103-
} elseif ($textElement instanceof RichText) {
103+
} else {
104104
$this->writeRichText($objWriter, $textElement);
105105
}
106106

@@ -130,14 +130,16 @@ public function writeRichText(XMLWriter $objWriter, RichText $richText, $prefix
130130
$objWriter->startElement($prefix . 'r');
131131

132132
// rPr
133-
if ($element instanceof Run) {
133+
if ($element instanceof Run && $element->getFont() !== null) {
134134
// rPr
135135
$objWriter->startElement($prefix . 'rPr');
136136

137137
// rFont
138-
$objWriter->startElement($prefix . 'rFont');
139-
$objWriter->writeAttribute('val', $element->getFont()->getName());
140-
$objWriter->endElement();
138+
if ($element->getFont()->getName() !== null) {
139+
$objWriter->startElement($prefix . 'rFont');
140+
$objWriter->writeAttribute('val', $element->getFont()->getName());
141+
$objWriter->endElement();
142+
}
141143

142144
// Bold
143145
$objWriter->startElement($prefix . 'b');
@@ -166,19 +168,25 @@ public function writeRichText(XMLWriter $objWriter, RichText $richText, $prefix
166168
$objWriter->endElement();
167169

168170
// Color
169-
$objWriter->startElement($prefix . 'color');
170-
$objWriter->writeAttribute('rgb', $element->getFont()->getColor()->getARGB());
171-
$objWriter->endElement();
171+
if ($element->getFont()->getColor()->getARGB() !== null) {
172+
$objWriter->startElement($prefix . 'color');
173+
$objWriter->writeAttribute('rgb', $element->getFont()->getColor()->getARGB());
174+
$objWriter->endElement();
175+
}
172176

173177
// Size
174-
$objWriter->startElement($prefix . 'sz');
175-
$objWriter->writeAttribute('val', $element->getFont()->getSize());
176-
$objWriter->endElement();
178+
if ($element->getFont()->getSize() !== null) {
179+
$objWriter->startElement($prefix . 'sz');
180+
$objWriter->writeAttribute('val', (string) $element->getFont()->getSize());
181+
$objWriter->endElement();
182+
}
177183

178184
// Underline
179-
$objWriter->startElement($prefix . 'u');
180-
$objWriter->writeAttribute('val', $element->getFont()->getUnderline());
181-
$objWriter->endElement();
185+
if ($element->getFont()->getUnderline() !== null) {
186+
$objWriter->startElement($prefix . 'u');
187+
$objWriter->writeAttribute('val', $element->getFont()->getUnderline());
188+
$objWriter->endElement();
189+
}
182190

183191
$objWriter->endElement();
184192
}
@@ -201,10 +209,10 @@ public function writeRichText(XMLWriter $objWriter, RichText $richText, $prefix
201209
*/
202210
public function writeRichTextForCharts(XMLWriter $objWriter, $richText = null, $prefix = ''): void
203211
{
204-
if (!$richText instanceof RichText) {
212+
if (!($richText instanceof RichText)) {
205213
$textRun = $richText;
206214
$richText = new RichText();
207-
$run = $richText->createTextRun($textRun);
215+
$run = $richText->createTextRun($textRun ?? '');
208216
$run->setFont(null);
209217
}
210218

@@ -226,9 +234,9 @@ public function writeRichTextForCharts(XMLWriter $objWriter, $richText = null, $
226234
}
227235

228236
// Bold
229-
$objWriter->writeAttribute('b', ($element->getFont()->getBold() ? 1 : 0));
237+
$objWriter->writeAttribute('b', ($element->getFont()->getBold() ? '1' : '0'));
230238
// Italic
231-
$objWriter->writeAttribute('i', ($element->getFont()->getItalic() ? 1 : 0));
239+
$objWriter->writeAttribute('i', ($element->getFont()->getItalic() ? '1' : '0'));
232240
// Underline
233241
$underlineType = $element->getFont()->getUnderline();
234242
switch ($underlineType) {
@@ -241,7 +249,9 @@ public function writeRichTextForCharts(XMLWriter $objWriter, $richText = null, $
241249

242250
break;
243251
}
244-
$objWriter->writeAttribute('u', $underlineType);
252+
if ($underlineType !== null) {
253+
$objWriter->writeAttribute('u', $underlineType);
254+
}
245255
// Strikethrough
246256
$objWriter->writeAttribute('strike', ($element->getFont()->getStriketype() ?: 'noStrike'));
247257
// Superscript/subscript

0 commit comments

Comments
 (0)