Expand [PR #2964](https://github.com/PHPOffice/PhpSpreadsheet/pull/2964) to cover all arithmetic operators, not just multiplication, and both left and right side values
This commit is contained in:
parent
8bde1ace44
commit
71b2c5ae89
|
|
@ -2285,11 +2285,6 @@ parameters:
|
|||
count: 1
|
||||
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\\.$#"
|
||||
count: 1
|
||||
|
|
@ -2370,11 +2365,6 @@ parameters:
|
|||
count: 2
|
||||
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\\.$#"
|
||||
count: 19
|
||||
|
|
|
|||
|
|
@ -533,14 +533,8 @@ class Matrix
|
|||
for ($j = 0; $j < $this->n; ++$j) {
|
||||
$validValues = true;
|
||||
$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] = trim($this->A[$i][$j], '"');
|
||||
$validValues &= StringHelper::convertToNumberIfFraction($this->A[$i][$j]);
|
||||
}
|
||||
if ((is_string($value)) && (strlen($value) > 0) && (!is_numeric($value))) {
|
||||
$value = trim($value, '"');
|
||||
$validValues &= StringHelper::convertToNumberIfFraction($value);
|
||||
}
|
||||
[$this->A[$i][$j], $validValues] = $this->validateExtractedValue($this->A[$i][$j], $validValues);
|
||||
[$value, $validValues] = $this->validateExtractedValue($value, $validValues);
|
||||
if ($validValues) {
|
||||
$this->A[$i][$j] += $value;
|
||||
} else {
|
||||
|
|
@ -633,14 +627,8 @@ class Matrix
|
|||
for ($j = 0; $j < $this->n; ++$j) {
|
||||
$validValues = true;
|
||||
$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] = trim($this->A[$i][$j], '"');
|
||||
$validValues &= StringHelper::convertToNumberIfFraction($this->A[$i][$j]);
|
||||
}
|
||||
if ((is_string($value)) && (strlen($value) > 0) && (!is_numeric($value))) {
|
||||
$value = trim($value, '"');
|
||||
$validValues &= StringHelper::convertToNumberIfFraction($value);
|
||||
}
|
||||
[$this->A[$i][$j], $validValues] = $this->validateExtractedValue($this->A[$i][$j], $validValues);
|
||||
[$value, $validValues] = $this->validateExtractedValue($value, $validValues);
|
||||
if ($validValues) {
|
||||
$this->A[$i][$j] -= $value;
|
||||
} else {
|
||||
|
|
@ -735,17 +723,8 @@ class Matrix
|
|||
for ($j = 0; $j < $this->n; ++$j) {
|
||||
$validValues = true;
|
||||
$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] = trim($this->A[$i][$j], '"');
|
||||
$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];
|
||||
}
|
||||
[$this->A[$i][$j], $validValues] = $this->validateExtractedValue($this->A[$i][$j], $validValues);
|
||||
[$value, $validValues] = $this->validateExtractedValue($value, $validValues);
|
||||
if ($validValues) {
|
||||
$this->A[$i][$j] *= $value;
|
||||
} else {
|
||||
|
|
@ -796,14 +775,8 @@ class Matrix
|
|||
for ($j = 0; $j < $this->n; ++$j) {
|
||||
$validValues = true;
|
||||
$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] = trim($this->A[$i][$j], '"');
|
||||
$validValues &= StringHelper::convertToNumberIfFraction($this->A[$i][$j]);
|
||||
}
|
||||
if ((is_string($value)) && (strlen($value) > 0) && (!is_numeric($value))) {
|
||||
$value = trim($value, '"');
|
||||
$validValues &= StringHelper::convertToNumberIfFraction($value);
|
||||
}
|
||||
[$this->A[$i][$j], $validValues] = $this->validateExtractedValue($this->A[$i][$j], $validValues);
|
||||
[$value, $validValues] = $this->validateExtractedValue($value, $validValues);
|
||||
if ($validValues) {
|
||||
if ($value == 0) {
|
||||
// Trap for Divide by Zero error
|
||||
|
|
@ -1083,14 +1056,8 @@ class Matrix
|
|||
for ($j = 0; $j < $this->n; ++$j) {
|
||||
$validValues = true;
|
||||
$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] = trim($this->A[$i][$j], '"');
|
||||
$validValues &= StringHelper::convertToNumberIfFraction($this->A[$i][$j]);
|
||||
}
|
||||
if ((is_string($value)) && (strlen($value) > 0) && (!is_numeric($value))) {
|
||||
$value = trim($value, '"');
|
||||
$validValues &= StringHelper::convertToNumberIfFraction($value);
|
||||
}
|
||||
[$this->A[$i][$j], $validValues] = $this->validateExtractedValue($this->A[$i][$j], $validValues);
|
||||
[$value, $validValues] = $this->validateExtractedValue($value, $validValues);
|
||||
if ($validValues) {
|
||||
$this->A[$i][$j] = $this->A[$i][$j] ** $value;
|
||||
} else {
|
||||
|
|
@ -1191,4 +1158,20 @@ class Matrix
|
|||
|
||||
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];
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -47,7 +47,12 @@ class SumTest extends AllSetupTeardown
|
|||
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();
|
||||
$sheet1 = $spreadsheet->getActiveSheet();
|
||||
|
|
@ -55,7 +60,7 @@ class SumTest extends AllSetupTeardown
|
|||
$sheet1->fromArray(
|
||||
[
|
||||
['Number', 'Formula'],
|
||||
[83, '=SUM(4 * INDEX(Lookup!B2, MATCH(A2, Lookup!A2, 0)))'],
|
||||
[83, $formula],
|
||||
]
|
||||
);
|
||||
$sheet2 = $spreadsheet->createSheet();
|
||||
|
|
@ -66,7 +71,12 @@ class SumTest extends AllSetupTeardown
|
|||
[83, 16],
|
||||
]
|
||||
);
|
||||
self::assertSame(64, $sheet1->getCell('B2')->getCalculatedValue());
|
||||
self::assertSame($expectedResult, $sheet1->getCell('B2')->getCalculatedValue());
|
||||
$spreadsheet->disconnectWorksheets();
|
||||
}
|
||||
|
||||
public function providerSUMWITHINDEXMATCH(): array
|
||||
{
|
||||
return require 'tests/data/Calculation/MathTrig/SUMWITHINDEXMATCH.php';
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)',
|
||||
],
|
||||
];
|
||||
Loading…
Reference in New Issue