Skip to content

Commit

Permalink
Catch Phpstan Up
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
oleibman committed Nov 12, 2024
1 parent 3e52499 commit 1419feb
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 21 deletions.
10 changes: 5 additions & 5 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions src/PhpSpreadsheet/Calculation/Calculation.php
Original file line number Diff line number Diff line change
Expand Up @@ -4149,9 +4149,9 @@ private function internalParseFormula(string $formula, ?Cell $cell = null): bool
$expectedArgumentCountString = null;
if (is_numeric($expectedArgumentCount)) {
if ($expectedArgumentCount < 0) {
if ($argumentCount > abs($expectedArgumentCount)) {
if ($argumentCount > abs($expectedArgumentCount + 0)) {
$argumentCountError = true;
$expectedArgumentCountString = 'no more than ' . abs($expectedArgumentCount);
$expectedArgumentCountString = 'no more than ' . abs($expectedArgumentCount + 0);
}
} else {
if ($argumentCount != $expectedArgumentCount) {
Expand Down Expand Up @@ -4236,7 +4236,7 @@ private function internalParseFormula(string $formula, ?Cell $cell = null): bool
// do we now have a function/variable/number?
$expectingOperator = true;
$expectingOperand = false;
$val = $match[1] ?? '';
$val = $match[1] ?? ''; //* @phpstan-ignore-line
$length = strlen($val);

if (preg_match('/^' . self::CALCULATION_REGEXP_FUNCTION . '$/miu', $val, $matches)) {
Expand Down
2 changes: 1 addition & 1 deletion src/PhpSpreadsheet/Calculation/Engineering/BitWise.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ private static function validateShiftAmount(mixed $value): int
$value = self::nullFalseTrueToNumber($value);

if (is_numeric($value)) {
if (abs($value) > 53) {
if (abs($value + 0) > 53) {
throw new Exception(ExcelError::NAN());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ private static function xirrBisection(array $values, array $dates, float $x1, fl
return $rslt;
}

/** @param array<int,float|int|numeric-string> $values> */
private static function xnpvOrdered(mixed $rate, mixed $values, mixed $dates, bool $ordered = true, bool $capAtNegative1 = false): float|string
{
$rate = Functions::flattenSingleValue($rate);
Expand Down Expand Up @@ -276,7 +277,7 @@ private static function xnpvOrdered(mixed $rate, mixed $values, mixed $dates, bo
return $dif;
}
if ($rate <= -1.0) {
$xnpv += -abs($values[$i]) / (-1 - $rate) ** ($dif / 365);
$xnpv += -abs($values[$i] + 0) / (-1 - $rate) ** ($dif / 365);
} else {
$xnpv += $values[$i] / (1 + $rate) ** ($dif / 365);
}
Expand Down
2 changes: 1 addition & 1 deletion src/PhpSpreadsheet/Reader/Xls.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ class Xls extends XlsBase
* The current MD5 context state.
* It is never set in the program, so code which uses it is suspect.
*/
private string $md5Ctxt; // @phpstan-ignore-line
private string $md5Ctxt = '';

protected int $textObjRef;

Expand Down
2 changes: 1 addition & 1 deletion src/PhpSpreadsheet/Shared/OLE/ChainedBlockStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function stream_open(string $path, string $mode, int $options, ?string &$
}
}
if (isset($this->params['size'])) {
$this->data = substr($this->data, 0, $this->params['size']);
$this->data = substr($this->data, 0, $this->params['size']); //* @phpstan-ignore-line
}

if ($options & STREAM_USE_PATH) {
Expand Down
2 changes: 1 addition & 1 deletion src/PhpSpreadsheet/Shared/Trend/PolynomialBestFit.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ private function polynomialRegression(int $order, array $yValues, array $xValues
$coefficients = [];
for ($i = 0; $i < $C->rows; ++$i) {
$r = $C->getValue($i + 1, 1); // row and column are origin-1
if (!is_numeric($r) || abs($r) <= 10 ** (-9)) {
if (!is_numeric($r) || abs($r + 0) <= 10 ** (-9)) {
$r = 0;
} else {
$r += 0;
Expand Down
6 changes: 3 additions & 3 deletions src/PhpSpreadsheet/Style/NumberFormat/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ private static function splitFormatComparison(
};
}

/** @param float|int|string $value value to be formatted */
/** @param float|int|numeric-string $value value to be formatted */
private static function splitFormatForSectionSelection(array $sections, mixed $value): array
{
// Extract the relevant section depending on whether number is positive, negative, or zero?
Expand Down Expand Up @@ -79,7 +79,7 @@ private static function splitFormatForSectionSelection(array $sections, mixed $v
$absval = $value;
switch ($sectionCount) {
case 2:
$absval = abs($value);
$absval = abs($value + 0);
if (!self::splitFormatComparison($value, $conditionOperations[0], $conditionComparisonValues[0], '>=', 0)) {
$color = $colors[1];
$format = $sections[1];
Expand All @@ -88,7 +88,7 @@ private static function splitFormatForSectionSelection(array $sections, mixed $v
break;
case 3:
case 4:
$absval = abs($value);
$absval = abs($value + 0);
if (!self::splitFormatComparison($value, $conditionOperations[0], $conditionComparisonValues[0], '>', 0)) {
if (self::splitFormatComparison($value, $conditionOperations[1], $conditionComparisonValues[1], '<', 0)) {
$color = $colors[1];
Expand Down
13 changes: 8 additions & 5 deletions src/PhpSpreadsheet/Writer/Xls/Worksheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -2362,7 +2362,7 @@ private function writeObjPicture(int $colL, int $dxL, int $rwT, int|float $dyT,
*
* @param GdImage $image The image to process
*
* @return array Array with data and properties of the bitmap
* @return array{0: float, 1: float, 2: int, 3: string} Data and properties of the bitmap
*/
public function processBitmapGd(GdImage $image): array
{
Expand All @@ -2372,9 +2372,9 @@ public function processBitmapGd(GdImage $image): array
$data = pack('Vvvvv', 0x000C, $width, $height, 0x01, 0x18);
for ($j = $height; --$j;) {
for ($i = 0; $i < $width; ++$i) {
/** @phpstan-ignore-next-line */
$color = imagecolorsforindex($image, imagecolorat($image, $i, $j));
if ($color !== false) {
$colorAt = imagecolorat($image, $i, $j);
if ($colorAt !== false) {
$color = imagecolorsforindex($image, $colorAt);
foreach (['red', 'green', 'blue'] as $key) {
$color[$key] = $color[$key] + (int) round((255 - $color[$key]) * $color['alpha'] / 127);
}
Expand All @@ -2385,8 +2385,11 @@ public function processBitmapGd(GdImage $image): array
$data .= str_repeat("\x00", 4 - 3 * $width % 4);
}
}
// Phpstan says this always throws an exception before getting here.
// I don't see why, but I think this is code is never exercised
// in unit tests, so I can't say for sure it's wrong.

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

/**
Expand Down

0 comments on commit 1419feb

Please sign in to comment.