Skip to content

Commit ca1ff07

Browse files
authored
Fix Unintential Deprecated Calls in Tests - DATABASE (#3175)
* Fix Unintential Deprecated Calls in Tests - DATABASE I think it's best to install these before PR #3166. There are no changes to source code, only to test members which continue to inadvertently use calls to deprecated functions. * Fix deprecation Blocks Deprecated->deprecated, adjust see and comments * Fix Deliberate Deprecated Tests Add annotations. * Change Unit Tests to Run in Spreadsheet Context ... rather than as direct calls. The major difference is that specifying an invalid column should result in an Excel error, not null. Minor code changes were needed, including to Statistical/Conditional which sometimes calls Database. * Correct Some DocBlocks Null is no longer a possible output for most of these functions. * 2 Overlooked Tests Change to run in spreadsheet context. * T Function Should Return Null-String, not Null Fix it.
1 parent 717ec4f commit ca1ff07

37 files changed

+674
-848
lines changed

phpstan-baseline.neon

Lines changed: 0 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,5 @@
11
parameters:
22
ignoreErrors:
3-
-
4-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Database\\:\\:DMAX\\(\\) should return float but returns float\\|string\\|null\\.$#"
5-
count: 1
6-
path: src/PhpSpreadsheet/Calculation/Database.php
7-
-
8-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Database\\:\\:DMIN\\(\\) should return float but returns float\\|string\\|null\\.$#"
9-
count: 1
10-
path: src/PhpSpreadsheet/Calculation/Database.php
11-
-
12-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Database\\:\\:DPRODUCT\\(\\) should return float\\|string but returns float\\|string\\|null\\.$#"
13-
count: 1
14-
path: src/PhpSpreadsheet/Calculation/Database.php
15-
-
16-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Database\\:\\:DSTDEV\\(\\) should return float\\|string but returns float\\|string\\|null\\.$#"
17-
count: 1
18-
path: src/PhpSpreadsheet/Calculation/Database.php
19-
-
20-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Database\\:\\:DSTDEVP\\(\\) should return float\\|string but returns float\\|string\\|null\\.$#"
21-
count: 1
22-
path: src/PhpSpreadsheet/Calculation/Database.php
23-
-
24-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Database\\:\\:DSUM\\(\\) should return float\\|string but returns float\\|string\\|null\\.$#"
25-
count: 1
26-
path: src/PhpSpreadsheet/Calculation/Database.php
27-
-
28-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Database\\:\\:DVAR\\(\\) should return float\\|string but returns float\\|string\\|null\\.$#"
29-
count: 1
30-
path: src/PhpSpreadsheet/Calculation/Database.php
31-
-
32-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Database\\:\\:DVARP\\(\\) should return float\\|string but returns float\\|string\\|null\\.$#"
33-
count: 1
34-
path: src/PhpSpreadsheet/Calculation/Database.php
35-
-
36-
message: "#^Parameter \\#2 \\$field of static method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Database\\\\DCountA\\:\\:evaluate\\(\\) expects int\\|string, int\\|string\\|null given\\.$#"
37-
count: 1
38-
path: src/PhpSpreadsheet/Calculation/Database.php
39-
-
40-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Database\\\\DatabaseAbstract\\:\\:buildCondition\\(\\) has parameter \\$criterion with no type specified\\.$#"
41-
count: 1
42-
path: src/PhpSpreadsheet/Calculation/Database/DatabaseAbstract.php
43-
-
44-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Database\\\\DatabaseAbstract\\:\\:evaluate\\(\\) has no return type specified\\.$#"
45-
count: 1
46-
path: src/PhpSpreadsheet/Calculation/Database/DatabaseAbstract.php
47-
-
48-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Database\\\\DatabaseAbstract\\:\\:evaluate\\(\\) has parameter \\$criteria with no type specified\\.$#"
49-
count: 1
50-
path: src/PhpSpreadsheet/Calculation/Database/DatabaseAbstract.php
51-
-
52-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Database\\\\DatabaseAbstract\\:\\:evaluate\\(\\) has parameter \\$database with no type specified\\.$#"
53-
count: 1
54-
path: src/PhpSpreadsheet/Calculation/Database/DatabaseAbstract.php
55-
-
56-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Database\\\\DatabaseAbstract\\:\\:evaluate\\(\\) has parameter \\$field with no type specified\\.$#"
57-
count: 1
58-
path: src/PhpSpreadsheet/Calculation/Database/DatabaseAbstract.php
59-
-
60-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Database\\\\DatabaseAbstract\\:\\:processCondition\\(\\) has no return type specified\\.$#"
61-
count: 1
62-
path: src/PhpSpreadsheet/Calculation/Database/DatabaseAbstract.php
633
-
644
message: "#^Variable \\$dateValue on left side of \\?\\? always exists and is not nullable\\.$#"
655
count: 1

src/PhpSpreadsheet/Calculation/Database.php

Lines changed: 33 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
/**
66
* @deprecated 1.17.0
7+
*
8+
* @codeCoverageIgnore
79
*/
810
class Database
911
{
@@ -15,10 +17,9 @@ class Database
1517
* Excel Function:
1618
* DAVERAGE(database,field,criteria)
1719
*
18-
* @Deprecated 1.17.0
19-
*
20-
* @see Database\DAverage::evaluate()
20+
* @deprecated 1.17.0
2121
* Use the evaluate() method in the Database\DAverage class instead
22+
* @see Database\DAverage::evaluate()
2223
*
2324
* @param mixed[] $database The range of cells that makes up the list or database.
2425
* A database is a list of related data in which rows of related
@@ -35,7 +36,7 @@ class Database
3536
* the column label in which you specify a condition for the
3637
* column.
3738
*
38-
* @return null|float|string
39+
* @return float|string
3940
*/
4041
public static function DAVERAGE($database, $field, $criteria)
4142
{
@@ -51,10 +52,9 @@ public static function DAVERAGE($database, $field, $criteria)
5152
* Excel Function:
5253
* DCOUNT(database,[field],criteria)
5354
*
54-
* @Deprecated 1.17.0
55-
*
56-
* @see Database\DCount::evaluate()
55+
* @deprecated 1.17.0
5756
* Use the evaluate() method in the Database\DCount class instead
57+
* @see Database\DCount::evaluate()
5858
*
5959
* @param mixed[] $database The range of cells that makes up the list or database.
6060
* A database is a list of related data in which rows of related
@@ -71,7 +71,7 @@ public static function DAVERAGE($database, $field, $criteria)
7171
* the column label in which you specify a condition for the
7272
* column.
7373
*
74-
* @return int
74+
* @return int|string
7575
*
7676
* @TODO The field argument is optional. If field is omitted, DCOUNT counts all records in the
7777
* database that match the criteria.
@@ -89,16 +89,15 @@ public static function DCOUNT($database, $field, $criteria)
8989
* Excel Function:
9090
* DCOUNTA(database,[field],criteria)
9191
*
92-
* @Deprecated 1.17.0
93-
*
94-
* @see Database\DCountA::evaluate()
92+
* @deprecated 1.17.0
9593
* Use the evaluate() method in the Database\DCountA class instead
94+
* @see Database\DCountA::evaluate()
9695
*
9796
* @param mixed[] $database The range of cells that makes up the list or database.
9897
* A database is a list of related data in which rows of related
9998
* information are records, and columns of data are fields. The
10099
* first row of the list contains labels for each column.
101-
* @param null|int|string $field Indicates which column is used in the function. Enter the
100+
* @param int|string $field Indicates which column is used in the function. Enter the
102101
* column label enclosed between double quotation marks, such as
103102
* "Age" or "Yield," or a number (without quotation marks) that
104103
* represents the position of the column within the list: 1 for
@@ -109,7 +108,7 @@ public static function DCOUNT($database, $field, $criteria)
109108
* the column label in which you specify a condition for the
110109
* column.
111110
*
112-
* @return int
111+
* @return int|string
113112
*/
114113
public static function DCOUNTA($database, $field, $criteria)
115114
{
@@ -125,10 +124,9 @@ public static function DCOUNTA($database, $field, $criteria)
125124
* Excel Function:
126125
* DGET(database,field,criteria)
127126
*
128-
* @Deprecated 1.17.0
129-
*
130-
* @see Database\DGet::evaluate()
127+
* @deprecated 1.17.0
131128
* Use the evaluate() method in the Database\DGet class instead
129+
* @see Database\DGet::evaluate()
132130
*
133131
* @param mixed[] $database The range of cells that makes up the list or database.
134132
* A database is a list of related data in which rows of related
@@ -161,10 +159,9 @@ public static function DGET($database, $field, $criteria)
161159
* Excel Function:
162160
* DMAX(database,field,criteria)
163161
*
164-
* @Deprecated 1.17.0
165-
*
166-
* @see Database\DMax::evaluate()
162+
* @deprecated 1.17.0
167163
* Use the evaluate() method in the Database\DMax class instead
164+
* @see Database\DMax::evaluate()
168165
*
169166
* @param mixed[] $database The range of cells that makes up the list or database.
170167
* A database is a list of related data in which rows of related
@@ -181,7 +178,7 @@ public static function DGET($database, $field, $criteria)
181178
* the column label in which you specify a condition for the
182179
* column.
183180
*
184-
* @return float
181+
* @return null|float|string
185182
*/
186183
public static function DMAX($database, $field, $criteria)
187184
{
@@ -197,10 +194,9 @@ public static function DMAX($database, $field, $criteria)
197194
* Excel Function:
198195
* DMIN(database,field,criteria)
199196
*
200-
* @Deprecated 1.17.0
201-
*
202-
* @see Database\DMin::evaluate()
197+
* @deprecated 1.17.0
203198
* Use the evaluate() method in the Database\DMin class instead
199+
* @see Database\DMin::evaluate()
204200
*
205201
* @param mixed[] $database The range of cells that makes up the list or database.
206202
* A database is a list of related data in which rows of related
@@ -217,7 +213,7 @@ public static function DMAX($database, $field, $criteria)
217213
* the column label in which you specify a condition for the
218214
* column.
219215
*
220-
* @return float
216+
* @return null|float|string
221217
*/
222218
public static function DMIN($database, $field, $criteria)
223219
{
@@ -232,10 +228,9 @@ public static function DMIN($database, $field, $criteria)
232228
* Excel Function:
233229
* DPRODUCT(database,field,criteria)
234230
*
235-
* @Deprecated 1.17.0
236-
*
237-
* @see Database\DProduct::evaluate()
231+
* @deprecated 1.17.0
238232
* Use the evaluate() method in the Database\DProduct class instead
233+
* @see Database\DProduct::evaluate()
239234
*
240235
* @param mixed[] $database The range of cells that makes up the list or database.
241236
* A database is a list of related data in which rows of related
@@ -268,10 +263,9 @@ public static function DPRODUCT($database, $field, $criteria)
268263
* Excel Function:
269264
* DSTDEV(database,field,criteria)
270265
*
271-
* @Deprecated 1.17.0
272-
*
273-
* @see Database\DStDev::evaluate()
266+
* @deprecated 1.17.0
274267
* Use the evaluate() method in the Database\DStDev class instead
268+
* @see Database\DStDev::evaluate()
275269
*
276270
* @param mixed[] $database The range of cells that makes up the list or database.
277271
* A database is a list of related data in which rows of related
@@ -304,10 +298,9 @@ public static function DSTDEV($database, $field, $criteria)
304298
* Excel Function:
305299
* DSTDEVP(database,field,criteria)
306300
*
307-
* @Deprecated 1.17.0
308-
*
309-
* @see Database\DStDevP::evaluate()
301+
* @deprecated 1.17.0
310302
* Use the evaluate() method in the Database\DStDevP class instead
303+
* @see Database\DStDevP::evaluate()
311304
*
312305
* @param mixed[] $database The range of cells that makes up the list or database.
313306
* A database is a list of related data in which rows of related
@@ -339,10 +332,9 @@ public static function DSTDEVP($database, $field, $criteria)
339332
* Excel Function:
340333
* DSUM(database,field,criteria)
341334
*
342-
* @Deprecated 1.17.0
343-
*
344-
* @see Database\DSum::evaluate()
335+
* @deprecated 1.17.0
345336
* Use the evaluate() method in the Database\DSum class instead
337+
* @see Database\DSum::evaluate()
346338
*
347339
* @param mixed[] $database The range of cells that makes up the list or database.
348340
* A database is a list of related data in which rows of related
@@ -359,7 +351,7 @@ public static function DSTDEVP($database, $field, $criteria)
359351
* the column label in which you specify a condition for the
360352
* column.
361353
*
362-
* @return float|string
354+
* @return null|float|string
363355
*/
364356
public static function DSUM($database, $field, $criteria)
365357
{
@@ -375,10 +367,9 @@ public static function DSUM($database, $field, $criteria)
375367
* Excel Function:
376368
* DVAR(database,field,criteria)
377369
*
378-
* @Deprecated 1.17.0
379-
*
380-
* @see Database\DVar::evaluate()
370+
* @deprecated 1.17.0
381371
* Use the evaluate() method in the Database\DVar class instead
372+
* @see Database\DVar::evaluate()
382373
*
383374
* @param mixed[] $database The range of cells that makes up the list or database.
384375
* A database is a list of related data in which rows of related
@@ -411,10 +402,9 @@ public static function DVAR($database, $field, $criteria)
411402
* Excel Function:
412403
* DVARP(database,field,criteria)
413404
*
414-
* @Deprecated 1.17.0
415-
*
416-
* @see Database\DVarP::evaluate()
405+
* @deprecated 1.17.0
417406
* Use the evaluate() method in the Database\DVarP class instead
407+
* @see Database\DVarP::evaluate()
418408
*
419409
* @param mixed[] $database The range of cells that makes up the list or database.
420410
* A database is a list of related data in which rows of related

src/PhpSpreadsheet/Calculation/Database/DAverage.php

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

33
namespace PhpOffice\PhpSpreadsheet\Calculation\Database;
44

5+
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
56
use PhpOffice\PhpSpreadsheet\Calculation\Statistical\Averages;
67

78
class DAverage extends DatabaseAbstract
@@ -29,13 +30,13 @@ class DAverage extends DatabaseAbstract
2930
* the column label in which you specify a condition for the
3031
* column.
3132
*
32-
* @return null|float|string
33+
* @return float|string
3334
*/
3435
public static function evaluate($database, $field, $criteria)
3536
{
3637
$field = self::fieldExtract($database, $field);
3738
if ($field === null) {
38-
return null;
39+
return ExcelError::VALUE();
3940
}
4041

4142
return Averages::average(

src/PhpSpreadsheet/Calculation/Database/DCount.php

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

33
namespace PhpOffice\PhpSpreadsheet\Calculation\Database;
44

5+
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
56
use PhpOffice\PhpSpreadsheet\Calculation\Statistical\Counts;
67

78
class DCount extends DatabaseAbstract
@@ -30,11 +31,14 @@ class DCount extends DatabaseAbstract
3031
* the column label in which you specify a condition for the
3132
* column.
3233
*
33-
* @return int
34+
* @return int|string
3435
*/
35-
public static function evaluate($database, $field, $criteria)
36+
public static function evaluate($database, $field, $criteria, bool $returnError = true)
3637
{
3738
$field = self::fieldExtract($database, $field);
39+
if ($returnError && $field === null) {
40+
return ExcelError::VALUE();
41+
}
3842

3943
return Counts::COUNT(
4044
self::getFilteredColumn($database, $field, $criteria)

src/PhpSpreadsheet/Calculation/Database/DCountA.php

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

33
namespace PhpOffice\PhpSpreadsheet\Calculation\Database;
44

5+
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
56
use PhpOffice\PhpSpreadsheet\Calculation\Statistical\Counts;
67

78
class DCountA extends DatabaseAbstract
@@ -29,14 +30,17 @@ class DCountA extends DatabaseAbstract
2930
* the column label in which you specify a condition for the
3031
* column.
3132
*
32-
* @return int
33+
* @return int|string
3334
*/
3435
public static function evaluate($database, $field, $criteria)
3536
{
3637
$field = self::fieldExtract($database, $field);
38+
if ($field === null) {
39+
return ExcelError::VALUE();
40+
}
3741

3842
return Counts::COUNTA(
39-
self::getFilteredColumn($database, $field ?? 0, $criteria)
43+
self::getFilteredColumn($database, $field, $criteria)
4044
);
4145
}
4246
}

src/PhpSpreadsheet/Calculation/Database/DGet.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public static function evaluate($database, $field, $criteria)
3636
{
3737
$field = self::fieldExtract($database, $field);
3838
if ($field === null) {
39-
return null;
39+
return ExcelError::VALUE();
4040
}
4141

4242
$columnData = self::getFilteredColumn($database, $field, $criteria);

src/PhpSpreadsheet/Calculation/Database/DMax.php

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

33
namespace PhpOffice\PhpSpreadsheet\Calculation\Database;
44

5+
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
56
use PhpOffice\PhpSpreadsheet\Calculation\Statistical\Maximum;
67

78
class DMax extends DatabaseAbstract
@@ -32,11 +33,11 @@ class DMax extends DatabaseAbstract
3233
*
3334
* @return null|float|string
3435
*/
35-
public static function evaluate($database, $field, $criteria)
36+
public static function evaluate($database, $field, $criteria, bool $returnError = true)
3637
{
3738
$field = self::fieldExtract($database, $field);
3839
if ($field === null) {
39-
return null;
40+
return $returnError ? ExcelError::VALUE() : null;
4041
}
4142

4243
return Maximum::max(

0 commit comments

Comments
 (0)