Skip to content

Commit

Permalink
Expand [PR #2964](#2964) to cover all arithmetic operators, not just …
Browse files Browse the repository at this point in the history
…multiplication, and both left and right side values
  • Loading branch information
MarkBaker committed Aug 7, 2022
1 parent 8bde1ac commit 71b2c5a
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 56 deletions.
10 changes: 0 additions & 10 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
69 changes: 26 additions & 43 deletions src/PhpSpreadsheet/Shared/JAMA/Matrix.php
Original file line number Diff line number Diff line change
Expand Up @@ -533,14 +533,8 @@ public function plusEquals(...$args)
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 {
Expand Down Expand Up @@ -633,14 +627,8 @@ public function minusEquals(...$args)
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 {
Expand Down Expand Up @@ -735,17 +723,8 @@ public function arrayTimesEquals(...$args)
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 {
Expand Down Expand Up @@ -796,14 +775,8 @@ public function arrayRightDivide(...$args)
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
Expand Down Expand Up @@ -1083,14 +1056,8 @@ public function power(...$args)
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 {
Expand Down Expand Up @@ -1191,4 +1158,20 @@ public function 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];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,20 @@ public function providerSUMLiterals(): array
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();
$sheet1->setTitle('Formula');
$sheet1->fromArray(
[
['Number', 'Formula'],
[83, '=SUM(4 * INDEX(Lookup!B2, MATCH(A2, Lookup!A2, 0)))'],
[83, $formula],
]
);
$sheet2 = $spreadsheet->createSheet();
Expand All @@ -66,7 +71,12 @@ public function testSumWithIndexMatch(): void
[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';
}
}
34 changes: 34 additions & 0 deletions tests/data/Calculation/MathTrig/SUMWITHINDEXMATCH.php
Original file line number Diff line number Diff line change
@@ -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)',
],
];

0 comments on commit 71b2c5a

Please sign in to comment.