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.
This commit is contained in:
oleibman 2021-04-24 09:12:17 -07:00 committed by GitHub
parent 557601f584
commit a01a401228
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 54 additions and 169 deletions

View File

@ -1230,136 +1230,6 @@ parameters:
count: 1 count: 1
path: src/PhpSpreadsheet/Calculation/LookupRef/VLookup.php 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\\.$#" message: "#^Binary operation \"\\-\" between float\\|int and float\\|string results in an error\\.$#"
count: 4 count: 4
@ -8095,16 +7965,6 @@ parameters:
count: 1 count: 1
path: tests/PhpSpreadsheetTests/Calculation/Engine/RangeTest.php 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\\.$#" message: "#^Cannot call method setAutoSize\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Worksheet\\\\ColumnDimension\\|null\\.$#"
count: 1 count: 1

View File

@ -124,7 +124,7 @@ class MathTrig
* @param int $numObjs Number of different objects * @param int $numObjs Number of different objects
* @param int $numInSet Number of objects in each combination * @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) public static function COMBIN($numObjs, $numInSet)
{ {
@ -150,7 +150,7 @@ class MathTrig
* *
* @param float $number Number to round * @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) public static function EVEN($number)
{ {
@ -186,7 +186,7 @@ class MathTrig
* *
* @param float $factVal Factorial Value * @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) public static function FACT($factVal)
{ {
@ -208,7 +208,7 @@ class MathTrig
* *
* @param float $factVal Factorial Value * @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) public static function FACTDOUBLE($factVal)
{ {
@ -452,7 +452,7 @@ class MathTrig
* @param int $a Dividend * @param int $a Dividend
* @param int $b Divisor * @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) public static function MOD($a = 1, $b = 1)
{ {
@ -510,7 +510,7 @@ class MathTrig
* *
* @param float $number Number to round * @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) public static function ODD($number)
{ {
@ -822,7 +822,7 @@ class MathTrig
* *
* @param mixed $args Data values * @param mixed $args Data values
* *
* @return float|string * @return null|float|string
*/ */
public static function SUMIFS(...$args) public static function SUMIFS(...$args)
{ {

View File

@ -47,11 +47,19 @@ class Arabic
return $sum; return $sum;
} }
/**
* @param mixed $value
*/
private static function mollifyScrutinizer($value): array
{
return is_array($value) ? $value : [];
}
private static function strSplit(string $roman): array private static function strSplit(string $roman): array
{ {
$rslt = str_split($roman); $rslt = str_split($roman);
return is_array($rslt) ? $rslt : []; return self::mollifyScrutinizer($rslt);
} }
/** /**

View File

@ -36,7 +36,7 @@ class Base
return Functions::NAN(); // Numeric range constraints 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) { if ($minLength !== null) {
$outcome = str_pad($outcome, (int) $minLength, '0', STR_PAD_LEFT); // String padding $outcome = str_pad($outcome, (int) $minLength, '0', STR_PAD_LEFT); // String padding
} }

View File

@ -34,7 +34,7 @@ class Helpers
return (int) $number; return (int) $number;
} }
if (is_numeric($number)) { if (is_numeric($number)) {
return $number; return 0 + $number;
} }
throw new Exception(Functions::VALUE()); throw new Exception(Functions::VALUE());
@ -55,7 +55,7 @@ class Helpers
return $substitute; return $substitute;
} }
if (is_numeric($number)) { if (is_numeric($number)) {
return $number; return 0 + $number;
} }
throw new Exception(Functions::VALUE()); throw new Exception(Functions::VALUE());

View File

@ -10,7 +10,7 @@ class Lcm
// //
// Private method to return an array of the factors of the input value // 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)); $startVal = floor(sqrt($value));

View File

@ -776,19 +776,14 @@ class Roman
const MAX_ROMAN_VALUE = 3999; const MAX_ROMAN_VALUE = 3999;
const MAX_ROMAN_STYLE = 4; 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 private static function valueOk(int $aValue, int $style): string
{ {
$origValue = $aValue; $origValue = $aValue;
$m = self::romanCut($aValue, 1000); $m = \intdiv($aValue, 1000);
$aValue %= 1000; $aValue %= 1000;
$c = self::romanCut($aValue, 100); $c = \intdiv($aValue, 100);
$aValue %= 100; $aValue %= 100;
$t = self::romanCut($aValue, 10); $t = \intdiv($aValue, 10);
$aValue %= 10; $aValue %= 10;
$result = self::THOUSANDS[$m] . self::HUNDREDS[$c] . self::TENS[$t] . self::ONES[$aValue]; $result = self::THOUSANDS[$m] . self::HUNDREDS[$c] . self::TENS[$t] . self::ONES[$aValue];
if ($style > 0) { if ($style > 0) {

View File

@ -20,7 +20,7 @@ class RoundDown
{ {
try { try {
$number = Helpers::validateNumericNullBool($number); $number = Helpers::validateNumericNullBool($number);
$digits = Helpers::validateNumericNullSubstitution($digits, null); $digits = (int) Helpers::validateNumericNullSubstitution($digits, null);
} catch (Exception $e) { } catch (Exception $e) {
return $e->getMessage(); return $e->getMessage();
} }

View File

@ -20,7 +20,7 @@ class RoundUp
{ {
try { try {
$number = Helpers::validateNumericNullBool($number); $number = Helpers::validateNumericNullBool($number);
$digits = Helpers::validateNumericNullSubstitution($digits, null); $digits = (int) Helpers::validateNumericNullSubstitution($digits, null);
} catch (Exception $e) { } catch (Exception $e) {
return $e->getMessage(); return $e->getMessage();
} }

View File

@ -8,7 +8,11 @@ use PhpOffice\PhpSpreadsheet\Calculation\Statistical;
class Subtotal class Subtotal
{ {
protected static function filterHiddenArgs($cellReference, $args) /**
* @param mixed $cellReference
* @param mixed $args
*/
protected static function filterHiddenArgs($cellReference, $args): array
{ {
return array_filter( return array_filter(
$args, $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( return array_filter(
$args, $args,
@ -42,6 +50,7 @@ class Subtotal
); );
} }
/** @var callable[] */
private const CALL_FUNCTIONS = [ private const CALL_FUNCTIONS = [
1 => [Statistical\Averages::class, 'AVERAGE'], 1 => [Statistical\Averages::class, 'AVERAGE'],
[Statistical\Counts::class, 'COUNT'], // 2 [Statistical\Counts::class, 'COUNT'], // 2
@ -67,7 +76,7 @@ class Subtotal
* list * list
* Numbers 101 to 111 shadow the functions of 1 to 11 * Numbers 101 to 111 shadow the functions of 1 to 11
* but ignore any values in the range that are * 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 * @param mixed[] $args A mixed data series of values
* *
* @return float|string * @return float|string
@ -91,7 +100,10 @@ class Subtotal
$aArgs = self::filterFormulaArgs($cellReference, $aArgs); $aArgs = self::filterFormulaArgs($cellReference, $aArgs);
if (array_key_exists($subtotal, self::CALL_FUNCTIONS)) { 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(); return Functions::VALUE();

View File

@ -30,7 +30,7 @@ class Trunc
// Truncate // Truncate
$adjust = 10 ** $digits; $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; return $value;
} }

View File

@ -56,7 +56,12 @@ class SubTotalTest extends AllSetupTeardown
'L' => false, 'L' => false,
]; ];
foreach ($hiddenColumns as $col => $hidden) { 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)"); $sheet->getCell('D2')->setValue("=SUBTOTAL($type, A1:$maxCol$maxRow)");
$result = $sheet->getCell('D2')->getCalculatedValue(); $result = $sheet->getCell('D2')->getCalculatedValue();
@ -91,7 +96,12 @@ class SubTotalTest extends AllSetupTeardown
'12' => false, '12' => false,
]; ];
foreach ($visibleRows as $row => $visible) { 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)"); $sheet->getCell('D2')->setValue("=SUBTOTAL($type, A1:$maxCol$maxRow)");
$result = $sheet->getCell('D2')->getCalculatedValue(); $result = $sheet->getCell('D2')->getCalculatedValue();