Skip to content

Commit

Permalink
Problems Using Builtin PHP Functions Directly As Excel Functions (#1799)
Browse files Browse the repository at this point in the history
* Problems Using Builtin PHP Functions Directly As Excel Functions

This fixes issue #1789.
As originally reported, stricter typing was causing PHP8 to throw
an exception when a non-numeric value was passed to the Round function.
Previous releases of PHP did not see this problem, however, on further
analysis, they were also incorrect in returning 0 as the result in the
erroneous situation, when they should have been returning a VALUE error.
Yet more analysis showed that other functions would also have problems,
and, in addition, might not handle invalid input (e.g. a negative length
passed to REPT) or output (e.g. NAN in the case of ACOS(2)) correctly.

The following MathTrig functions are affected:
ABS, ACOS, ACOSH, ASIN, ASINH, ATAN, ATANH,
COS, COSH, DEGREES (rad2deg), EXP, LN (log), LOG10,
RADIANS (deg2rad), REPT (str_repeat), SIN, SINH, SQRT, TAN, TANH.
One TextData function (REPT) is also affected.

This change lets PhpSpreadsheet validate the input for each of these
functions before passing control to the builtin, and handle the output
afterwards.

There were no explicit tests for any of these functions, a fact made
easy to ignore by the fact that PhpSpreadsheet delegated the heavy
lifting to PHP itself for these cases. A full suite of tests is
now added for each of the affected functions.

* Scrutinizer Recommendations

Only in 3 modules which are part of this PR.

* Improved Handling of Tan(PI/2)

Return DIV0 error for TAN when COS is very small.

* Additional Trig Tests

Results which should be infinity, i.e. DIV/0 error.
  • Loading branch information
oleibman authored Jan 26, 2021
1 parent ec51b75 commit 4134ff2
Show file tree
Hide file tree
Showing 48 changed files with 1,482 additions and 28 deletions.
42 changes: 21 additions & 21 deletions src/PhpSpreadsheet/Calculation/Calculation.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ class Calculation
private static $phpSpreadsheetFunctions = [
'ABS' => [
'category' => Category::CATEGORY_MATH_AND_TRIG,
'functionCall' => 'abs',
'functionCall' => [MathTrig::class, 'builtinABS'],
'argumentCount' => '1',
],
'ACCRINT' => [
Expand All @@ -243,12 +243,12 @@ class Calculation
],
'ACOS' => [
'category' => Category::CATEGORY_MATH_AND_TRIG,
'functionCall' => 'acos',
'functionCall' => [MathTrig::class, 'builtinACOS'],
'argumentCount' => '1',
],
'ACOSH' => [
'category' => Category::CATEGORY_MATH_AND_TRIG,
'functionCall' => 'acosh',
'functionCall' => [MathTrig::class, 'builtinACOSH'],
'argumentCount' => '1',
],
'ACOT' => [
Expand Down Expand Up @@ -303,17 +303,17 @@ class Calculation
],
'ASIN' => [
'category' => Category::CATEGORY_MATH_AND_TRIG,
'functionCall' => 'asin',
'functionCall' => [MathTrig::class, 'builtinASIN'],
'argumentCount' => '1',
],
'ASINH' => [
'category' => Category::CATEGORY_MATH_AND_TRIG,
'functionCall' => 'asinh',
'functionCall' => [MathTrig::class, 'builtinASINH'],
'argumentCount' => '1',
],
'ATAN' => [
'category' => Category::CATEGORY_MATH_AND_TRIG,
'functionCall' => 'atan',
'functionCall' => [MathTrig::class, 'builtinATAN'],
'argumentCount' => '1',
],
'ATAN2' => [
Expand All @@ -323,7 +323,7 @@ class Calculation
],
'ATANH' => [
'category' => Category::CATEGORY_MATH_AND_TRIG,
'functionCall' => 'atanh',
'functionCall' => [MathTrig::class, 'builtinATANH'],
'argumentCount' => '1',
],
'AVEDEV' => [
Expand Down Expand Up @@ -604,12 +604,12 @@ class Calculation
],
'COS' => [
'category' => Category::CATEGORY_MATH_AND_TRIG,
'functionCall' => 'cos',
'functionCall' => [MathTrig::class, 'builtinCOS'],
'argumentCount' => '1',
],
'COSH' => [
'category' => Category::CATEGORY_MATH_AND_TRIG,
'functionCall' => 'cosh',
'functionCall' => [MathTrig::class, 'builtinCOSH'],
'argumentCount' => '1',
],
'COT' => [
Expand Down Expand Up @@ -834,7 +834,7 @@ class Calculation
],
'DEGREES' => [
'category' => Category::CATEGORY_MATH_AND_TRIG,
'functionCall' => 'rad2deg',
'functionCall' => [MathTrig::class, 'builtinDEGREES'],
'argumentCount' => '1',
],
'DELTA' => [
Expand Down Expand Up @@ -974,7 +974,7 @@ class Calculation
],
'EXP' => [
'category' => Category::CATEGORY_MATH_AND_TRIG,
'functionCall' => 'exp',
'functionCall' => [MathTrig::class, 'builtinEXP'],
'argumentCount' => '1',
],
'EXPONDIST' => [
Expand Down Expand Up @@ -1565,7 +1565,7 @@ class Calculation
],
'LN' => [
'category' => Category::CATEGORY_MATH_AND_TRIG,
'functionCall' => 'log',
'functionCall' => [MathTrig::class, 'builtinLN'],
'argumentCount' => '1',
],
'LOG' => [
Expand All @@ -1575,7 +1575,7 @@ class Calculation
],
'LOG10' => [
'category' => Category::CATEGORY_MATH_AND_TRIG,
'functionCall' => 'log10',
'functionCall' => [MathTrig::class, 'builtinLOG10'],
'argumentCount' => '1',
],
'LOGEST' => [
Expand Down Expand Up @@ -2037,7 +2037,7 @@ class Calculation
],
'RADIANS' => [
'category' => Category::CATEGORY_MATH_AND_TRIG,
'functionCall' => 'deg2rad',
'functionCall' => [MathTrig::class, 'builtinRADIANS'],
'argumentCount' => '1',
],
'RAND' => [
Expand Down Expand Up @@ -2092,7 +2092,7 @@ class Calculation
],
'REPT' => [
'category' => Category::CATEGORY_TEXT_AND_DATA,
'functionCall' => 'str_repeat',
'functionCall' => [TextData::class, 'builtinREPT'],
'argumentCount' => '2',
],
'RIGHT' => [
Expand All @@ -2112,7 +2112,7 @@ class Calculation
],
'ROUND' => [
'category' => Category::CATEGORY_MATH_AND_TRIG,
'functionCall' => 'round',
'functionCall' => [MathTrig::class, 'builtinROUND'],
'argumentCount' => '2',
],
'ROUNDDOWN' => [
Expand Down Expand Up @@ -2203,12 +2203,12 @@ class Calculation
],
'SIN' => [
'category' => Category::CATEGORY_MATH_AND_TRIG,
'functionCall' => 'sin',
'functionCall' => [MathTrig::class, 'builtinSIN'],
'argumentCount' => '1',
],
'SINH' => [
'category' => Category::CATEGORY_MATH_AND_TRIG,
'functionCall' => 'sinh',
'functionCall' => [MathTrig::class, 'builtinSINH'],
'argumentCount' => '1',
],
'SKEW' => [
Expand Down Expand Up @@ -2248,7 +2248,7 @@ class Calculation
],
'SQRT' => [
'category' => Category::CATEGORY_MATH_AND_TRIG,
'functionCall' => 'sqrt',
'functionCall' => [MathTrig::class, 'builtinSQRT'],
'argumentCount' => '1',
],
'SQRTPI' => [
Expand Down Expand Up @@ -2364,12 +2364,12 @@ class Calculation
],
'TAN' => [
'category' => Category::CATEGORY_MATH_AND_TRIG,
'functionCall' => 'tan',
'functionCall' => [MathTrig::class, 'builtinTAN'],
'argumentCount' => '1',
],
'TANH' => [
'category' => Category::CATEGORY_MATH_AND_TRIG,
'functionCall' => 'tanh',
'functionCall' => [MathTrig::class, 'builtinTANH'],
'argumentCount' => '1',
],
'TBILLEQ' => [
Expand Down
Loading

0 comments on commit 4134ff2

Please sign in to comment.