Skip to content

Commit d620497

Browse files
authored
Merge pull request #3893 from oleibman/stanarray2
Phpstan Fixes
2 parents 3512304 + 101a31b commit d620497

File tree

8 files changed

+47
-90
lines changed

8 files changed

+47
-90
lines changed

phpstan-baseline.neon

Lines changed: 0 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,6 @@
11
parameters:
22
ignoreErrors:
3-
-
4-
message: "#^Strict comparison using \\=\\=\\= between mixed and null will always evaluate to false\\.$#"
5-
count: 1
6-
path: src/PhpSpreadsheet/Calculation/Calculation.php
7-
83
-
94
message: "#^Binary operation \"/\" between float and array\\|float\\|int\\|string results in an error\\.$#"
105
count: 1
116
path: src/PhpSpreadsheet/Calculation/MathTrig/Combinations.php
12-
13-
-
14-
message: "#^Argument of an invalid type array\\|null supplied for foreach, only iterables are supported\\.$#"
15-
count: 1
16-
path: src/PhpSpreadsheet/Chart/DataSeriesValues.php
17-
18-
-
19-
message: "#^Offset 0 does not exist on array\\|null\\.$#"
20-
count: 1
21-
path: src/PhpSpreadsheet/Chart/DataSeriesValues.php
22-
23-
-
24-
message: "#^Parameter \\#1 \\$value of function count expects array\\|Countable, array\\|null given\\.$#"
25-
count: 1
26-
path: src/PhpSpreadsheet/Chart/DataSeriesValues.php
27-
28-
-
29-
message: "#^Argument of an invalid type array\\<int, \\(SimpleXMLElement\\|null\\)\\>\\|SimpleXMLElement\\|null supplied for foreach, only iterables are supported\\.$#"
30-
count: 2
31-
path: src/PhpSpreadsheet/Reader/Xlsx/ConditionalStyles.php
32-
33-
-
34-
message: "#^Cannot access property \\$color on SimpleXMLElement\\|null\\.$#"
35-
count: 2
36-
path: src/PhpSpreadsheet/Reader/Xlsx/Styles.php
37-
38-
-
39-
message: "#^Cannot call method beforeCellAddress\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\CellReferenceHelper\\|null\\.$#"
40-
count: 1
41-
path: src/PhpSpreadsheet/ReferenceHelper.php
42-
43-
-
44-
message: "#^Cannot call method cellAddressInDeleteRange\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\CellReferenceHelper\\|null\\.$#"
45-
count: 3
46-
path: src/PhpSpreadsheet/ReferenceHelper.php
47-
48-
-
49-
message: "#^Cannot call method updateCellReference\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\CellReferenceHelper\\|null\\.$#"
50-
count: 4
51-
path: src/PhpSpreadsheet/ReferenceHelper.php
52-
53-
-
54-
message: "#^Argument of an invalid type array\\|null supplied for foreach, only iterables are supported\\.$#"
55-
count: 3
56-
path: src/PhpSpreadsheet/Writer/Xlsx/Chart.php
57-
58-
-
59-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Writer\\\\Xlsx\\\\Chart\\:\\:getChartType\\(\\) should return array\\<string\\> but returns array\\<int\\<0, max\\>, string\\|null\\>\\.$#"
60-
count: 1
61-
path: src/PhpSpreadsheet/Writer/Xlsx/Chart.php
62-
63-
-
64-
message: "#^Parameter \\#2 \\$array of function array_key_exists expects array, array\\|null given\\.$#"
65-
count: 1
66-
path: src/PhpSpreadsheet/Writer/Xlsx/Chart.php
67-
68-
-
69-
message: "#^Offset 0 does not exist on array\\|null\\.$#"
70-
count: 1
71-
path: tests/PhpSpreadsheetTests/Reader/Xlsx/Issue3767Test.php

src/PhpSpreadsheet/Calculation/Calculation.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4648,10 +4648,12 @@ private function processTokenStack(mixed $tokens, ?string $cellID = null, ?Cell
46484648
} elseif (!is_numeric($token) && !is_object($token) && isset(self::BINARY_OPERATORS[$token])) {
46494649
// if the token is a binary operator, pop the top two values off the stack, do the operation, and push the result back on the stack
46504650
// We must have two operands, error if we don't
4651-
if (($operand2Data = $stack->pop()) === null) {
4651+
$operand2Data = $stack->pop();
4652+
if ($operand2Data === null) {
46524653
return $this->raiseFormulaError('Internal error - Operand value missing from stack');
46534654
}
4654-
if (($operand1Data = $stack->pop()) === null) {
4655+
$operand1Data = $stack->pop();
4656+
if ($operand1Data === null) {
46554657
return $this->raiseFormulaError('Internal error - Operand value missing from stack');
46564658
}
46574659

src/PhpSpreadsheet/Chart/DataSeriesValues.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ public function isMultiLevelSeries(): ?bool
380380
public function multiLevelCount(): int
381381
{
382382
$levelCount = 0;
383-
foreach ($this->dataValues as $dataValueSet) {
383+
foreach (($this->dataValues ?? []) as $dataValueSet) {
384384
$levelCount = max($levelCount, count($dataValueSet));
385385
}
386386

@@ -400,6 +400,9 @@ public function getDataValues(): ?array
400400
*/
401401
public function getDataValue(): mixed
402402
{
403+
if ($this->dataValues === null) {
404+
return null;
405+
}
403406
$count = count($this->dataValues);
404407
if ($count == 0) {
405408
return null;

src/PhpSpreadsheet/Reader/Xlsx/ConditionalStyles.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ private function readDataBarOfConditionalRule(SimpleXMLElement $cfRule, array $c
262262
//conditionalFormatValueObjects
263263
$cfvoXml = $cfRule->dataBar->cfvo;
264264
$cfvoIndex = 0;
265-
foreach ((count($cfvoXml) > 1 ? $cfvoXml : [$cfvoXml]) as $cfvo) {
265+
foreach ((count($cfvoXml) > 1 ? $cfvoXml : [$cfvoXml]) as $cfvo) { //* @phpstan-ignore-line
266266
if ($cfvoIndex === 0) {
267267
$dataBar->setMinimumConditionalFormatValueObject(new ConditionalFormatValueObject((string) $cfvo['type'], (string) $cfvo['val']));
268268
}
@@ -320,7 +320,7 @@ private function readDataBarExtLstOfConditionalRule(ConditionalDataBar $dataBar,
320320
{
321321
if (isset($cfRule->extLst)) {
322322
$ns = $cfRule->extLst->getNamespaces(true);
323-
foreach ((count($cfRule->extLst) > 0 ? $cfRule->extLst->ext : [$cfRule->extLst->ext]) as $ext) {
323+
foreach ((count($cfRule->extLst) > 0 ? $cfRule->extLst->ext : [$cfRule->extLst->ext]) as $ext) { //* @phpstan-ignore-line
324324
$extId = (string) $ext->children($ns['x14'])->id;
325325
if (isset($conditionalFormattingRuleExtensions[$extId]) && (string) $ext['uri'] === '{B025F937-C7B1-47D3-B67F-A62EFF666E3E}') {
326326
$dataBar->setConditionalFormattingRuleExt($conditionalFormattingRuleExtensions[$extId]);

src/PhpSpreadsheet/Reader/Xlsx/Styles.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,8 @@ public function readFillStyle(Fill $fillStyle, SimpleXMLElement $fillStyleXml):
148148
}
149149
$fillStyle->setRotation((float) ($attr['degree']));
150150
$gradientFill->registerXPathNamespace('sml', Namespaces::MAIN);
151-
$fillStyle->getStartColor()->setARGB($this->readColor(self::getArrayItem($gradientFill->xpath('sml:stop[@position=0]'))->color));
152-
$fillStyle->getEndColor()->setARGB($this->readColor(self::getArrayItem($gradientFill->xpath('sml:stop[@position=1]'))->color));
151+
$fillStyle->getStartColor()->setARGB($this->readColor(self::getArrayItem($gradientFill->xpath('sml:stop[@position=0]'))->color)); //* @phpstan-ignore-line
152+
$fillStyle->getEndColor()->setARGB($this->readColor(self::getArrayItem($gradientFill->xpath('sml:stop[@position=1]'))->color)); //* @phpstan-ignore-line
153153
} elseif ($fillStyleXml->patternFill) {
154154
$defaultFillStyle = Fill::FILL_NONE;
155155
if ($fillStyleXml->patternFill->fgColor) {

src/PhpSpreadsheet/ReferenceHelper.php

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,9 @@ protected function adjustPageBreaks(Worksheet $worksheet, int $numberOfColumns,
131131
: uksort($aBreaks, [self::class, 'cellSort']);
132132

133133
foreach ($aBreaks as $cellAddress => $value) {
134-
if ($this->cellReferenceHelper->cellAddressInDeleteRange($cellAddress) === true) {
134+
/** @var CellReferenceHelper */
135+
$cellReferenceHelper = $this->cellReferenceHelper;
136+
if ($cellReferenceHelper->cellAddressInDeleteRange($cellAddress) === true) {
135137
// If we're deleting, then clear any defined breaks that are within the range
136138
// of rows/columns that we're deleting
137139
$worksheet->setBreak($cellAddress, Worksheet::BREAK_NONE);
@@ -159,7 +161,9 @@ protected function adjustComments(Worksheet $worksheet): void
159161

160162
foreach ($aComments as $cellAddress => &$value) {
161163
// Any comments inside a deleted range will be ignored
162-
if ($this->cellReferenceHelper->cellAddressInDeleteRange($cellAddress) === false) {
164+
/** @var CellReferenceHelper */
165+
$cellReferenceHelper = $this->cellReferenceHelper;
166+
if ($cellReferenceHelper->cellAddressInDeleteRange($cellAddress) === false) {
163167
// Otherwise build a new array of comments indexed by the adjusted cell reference
164168
$newReference = $this->updateCellReference($cellAddress);
165169
$aNewComments[$newReference] = $value;
@@ -185,7 +189,9 @@ protected function adjustHyperlinks(Worksheet $worksheet, int $numberOfColumns,
185189

186190
foreach ($aHyperlinkCollection as $cellAddress => $value) {
187191
$newReference = $this->updateCellReference($cellAddress);
188-
if ($this->cellReferenceHelper->cellAddressInDeleteRange($cellAddress) === true) {
192+
/** @var CellReferenceHelper */
193+
$cellReferenceHelper = $this->cellReferenceHelper;
194+
if ($cellReferenceHelper->cellAddressInDeleteRange($cellAddress) === true) {
189195
$worksheet->setHyperlink($cellAddress, null);
190196
} elseif ($cellAddress !== $newReference) {
191197
$worksheet->setHyperlink($newReference, $value);
@@ -217,9 +223,11 @@ protected function adjustConditionalFormatting(Worksheet $worksheet, int $number
217223
$conditions = $cfRule->getConditions();
218224
foreach ($conditions as &$condition) {
219225
if (is_string($condition)) {
226+
/** @var CellReferenceHelper */
227+
$cellReferenceHelper = $this->cellReferenceHelper;
220228
$condition = $this->updateFormulaReferences(
221229
$condition,
222-
$this->cellReferenceHelper->beforeCellAddress(),
230+
$cellReferenceHelper->beforeCellAddress(),
223231
$numberOfColumns,
224232
$numberOfRows,
225233
$worksheet->getTitle(),
@@ -848,7 +856,10 @@ private function updateCellReference(string $cellReference = 'A1', bool $include
848856
// Is it a range or a single cell?
849857
if (!Coordinate::coordinateIsRange($cellReference)) {
850858
// Single cell
851-
return $this->cellReferenceHelper->updateCellReference($cellReference, $includeAbsoluteReferences, $onlyAbsoluteReferences);
859+
/** @var CellReferenceHelper */
860+
$cellReferenceHelper = $this->cellReferenceHelper;
861+
862+
return $cellReferenceHelper->updateCellReference($cellReference, $includeAbsoluteReferences, $onlyAbsoluteReferences);
852863
}
853864

854865
// Range
@@ -950,16 +961,18 @@ private function updateCellRange(string $cellRange = 'A1:A1', bool $includeAbsol
950961
for ($i = 0; $i < $ic; ++$i) {
951962
$jc = count($range[$i]);
952963
for ($j = 0; $j < $jc; ++$j) {
964+
/** @var CellReferenceHelper */
965+
$cellReferenceHelper = $this->cellReferenceHelper;
953966
if (ctype_alpha($range[$i][$j])) {
954967
$range[$i][$j] = Coordinate::coordinateFromString(
955-
$this->cellReferenceHelper->updateCellReference($range[$i][$j] . '1', $includeAbsoluteReferences, $onlyAbsoluteReferences)
968+
$cellReferenceHelper->updateCellReference($range[$i][$j] . '1', $includeAbsoluteReferences, $onlyAbsoluteReferences)
956969
)[0];
957970
} elseif (ctype_digit($range[$i][$j])) {
958971
$range[$i][$j] = Coordinate::coordinateFromString(
959-
$this->cellReferenceHelper->updateCellReference('A' . $range[$i][$j], $includeAbsoluteReferences, $onlyAbsoluteReferences)
972+
$cellReferenceHelper->updateCellReference('A' . $range[$i][$j], $includeAbsoluteReferences, $onlyAbsoluteReferences)
960973
)[1];
961974
} else {
962-
$range[$i][$j] = $this->cellReferenceHelper->updateCellReference($range[$i][$j], $includeAbsoluteReferences, $onlyAbsoluteReferences);
975+
$range[$i][$j] = $cellReferenceHelper->updateCellReference($range[$i][$j], $includeAbsoluteReferences, $onlyAbsoluteReferences);
963976
}
964977
}
965978
}

src/PhpSpreadsheet/Writer/Xlsx/Chart.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,16 +1064,20 @@ private static function getChartType(PlotArea $plotArea): array
10641064
$groupCount = $plotArea->getPlotGroupCount();
10651065

10661066
if ($groupCount == 1) {
1067-
$chartType = [$plotArea->getPlotGroupByIndex(0)->getPlotType()];
1067+
$plotType = $plotArea->getPlotGroupByIndex(0)->getPlotType();
1068+
$chartType = ($plotType === null) ? [] : [$plotType];
10681069
} else {
10691070
$chartTypes = [];
10701071
for ($i = 0; $i < $groupCount; ++$i) {
1071-
$chartTypes[] = $plotArea->getPlotGroupByIndex($i)->getPlotType();
1072+
$plotType = $plotArea->getPlotGroupByIndex($i)->getPlotType();
1073+
if ($plotType !== null) {
1074+
$chartTypes[] = $plotType;
1075+
}
10721076
}
10731077
$chartType = array_unique($chartTypes);
1074-
if (count($chartTypes) == 0) {
1075-
throw new WriterException('Chart is not yet implemented');
1076-
}
1078+
}
1079+
if (count($chartType) == 0) {
1080+
throw new WriterException('Chart is not yet implemented');
10771081
}
10781082

10791083
return $chartType;
@@ -1171,7 +1175,7 @@ private function writePlotGroup(?DataSeries $plotGroup, string $groupType, XMLWr
11711175
if ($plotSeriesValues !== false && in_array($groupType, self::CUSTOM_COLOR_TYPES, true)) {
11721176
$fillColorValues = $plotSeriesValues->getFillColorObject();
11731177
if ($fillColorValues !== null && is_array($fillColorValues)) {
1174-
foreach ($plotSeriesValues->getDataValues() as $dataKey => $dataValue) {
1178+
foreach (($plotSeriesValues->getDataValues() ?? []) as $dataKey => $dataValue) {
11751179
$this->writePlotSeriesValuesElement($objWriter, $dataKey, $fillColorValues[$dataKey] ?? null);
11761180
}
11771181
}
@@ -1435,7 +1439,7 @@ private function writePlotSeriesLabel(?DataSeriesValues $plotSeriesLabel, XMLWri
14351439
$objWriter->writeAttribute('val', (string) $plotSeriesLabel->getPointCount());
14361440
$objWriter->endElement();
14371441

1438-
foreach ($plotSeriesLabel->getDataValues() as $plotLabelKey => $plotLabelValue) {
1442+
foreach (($plotSeriesLabel->getDataValues() ?? []) as $plotLabelKey => $plotLabelValue) {
14391443
$objWriter->startElement('c:pt');
14401444
$objWriter->writeAttribute('idx', $plotLabelKey);
14411445

@@ -1477,7 +1481,7 @@ private function writePlotSeriesValues(?DataSeriesValues $plotSeriesValues, XMLW
14771481
for ($level = 0; $level < $levelCount; ++$level) {
14781482
$objWriter->startElement('c:lvl');
14791483

1480-
foreach ($plotSeriesValues->getDataValues() as $plotSeriesKey => $plotSeriesValue) {
1484+
foreach (($plotSeriesValues->getDataValues() ?? []) as $plotSeriesKey => $plotSeriesValue) {
14811485
if (isset($plotSeriesValue[$level])) {
14821486
$objWriter->startElement('c:pt');
14831487
$objWriter->writeAttribute('idx', $plotSeriesKey);
@@ -1505,7 +1509,7 @@ private function writePlotSeriesValues(?DataSeriesValues $plotSeriesValues, XMLW
15051509
$count = $plotSeriesValues->getPointCount();
15061510
$source = $plotSeriesValues->getDataSource();
15071511
$values = $plotSeriesValues->getDataValues();
1508-
if ($count > 1 || ($count === 1 && array_key_exists(0, $values) && "=$source" !== (string) $values[0])) {
1512+
if ($count > 1 || ($count === 1 && is_array($values) && array_key_exists(0, $values) && "=$source" !== (string) $values[0])) {
15091513
$objWriter->startElement('c:' . $dataType . 'Cache');
15101514

15111515
if (($groupType != DataSeries::TYPE_PIECHART) && ($groupType != DataSeries::TYPE_PIECHART_3D) && ($groupType != DataSeries::TYPE_DONUTCHART)) {

tests/PhpSpreadsheetTests/Reader/Xlsx/Issue3767Test.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public function testReadWithCharts(): void
8282
// I can't find that anywhere in the Xml.
8383
self::assertSame('', $charts[0]?->getTitle()?->getCaptionText());
8484
// Just test anything on the chart.
85-
self::assertSame($sheet->getCell('B2')->getValue(), $charts[0]->getPlotArea()?->getPlotGroup()[0]->getPlotValues()[0]->getDataValues()[0]);
85+
self::assertSame($sheet->getCell('B2')->getValue(), $charts[0]->getPlotArea()?->getPlotGroup()[0]?->getPlotValues()[0]?->getDataValues()[0] ?? null);
8686
$reloadedSpreadsheet->disconnectWorksheets();
8787
}
8888
}

0 commit comments

Comments
 (0)