Skip to content

Commit c34662b

Browse files
authored
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
2 parents b65ff9f + b627770 commit c34662b

File tree

4 files changed

+73
-56
lines changed

4 files changed

+73
-56
lines changed

phpstan-baseline.neon

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2285,11 +2285,6 @@ parameters:
22852285
count: 1
22862286
path: src/PhpSpreadsheet/Shared/JAMA/LUDecomposition.php
22872287

2288-
-
2289-
message: "#^Call to function is_string\\(\\) with float\\|int will always evaluate to false\\.$#"
2290-
count: 5
2291-
path: src/PhpSpreadsheet/Shared/JAMA/Matrix.php
2292-
22932288
-
22942289
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Shared\\\\JAMA\\\\Matrix\\:\\:__construct\\(\\) has parameter \\$args with no type specified\\.$#"
22952290
count: 1
@@ -2370,11 +2365,6 @@ parameters:
23702365
count: 2
23712366
path: src/PhpSpreadsheet/Shared/JAMA/Matrix.php
23722367

2373-
-
2374-
message: "#^Result of && is always false\\.$#"
2375-
count: 11
2376-
path: src/PhpSpreadsheet/Shared/JAMA/Matrix.php
2377-
23782368
-
23792369
message: "#^Unreachable statement \\- code above always terminates\\.$#"
23802370
count: 19

src/PhpSpreadsheet/Shared/JAMA/Matrix.php

Lines changed: 26 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -533,14 +533,8 @@ public function plusEquals(...$args)
533533
for ($j = 0; $j < $this->n; ++$j) {
534534
$validValues = true;
535535
$value = $M->get($i, $j);
536-
if ((is_string($this->A[$i][$j])) && (strlen($this->A[$i][$j]) > 0) && (!is_numeric($this->A[$i][$j]))) {
537-
$this->A[$i][$j] = trim($this->A[$i][$j], '"');
538-
$validValues &= StringHelper::convertToNumberIfFraction($this->A[$i][$j]);
539-
}
540-
if ((is_string($value)) && (strlen($value) > 0) && (!is_numeric($value))) {
541-
$value = trim($value, '"');
542-
$validValues &= StringHelper::convertToNumberIfFraction($value);
543-
}
536+
[$this->A[$i][$j], $validValues] = $this->validateExtractedValue($this->A[$i][$j], $validValues);
537+
[$value, $validValues] = $this->validateExtractedValue($value, $validValues);
544538
if ($validValues) {
545539
$this->A[$i][$j] += $value;
546540
} else {
@@ -633,14 +627,8 @@ public function minusEquals(...$args)
633627
for ($j = 0; $j < $this->n; ++$j) {
634628
$validValues = true;
635629
$value = $M->get($i, $j);
636-
if ((is_string($this->A[$i][$j])) && (strlen($this->A[$i][$j]) > 0) && (!is_numeric($this->A[$i][$j]))) {
637-
$this->A[$i][$j] = trim($this->A[$i][$j], '"');
638-
$validValues &= StringHelper::convertToNumberIfFraction($this->A[$i][$j]);
639-
}
640-
if ((is_string($value)) && (strlen($value) > 0) && (!is_numeric($value))) {
641-
$value = trim($value, '"');
642-
$validValues &= StringHelper::convertToNumberIfFraction($value);
643-
}
630+
[$this->A[$i][$j], $validValues] = $this->validateExtractedValue($this->A[$i][$j], $validValues);
631+
[$value, $validValues] = $this->validateExtractedValue($value, $validValues);
644632
if ($validValues) {
645633
$this->A[$i][$j] -= $value;
646634
} else {
@@ -735,17 +723,8 @@ public function arrayTimesEquals(...$args)
735723
for ($j = 0; $j < $this->n; ++$j) {
736724
$validValues = true;
737725
$value = $M->get($i, $j);
738-
if ((is_string($this->A[$i][$j])) && (strlen($this->A[$i][$j]) > 0) && (!is_numeric($this->A[$i][$j]))) {
739-
$this->A[$i][$j] = trim($this->A[$i][$j], '"');
740-
$validValues &= StringHelper::convertToNumberIfFraction($this->A[$i][$j]);
741-
}
742-
if ((is_string($value)) && (strlen($value) > 0) && (!is_numeric($value))) {
743-
$value = trim($value, '"');
744-
$validValues &= StringHelper::convertToNumberIfFraction($value);
745-
}
746-
if (!is_numeric($value) && is_array($value)) {
747-
$value = Functions::flattenArray($value)[0];
748-
}
726+
[$this->A[$i][$j], $validValues] = $this->validateExtractedValue($this->A[$i][$j], $validValues);
727+
[$value, $validValues] = $this->validateExtractedValue($value, $validValues);
749728
if ($validValues) {
750729
$this->A[$i][$j] *= $value;
751730
} else {
@@ -796,14 +775,8 @@ public function arrayRightDivide(...$args)
796775
for ($j = 0; $j < $this->n; ++$j) {
797776
$validValues = true;
798777
$value = $M->get($i, $j);
799-
if ((is_string($this->A[$i][$j])) && (strlen($this->A[$i][$j]) > 0) && (!is_numeric($this->A[$i][$j]))) {
800-
$this->A[$i][$j] = trim($this->A[$i][$j], '"');
801-
$validValues &= StringHelper::convertToNumberIfFraction($this->A[$i][$j]);
802-
}
803-
if ((is_string($value)) && (strlen($value) > 0) && (!is_numeric($value))) {
804-
$value = trim($value, '"');
805-
$validValues &= StringHelper::convertToNumberIfFraction($value);
806-
}
778+
[$this->A[$i][$j], $validValues] = $this->validateExtractedValue($this->A[$i][$j], $validValues);
779+
[$value, $validValues] = $this->validateExtractedValue($value, $validValues);
807780
if ($validValues) {
808781
if ($value == 0) {
809782
// Trap for Divide by Zero error
@@ -1083,14 +1056,8 @@ public function power(...$args)
10831056
for ($j = 0; $j < $this->n; ++$j) {
10841057
$validValues = true;
10851058
$value = $M->get($i, $j);
1086-
if ((is_string($this->A[$i][$j])) && (strlen($this->A[$i][$j]) > 0) && (!is_numeric($this->A[$i][$j]))) {
1087-
$this->A[$i][$j] = trim($this->A[$i][$j], '"');
1088-
$validValues &= StringHelper::convertToNumberIfFraction($this->A[$i][$j]);
1089-
}
1090-
if ((is_string($value)) && (strlen($value) > 0) && (!is_numeric($value))) {
1091-
$value = trim($value, '"');
1092-
$validValues &= StringHelper::convertToNumberIfFraction($value);
1093-
}
1059+
[$this->A[$i][$j], $validValues] = $this->validateExtractedValue($this->A[$i][$j], $validValues);
1060+
[$value, $validValues] = $this->validateExtractedValue($value, $validValues);
10941061
if ($validValues) {
10951062
$this->A[$i][$j] = $this->A[$i][$j] ** $value;
10961063
} else {
@@ -1191,4 +1158,20 @@ public function det()
11911158

11921159
return $L->det();
11931160
}
1161+
1162+
/**
1163+
* @param mixed $value
1164+
*/
1165+
private function validateExtractedValue($value, bool $validValues): array
1166+
{
1167+
if ((is_string($value)) && (strlen($value) > 0) && (!is_numeric($value))) {
1168+
$value = trim($value, '"');
1169+
$validValues &= StringHelper::convertToNumberIfFraction($value);
1170+
}
1171+
if (!is_numeric($value) && is_array($value)) {
1172+
$value = Functions::flattenArray($value)[0];
1173+
}
1174+
1175+
return [$value, $validValues];
1176+
}
11941177
}

tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/SumTest.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,20 @@ public function providerSUMLiterals(): array
4747
return require 'tests/data/Calculation/MathTrig/SUMLITERALS.php';
4848
}
4949

50-
public function testSumWithIndexMatch(): void
50+
/**
51+
* @dataProvider providerSUMWITHINDEXMATCH
52+
*
53+
* @param mixed $expectedResult
54+
*/
55+
public function testSumWithIndexMatch($expectedResult, string $formula): void
5156
{
5257
$spreadsheet = new Spreadsheet();
5358
$sheet1 = $spreadsheet->getActiveSheet();
5459
$sheet1->setTitle('Formula');
5560
$sheet1->fromArray(
5661
[
5762
['Number', 'Formula'],
58-
[83, '=SUM(4 * INDEX(Lookup!B2, MATCH(A2, Lookup!A2, 0)))'],
63+
[83, $formula],
5964
]
6065
);
6166
$sheet2 = $spreadsheet->createSheet();
@@ -66,7 +71,12 @@ public function testSumWithIndexMatch(): void
6671
[83, 16],
6772
]
6873
);
69-
self::assertSame(64, $sheet1->getCell('B2')->getCalculatedValue());
74+
self::assertSame($expectedResult, $sheet1->getCell('B2')->getCalculatedValue());
7075
$spreadsheet->disconnectWorksheets();
7176
}
77+
78+
public function providerSUMWITHINDEXMATCH(): array
79+
{
80+
return require 'tests/data/Calculation/MathTrig/SUMWITHINDEXMATCH.php';
81+
}
7282
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
return [
4+
[
5+
64, '=SUM(4 * INDEX(Lookup!B2, MATCH(A2, Lookup!A2, 0)))',
6+
],
7+
[
8+
64, '=SUM(INDEX(Lookup!B2, MATCH(A2, Lookup!A2, 0)) * 4)',
9+
],
10+
[
11+
20, '=SUM(4 + INDEX(Lookup!B2, MATCH(A2, Lookup!A2, 0)))',
12+
],
13+
[
14+
20, '=SUM(INDEX(Lookup!B2, MATCH(A2, Lookup!A2, 0)) + 4)',
15+
],
16+
[
17+
-12, '=SUM(4 - INDEX(Lookup!B2, MATCH(A2, Lookup!A2, 0)))',
18+
],
19+
[
20+
12, '=SUM(INDEX(Lookup!B2, MATCH(A2, Lookup!A2, 0)) - 4)',
21+
],
22+
[
23+
0.25, '=SUM(4 / INDEX(Lookup!B2, MATCH(A2, Lookup!A2, 0)))',
24+
],
25+
[
26+
4, '=SUM(INDEX(Lookup!B2, MATCH(A2, Lookup!A2, 0)) / 4)',
27+
],
28+
[
29+
4294967296, '=SUM(4 ^ INDEX(Lookup!B2, MATCH(A2, Lookup!A2, 0)))',
30+
],
31+
[
32+
65536, '=SUM(INDEX(Lookup!B2, MATCH(A2, Lookup!A2, 0)) ^ 4)',
33+
],
34+
];

0 commit comments

Comments
 (0)