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

Lines changed: 0 additions & 4 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 16 additions & 4 deletions
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

Lines changed: 3 additions & 2 deletions
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

Lines changed: 4 additions & 4 deletions
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

Lines changed: 3 additions & 2 deletions
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

Lines changed: 3 additions & 1 deletion
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 0 deletions
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
}

0 commit comments

Comments
 (0)