Skip to content

Commit 1419feb

Browse files
committed
Catch Phpstan Up
Its last few updates have been irksome. I need to eliminate more of the new problems with annotations rather than code because I can't figure out what it's objecting to. Nevertheless, it provides a very valuable service, so ... Its latest release is flagging calling abs with a string, even when the string is defined as numeric-string in a doc block. Php generally allows it, though not with strict types. Changed to add 0 in those situations.
1 parent 3e52499 commit 1419feb

File tree

9 files changed

+25
-21
lines changed

9 files changed

+25
-21
lines changed

composer.lock

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/PhpSpreadsheet/Calculation/Calculation.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4149,9 +4149,9 @@ private function internalParseFormula(string $formula, ?Cell $cell = null): bool
41494149
$expectedArgumentCountString = null;
41504150
if (is_numeric($expectedArgumentCount)) {
41514151
if ($expectedArgumentCount < 0) {
4152-
if ($argumentCount > abs($expectedArgumentCount)) {
4152+
if ($argumentCount > abs($expectedArgumentCount + 0)) {
41534153
$argumentCountError = true;
4154-
$expectedArgumentCountString = 'no more than ' . abs($expectedArgumentCount);
4154+
$expectedArgumentCountString = 'no more than ' . abs($expectedArgumentCount + 0);
41554155
}
41564156
} else {
41574157
if ($argumentCount != $expectedArgumentCount) {
@@ -4236,7 +4236,7 @@ private function internalParseFormula(string $formula, ?Cell $cell = null): bool
42364236
// do we now have a function/variable/number?
42374237
$expectingOperator = true;
42384238
$expectingOperand = false;
4239-
$val = $match[1] ?? '';
4239+
$val = $match[1] ?? ''; //* @phpstan-ignore-line
42404240
$length = strlen($val);
42414241

42424242
if (preg_match('/^' . self::CALCULATION_REGEXP_FUNCTION . '$/miu', $val, $matches)) {

src/PhpSpreadsheet/Calculation/Engineering/BitWise.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ private static function validateShiftAmount(mixed $value): int
221221
$value = self::nullFalseTrueToNumber($value);
222222

223223
if (is_numeric($value)) {
224-
if (abs($value) > 53) {
224+
if (abs($value + 0) > 53) {
225225
throw new Exception(ExcelError::NAN());
226226
}
227227

src/PhpSpreadsheet/Calculation/Financial/CashFlow/Variable/NonPeriodic.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ private static function xirrBisection(array $values, array $dates, float $x1, fl
239239
return $rslt;
240240
}
241241

242+
/** @param array<int,float|int|numeric-string> $values> */
242243
private static function xnpvOrdered(mixed $rate, mixed $values, mixed $dates, bool $ordered = true, bool $capAtNegative1 = false): float|string
243244
{
244245
$rate = Functions::flattenSingleValue($rate);
@@ -276,7 +277,7 @@ private static function xnpvOrdered(mixed $rate, mixed $values, mixed $dates, bo
276277
return $dif;
277278
}
278279
if ($rate <= -1.0) {
279-
$xnpv += -abs($values[$i]) / (-1 - $rate) ** ($dif / 365);
280+
$xnpv += -abs($values[$i] + 0) / (-1 - $rate) ** ($dif / 365);
280281
} else {
281282
$xnpv += $values[$i] / (1 + $rate) ** ($dif / 365);
282283
}

src/PhpSpreadsheet/Reader/Xls.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ class Xls extends XlsBase
234234
* The current MD5 context state.
235235
* It is never set in the program, so code which uses it is suspect.
236236
*/
237-
private string $md5Ctxt; // @phpstan-ignore-line
237+
private string $md5Ctxt = '';
238238

239239
protected int $textObjRef;
240240

src/PhpSpreadsheet/Shared/OLE/ChainedBlockStream.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public function stream_open(string $path, string $mode, int $options, ?string &$
8585
}
8686
}
8787
if (isset($this->params['size'])) {
88-
$this->data = substr($this->data, 0, $this->params['size']);
88+
$this->data = substr($this->data, 0, $this->params['size']); //* @phpstan-ignore-line
8989
}
9090

9191
if ($options & STREAM_USE_PATH) {

src/PhpSpreadsheet/Shared/Trend/PolynomialBestFit.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ private function polynomialRegression(int $order, array $yValues, array $xValues
159159
$coefficients = [];
160160
for ($i = 0; $i < $C->rows; ++$i) {
161161
$r = $C->getValue($i + 1, 1); // row and column are origin-1
162-
if (!is_numeric($r) || abs($r) <= 10 ** (-9)) {
162+
if (!is_numeric($r) || abs($r + 0) <= 10 ** (-9)) {
163163
$r = 0;
164164
} else {
165165
$r += 0;

src/PhpSpreadsheet/Style/NumberFormat/Formatter.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ private static function splitFormatComparison(
4242
};
4343
}
4444

45-
/** @param float|int|string $value value to be formatted */
45+
/** @param float|int|numeric-string $value value to be formatted */
4646
private static function splitFormatForSectionSelection(array $sections, mixed $value): array
4747
{
4848
// Extract the relevant section depending on whether number is positive, negative, or zero?
@@ -79,7 +79,7 @@ private static function splitFormatForSectionSelection(array $sections, mixed $v
7979
$absval = $value;
8080
switch ($sectionCount) {
8181
case 2:
82-
$absval = abs($value);
82+
$absval = abs($value + 0);
8383
if (!self::splitFormatComparison($value, $conditionOperations[0], $conditionComparisonValues[0], '>=', 0)) {
8484
$color = $colors[1];
8585
$format = $sections[1];
@@ -88,7 +88,7 @@ private static function splitFormatForSectionSelection(array $sections, mixed $v
8888
break;
8989
case 3:
9090
case 4:
91-
$absval = abs($value);
91+
$absval = abs($value + 0);
9292
if (!self::splitFormatComparison($value, $conditionOperations[0], $conditionComparisonValues[0], '>', 0)) {
9393
if (self::splitFormatComparison($value, $conditionOperations[1], $conditionComparisonValues[1], '<', 0)) {
9494
$color = $colors[1];

src/PhpSpreadsheet/Writer/Xls/Worksheet.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2362,7 +2362,7 @@ private function writeObjPicture(int $colL, int $dxL, int $rwT, int|float $dyT,
23622362
*
23632363
* @param GdImage $image The image to process
23642364
*
2365-
* @return array Array with data and properties of the bitmap
2365+
* @return array{0: float, 1: float, 2: int, 3: string} Data and properties of the bitmap
23662366
*/
23672367
public function processBitmapGd(GdImage $image): array
23682368
{
@@ -2372,9 +2372,9 @@ public function processBitmapGd(GdImage $image): array
23722372
$data = pack('Vvvvv', 0x000C, $width, $height, 0x01, 0x18);
23732373
for ($j = $height; --$j;) {
23742374
for ($i = 0; $i < $width; ++$i) {
2375-
/** @phpstan-ignore-next-line */
2376-
$color = imagecolorsforindex($image, imagecolorat($image, $i, $j));
2377-
if ($color !== false) {
2375+
$colorAt = imagecolorat($image, $i, $j);
2376+
if ($colorAt !== false) {
2377+
$color = imagecolorsforindex($image, $colorAt);
23782378
foreach (['red', 'green', 'blue'] as $key) {
23792379
$color[$key] = $color[$key] + (int) round((255 - $color[$key]) * $color['alpha'] / 127);
23802380
}
@@ -2385,8 +2385,11 @@ public function processBitmapGd(GdImage $image): array
23852385
$data .= str_repeat("\x00", 4 - 3 * $width % 4);
23862386
}
23872387
}
2388+
// Phpstan says this always throws an exception before getting here.
2389+
// I don't see why, but I think this is code is never exercised
2390+
// in unit tests, so I can't say for sure it's wrong.
23882391

2389-
return [$width, $height, strlen($data), $data];
2392+
return [$width, $height, strlen($data), $data]; //* @phpstan-ignore-line
23902393
}
23912394

23922395
/**

0 commit comments

Comments
 (0)