Merge pull request #2993 from PHPOffice/Matrix-Arithmetic-Value-Testing

Expand PR #2964 to cover all arithmetic operators, and both left and right side values
This commit is contained in:
Mark Baker 2022-08-07 14:18:58 +02:00 committed by GitHub
commit c34662b8b2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 73 additions and 56 deletions

View File

@ -2285,11 +2285,6 @@ parameters:
count: 1 count: 1
path: src/PhpSpreadsheet/Shared/JAMA/LUDecomposition.php path: src/PhpSpreadsheet/Shared/JAMA/LUDecomposition.php
-
message: "#^Call to function is_string\\(\\) with float\\|int will always evaluate to false\\.$#"
count: 5
path: src/PhpSpreadsheet/Shared/JAMA/Matrix.php
- -
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Shared\\\\JAMA\\\\Matrix\\:\\:__construct\\(\\) has parameter \\$args with no type specified\\.$#" message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Shared\\\\JAMA\\\\Matrix\\:\\:__construct\\(\\) has parameter \\$args with no type specified\\.$#"
count: 1 count: 1
@ -2370,11 +2365,6 @@ parameters:
count: 2 count: 2
path: src/PhpSpreadsheet/Shared/JAMA/Matrix.php path: src/PhpSpreadsheet/Shared/JAMA/Matrix.php
-
message: "#^Result of && is always false\\.$#"
count: 11
path: src/PhpSpreadsheet/Shared/JAMA/Matrix.php
- -
message: "#^Unreachable statement \\- code above always terminates\\.$#" message: "#^Unreachable statement \\- code above always terminates\\.$#"
count: 19 count: 19

View File

@ -533,14 +533,8 @@ class Matrix
for ($j = 0; $j < $this->n; ++$j) { for ($j = 0; $j < $this->n; ++$j) {
$validValues = true; $validValues = true;
$value = $M->get($i, $j); $value = $M->get($i, $j);
if ((is_string($this->A[$i][$j])) && (strlen($this->A[$i][$j]) > 0) && (!is_numeric($this->A[$i][$j]))) { [$this->A[$i][$j], $validValues] = $this->validateExtractedValue($this->A[$i][$j], $validValues);
$this->A[$i][$j] = trim($this->A[$i][$j], '"'); [$value, $validValues] = $this->validateExtractedValue($value, $validValues);
$validValues &= StringHelper::convertToNumberIfFraction($this->A[$i][$j]);
}
if ((is_string($value)) && (strlen($value) > 0) && (!is_numeric($value))) {
$value = trim($value, '"');
$validValues &= StringHelper::convertToNumberIfFraction($value);
}
if ($validValues) { if ($validValues) {
$this->A[$i][$j] += $value; $this->A[$i][$j] += $value;
} else { } else {
@ -633,14 +627,8 @@ class Matrix
for ($j = 0; $j < $this->n; ++$j) { for ($j = 0; $j < $this->n; ++$j) {
$validValues = true; $validValues = true;
$value = $M->get($i, $j); $value = $M->get($i, $j);
if ((is_string($this->A[$i][$j])) && (strlen($this->A[$i][$j]) > 0) && (!is_numeric($this->A[$i][$j]))) { [$this->A[$i][$j], $validValues] = $this->validateExtractedValue($this->A[$i][$j], $validValues);
$this->A[$i][$j] = trim($this->A[$i][$j], '"'); [$value, $validValues] = $this->validateExtractedValue($value, $validValues);
$validValues &= StringHelper::convertToNumberIfFraction($this->A[$i][$j]);
}
if ((is_string($value)) && (strlen($value) > 0) && (!is_numeric($value))) {
$value = trim($value, '"');
$validValues &= StringHelper::convertToNumberIfFraction($value);
}
if ($validValues) { if ($validValues) {
$this->A[$i][$j] -= $value; $this->A[$i][$j] -= $value;
} else { } else {
@ -735,17 +723,8 @@ class Matrix
for ($j = 0; $j < $this->n; ++$j) { for ($j = 0; $j < $this->n; ++$j) {
$validValues = true; $validValues = true;
$value = $M->get($i, $j); $value = $M->get($i, $j);
if ((is_string($this->A[$i][$j])) && (strlen($this->A[$i][$j]) > 0) && (!is_numeric($this->A[$i][$j]))) { [$this->A[$i][$j], $validValues] = $this->validateExtractedValue($this->A[$i][$j], $validValues);
$this->A[$i][$j] = trim($this->A[$i][$j], '"'); [$value, $validValues] = $this->validateExtractedValue($value, $validValues);
$validValues &= StringHelper::convertToNumberIfFraction($this->A[$i][$j]);
}
if ((is_string($value)) && (strlen($value) > 0) && (!is_numeric($value))) {
$value = trim($value, '"');
$validValues &= StringHelper::convertToNumberIfFraction($value);
}
if (!is_numeric($value) && is_array($value)) {
$value = Functions::flattenArray($value)[0];
}
if ($validValues) { if ($validValues) {
$this->A[$i][$j] *= $value; $this->A[$i][$j] *= $value;
} else { } else {
@ -796,14 +775,8 @@ class Matrix
for ($j = 0; $j < $this->n; ++$j) { for ($j = 0; $j < $this->n; ++$j) {
$validValues = true; $validValues = true;
$value = $M->get($i, $j); $value = $M->get($i, $j);
if ((is_string($this->A[$i][$j])) && (strlen($this->A[$i][$j]) > 0) && (!is_numeric($this->A[$i][$j]))) { [$this->A[$i][$j], $validValues] = $this->validateExtractedValue($this->A[$i][$j], $validValues);
$this->A[$i][$j] = trim($this->A[$i][$j], '"'); [$value, $validValues] = $this->validateExtractedValue($value, $validValues);
$validValues &= StringHelper::convertToNumberIfFraction($this->A[$i][$j]);
}
if ((is_string($value)) && (strlen($value) > 0) && (!is_numeric($value))) {
$value = trim($value, '"');
$validValues &= StringHelper::convertToNumberIfFraction($value);
}
if ($validValues) { if ($validValues) {
if ($value == 0) { if ($value == 0) {
// Trap for Divide by Zero error // Trap for Divide by Zero error
@ -1083,14 +1056,8 @@ class Matrix
for ($j = 0; $j < $this->n; ++$j) { for ($j = 0; $j < $this->n; ++$j) {
$validValues = true; $validValues = true;
$value = $M->get($i, $j); $value = $M->get($i, $j);
if ((is_string($this->A[$i][$j])) && (strlen($this->A[$i][$j]) > 0) && (!is_numeric($this->A[$i][$j]))) { [$this->A[$i][$j], $validValues] = $this->validateExtractedValue($this->A[$i][$j], $validValues);
$this->A[$i][$j] = trim($this->A[$i][$j], '"'); [$value, $validValues] = $this->validateExtractedValue($value, $validValues);
$validValues &= StringHelper::convertToNumberIfFraction($this->A[$i][$j]);
}
if ((is_string($value)) && (strlen($value) > 0) && (!is_numeric($value))) {
$value = trim($value, '"');
$validValues &= StringHelper::convertToNumberIfFraction($value);
}
if ($validValues) { if ($validValues) {
$this->A[$i][$j] = $this->A[$i][$j] ** $value; $this->A[$i][$j] = $this->A[$i][$j] ** $value;
} else { } else {
@ -1191,4 +1158,20 @@ class Matrix
return $L->det(); return $L->det();
} }
/**
* @param mixed $value
*/
private function validateExtractedValue($value, bool $validValues): array
{
if ((is_string($value)) && (strlen($value) > 0) && (!is_numeric($value))) {
$value = trim($value, '"');
$validValues &= StringHelper::convertToNumberIfFraction($value);
}
if (!is_numeric($value) && is_array($value)) {
$value = Functions::flattenArray($value)[0];
}
return [$value, $validValues];
}
} }

View File

@ -47,7 +47,12 @@ class SumTest extends AllSetupTeardown
return require 'tests/data/Calculation/MathTrig/SUMLITERALS.php'; return require 'tests/data/Calculation/MathTrig/SUMLITERALS.php';
} }
public function testSumWithIndexMatch(): void /**
* @dataProvider providerSUMWITHINDEXMATCH
*
* @param mixed $expectedResult
*/
public function testSumWithIndexMatch($expectedResult, string $formula): void
{ {
$spreadsheet = new Spreadsheet(); $spreadsheet = new Spreadsheet();
$sheet1 = $spreadsheet->getActiveSheet(); $sheet1 = $spreadsheet->getActiveSheet();
@ -55,7 +60,7 @@ class SumTest extends AllSetupTeardown
$sheet1->fromArray( $sheet1->fromArray(
[ [
['Number', 'Formula'], ['Number', 'Formula'],
[83, '=SUM(4 * INDEX(Lookup!B2, MATCH(A2, Lookup!A2, 0)))'], [83, $formula],
] ]
); );
$sheet2 = $spreadsheet->createSheet(); $sheet2 = $spreadsheet->createSheet();
@ -66,7 +71,12 @@ class SumTest extends AllSetupTeardown
[83, 16], [83, 16],
] ]
); );
self::assertSame(64, $sheet1->getCell('B2')->getCalculatedValue()); self::assertSame($expectedResult, $sheet1->getCell('B2')->getCalculatedValue());
$spreadsheet->disconnectWorksheets(); $spreadsheet->disconnectWorksheets();
} }
public function providerSUMWITHINDEXMATCH(): array
{
return require 'tests/data/Calculation/MathTrig/SUMWITHINDEXMATCH.php';
}
} }

View File

@ -0,0 +1,34 @@
<?php
return [
[
64, '=SUM(4 * INDEX(Lookup!B2, MATCH(A2, Lookup!A2, 0)))',
],
[
64, '=SUM(INDEX(Lookup!B2, MATCH(A2, Lookup!A2, 0)) * 4)',
],
[
20, '=SUM(4 + INDEX(Lookup!B2, MATCH(A2, Lookup!A2, 0)))',
],
[
20, '=SUM(INDEX(Lookup!B2, MATCH(A2, Lookup!A2, 0)) + 4)',
],
[
-12, '=SUM(4 - INDEX(Lookup!B2, MATCH(A2, Lookup!A2, 0)))',
],
[
12, '=SUM(INDEX(Lookup!B2, MATCH(A2, Lookup!A2, 0)) - 4)',
],
[
0.25, '=SUM(4 / INDEX(Lookup!B2, MATCH(A2, Lookup!A2, 0)))',
],
[
4, '=SUM(INDEX(Lookup!B2, MATCH(A2, Lookup!A2, 0)) / 4)',
],
[
4294967296, '=SUM(4 ^ INDEX(Lookup!B2, MATCH(A2, Lookup!A2, 0)))',
],
[
65536, '=SUM(INDEX(Lookup!B2, MATCH(A2, Lookup!A2, 0)) ^ 4)',
],
];