Skip to content

Commit 1ac1e7f

Browse files
committed
Eliminate Some Phpstan-ignore Annotations
This PR resolves about half the statements which we tell Phpstan to ignore. There will still be 23 such annotations spread over 10 source modules for various reasons (too complicated, suspect PhpSpreadsheet code, one Phpstan bug), plus some deliberate errors in the test suite.
1 parent d620497 commit 1ac1e7f

File tree

22 files changed

+115
-61
lines changed

22 files changed

+115
-61
lines changed

phpstan-baseline.neon

-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,2 @@
11
parameters:
22
ignoreErrors:
3-
-
4-
message: "#^Binary operation \"/\" between float and array\\|float\\|int\\|string results in an error\\.$#"
5-
count: 1
6-
path: src/PhpSpreadsheet/Calculation/MathTrig/Combinations.php

src/PhpSpreadsheet/Calculation/DateTimeExcel/TimeValue.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class TimeValue
2525
* Excel Function:
2626
* TIMEVALUE(timeValue)
2727
*
28-
* @param null|array|bool|int|string $timeValue A text string that represents a time in any one of the Microsoft
28+
* @param null|array|bool|float|int|string $timeValue A text string that represents a time in any one of the Microsoft
2929
* Excel time formats; for example, "6:45 PM" and "18:45" text strings
3030
* within quotation marks that represent time.
3131
* Date information in time_text is ignored.
@@ -36,7 +36,7 @@ class TimeValue
3636
* If an array of numbers is passed as the argument, then the returned result will also be an array
3737
* with the same dimensions
3838
*/
39-
public static function fromString(null|array|string|int|bool $timeValue): array|string|Datetime|int|float
39+
public static function fromString(null|array|string|int|bool|float $timeValue): array|string|Datetime|int|float
4040
{
4141
if (is_array($timeValue)) {
4242
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $timeValue);

src/PhpSpreadsheet/Calculation/MathTrig/Combinations.php

+16-4
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,14 @@ public static function withoutRepetition(mixed $numObjs, mixed $numInSet): array
4040
return $e->getMessage();
4141
}
4242

43-
return round(Factorial::fact($numObjs) / Factorial::fact($numObjs - $numInSet)) / Factorial::fact($numInSet); // @phpstan-ignore-line
43+
/** @var float */
44+
$quotient = Factorial::fact($numObjs);
45+
/** @var float */
46+
$divisor1 = Factorial::fact($numObjs - $numInSet);
47+
/** @var float */
48+
$divisor2 = Factorial::fact($numInSet);
49+
50+
return round($quotient / ($divisor1 * $divisor2));
4451
}
4552

4653
/**
@@ -84,8 +91,13 @@ public static function withRepetition(mixed $numObjs, mixed $numInSet): array|in
8491
return $e->getMessage();
8592
}
8693

87-
return round(
88-
Factorial::fact($numObjs + $numInSet - 1) / Factorial::fact($numObjs - 1) // @phpstan-ignore-line
89-
) / Factorial::fact($numInSet);
94+
/** @var float */
95+
$quotient = Factorial::fact($numObjs + $numInSet - 1);
96+
/** @var float */
97+
$divisor1 = Factorial::fact($numObjs - 1);
98+
/** @var float */
99+
$divisor2 = Factorial::fact($numInSet);
100+
101+
return round($quotient / ($divisor1 * $divisor2));
90102
}
91103
}

src/PhpSpreadsheet/Calculation/Statistical/Permutations.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,17 @@ public static function PERMUT(mixed $numObjs, mixed $numInSet)
4646
if ($numObjs < $numInSet) {
4747
return ExcelError::NAN();
4848
}
49+
/** @var float|int|string */
4950
$result1 = MathTrig\Factorial::fact($numObjs);
5051
if (is_string($result1)) {
5152
return $result1;
5253
}
54+
/** @var float|int|string */
5355
$result2 = MathTrig\Factorial::fact($numObjs - $numInSet);
5456
if (is_string($result2)) {
5557
return $result2;
5658
}
57-
// phpstan thinks result1 and result2 can be arrays; they can't.
58-
$result = round($result1 / $result2); // @phpstan-ignore-line
59+
$result = round($result1 / $result2);
5960

6061
return IntOrFloat::evaluate($result);
6162
}

src/PhpSpreadsheet/Calculation/Statistical/Trends.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public static function FORECAST(mixed $xValue, array $yValues, array $xValues)
148148
* @param mixed[] $newValues Values of X for which we want to find Y
149149
* @param mixed $const A logical (boolean) value specifying whether to force the intersect to equal 0 or not
150150
*
151-
* @return float[]
151+
* @return array<int, array<int, array<int, float>>>
152152
*/
153153
public static function GROWTH(array $yValues, array $xValues = [], array $newValues = [], mixed $const = true): array
154154
{
@@ -167,7 +167,7 @@ public static function GROWTH(array $yValues, array $xValues = [], array $newVal
167167
$returnArray[0][] = [$bestFitExponential->getValueOfYForX($xValue)];
168168
}
169169

170-
return $returnArray; //* @phpstan-ignore-line
170+
return $returnArray;
171171
}
172172

173173
/**
@@ -401,7 +401,7 @@ public static function STEYX(array $yValues, array $xValues): float|string
401401
* @param mixed[] $newValues Values of X for which we want to find Y
402402
* @param mixed $const A logical (boolean) value specifying whether to force the intersect to equal 0 or not
403403
*
404-
* @return float[]
404+
* @return array<int, array<int, array<int, float>>>
405405
*/
406406
public static function TREND(array $yValues, array $xValues = [], array $newValues = [], mixed $const = true): array
407407
{
@@ -420,6 +420,6 @@ public static function TREND(array $yValues, array $xValues = [], array $newValu
420420
$returnArray[0][] = [$bestFitLinear->getValueOfYForX($xValue)];
421421
}
422422

423-
return $returnArray; //* @phpstan-ignore-line
423+
return $returnArray;
424424
}
425425
}

src/PhpSpreadsheet/Calculation/TextData/Format.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,9 @@ public static function TEXTFORMAT(mixed $value, mixed $format): array|string
126126
$format = Helpers::extractString($format);
127127

128128
if (!is_numeric($value) && Date::isDateTimeFormatCode($format)) {
129-
// @phpstan-ignore-next-line
130-
$value = DateTimeExcel\DateValue::fromString($value) + DateTimeExcel\TimeValue::fromString($value);
129+
$value1 = DateTimeExcel\DateValue::fromString($value);
130+
$value2 = DateTimeExcel\TimeValue::fromString($value);
131+
$value = (is_numeric($value1) && is_numeric($value2)) ? ($value1 + $value2) : (is_numeric($value1) ? $value2 : $value1);
131132
}
132133

133134
return (string) NumberFormat::toFormattedString($value, $format);

src/PhpSpreadsheet/Cell/AdvancedValueBinder.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ public function bindValue(Cell $cell, mixed $value = null): bool
6363
// Convert value to number
6464
$sign = ($matches['PrefixedSign'] ?? $matches['PrefixedSign2'] ?? $matches['PostfixedSign']) ?? null;
6565
$currencyCode = $matches['PrefixedCurrency'] ?? $matches['PostfixedCurrency'];
66-
$value = (float) ($sign . trim(str_replace([$decimalSeparatorNoPreg, $currencyCode, ' ', '-'], ['.', '', '', ''], preg_replace('/(\d)' . $thousandsSeparator . '(\d)/u', '$1$2', $value)))); // @phpstan-ignore-line
66+
/** @var string */
67+
$temp = str_replace([$decimalSeparatorNoPreg, $currencyCode, ' ', '-'], ['.', '', '', ''], preg_replace('/(\d)' . $thousandsSeparator . '(\d)/u', '$1$2', $value));
68+
$value = (float) ($sign . trim($temp));
6769

6870
return $this->setCurrency($value, $cell, $currencyCode ?? '');
6971
}

src/PhpSpreadsheet/Document/Properties.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,9 @@ public function setLastModifiedBy(string $modifiedBy): self
140140
return $this;
141141
}
142142

143-
private static function intOrFloatTimestamp(null|float|int|string $timestamp): float|int
143+
private static function intOrFloatTimestamp(null|bool|float|int|string $timestamp): float|int
144144
{
145-
if ($timestamp === null) {
145+
if ($timestamp === null || is_bool($timestamp)) {
146146
$timestamp = (float) (new DateTime())->format('U');
147147
} elseif (is_string($timestamp)) {
148148
if (is_numeric($timestamp)) {
@@ -468,7 +468,7 @@ private static function convertProperty2(bool|int|float|string|null $propertyVal
468468
case self::PROPERTY_TYPE_FLOAT:
469469
return (float) $propertyValue;
470470
case self::PROPERTY_TYPE_DATE:
471-
return self::intOrFloatTimestamp($propertyValue); // @phpstan-ignore-line
471+
return self::intOrFloatTimestamp($propertyValue);
472472
case self::PROPERTY_TYPE_BOOLEAN:
473473
return is_bool($propertyValue) ? $propertyValue : ($propertyValue === 'true');
474474
default: // includes string

src/PhpSpreadsheet/HashTable.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class HashTable
2626
*
2727
* @param T[] $source Optional source array to create HashTable from
2828
*/
29-
public function __construct(?array $source = null)
29+
public function __construct(?array $source = [])
3030
{
3131
if ($source !== null) {
3232
// Create HashTable

src/PhpSpreadsheet/Reader/Xlsx.php

+1
Original file line numberDiff line numberDiff line change
@@ -1002,6 +1002,7 @@ protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
10021002
if (isset($item->formula1)) {
10031003
$childNode = $node->addChild('formula1');
10041004
if ($childNode !== null) { // null should never happen
1005+
// see https://github.com/phpstan/phpstan/issues/8236
10051006
$childNode[0] = (string) $item->formula1->children(Namespaces::DATA_VALIDATIONS2)->f; // @phpstan-ignore-line
10061007
}
10071008
}

src/PhpSpreadsheet/Reader/Xlsx/Chart.php

+30-11
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,7 @@ private function chartDataSeries(SimpleXMLElement $chartDetail, string $plotType
574574
$multiSeriesType = null;
575575
$smoothLine = false;
576576
$seriesLabel = $seriesCategory = $seriesValues = $plotOrder = $seriesBubbles = [];
577+
$plotDirection = null;
577578

578579
$seriesDetailSet = $chartDetail->children($this->cNamespace);
579580
foreach ($seriesDetailSet as $seriesDetailKey => $seriesDetails) {
@@ -604,11 +605,16 @@ private function chartDataSeries(SimpleXMLElement $chartDetail, string $plotType
604605
break;
605606
case 'order':
606607
$seriesOrder = self::getAttributeInteger($seriesDetail, 'val');
607-
$plotOrder[$seriesIndex] = $seriesOrder;
608+
if ($seriesOrder !== null) {
609+
$plotOrder[$seriesIndex] = $seriesOrder;
610+
}
608611

609612
break;
610613
case 'tx':
611-
$seriesLabel[$seriesIndex] = $this->chartDataSeriesValueSet($seriesDetail);
614+
$temp = $this->chartDataSeriesValueSet($seriesDetail);
615+
if ($temp !== null) {
616+
$seriesLabel[$seriesIndex] = $temp;
617+
}
612618

613619
break;
614620
case 'spPr':
@@ -685,27 +691,42 @@ private function chartDataSeries(SimpleXMLElement $chartDetail, string $plotType
685691

686692
break;
687693
case 'smooth':
688-
$smoothLine = self::getAttributeBoolean($seriesDetail, 'val');
694+
$smoothLine = self::getAttributeBoolean($seriesDetail, 'val') ?? false;
689695

690696
break;
691697
case 'cat':
692-
$seriesCategory[$seriesIndex] = $this->chartDataSeriesValueSet($seriesDetail);
698+
$temp = $this->chartDataSeriesValueSet($seriesDetail);
699+
if ($temp !== null) {
700+
$seriesCategory[$seriesIndex] = $temp;
701+
}
693702

694703
break;
695704
case 'val':
696-
$seriesValues[$seriesIndex] = $this->chartDataSeriesValueSet($seriesDetail, "$marker", $fillColor, "$pointSize");
705+
$temp = $this->chartDataSeriesValueSet($seriesDetail, "$marker", $fillColor, "$pointSize");
706+
if ($temp !== null) {
707+
$seriesValues[$seriesIndex] = $temp;
708+
}
697709

698710
break;
699711
case 'xVal':
700-
$seriesCategory[$seriesIndex] = $this->chartDataSeriesValueSet($seriesDetail, "$marker", $fillColor, "$pointSize");
712+
$temp = $this->chartDataSeriesValueSet($seriesDetail, "$marker", $fillColor, "$pointSize");
713+
if ($temp !== null) {
714+
$seriesCategory[$seriesIndex] = $temp;
715+
}
701716

702717
break;
703718
case 'yVal':
704-
$seriesValues[$seriesIndex] = $this->chartDataSeriesValueSet($seriesDetail, "$marker", $fillColor, "$pointSize");
719+
$temp = $this->chartDataSeriesValueSet($seriesDetail, "$marker", $fillColor, "$pointSize");
720+
if ($temp !== null) {
721+
$seriesValues[$seriesIndex] = $temp;
722+
}
705723

706724
break;
707725
case 'bubbleSize':
708-
$seriesBubbles[$seriesIndex] = $this->chartDataSeriesValueSet($seriesDetail, "$marker", $fillColor, "$pointSize");
726+
$seriesBubble = $this->chartDataSeriesValueSet($seriesDetail, "$marker", $fillColor, "$pointSize");
727+
if ($seriesBubble !== null) {
728+
$seriesBubbles[$seriesIndex] = $seriesBubble;
729+
}
709730

710731
break;
711732
case 'bubble3D':
@@ -819,9 +840,7 @@ private function chartDataSeries(SimpleXMLElement $chartDetail, string $plotType
819840
}
820841
}
821842
}
822-
/** @phpstan-ignore-next-line */
823-
$series = new DataSeries($plotType, $multiSeriesType, $plotOrder, $seriesLabel, $seriesCategory, $seriesValues, $smoothLine);
824-
/** @phpstan-ignore-next-line */
843+
$series = new DataSeries($plotType, $multiSeriesType, $plotOrder, $seriesLabel, $seriesCategory, $seriesValues, $plotDirection, $smoothLine);
825844
$series->setPlotBubbleSizes($seriesBubbles);
826845

827846
return $series;

src/PhpSpreadsheet/Shared/Escher/DggContainer/BstoreContainer/BSE.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ class BSE
1919

2020
/**
2121
* The parent BLIP Store Entry Container.
22-
* Property is never currently read.
22+
* Property is currently unused.
2323
*/
24-
private BstoreContainer $parent; // @phpstan-ignore-line
24+
private BstoreContainer $parent;
2525

2626
/**
2727
* The BLIP (Big Large Image or Picture).
@@ -43,6 +43,11 @@ public function setParent(BstoreContainer $parent): void
4343
$this->parent = $parent;
4444
}
4545

46+
public function getParent(): BstoreContainer
47+
{
48+
return $this->parent;
49+
}
50+
4651
/**
4752
* Get the BLIP.
4853
*/

src/PhpSpreadsheet/Style/NumberFormat/NumberFormatter.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,12 @@ private static function pregReplace(string $pattern, string $replacement, string
265265

266266
public static function padValue(string $value, string $baseFormat): string
267267
{
268-
/** @phpstan-ignore-next-line */
269-
[$preDecimal, $postDecimal] = preg_split('/\.(?=(?:[^"]*"[^"]*")*[^"]*\Z)/miu', $baseFormat . '.?');
268+
$preDecimal = $postDecimal = '';
269+
$pregArray = preg_split('/\.(?=(?:[^"]*"[^"]*")*[^"]*\Z)/miu', $baseFormat . '.?');
270+
if (is_array($pregArray)) {
271+
$preDecimal = $pregArray[0] ?? '';
272+
$postDecimal = $pregArray[1] ?? '';
273+
}
270274

271275
$length = strlen($value);
272276
if (str_contains($postDecimal, '?')) {

src/PhpSpreadsheet/Worksheet/AutoFilter.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -1001,9 +1001,10 @@ public function showHideRows(): static
10011001
foreach ($columnFilterTests as $columnID => $columnFilterTest) {
10021002
$cellValue = $this->workSheet->getCell($columnID . $row)->getCalculatedValue();
10031003
// Execute the filter test
1004+
/** @var callable */
1005+
$temp = [self::class, $columnFilterTest['method']];
10041006
$result // $result && // phpstan says $result is always true here
1005-
// @phpstan-ignore-next-line
1006-
= call_user_func_array([self::class, $columnFilterTest['method']], [$cellValue, $columnFilterTest['arguments']]);
1007+
= call_user_func_array($temp, [$cellValue, $columnFilterTest['arguments']]);
10071008
// If filter test has resulted in FALSE, exit the loop straightaway rather than running any more tests
10081009
if (!$result) {
10091010
break;

src/PhpSpreadsheet/Worksheet/MemoryDrawing.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ private static function identifyMimeTypeUsingGd(string $temporaryFileName): ?str
217217
if (function_exists('getimagesize')) {
218218
$imageSize = @getimagesize($temporaryFileName);
219219
if (is_array($imageSize)) {
220-
$mimeType = $imageSize['mime'] ?? null; // @phpstan-ignore-line
220+
$mimeType = $imageSize['mime'];
221221

222222
return self::supportedMimeTypes($mimeType);
223223
}

src/PhpSpreadsheet/Worksheet/Table.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,8 @@ private function updateStructuredReferencesInNamedFormulae(Spreadsheet $spreadsh
194194
foreach ($spreadsheet->getNamedFormulae() as $namedFormula) {
195195
$formula = $namedFormula->getValue();
196196
if (preg_match($pattern, $formula) === 1) {
197-
$formula = preg_replace($pattern, "{$newName}[", $formula);
198-
$namedFormula->setValue($formula); // @phpstan-ignore-line
197+
$formula = preg_replace($pattern, "{$newName}[", $formula) ?? '';
198+
$namedFormula->setValue($formula);
199199
}
200200
}
201201
}

src/PhpSpreadsheet/Worksheet/Table/Column.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,8 @@ private static function updateStructuredReferencesInNamedFormulae(Spreadsheet $s
232232
foreach ($spreadsheet->getNamedFormulae() as $namedFormula) {
233233
$formula = $namedFormula->getValue();
234234
if (preg_match($pattern, $formula) === 1) {
235-
$formula = preg_replace($pattern, "[$1{$newTitle}]", $formula);
236-
$namedFormula->setValue($formula); // @phpstan-ignore-line
235+
$formula = preg_replace($pattern, "[$1{$newTitle}]", $formula) ?? '';
236+
$namedFormula->setValue($formula);
237237
}
238238
}
239239
}

src/PhpSpreadsheet/Writer/Ods/Meta.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ private static function writeDocPropsCustom(XMLWriter $objWriter, Spreadsheet $s
9696
case Properties::PROPERTY_TYPE_INTEGER:
9797
case Properties::PROPERTY_TYPE_FLOAT:
9898
$objWriter->writeAttribute('meta:value-type', 'float');
99-
$objWriter->writeRawData($propertyValue); // @phpstan-ignore-line
99+
$objWriter->writeRawData((string) $propertyValue);
100100

101101
break;
102102
case Properties::PROPERTY_TYPE_BOOLEAN:
@@ -106,12 +106,12 @@ private static function writeDocPropsCustom(XMLWriter $objWriter, Spreadsheet $s
106106
break;
107107
case Properties::PROPERTY_TYPE_DATE:
108108
$objWriter->writeAttribute('meta:value-type', 'date');
109-
$dtobj = Date::dateTimeFromTimestamp($propertyValue ?? 0); // @phpstan-ignore-line
109+
$dtobj = Date::dateTimeFromTimestamp((string) ($propertyValue ?? 0));
110110
$objWriter->writeRawData($dtobj->format(DATE_W3C));
111111

112112
break;
113113
default:
114-
$objWriter->writeRawData($propertyValue); // @phpstan-ignore-line
114+
$objWriter->writeRawData((string) $propertyValue);
115115

116116
break;
117117
}

0 commit comments

Comments
 (0)