Skip to content

Commit d5587e9

Browse files
authored
Merge branch 'master' into Structured-References_On-Changing-a-Column-Heading
2 parents 6e4a875 + 73ff527 commit d5587e9

File tree

17 files changed

+734
-72
lines changed

17 files changed

+734
-72
lines changed

phpstan-baseline.neon

Lines changed: 425 additions & 0 deletions
Large diffs are not rendered by default.

src/PhpSpreadsheet/Calculation/Database/DatabaseAbstract.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ abstract public static function evaluate($database, $field, $criteria);
3434
*/
3535
protected static function fieldExtract(array $database, $field): ?int
3636
{
37-
$field = strtoupper(Functions::flattenSingleValue($field ?? ''));
37+
$field = strtoupper(Functions::flattenSingleValue($field) ?? '');
3838
if ($field === '') {
3939
return null;
4040
}

src/PhpSpreadsheet/Calculation/Engineering/BitWise.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ class BitWise
1717
* Split a number into upper and lower portions for full 32-bit support.
1818
*
1919
* @param float|int $number
20+
*
21+
* @return int[]
2022
*/
2123
private static function splitNumber($number): array
2224
{
23-
return [floor($number / self::SPLIT_DIVISOR), fmod($number, self::SPLIT_DIVISOR)];
25+
return [(int) floor($number / self::SPLIT_DIVISOR), (int) fmod($number, self::SPLIT_DIVISOR)];
2426
}
2527

2628
/**
@@ -55,7 +57,6 @@ public static function BITAND($number1, $number2)
5557
$split1 = self::splitNumber($number1);
5658
$split2 = self::splitNumber($number2);
5759

58-
// Scrutinizer does not like bitwise, and doesn't let you override its warning
5960
return self::SPLIT_DIVISOR * ($split1[0] & $split2[0]) + ($split1[1] & $split2[1]);
6061
}
6162

@@ -92,7 +93,6 @@ public static function BITOR($number1, $number2)
9293
$split1 = self::splitNumber($number1);
9394
$split2 = self::splitNumber($number2);
9495

95-
// Scrutinizer does not like bitwise, and doesn't let you override its warning
9696
return self::SPLIT_DIVISOR * ($split1[0] | $split2[0]) + ($split1[1] | $split2[1]);
9797
}
9898

src/PhpSpreadsheet/Worksheet/Validations.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public static function validateCellAddress($cellAddress): string
2323
// throw new Exception('Reference is not for this worksheet');
2424
// }
2525

26-
return empty($worksheet) ? strtoupper($address) : $worksheet . '!' . strtoupper($address);
26+
return empty($worksheet) ? strtoupper("$address") : $worksheet . '!' . strtoupper("$address");
2727
}
2828

2929
if (is_array($cellAddress)) {

tests/PhpSpreadsheetTests/Calculation/Functions/Database/AllSetupTeardown.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
class AllSetupTeardown extends TestCase
1212
{
13+
protected const RESULT_CELL = 'Z1';
14+
1315
/**
1416
* @var ?Spreadsheet
1517
*/
@@ -78,10 +80,9 @@ protected function getSheet(): Worksheet
7880
}
7981

8082
/**
81-
* @param mixed $expectedResult
8283
* @param int|string $field
8384
*/
84-
public function runTestCase(string $functionName, $expectedResult, array $database, $field, array $criteria): void
85+
public function prepareWorksheetWithFormula(string $functionName, array $database, $field, array $criteria): void
8586
{
8687
$sheet = $this->getSheet();
8788
$maxCol = '';
@@ -119,10 +120,7 @@ public function runTestCase(string $functionName, $expectedResult, array $databa
119120
}
120121
$criteriaCells = "$startCol$startRow:$maxCol$maxRow";
121122
$sheet->getCell('N1')->setValue($field);
122-
$sheet->getCell('Z1')->setValue("=$functionName($databaseCells, N1, $criteriaCells)");
123-
124-
$result = $sheet->getCell('Z1')->getCalculatedValue();
125-
self::assertEqualsWithDelta($expectedResult, $result, 1.0e-12);
123+
$sheet->getCell(self::RESULT_CELL)->setValue("=$functionName($databaseCells, N1, $criteriaCells)");
126124
}
127125

128126
protected function database1(): array

tests/PhpSpreadsheetTests/Calculation/Functions/Database/DAverageTest.php

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,37 @@
22

33
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\Database;
44

5+
use PhpOffice\PhpSpreadsheet\Calculation\Database\DAverage;
6+
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
7+
58
class DAverageTest extends AllSetupTeardown
69
{
10+
/**
11+
* @dataProvider providerDAverage
12+
*
13+
* @param mixed $expectedResult
14+
* @param mixed $database
15+
* @param mixed $field
16+
* @param mixed $criteria
17+
*/
18+
public function testDirectCallToDAverage($expectedResult, $database, $field, $criteria): void
19+
{
20+
$result = DAverage::evaluate($database, $field, $criteria);
21+
self::assertEqualsWithDelta($expectedResult, $result, 1.0e-12);
22+
}
23+
724
/**
825
* @dataProvider providerDAverage
926
*
1027
* @param mixed $expectedResult
1128
* @param int|string $field
1229
*/
13-
public function testDAverage($expectedResult, array $database, $field, array $criteria): void
30+
public function testDAverageAsWorksheetFormula($expectedResult, array $database, $field, array $criteria): void
1431
{
15-
$this->runTestCase('DAVERAGE', $expectedResult, $database, $field, $criteria);
32+
$this->prepareWorksheetWithFormula('DAVERAGE', $database, $field, $criteria);
33+
34+
$result = $this->getSheet()->getCell(self::RESULT_CELL)->getCalculatedValue();
35+
self::assertEqualsWithDelta($expectedResult, $result, 1.0e-12);
1636
}
1737

1838
public function providerDAverage(): array
@@ -27,12 +47,6 @@ public function providerDAverage(): array
2747
['=Apple', '>10'],
2848
],
2949
],
30-
'numeric column, in this case referring to age' => [
31-
13,
32-
$this->database1(),
33-
3,
34-
$this->database1(),
35-
],
3650
[
3751
268333.333333333333,
3852
$this->database2(),
@@ -51,14 +65,20 @@ public function providerDAverage(): array
5165
['1', 'South'],
5266
],
5367
],
68+
'numeric column, in this case referring to age' => [
69+
13,
70+
$this->database1(),
71+
3,
72+
$this->database1(),
73+
],
5474
'null field' => [
55-
'#VALUE!',
75+
ExcelError::VALUE(),
5676
$this->database1(),
5777
null,
5878
$this->database1(),
5979
],
6080
'field unknown column' => [
61-
'#VALUE!',
81+
ExcelError::VALUE(),
6282
$this->database1(),
6383
'xyz',
6484
$this->database1(),
@@ -87,13 +107,13 @@ public function providerDAverage(): array
87107
to me that I'm not going to bother coding that up,
88108
content to return #VALUE! as an invalid name would */
89109
'field column number too high' => [
90-
'#VALUE!',
110+
ExcelError::VALUE(),
91111
$this->database1(),
92112
99,
93113
$this->database1(),
94114
],
95115
'field column number too low' => [
96-
'#VALUE!',
116+
ExcelError::VALUE(),
97117
$this->database1(),
98118
0,
99119
$this->database1(),

tests/PhpSpreadsheetTests/Calculation/Functions/Database/DCountATest.php

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\Database;
44

5+
use PhpOffice\PhpSpreadsheet\Calculation\Database\DCountA;
6+
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
7+
58
class DCountATest extends AllSetupTeardown
69
{
710
/**
@@ -12,9 +15,26 @@ class DCountATest extends AllSetupTeardown
1215
* @param mixed $field
1316
* @param mixed $criteria
1417
*/
15-
public function testDCountA($expectedResult, $database, $field, $criteria): void
18+
public function testDirectCallToDCountA($expectedResult, $database, $field, $criteria): void
1619
{
17-
$this->runTestCase('DCOUNTA', $expectedResult, $database, $field, $criteria);
20+
$result = DCountA::evaluate($database, $field, $criteria);
21+
self::assertEqualsWithDelta($expectedResult, $result, 1.0e-12);
22+
}
23+
24+
/**
25+
* @dataProvider providerDCountA
26+
*
27+
* @param mixed $expectedResult
28+
* @param mixed $database
29+
* @param mixed $field
30+
* @param mixed $criteria
31+
*/
32+
public function testDCountAAsWorksheetFormula($expectedResult, $database, $field, $criteria): void
33+
{
34+
$this->prepareWorksheetWithFormula('DCOUNTA', $database, $field, $criteria);
35+
36+
$result = $this->getSheet()->getCell(self::RESULT_CELL)->getCalculatedValue();
37+
self::assertEqualsWithDelta($expectedResult, $result, 1.0e-12);
1838
}
1939

2040
public function providerDCountA(): array
@@ -57,7 +77,7 @@ public function providerDCountA(): array
5777
],
5878
],
5979
'invalid field name' => [
60-
'#VALUE!',
80+
ExcelError::VALUE(),
6181
$this->database3(),
6282
'Scorex',
6383
[

tests/PhpSpreadsheetTests/Calculation/Functions/Database/DCountTest.php

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\Database;
44

5+
use PhpOffice\PhpSpreadsheet\Calculation\Database\DCount;
6+
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
7+
58
class DCountTest extends AllSetupTeardown
69
{
710
/**
@@ -12,9 +15,26 @@ class DCountTest extends AllSetupTeardown
1215
* @param mixed $field
1316
* @param mixed $criteria
1417
*/
15-
public function testDCount($expectedResult, $database, $field, $criteria): void
18+
public function testDirectCallToDCount($expectedResult, $database, $field, $criteria): void
1619
{
17-
$this->runTestCase('DCOUNT', $expectedResult, $database, $field, $criteria);
20+
$result = DCount::evaluate($database, $field, $criteria);
21+
self::assertEqualsWithDelta($expectedResult, $result, 1.0e-12);
22+
}
23+
24+
/**
25+
* @dataProvider providerDCount
26+
*
27+
* @param mixed $expectedResult
28+
* @param mixed $database
29+
* @param mixed $field
30+
* @param mixed $criteria
31+
*/
32+
public function testDCountAsWorksheetFormula($expectedResult, $database, $field, $criteria): void
33+
{
34+
$this->prepareWorksheetWithFormula('DCOUNT', $database, $field, $criteria);
35+
36+
$result = $this->getSheet()->getCell(self::RESULT_CELL)->getCalculatedValue();
37+
self::assertEqualsWithDelta($expectedResult, $result, 1.0e-12);
1838
}
1939

2040
private function database4(): array
@@ -62,15 +82,6 @@ public function providerDCount(): array
6282
['Math', 'Female'],
6383
],
6484
],
65-
'omitted field name' => [
66-
'#VALUE!',
67-
$this->database3(),
68-
null,
69-
[
70-
['Subject', 'Score'],
71-
['English', '>63%'],
72-
],
73-
],
7485
[
7586
3,
7687
$this->database4(),
@@ -95,18 +106,27 @@ public function providerDCount(): array
95106
1,
96107
$this->database1(),
97108
],
109+
'omitted field name' => [
110+
ExcelError::VALUE(),
111+
$this->database3(),
112+
null,
113+
[
114+
['Subject', 'Score'],
115+
['English', '>63%'],
116+
],
117+
],
98118
/* Excel seems to return #NAME? when column number
99119
is too high or too low. This makes so little sense
100120
to me that I'm not going to bother coding that up,
101121
content to return #VALUE! as an invalid name would */
102122
'field column number too high' => [
103-
'#VALUE!',
123+
ExcelError::VALUE(),
104124
$this->database1(),
105125
99,
106126
$this->database1(),
107127
],
108128
'field column number too low' => [
109-
'#VALUE!',
129+
ExcelError::VALUE(),
110130
$this->database1(),
111131
0,
112132
$this->database1(),

tests/PhpSpreadsheetTests/Calculation/Functions/Database/DGetTest.php

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\Database;
44

5+
use PhpOffice\PhpSpreadsheet\Calculation\Database\DGet;
6+
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
7+
58
class DGetTest extends AllSetupTeardown
69
{
710
/**
@@ -12,16 +15,33 @@ class DGetTest extends AllSetupTeardown
1215
* @param mixed $field
1316
* @param mixed $criteria
1417
*/
15-
public function testDGet($expectedResult, $database, $field, $criteria): void
18+
public function testDirectCallToDGet($expectedResult, $database, $field, $criteria): void
1619
{
17-
$this->runTestCase('DGET', $expectedResult, $database, $field, $criteria);
20+
$result = DGet::evaluate($database, $field, $criteria);
21+
self::assertEqualsWithDelta($expectedResult, $result, 1.0e-12);
22+
}
23+
24+
/**
25+
* @dataProvider providerDGet
26+
*
27+
* @param mixed $expectedResult
28+
* @param mixed $database
29+
* @param mixed $field
30+
* @param mixed $criteria
31+
*/
32+
public function testDGetAsWorksheetFormula($expectedResult, $database, $field, $criteria): void
33+
{
34+
$this->prepareWorksheetWithFormula('DGET', $database, $field, $criteria);
35+
36+
$result = $this->getSheet()->getCell(self::RESULT_CELL)->getCalculatedValue();
37+
self::assertEqualsWithDelta($expectedResult, $result, 1.0e-12);
1838
}
1939

2040
public function providerDGet(): array
2141
{
2242
return [
2343
[
24-
'#NUM!',
44+
ExcelError::NAN(),
2545
$this->database1(),
2646
'Yield',
2747
[
@@ -50,7 +70,7 @@ public function providerDGet(): array
5070
],
5171
],
5272
[
53-
'#NUM!',
73+
ExcelError::NAN(),
5474
$this->database2(),
5575
'Sales',
5676
[
@@ -59,7 +79,7 @@ public function providerDGet(): array
5979
],
6080
],
6181
'omitted field name' => [
62-
'#VALUE!',
82+
ExcelError::VALUE(),
6383
$this->database1(),
6484
null,
6585
$this->database1(),

0 commit comments

Comments
 (0)