Skip to content

Commit 9eb5e7e

Browse files
authored
Phpstan Baseline < 4000 Lines Part 1 (#3023)
A lot of easily fixed problems throughout Writer/Xlsx/*, mostly supplying int rather than string as input to WriteAttribute/WriteElement. There are, in fact, so many opportunities, that I will split it over 2 or 3 PRs. But this first one will get Phpstan baseline down to the goal on its own. Some of the other problems are also easily fixed. In particular, the docBlocks in Style/ConditionalFormatting/ConditionalDataBar do not allow for null values, and should.
1 parent 026f699 commit 9eb5e7e

File tree

7 files changed

+71
-169
lines changed

7 files changed

+71
-169
lines changed

phpstan-baseline.neon

Lines changed: 0 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -3810,31 +3810,11 @@ parameters:
38103810
count: 1
38113811
path: src/PhpSpreadsheet/Writer/Xlsx.php
38123812

3813-
-
3814-
message: "#^Parameter \\#1 \\$string of function substr expects string, int given\\.$#"
3815-
count: 1
3816-
path: src/PhpSpreadsheet/Writer/Xlsx/Comments.php
3817-
3818-
-
3819-
message: "#^Parameter \\#2 \\$content of method XMLWriter\\:\\:writeElement\\(\\) expects string\\|null, int given\\.$#"
3820-
count: 2
3821-
path: src/PhpSpreadsheet/Writer/Xlsx/Comments.php
3822-
38233813
-
38243814
message: "#^Expression on left side of \\?\\? is not nullable\\.$#"
38253815
count: 1
38263816
path: src/PhpSpreadsheet/Writer/Xlsx/DefinedNames.php
38273817

3828-
-
3829-
message: "#^Parameter \\#2 \\$content of method XMLWriter\\:\\:writeElement\\(\\) expects string\\|null, int given\\.$#"
3830-
count: 1
3831-
path: src/PhpSpreadsheet/Writer/Xlsx/DocProps.php
3832-
3833-
-
3834-
message: "#^Parameter \\#2 \\$value of method XMLWriter\\:\\:writeAttribute\\(\\) expects string, int given\\.$#"
3835-
count: 2
3836-
path: src/PhpSpreadsheet/Writer/Xlsx/DocProps.php
3837-
38383818
-
38393819
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Writer\\\\Xlsx\\\\Rels\\:\\:writeUnparsedRelationship\\(\\) has parameter \\$relationship with no type specified\\.$#"
38403820
count: 1
@@ -3999,74 +3979,3 @@ parameters:
39993979
message: "#^Result of \\|\\| is always true\\.$#"
40003980
count: 1
40013981
path: src/PhpSpreadsheet/Writer/Xlsx/Style.php
4002-
4003-
-
4004-
message: "#^Parameter \\#2 \\$value of method XMLWriter\\:\\:writeAttribute\\(\\) expects string, int given\\.$#"
4005-
count: 7
4006-
path: src/PhpSpreadsheet/Writer/Xlsx/Workbook.php
4007-
4008-
-
4009-
message: "#^Expression on left side of \\?\\? is not nullable\\.$#"
4010-
count: 1
4011-
path: src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php
4012-
4013-
-
4014-
message: "#^If condition is always true\\.$#"
4015-
count: 6
4016-
path: src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php
4017-
4018-
-
4019-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Writer\\\\Xlsx\\\\Worksheet\\:\\:writeAttributeIf\\(\\) has parameter \\$condition with no type specified\\.$#"
4020-
count: 1
4021-
path: src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php
4022-
4023-
-
4024-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Writer\\\\Xlsx\\\\Worksheet\\:\\:writeDataBarElements\\(\\) has parameter \\$dataBar with no type specified\\.$#"
4025-
count: 1
4026-
path: src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php
4027-
4028-
-
4029-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Writer\\\\Xlsx\\\\Worksheet\\:\\:writeElementIf\\(\\) has parameter \\$condition with no type specified\\.$#"
4030-
count: 1
4031-
path: src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php
4032-
4033-
-
4034-
message: "#^Parameter \\#2 \\$content of method XMLWriter\\:\\:writeElement\\(\\) expects string\\|null, int\\|string given\\.$#"
4035-
count: 1
4036-
path: src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php
4037-
4038-
-
4039-
message: "#^Parameter \\#2 \\$value of method XMLWriter\\:\\:writeAttribute\\(\\) expects string, int given\\.$#"
4040-
count: 15
4041-
path: src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php
4042-
4043-
-
4044-
message: "#^Parameter \\#2 \\$value of method XMLWriter\\:\\:writeAttribute\\(\\) expects string, int\\<0, max\\> given\\.$#"
4045-
count: 3
4046-
path: src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php
4047-
4048-
-
4049-
message: "#^Parameter \\#2 \\$value of method XMLWriter\\:\\:writeAttribute\\(\\) expects string, int\\<1, max\\> given\\.$#"
4050-
count: 9
4051-
path: src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php
4052-
4053-
-
4054-
message: "#^Parameter \\#2 \\$value of method XMLWriter\\:\\:writeAttribute\\(\\) expects string, int\\|null given\\.$#"
4055-
count: 1
4056-
path: src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php
4057-
4058-
-
4059-
message: "#^Parameter \\#2 \\$value of method XMLWriter\\:\\:writeAttribute\\(\\) expects string, string\\|null given\\.$#"
4060-
count: 1
4061-
path: src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php
4062-
4063-
-
4064-
message: "#^Parameter \\#3 \\$stringTable of method PhpOffice\\\\PhpSpreadsheet\\\\Writer\\\\Xlsx\\\\Worksheet\\:\\:writeSheetData\\(\\) expects array\\<string\\>, array\\<string\\>\\|null given\\.$#"
4065-
count: 1
4066-
path: src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php
4067-
4068-
-
4069-
message: "#^Parameter \\#4 \\$val of static method PhpOffice\\\\PhpSpreadsheet\\\\Writer\\\\Xlsx\\\\Worksheet\\:\\:writeAttributeIf\\(\\) expects string, int given\\.$#"
4070-
count: 1
4071-
path: src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php
4072-

src/PhpSpreadsheet/Style/ConditionalFormatting/ConditionalDataBar.php

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,18 @@ class ConditionalDataBar
1111

1212
/** <dataBar> children */
1313

14-
/** @var ConditionalFormatValueObject */
14+
/** @var ?ConditionalFormatValueObject */
1515
private $minimumConditionalFormatValueObject;
1616

17-
/** @var ConditionalFormatValueObject */
17+
/** @var ?ConditionalFormatValueObject */
1818
private $maximumConditionalFormatValueObject;
1919

2020
/** @var string */
2121
private $color;
2222

2323
/** <extLst> */
2424

25-
/** @var ConditionalFormattingRuleExtension */
25+
/** @var ?ConditionalFormattingRuleExtension */
2626
private $conditionalFormattingRuleExt;
2727

2828
/**
@@ -43,10 +43,7 @@ public function setShowValue($showValue)
4343
return $this;
4444
}
4545

46-
/**
47-
* @return ConditionalFormatValueObject
48-
*/
49-
public function getMinimumConditionalFormatValueObject()
46+
public function getMinimumConditionalFormatValueObject(): ?ConditionalFormatValueObject
5047
{
5148
return $this->minimumConditionalFormatValueObject;
5249
}
@@ -58,10 +55,7 @@ public function setMinimumConditionalFormatValueObject(ConditionalFormatValueObj
5855
return $this;
5956
}
6057

61-
/**
62-
* @return ConditionalFormatValueObject
63-
*/
64-
public function getMaximumConditionalFormatValueObject()
58+
public function getMaximumConditionalFormatValueObject(): ?ConditionalFormatValueObject
6559
{
6660
return $this->maximumConditionalFormatValueObject;
6761
}
@@ -85,10 +79,7 @@ public function setColor(string $color): self
8579
return $this;
8680
}
8781

88-
/**
89-
* @return ConditionalFormattingRuleExtension
90-
*/
91-
public function getConditionalFormattingRuleExt()
82+
public function getConditionalFormattingRuleExt(): ?ConditionalFormattingRuleExtension
9283
{
9384
return $this->conditionalFormattingRuleExt;
9485
}

src/PhpSpreadsheet/Writer/Xlsx/Comments.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ private function writeVMLComment(XMLWriter $objWriter, $cellReference, Comment $
165165
// Metadata
166166
[$column, $row] = Coordinate::indexesFromString($cellReference);
167167
$id = 1024 + $column + $row;
168-
$id = substr($id, 0, 4);
168+
$id = substr("$id", 0, 4);
169169

170170
// v:shape
171171
$objWriter->startElement('v:shape');
@@ -223,10 +223,10 @@ private function writeVMLComment(XMLWriter $objWriter, $cellReference, Comment $
223223
$objWriter->writeElement('x:AutoFill', 'False');
224224

225225
// x:Row
226-
$objWriter->writeElement('x:Row', ($row - 1));
226+
$objWriter->writeElement('x:Row', (string) ($row - 1));
227227

228228
// x:Column
229-
$objWriter->writeElement('x:Column', ($column - 1));
229+
$objWriter->writeElement('x:Column', (string) ($column - 1));
230230

231231
$objWriter->endElement();
232232

src/PhpSpreadsheet/Writer/Xlsx/DocProps.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function writeDocPropsApp(Spreadsheet $spreadsheet)
5656

5757
// Variant
5858
$objWriter->startElement('vt:variant');
59-
$objWriter->writeElement('vt:i4', $spreadsheet->getSheetCount());
59+
$objWriter->writeElement('vt:i4', (string) $spreadsheet->getSheetCount());
6060
$objWriter->endElement();
6161

6262
$objWriter->endElement();
@@ -68,7 +68,7 @@ public function writeDocPropsApp(Spreadsheet $spreadsheet)
6868

6969
// Vector
7070
$objWriter->startElement('vt:vector');
71-
$objWriter->writeAttribute('size', $spreadsheet->getSheetCount());
71+
$objWriter->writeAttribute('size', (string) $spreadsheet->getSheetCount());
7272
$objWriter->writeAttribute('baseType', 'lpstr');
7373

7474
$sheetCount = $spreadsheet->getSheetCount();
@@ -207,7 +207,7 @@ public function writeDocPropsCustom(Spreadsheet $spreadsheet)
207207

208208
$objWriter->startElement('property');
209209
$objWriter->writeAttribute('fmtid', '{D5CDD505-2E9C-101B-9397-08002B2CF9AE}');
210-
$objWriter->writeAttribute('pid', $key + 2);
210+
$objWriter->writeAttribute('pid', (string) ($key + 2));
211211
$objWriter->writeAttribute('name', $customProperty);
212212

213213
switch ($propertyType) {

src/PhpSpreadsheet/Writer/Xlsx/Workbook.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,14 @@ private function writeBookViews(XMLWriter $objWriter, Spreadsheet $spreadsheet):
104104
// workbookView
105105
$objWriter->startElement('workbookView');
106106

107-
$objWriter->writeAttribute('activeTab', $spreadsheet->getActiveSheetIndex());
107+
$objWriter->writeAttribute('activeTab', (string) $spreadsheet->getActiveSheetIndex());
108108
$objWriter->writeAttribute('autoFilterDateGrouping', ($spreadsheet->getAutoFilterDateGrouping() ? 'true' : 'false'));
109-
$objWriter->writeAttribute('firstSheet', $spreadsheet->getFirstSheetIndex());
109+
$objWriter->writeAttribute('firstSheet', (string) $spreadsheet->getFirstSheetIndex());
110110
$objWriter->writeAttribute('minimized', ($spreadsheet->getMinimized() ? 'true' : 'false'));
111111
$objWriter->writeAttribute('showHorizontalScroll', ($spreadsheet->getShowHorizontalScroll() ? 'true' : 'false'));
112112
$objWriter->writeAttribute('showSheetTabs', ($spreadsheet->getShowSheetTabs() ? 'true' : 'false'));
113113
$objWriter->writeAttribute('showVerticalScroll', ($spreadsheet->getShowVerticalScroll() ? 'true' : 'false'));
114-
$objWriter->writeAttribute('tabRatio', $spreadsheet->getTabRatio());
114+
$objWriter->writeAttribute('tabRatio', (string) $spreadsheet->getTabRatio());
115115
$objWriter->writeAttribute('visibility', $spreadsheet->getVisibility());
116116

117117
$objWriter->endElement();
@@ -157,9 +157,9 @@ private function writeCalcPr(XMLWriter $objWriter, $recalcRequired = true): void
157157
$objWriter->writeAttribute('calcId', '999999');
158158
$objWriter->writeAttribute('calcMode', 'auto');
159159
// fullCalcOnLoad isn't needed if we've recalculating for the save
160-
$objWriter->writeAttribute('calcCompleted', ($recalcRequired) ? 1 : 0);
161-
$objWriter->writeAttribute('fullCalcOnLoad', ($recalcRequired) ? 0 : 1);
162-
$objWriter->writeAttribute('forceFullCalc', ($recalcRequired) ? 0 : 1);
160+
$objWriter->writeAttribute('calcCompleted', ($recalcRequired) ? '1' : '0');
161+
$objWriter->writeAttribute('fullCalcOnLoad', ($recalcRequired) ? '0' : '1');
162+
$objWriter->writeAttribute('forceFullCalc', ($recalcRequired) ? '0' : '1');
163163

164164
$objWriter->endElement();
165165
}
@@ -200,7 +200,7 @@ private function writeSheet(XMLWriter $objWriter, $worksheetName, $worksheetId =
200200
// Write sheet
201201
$objWriter->startElement('sheet');
202202
$objWriter->writeAttribute('name', $worksheetName);
203-
$objWriter->writeAttribute('sheetId', $worksheetId);
203+
$objWriter->writeAttribute('sheetId', (string) $worksheetId);
204204
if ($sheetState !== 'visible' && $sheetState != '') {
205205
$objWriter->writeAttribute('state', $sheetState);
206206
}

0 commit comments

Comments
 (0)