Skip to content

Commit adbda63

Browse files
authored
Phpstan and Xlsx Reader (#3043)
Eliminate most Phpstan messages in Xlsx Reader. In combination with similar changes to Xlsx Writer, baseline will shrink to just over 3,000 lines.
1 parent 57a7203 commit adbda63

23 files changed

+146
-131
lines changed

samples/Chart/33_Chart_create_scatter2.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
use PhpOffice\PhpSpreadsheet\Chart\Axis;
3+
use PhpOffice\PhpSpreadsheet\Chart\Axis as ChartAxis;
44
use PhpOffice\PhpSpreadsheet\Chart\Chart;
55
use PhpOffice\PhpSpreadsheet\Chart\ChartColor;
66
use PhpOffice\PhpSpreadsheet\Chart\DataSeries;
@@ -121,12 +121,12 @@
121121
$dataSeriesValues[2]->setScatterLines(false); // points not connected
122122

123123
// Added so that Xaxis shows dates instead of Excel-equivalent-year1900-numbers
124-
$xAxis = new Axis();
124+
$xAxis = new ChartAxis();
125125
//$xAxis->setAxisNumberProperties(Properties::FORMAT_CODE_DATE );
126126
$xAxis->setAxisNumberProperties(Properties::FORMAT_CODE_DATE_ISO8601, true);
127127
$xAxis->setAxisOption('textRotation', '45');
128128

129-
$yAxis = new Axis();
129+
$yAxis = new ChartAxis();
130130
$yAxis->setLineStyleProperties(
131131
2.5, // width in points
132132
Properties::LINE_STYLE_COMPOUND_SIMPLE,

samples/Chart/33_Chart_create_scatter3.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
use PhpOffice\PhpSpreadsheet\Chart\Axis;
3+
use PhpOffice\PhpSpreadsheet\Chart\Axis as ChartAxis;
44
use PhpOffice\PhpSpreadsheet\Chart\Chart;
55
use PhpOffice\PhpSpreadsheet\Chart\ChartColor;
66
use PhpOffice\PhpSpreadsheet\Chart\DataSeries;
@@ -121,13 +121,13 @@
121121
$dataSeriesValues[2]->setScatterLines(false); // points not connected
122122

123123
// Added so that Xaxis shows dates instead of Excel-equivalent-year1900-numbers
124-
$xAxis = new Axis();
124+
$xAxis = new ChartAxis();
125125
//$xAxis->setAxisNumberProperties(Properties::FORMAT_CODE_DATE );
126126
$xAxis->setAxisNumberProperties(Properties::FORMAT_CODE_DATE_ISO8601, true);
127127
$xAxis->setAxisOption('textRotation', '45');
128128
$xAxis->setAxisOption('hidden', '1');
129129

130-
$yAxis = new Axis();
130+
$yAxis = new ChartAxis();
131131
$yAxis->setLineStyleProperties(
132132
2.5, // width in points
133133
Properties::LINE_STYLE_COMPOUND_SIMPLE,

samples/Chart/33_Chart_create_scatter5_trendlines.php

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
use PhpOffice\PhpSpreadsheet\Chart\Axis;
3+
use PhpOffice\PhpSpreadsheet\Chart\Axis as ChartAxis;
44
use PhpOffice\PhpSpreadsheet\Chart\Chart;
55
use PhpOffice\PhpSpreadsheet\Chart\ChartColor;
66
use PhpOffice\PhpSpreadsheet\Chart\DataSeries;
@@ -77,10 +77,10 @@
7777
// marker details
7878
$dataSeriesValues[0]
7979
->getMarkerFillColor()
80-
->setColorProperties('0070C0', null, ChartColor::EXCEL_COLOR_TYPE_ARGB);
80+
->setColorProperties('0070C0', null, ChartColor::EXCEL_COLOR_TYPE_RGB);
8181
$dataSeriesValues[0]
8282
->getMarkerBorderColor()
83-
->setColorProperties('002060', null, ChartColor::EXCEL_COLOR_TYPE_ARGB);
83+
->setColorProperties('002060', null, ChartColor::EXCEL_COLOR_TYPE_RGB);
8484

8585
// line details - dashed, smooth line (Bezier) with arrows, 40% transparent
8686
$dataSeriesValues[0]
@@ -105,24 +105,24 @@
105105
->setColorProperties('accent6', 3, ChartColor::EXCEL_COLOR_TYPE_SCHEME);
106106
$dataSeriesValues[1] // square marker fill color
107107
->getMarkerFillColor()
108-
->setColorProperties('0FFF00', null, ChartColor::EXCEL_COLOR_TYPE_ARGB);
108+
->setColorProperties('0FFF00', null, ChartColor::EXCEL_COLOR_TYPE_RGB);
109109
$dataSeriesValues[1]
110110
->setScatterLines(true)
111111
->setSmoothLine(false)
112-
->setLineColorProperties('FF0000', 80, ChartColor::EXCEL_COLOR_TYPE_ARGB);
112+
->setLineColorProperties('FF0000', 80, ChartColor::EXCEL_COLOR_TYPE_RGB);
113113
$dataSeriesValues[1]->setLineWidth(2.0);
114114

115115
// series 3 - metric3, markers, no line
116116
$dataSeriesValues[2] // triangle? fill
117117
//->setPointMarker('triangle') // let Excel choose shape, which is predicted to be a triangle
118118
->getMarkerFillColor()
119-
->setColorProperties('FFFF00', null, ChartColor::EXCEL_COLOR_TYPE_ARGB);
119+
->setColorProperties('FFFF00', null, ChartColor::EXCEL_COLOR_TYPE_RGB);
120120
$dataSeriesValues[2] // triangle border
121121
->getMarkerBorderColor()
122122
->setColorProperties('accent4', null, ChartColor::EXCEL_COLOR_TYPE_SCHEME);
123123
$dataSeriesValues[2]->setScatterLines(false); // points not connected
124124
// Added so that Xaxis shows dates instead of Excel-equivalent-year1900-numbers
125-
$xAxis = new Axis();
125+
$xAxis = new ChartAxis();
126126
$xAxis->setAxisNumberProperties(Properties::FORMAT_CODE_DATE_ISO8601, true);
127127

128128
// Build the dataseries
@@ -204,7 +204,7 @@
204204

205205
$dataSeriesValues[0]->setScatterLines(false); // points not connected
206206
$dataSeriesValues[0]->getMarkerFillColor()
207-
->setColorProperties('FFFF00', null, ChartColor::EXCEL_COLOR_TYPE_ARGB);
207+
->setColorProperties('FFFF00', null, ChartColor::EXCEL_COLOR_TYPE_RGB);
208208
$dataSeriesValues[0]->getMarkerBorderColor()
209209
->setColorProperties('accent4', null, ChartColor::EXCEL_COLOR_TYPE_SCHEME);
210210

@@ -218,7 +218,7 @@
218218
$dataSeriesValues[0]->getTrendLines()[2]->getLineColor()->setColorProperties('accent2', null, ChartColor::EXCEL_COLOR_TYPE_SCHEME);
219219
$dataSeriesValues[0]->getTrendLines()[2]->setLineStyleProperties(1.5, null, null, null, null, null, null, Properties::LINE_STYLE_ARROW_TYPE_OPEN, 8);
220220

221-
$xAxis = new Axis();
221+
$xAxis = new ChartAxis();
222222
$xAxis->setAxisNumberProperties(Properties::FORMAT_CODE_DATE_ISO8601); // m/d/yyyy
223223

224224
// Build the dataseries

src/PhpSpreadsheet/Chart/DataSeriesValues.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -324,13 +324,13 @@ public function getFillColor()
324324
if (is_array($this->fillColor)) {
325325
$array = [];
326326
foreach ($this->fillColor as $chartColor) {
327-
$array[] = self::chartColorToString($chartColor);
327+
$array[] = $this->chartColorToString($chartColor);
328328
}
329329

330330
return $array;
331331
}
332332

333-
return self::chartColorToString($this->fillColor);
333+
return $this->chartColorToString($this->fillColor);
334334
}
335335

336336
/**
@@ -348,13 +348,13 @@ public function setFillColor($color)
348348
if ($fillString instanceof ChartColor) {
349349
$this->fillColor[] = $fillString;
350350
} else {
351-
$this->fillColor[] = self::stringToChartColor($fillString);
351+
$this->fillColor[] = $this->stringToChartColor($fillString);
352352
}
353353
}
354354
} elseif ($color instanceof ChartColor) {
355355
$this->fillColor = $color;
356-
} elseif (is_string($color)) {
357-
$this->fillColor = self::stringToChartColor($color);
356+
} else {
357+
$this->fillColor = $this->stringToChartColor($color);
358358
}
359359

360360
return $this;

src/PhpSpreadsheet/Reader/Xlsx/Chart.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ public function readChart(SimpleXMLElement $chartElements, $chartName)
311311
break;
312312
case 'stockChart':
313313
$plotSeries[] = $this->chartDataSeries($chartDetail, $chartDetailKey);
314-
$plotAttributes = $this->readChartAttributes($plotAreaLayout);
314+
$plotAttributes = $this->readChartAttributes($chartDetail);
315315

316316
break;
317317
}
@@ -1068,7 +1068,7 @@ private function parseRichText(SimpleXMLElement $titleDetailPart): RichText
10681068
}
10691069

10701070
/**
1071-
* @param null|Layout|SimpleXMLElement $chartDetail
1071+
* @param ?SimpleXMLElement $chartDetail
10721072
*/
10731073
private function readChartAttributes($chartDetail): array
10741074
{

src/PhpSpreadsheet/Shared/Font.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ public static function calculateColumnWidth(
349349

350350
// Special case if there are one or more newline characters ("\n")
351351
$cellText = $cellText ?? '';
352-
if (strpos($cellText, "\n") !== false) {
352+
if (strpos(/** @scrutinizer ignore-type */ $cellText, "\n") !== false) {
353353
$lineTexts = explode("\n", $cellText);
354354
$lineWidths = [];
355355
foreach ($lineTexts as $lineText) {

src/PhpSpreadsheet/Shared/StringHelper.php

+13-3
Original file line numberDiff line numberDiff line change
@@ -337,9 +337,19 @@ public static function sanitizeUTF8(string $textValue): string
337337
mb_substitute_character(65533); // Unicode substitution character
338338
// Phpstan does not think this can return false.
339339
$returnValue = mb_convert_encoding($textValue, 'UTF-8', 'UTF-8');
340-
mb_substitute_character($subst);
340+
mb_substitute_character(/** @scrutinizer ignore-type */ $subst);
341341

342-
return $returnValue;
342+
return self::returnString($returnValue);
343+
}
344+
345+
/**
346+
* Strictly to satisfy Scrutinizer.
347+
*
348+
* @param mixed $value
349+
*/
350+
private static function returnString($value): string
351+
{
352+
return is_string($value) ? $value : '';
343353
}
344354

345355
/**
@@ -433,7 +443,7 @@ public static function convertEncoding(string $textValue, string $to, string $fr
433443
}
434444
}
435445

436-
return mb_convert_encoding($textValue, $to, $from);
446+
return self::returnString(mb_convert_encoding($textValue, $to, $from));
437447
}
438448

439449
/**

src/PhpSpreadsheet/Style/Font.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -743,14 +743,14 @@ public function setColor(Color $color)
743743

744744
private function hashChartColor(?ChartColor $underlineColor): string
745745
{
746-
if ($this->underlineColor === null) {
746+
if ($underlineColor === null) {
747747
return '';
748748
}
749749

750750
return
751-
$this->underlineColor->getValue()
752-
. $this->underlineColor->getType()
753-
. (string) $this->underlineColor->getAlpha();
751+
$underlineColor->getValue()
752+
. $underlineColor->getType()
753+
. (string) $underlineColor->getAlpha();
754754
}
755755

756756
/**

src/PhpSpreadsheet/Writer/Pdf/Dompdf.php

-3
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,6 @@ public function save($filename, int $flags = 0): void
3333
{
3434
$fileHandle = parent::prepareForSave($filename);
3535

36-
// Default PDF paper size
37-
$paperSize = 'LETTER'; // Letter (8.5 in. by 11 in.)
38-
3936
// Check for paper size and page orientation
4037
$setup = $this->spreadsheet->getSheet($this->getSheetIndex() ?? 0)->getPageSetup();
4138
$orientation = $this->getOrientation() ?? $setup->getOrientation();

src/PhpSpreadsheet/Writer/Xlsx/Chart.php

-3
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,6 @@ private function writePlotArea(XMLWriter $objWriter, ?PlotArea $plotArea, ?Title
233233
if ($plotArea === null) {
234234
return;
235235
}
236-
$majorGridlines = ($yAxis === null) ? null : $yAxis->getMajorGridlines();
237-
$minorGridlines = ($yAxis === null) ? null : $yAxis->getMinorGridlines();
238236

239237
$id1 = $id2 = $id3 = '0';
240238
$this->seriesIndex = 0;
@@ -1163,7 +1161,6 @@ private function writePlotGroup(?DataSeries $plotGroup, string $groupType, XMLWr
11631161
$intercept = $trendLine->getIntercept();
11641162
$name = $trendLine->getName();
11651163
$trendLineColor = $trendLine->getLineColor(); // ChartColor
1166-
$trendLineWidth = $trendLine->getLineStyleProperty('width');
11671164

11681165
$objWriter->startElement('c:trendline'); // N.B. lowercase 'ell'
11691166
if ($name !== '') {

tests/PhpSpreadsheetTests/Calculation/FormulaParserTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public function testNullFormula(): void
1212
{
1313
$this->expectException(CalcException::class);
1414
$this->expectExceptionMessage('Invalid parameter passed: formula');
15-
$result = new FormulaParser(null);
15+
new FormulaParser(null);
1616
}
1717

1818
public function testInvalidTokenId(): void

tests/PhpSpreadsheetTests/Chart/AxisGlowTest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
namespace PhpOffice\PhpSpreadsheetTests\Chart;
44

55
use PhpOffice\PhpSpreadsheet\Chart\Chart;
6+
use PhpOffice\PhpSpreadsheet\Chart\ChartColor;
67
use PhpOffice\PhpSpreadsheet\Chart\DataSeries;
78
use PhpOffice\PhpSpreadsheet\Chart\DataSeriesValues;
89
use PhpOffice\PhpSpreadsheet\Chart\Legend as ChartLegend;
910
use PhpOffice\PhpSpreadsheet\Chart\PlotArea;
10-
use PhpOffice\PhpSpreadsheet\Chart\Properties;
1111
use PhpOffice\PhpSpreadsheet\Chart\Title;
1212
use PhpOffice\PhpSpreadsheet\Reader\Xlsx as XlsxReader;
1313
use PhpOffice\PhpSpreadsheet\Spreadsheet;
@@ -107,7 +107,7 @@ public function testGlowY(): void
107107
$yAxis = $chart->getChartAxisY();
108108
$xAxis = $chart->getChartAxisX();
109109
$yGlowSize = 10.0;
110-
$yAxis->setGlowProperties($yGlowSize, 'FFFF00', 30, Properties::EXCEL_COLOR_TYPE_ARGB);
110+
$yAxis->setGlowProperties($yGlowSize, 'FFFF00', 30, ChartColor::EXCEL_COLOR_TYPE_RGB);
111111
$expectedGlowColor = [
112112
'type' => 'srgbClr',
113113
'value' => 'FFFF00',
@@ -231,7 +231,7 @@ public function testGlowX(): void
231231
);
232232
$yAxis = $chart->getChartAxisX(); // deliberate
233233
$yGlowSize = 20.0;
234-
$yAxis->setGlowProperties($yGlowSize, 'accent1', 20, Properties::EXCEL_COLOR_TYPE_SCHEME);
234+
$yAxis->setGlowProperties($yGlowSize, 'accent1', 20, ChartColor::EXCEL_COLOR_TYPE_SCHEME);
235235
$expectedGlowColor = [
236236
'type' => 'schemeClr',
237237
'value' => 'accent1',

tests/PhpSpreadsheetTests/Chart/AxisShadowTest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
namespace PhpOffice\PhpSpreadsheetTests\Chart;
44

55
use PhpOffice\PhpSpreadsheet\Chart\Chart;
6+
use PhpOffice\PhpSpreadsheet\Chart\ChartColor;
67
use PhpOffice\PhpSpreadsheet\Chart\DataSeries;
78
use PhpOffice\PhpSpreadsheet\Chart\DataSeriesValues;
89
use PhpOffice\PhpSpreadsheet\Chart\Legend as ChartLegend;
910
use PhpOffice\PhpSpreadsheet\Chart\PlotArea;
10-
use PhpOffice\PhpSpreadsheet\Chart\Properties;
1111
use PhpOffice\PhpSpreadsheet\Chart\Title;
1212
use PhpOffice\PhpSpreadsheet\Reader\Xlsx as XlsxReader;
1313
use PhpOffice\PhpSpreadsheet\Spreadsheet;
@@ -113,7 +113,7 @@ public function testGlowY(): void
113113
'distance' => 3,
114114
'rotWithShape' => 0,
115115
'color' => [
116-
'type' => Properties::EXCEL_COLOR_TYPE_STANDARD,
116+
'type' => ChartColor::EXCEL_COLOR_TYPE_STANDARD,
117117
'value' => 'black',
118118
'alpha' => 40,
119119
],
@@ -139,7 +139,7 @@ public function testGlowY(): void
139139
'ky' => null,
140140
],
141141
'color' => [
142-
'type' => Properties::EXCEL_COLOR_TYPE_ARGB,
142+
'type' => ChartColor::EXCEL_COLOR_TYPE_RGB,
143143
'value' => 'FF0000',
144144
'alpha' => 20,
145145
],

tests/PhpSpreadsheetTests/Chart/GridlinesShadowGlowTest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
use PhpOffice\PhpSpreadsheet\Chart\Axis;
66
use PhpOffice\PhpSpreadsheet\Chart\Chart;
7+
use PhpOffice\PhpSpreadsheet\Chart\ChartColor;
78
use PhpOffice\PhpSpreadsheet\Chart\DataSeries;
89
use PhpOffice\PhpSpreadsheet\Chart\DataSeriesValues;
910
use PhpOffice\PhpSpreadsheet\Chart\GridLines;
1011
use PhpOffice\PhpSpreadsheet\Chart\Legend as ChartLegend;
1112
use PhpOffice\PhpSpreadsheet\Chart\PlotArea;
12-
use PhpOffice\PhpSpreadsheet\Chart\Properties;
1313
use PhpOffice\PhpSpreadsheet\Chart\Title;
1414
use PhpOffice\PhpSpreadsheet\Reader\Xlsx as XlsxReader;
1515
use PhpOffice\PhpSpreadsheet\Spreadsheet;
@@ -98,7 +98,7 @@ public function testGlowY(): void
9898
$majorGridlines = new GridLines();
9999
$yAxis->setMajorGridlines($majorGridlines);
100100
$majorGlowSize = 10.0;
101-
$majorGridlines->setGlowProperties($majorGlowSize, 'FFFF00', 30, Properties::EXCEL_COLOR_TYPE_ARGB);
101+
$majorGridlines->setGlowProperties($majorGlowSize, 'FFFF00', 30, ChartColor::EXCEL_COLOR_TYPE_RGB);
102102
$softEdgeSize = 2.5;
103103
$majorGridlines->setSoftEdges($softEdgeSize);
104104
$expectedGlowColor = [
@@ -122,7 +122,7 @@ public function testGlowY(): void
122122
'distance' => 3,
123123
'rotWithShape' => 0,
124124
'color' => [
125-
'type' => Properties::EXCEL_COLOR_TYPE_STANDARD,
125+
'type' => ChartColor::EXCEL_COLOR_TYPE_STANDARD,
126126
'value' => 'black',
127127
'alpha' => 40,
128128
],

tests/PhpSpreadsheetTests/Chart/Issue2506Test.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function writeCharts(XlsxWriter $writer): void
2323
public function testDataSeriesValues(): void
2424
{
2525
$reader = new XlsxReader();
26-
self::readCharts($reader);
26+
$this->readCharts($reader);
2727
$spreadsheet = $reader->load(self::DIRECTORY . 'issue.2506.xlsx');
2828
$worksheet = $spreadsheet->getActiveSheet();
2929
$charts = $worksheet->getChartCollection();

tests/PhpSpreadsheetTests/Chart/MultiplierTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
namespace PhpOffice\PhpSpreadsheetTests\Chart;
44

55
use PhpOffice\PhpSpreadsheet\Chart\Chart;
6+
use PhpOffice\PhpSpreadsheet\Chart\ChartColor;
67
use PhpOffice\PhpSpreadsheet\Chart\DataSeries;
78
use PhpOffice\PhpSpreadsheet\Chart\DataSeriesValues;
89
use PhpOffice\PhpSpreadsheet\Chart\Legend as ChartLegend;
910
use PhpOffice\PhpSpreadsheet\Chart\PlotArea;
10-
use PhpOffice\PhpSpreadsheet\Chart\Properties;
1111
use PhpOffice\PhpSpreadsheet\Chart\Title;
1212
use PhpOffice\PhpSpreadsheet\Shared\File;
1313
use PhpOffice\PhpSpreadsheet\Spreadsheet;
@@ -109,7 +109,7 @@ public function testMultiplier(): void
109109
'ky' => null,
110110
],
111111
'color' => [
112-
'type' => Properties::EXCEL_COLOR_TYPE_ARGB,
112+
'type' => ChartColor::EXCEL_COLOR_TYPE_RGB,
113113
'value' => 'FF0000',
114114
'alpha' => 20,
115115
],

0 commit comments

Comments
 (0)