From a01a4012281b650d1d5e76a3a134c989c6c28745 Mon Sep 17 00:00:00 2001 From: oleibman Date: Sat, 24 Apr 2021 09:12:17 -0700 Subject: [PATCH] MathTrig - Fix Phpstan Accomodations (#2020) * MathTrig - Fix Phpstan Accomodations This should be the last of my mass changes to MathTrig. All he Phpstan violations found in baseline which are part of MathTrig are now fixed and removed from baseline. There were about 20 of these. --- phpstan-baseline.neon | 140 ------------------ src/PhpSpreadsheet/Calculation/MathTrig.php | 14 +- .../Calculation/MathTrig/Arabic.php | 10 +- .../Calculation/MathTrig/Base.php | 2 +- .../Calculation/MathTrig/Helpers.php | 4 +- .../Calculation/MathTrig/Lcm.php | 2 +- .../Calculation/MathTrig/Roman.php | 11 +- .../Calculation/MathTrig/RoundDown.php | 2 +- .../Calculation/MathTrig/RoundUp.php | 2 +- .../Calculation/MathTrig/Subtotal.php | 20 ++- .../Calculation/MathTrig/Trunc.php | 2 +- .../Functions/MathTrig/SubTotalTest.php | 14 +- 12 files changed, 54 insertions(+), 169 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 2228974e..1b57a748 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -1230,136 +1230,6 @@ parameters: count: 1 path: src/PhpSpreadsheet/Calculation/LookupRef/VLookup.php - - - message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\MathTrig\\:\\:COMBIN\\(\\) should return int\\|string but returns float\\|int\\|string\\.$#" - count: 1 - path: src/PhpSpreadsheet/Calculation/MathTrig.php - - - - message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\MathTrig\\:\\:EVEN\\(\\) should return int\\|string but returns float\\|string\\.$#" - count: 1 - path: src/PhpSpreadsheet/Calculation/MathTrig.php - - - - message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\MathTrig\\:\\:FACT\\(\\) should return int\\|string but returns float\\|int\\|string\\.$#" - count: 1 - path: src/PhpSpreadsheet/Calculation/MathTrig.php - - - - message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\MathTrig\\:\\:FACTDOUBLE\\(\\) should return int\\|string but returns float\\|int\\|string\\.$#" - count: 1 - path: src/PhpSpreadsheet/Calculation/MathTrig.php - - - - message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\MathTrig\\:\\:MOD\\(\\) should return int\\|string but returns float\\|int\\|string\\.$#" - count: 1 - path: src/PhpSpreadsheet/Calculation/MathTrig.php - - - - message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\MathTrig\\:\\:ODD\\(\\) should return int\\|string but returns float\\|string\\.$#" - count: 1 - path: src/PhpSpreadsheet/Calculation/MathTrig.php - - - - message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\MathTrig\\:\\:SUMIFS\\(\\) should return float\\|string but returns float\\|string\\|null\\.$#" - count: 1 - path: src/PhpSpreadsheet/Calculation/MathTrig.php - - - - message: "#^Else branch is unreachable because ternary operator condition is always true\\.$#" - count: 1 - path: src/PhpSpreadsheet/Calculation/MathTrig/Arabic.php - - - - message: "#^Parameter \\#1 \\$number of function base_convert expects string, int\\<0, 9007199254740991\\> given\\.$#" - count: 1 - path: src/PhpSpreadsheet/Calculation/MathTrig/Base.php - - - - message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\MathTrig\\\\Helpers\\:\\:validateNumericNullBool\\(\\) should return float\\|int but returns float\\|int\\|string\\.$#" - count: 1 - path: src/PhpSpreadsheet/Calculation/MathTrig/Helpers.php - - - - message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\MathTrig\\\\Helpers\\:\\:validateNumericNullSubstitution\\(\\) should return float\\|int but returns float\\|int\\|string\\.$#" - count: 1 - path: src/PhpSpreadsheet/Calculation/MathTrig/Helpers.php - - - - message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\MathTrig\\\\Lcm\\:\\:factors\\(\\) has no return typehint specified\\.$#" - count: 1 - path: src/PhpSpreadsheet/Calculation/MathTrig/Lcm.php - - - - message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\MathTrig\\\\Lcm\\:\\:factors\\(\\) has parameter \\$value with no typehint specified\\.$#" - count: 1 - path: src/PhpSpreadsheet/Calculation/MathTrig/Lcm.php - - - - message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\MathTrig\\\\Roman\\:\\:romanCut\\(\\) has no return typehint specified\\.$#" - count: 1 - path: src/PhpSpreadsheet/Calculation/MathTrig/Roman.php - - - - message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\MathTrig\\\\Roman\\:\\:romanCut\\(\\) has parameter \\$n with no typehint specified\\.$#" - count: 1 - path: src/PhpSpreadsheet/Calculation/MathTrig/Roman.php - - - - message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\MathTrig\\\\Roman\\:\\:romanCut\\(\\) has parameter \\$num with no typehint specified\\.$#" - count: 1 - path: src/PhpSpreadsheet/Calculation/MathTrig/Roman.php - - - - message: "#^Parameter \\#2 \\$precision of function round expects int, float\\|int given\\.$#" - count: 2 - path: src/PhpSpreadsheet/Calculation/MathTrig/RoundDown.php - - - - message: "#^Parameter \\#2 \\$precision of function round expects int, float\\|int given\\.$#" - count: 2 - path: src/PhpSpreadsheet/Calculation/MathTrig/RoundUp.php - - - - message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\MathTrig\\\\Subtotal\\:\\:filterHiddenArgs\\(\\) has no return typehint specified\\.$#" - count: 1 - path: src/PhpSpreadsheet/Calculation/MathTrig/Subtotal.php - - - - message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\MathTrig\\\\Subtotal\\:\\:filterHiddenArgs\\(\\) has parameter \\$args with no typehint specified\\.$#" - count: 1 - path: src/PhpSpreadsheet/Calculation/MathTrig/Subtotal.php - - - - message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\MathTrig\\\\Subtotal\\:\\:filterHiddenArgs\\(\\) has parameter \\$cellReference with no typehint specified\\.$#" - count: 1 - path: src/PhpSpreadsheet/Calculation/MathTrig/Subtotal.php - - - - message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\MathTrig\\\\Subtotal\\:\\:filterFormulaArgs\\(\\) has no return typehint specified\\.$#" - count: 1 - path: src/PhpSpreadsheet/Calculation/MathTrig/Subtotal.php - - - - message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\MathTrig\\\\Subtotal\\:\\:filterFormulaArgs\\(\\) has parameter \\$args with no typehint specified\\.$#" - count: 1 - path: src/PhpSpreadsheet/Calculation/MathTrig/Subtotal.php - - - - message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\MathTrig\\\\Subtotal\\:\\:filterFormulaArgs\\(\\) has parameter \\$cellReference with no typehint specified\\.$#" - count: 1 - path: src/PhpSpreadsheet/Calculation/MathTrig/Subtotal.php - - - - message: "#^Parameter \\#1 \\$function of function call_user_func_array expects callable\\(\\)\\: mixed, array\\('PhpOffice…'\\|'PhpOffice…'\\|'PhpOffice…'\\|'PhpOffice…'\\|'PhpOffice…'\\|'PhpOffice…'\\|'PhpOffice…'\\|'PhpOffice…', string\\) given\\.$#" - count: 1 - path: src/PhpSpreadsheet/Calculation/MathTrig/Subtotal.php - - - - message: "#^Parameter \\#1 \\$str of function rtrim expects string, int given\\.$#" - count: 1 - path: src/PhpSpreadsheet/Calculation/MathTrig/Trunc.php - - message: "#^Binary operation \"\\-\" between float\\|int and float\\|string results in an error\\.$#" count: 4 @@ -8095,16 +7965,6 @@ parameters: count: 1 path: tests/PhpSpreadsheetTests/Calculation/Engine/RangeTest.php - - - message: "#^Cannot call method setVisible\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Worksheet\\\\ColumnDimension\\|null\\.$#" - count: 1 - path: tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/SubTotalTest.php - - - - message: "#^Cannot call method setVisible\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Worksheet\\\\RowDimension\\|null\\.$#" - count: 1 - path: tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/SubTotalTest.php - - message: "#^Cannot call method setAutoSize\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Worksheet\\\\ColumnDimension\\|null\\.$#" count: 1 diff --git a/src/PhpSpreadsheet/Calculation/MathTrig.php b/src/PhpSpreadsheet/Calculation/MathTrig.php index 121efde5..8398ba13 100644 --- a/src/PhpSpreadsheet/Calculation/MathTrig.php +++ b/src/PhpSpreadsheet/Calculation/MathTrig.php @@ -124,7 +124,7 @@ class MathTrig * @param int $numObjs Number of different objects * @param int $numInSet Number of objects in each combination * - * @return int|string Number of combinations, or a string containing an error + * @return float|int|string Number of combinations, or a string containing an error */ public static function COMBIN($numObjs, $numInSet) { @@ -150,7 +150,7 @@ class MathTrig * * @param float $number Number to round * - * @return int|string Rounded Number, or a string containing an error + * @return float|int|string Rounded Number, or a string containing an error */ public static function EVEN($number) { @@ -186,7 +186,7 @@ class MathTrig * * @param float $factVal Factorial Value * - * @return int|string Factorial, or a string containing an error + * @return float|int|string Factorial, or a string containing an error */ public static function FACT($factVal) { @@ -208,7 +208,7 @@ class MathTrig * * @param float $factVal Factorial Value * - * @return int|string Double Factorial, or a string containing an error + * @return float|int|string Double Factorial, or a string containing an error */ public static function FACTDOUBLE($factVal) { @@ -452,7 +452,7 @@ class MathTrig * @param int $a Dividend * @param int $b Divisor * - * @return int|string Remainder, or a string containing an error + * @return float|int|string Remainder, or a string containing an error */ public static function MOD($a = 1, $b = 1) { @@ -510,7 +510,7 @@ class MathTrig * * @param float $number Number to round * - * @return int|string Rounded Number, or a string containing an error + * @return float|int|string Rounded Number, or a string containing an error */ public static function ODD($number) { @@ -822,7 +822,7 @@ class MathTrig * * @param mixed $args Data values * - * @return float|string + * @return null|float|string */ public static function SUMIFS(...$args) { diff --git a/src/PhpSpreadsheet/Calculation/MathTrig/Arabic.php b/src/PhpSpreadsheet/Calculation/MathTrig/Arabic.php index 320856b9..b3d8c330 100644 --- a/src/PhpSpreadsheet/Calculation/MathTrig/Arabic.php +++ b/src/PhpSpreadsheet/Calculation/MathTrig/Arabic.php @@ -47,11 +47,19 @@ class Arabic return $sum; } + /** + * @param mixed $value + */ + private static function mollifyScrutinizer($value): array + { + return is_array($value) ? $value : []; + } + private static function strSplit(string $roman): array { $rslt = str_split($roman); - return is_array($rslt) ? $rslt : []; + return self::mollifyScrutinizer($rslt); } /** diff --git a/src/PhpSpreadsheet/Calculation/MathTrig/Base.php b/src/PhpSpreadsheet/Calculation/MathTrig/Base.php index 6f44a122..373bcf08 100644 --- a/src/PhpSpreadsheet/Calculation/MathTrig/Base.php +++ b/src/PhpSpreadsheet/Calculation/MathTrig/Base.php @@ -36,7 +36,7 @@ class Base return Functions::NAN(); // Numeric range constraints } - $outcome = strtoupper((string) base_convert($number, 10, $radix)); + $outcome = strtoupper((string) base_convert((string) $number, 10, $radix)); if ($minLength !== null) { $outcome = str_pad($outcome, (int) $minLength, '0', STR_PAD_LEFT); // String padding } diff --git a/src/PhpSpreadsheet/Calculation/MathTrig/Helpers.php b/src/PhpSpreadsheet/Calculation/MathTrig/Helpers.php index 26716467..b89644a9 100644 --- a/src/PhpSpreadsheet/Calculation/MathTrig/Helpers.php +++ b/src/PhpSpreadsheet/Calculation/MathTrig/Helpers.php @@ -34,7 +34,7 @@ class Helpers return (int) $number; } if (is_numeric($number)) { - return $number; + return 0 + $number; } throw new Exception(Functions::VALUE()); @@ -55,7 +55,7 @@ class Helpers return $substitute; } if (is_numeric($number)) { - return $number; + return 0 + $number; } throw new Exception(Functions::VALUE()); diff --git a/src/PhpSpreadsheet/Calculation/MathTrig/Lcm.php b/src/PhpSpreadsheet/Calculation/MathTrig/Lcm.php index 5c28684f..cc1e16bc 100644 --- a/src/PhpSpreadsheet/Calculation/MathTrig/Lcm.php +++ b/src/PhpSpreadsheet/Calculation/MathTrig/Lcm.php @@ -10,7 +10,7 @@ class Lcm // // Private method to return an array of the factors of the input value // - private static function factors($value) + private static function factors(float $value): array { $startVal = floor(sqrt($value)); diff --git a/src/PhpSpreadsheet/Calculation/MathTrig/Roman.php b/src/PhpSpreadsheet/Calculation/MathTrig/Roman.php index 0ee7a919..865a5101 100644 --- a/src/PhpSpreadsheet/Calculation/MathTrig/Roman.php +++ b/src/PhpSpreadsheet/Calculation/MathTrig/Roman.php @@ -776,19 +776,14 @@ class Roman const MAX_ROMAN_VALUE = 3999; const MAX_ROMAN_STYLE = 4; - private static function romanCut($num, $n) - { - return ($num - ($num % $n)) / $n; - } - private static function valueOk(int $aValue, int $style): string { $origValue = $aValue; - $m = self::romanCut($aValue, 1000); + $m = \intdiv($aValue, 1000); $aValue %= 1000; - $c = self::romanCut($aValue, 100); + $c = \intdiv($aValue, 100); $aValue %= 100; - $t = self::romanCut($aValue, 10); + $t = \intdiv($aValue, 10); $aValue %= 10; $result = self::THOUSANDS[$m] . self::HUNDREDS[$c] . self::TENS[$t] . self::ONES[$aValue]; if ($style > 0) { diff --git a/src/PhpSpreadsheet/Calculation/MathTrig/RoundDown.php b/src/PhpSpreadsheet/Calculation/MathTrig/RoundDown.php index e843a6f9..0054a227 100644 --- a/src/PhpSpreadsheet/Calculation/MathTrig/RoundDown.php +++ b/src/PhpSpreadsheet/Calculation/MathTrig/RoundDown.php @@ -20,7 +20,7 @@ class RoundDown { try { $number = Helpers::validateNumericNullBool($number); - $digits = Helpers::validateNumericNullSubstitution($digits, null); + $digits = (int) Helpers::validateNumericNullSubstitution($digits, null); } catch (Exception $e) { return $e->getMessage(); } diff --git a/src/PhpSpreadsheet/Calculation/MathTrig/RoundUp.php b/src/PhpSpreadsheet/Calculation/MathTrig/RoundUp.php index c6704ad2..6b2e6d1a 100644 --- a/src/PhpSpreadsheet/Calculation/MathTrig/RoundUp.php +++ b/src/PhpSpreadsheet/Calculation/MathTrig/RoundUp.php @@ -20,7 +20,7 @@ class RoundUp { try { $number = Helpers::validateNumericNullBool($number); - $digits = Helpers::validateNumericNullSubstitution($digits, null); + $digits = (int) Helpers::validateNumericNullSubstitution($digits, null); } catch (Exception $e) { return $e->getMessage(); } diff --git a/src/PhpSpreadsheet/Calculation/MathTrig/Subtotal.php b/src/PhpSpreadsheet/Calculation/MathTrig/Subtotal.php index 5477d936..0700a5d8 100644 --- a/src/PhpSpreadsheet/Calculation/MathTrig/Subtotal.php +++ b/src/PhpSpreadsheet/Calculation/MathTrig/Subtotal.php @@ -8,7 +8,11 @@ use PhpOffice\PhpSpreadsheet\Calculation\Statistical; class Subtotal { - protected static function filterHiddenArgs($cellReference, $args) + /** + * @param mixed $cellReference + * @param mixed $args + */ + protected static function filterHiddenArgs($cellReference, $args): array { return array_filter( $args, @@ -21,7 +25,11 @@ class Subtotal ); } - protected static function filterFormulaArgs($cellReference, $args) + /** + * @param mixed $cellReference + * @param mixed $args + */ + protected static function filterFormulaArgs($cellReference, $args): array { return array_filter( $args, @@ -42,6 +50,7 @@ class Subtotal ); } + /** @var callable[] */ private const CALL_FUNCTIONS = [ 1 => [Statistical\Averages::class, 'AVERAGE'], [Statistical\Counts::class, 'COUNT'], // 2 @@ -67,7 +76,7 @@ class Subtotal * list * Numbers 101 to 111 shadow the functions of 1 to 11 * but ignore any values in the range that are - * in hidden rows or columns + * in hidden rows * @param mixed[] $args A mixed data series of values * * @return float|string @@ -91,7 +100,10 @@ class Subtotal $aArgs = self::filterFormulaArgs($cellReference, $aArgs); if (array_key_exists($subtotal, self::CALL_FUNCTIONS)) { - return call_user_func_array(self::CALL_FUNCTIONS[$subtotal], $aArgs); + /** @var callable */ + $call = self::CALL_FUNCTIONS[$subtotal]; + + return call_user_func_array($call, $aArgs); } return Functions::VALUE(); diff --git a/src/PhpSpreadsheet/Calculation/MathTrig/Trunc.php b/src/PhpSpreadsheet/Calculation/MathTrig/Trunc.php index 57c389e6..0a1c827e 100644 --- a/src/PhpSpreadsheet/Calculation/MathTrig/Trunc.php +++ b/src/PhpSpreadsheet/Calculation/MathTrig/Trunc.php @@ -30,7 +30,7 @@ class Trunc // Truncate $adjust = 10 ** $digits; - if (($digits > 0) && (rtrim((int) ((abs($value) - abs((int) $value)) * $adjust), '0') < $adjust / 10)) { + if (($digits > 0) && (rtrim((string) (int) ((abs($value) - abs((int) $value)) * $adjust), '0') < $adjust / 10)) { return $value; } diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/SubTotalTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/SubTotalTest.php index fde1d16e..be63f694 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/SubTotalTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/SubTotalTest.php @@ -56,7 +56,12 @@ class SubTotalTest extends AllSetupTeardown 'L' => false, ]; foreach ($hiddenColumns as $col => $hidden) { - $sheet->getColumnDimension($col)->setVisible($hidden); + $colDim = $sheet->getColumnDimension($col); + if ($colDim === null) { + self::fail('Unexpected null column dimension'); + } else { + $colDim->setVisible($hidden); + } } $sheet->getCell('D2')->setValue("=SUBTOTAL($type, A1:$maxCol$maxRow)"); $result = $sheet->getCell('D2')->getCalculatedValue(); @@ -91,7 +96,12 @@ class SubTotalTest extends AllSetupTeardown '12' => false, ]; foreach ($visibleRows as $row => $visible) { - $sheet->getRowDimension($row)->setVisible($visible); + $rowDim = $sheet->getRowDimension($row); + if ($rowDim === null) { + self::fail('Unexpected null row dimension'); + } else { + $rowDim->setVisible($visible); + } } $sheet->getCell('D2')->setValue("=SUBTOTAL($type, A1:$maxCol$maxRow)"); $result = $sheet->getCell('D2')->getCalculatedValue();