Skip to content

Commit 00ededb

Browse files
committed
Phpstan Level 9 - Part 7 of Many: Functions, LookupRef
1 parent 729c4dc commit 00ededb

File tree

6 files changed

+29
-163
lines changed

6 files changed

+29
-163
lines changed

phpstan-baseline.neon

Lines changed: 0 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -1,155 +1,5 @@
11
parameters:
22
ignoreErrors:
3-
-
4-
message: '#^Binary operation "\." between ''\='' and mixed results in an error\.$#'
5-
identifier: binaryOp.invalid
6-
count: 1
7-
path: src/PhpSpreadsheet/Calculation/Functions.php
8-
9-
-
10-
message: '#^Binary operation "\." between string and mixed results in an error\.$#'
11-
identifier: binaryOp.invalid
12-
count: 1
13-
path: src/PhpSpreadsheet/Calculation/Functions.php
14-
15-
-
16-
message: '#^Parameter \#1 \$dateValue of static method PhpOffice\\PhpSpreadsheet\\Shared\\Date\:\:stringToExcel\(\) expects string, mixed given\.$#'
17-
identifier: argument.type
18-
count: 1
19-
path: src/PhpSpreadsheet/Calculation/Functions.php
20-
21-
-
22-
message: '#^Parameter \#1 \$haystack of function substr_count expects string, mixed given\.$#'
23-
identifier: argument.type
24-
count: 3
25-
path: src/PhpSpreadsheet/Calculation/Functions.php
26-
27-
-
28-
message: '#^Parameter \#1 \$string of function rtrim expects string, mixed given\.$#'
29-
identifier: argument.type
30-
count: 1
31-
path: src/PhpSpreadsheet/Calculation/Functions.php
32-
33-
-
34-
message: '#^Parameter \#1 \$string of function strtoupper expects string, mixed given\.$#'
35-
identifier: argument.type
36-
count: 3
37-
path: src/PhpSpreadsheet/Calculation/Functions.php
38-
39-
-
40-
message: '#^Parameter \#1 \$string of function trim expects string, mixed given\.$#'
41-
identifier: argument.type
42-
count: 2
43-
path: src/PhpSpreadsheet/Calculation/Functions.php
44-
45-
-
46-
message: '#^Parameter \#2 \$subject of function preg_match expects string, mixed given\.$#'
47-
identifier: argument.type
48-
count: 2
49-
path: src/PhpSpreadsheet/Calculation/Functions.php
50-
51-
-
52-
message: '#^Parameter \#3 \$subject of function preg_replace expects array\<float\|int\|string\>\|string, mixed given\.$#'
53-
identifier: argument.type
54-
count: 1
55-
path: src/PhpSpreadsheet/Calculation/Functions.php
56-
57-
-
58-
message: '#^Parameter \#3 \$subject of function str_replace expects array\<string\>\|string, mixed given\.$#'
59-
identifier: argument.type
60-
count: 1
61-
path: src/PhpSpreadsheet/Calculation/Functions.php
62-
63-
-
64-
message: '#^Parameter \#1 \$row of static method PhpOffice\\PhpSpreadsheet\\Calculation\\LookupRef\\Address\:\:formatAsA1\(\) expects int, mixed given\.$#'
65-
identifier: argument.type
66-
count: 1
67-
path: src/PhpSpreadsheet/Calculation/LookupRef/Address.php
68-
69-
-
70-
message: '#^Parameter \#1 \$row of static method PhpOffice\\PhpSpreadsheet\\Calculation\\LookupRef\\Address\:\:formatAsR1C1\(\) expects int, mixed given\.$#'
71-
identifier: argument.type
72-
count: 1
73-
path: src/PhpSpreadsheet/Calculation/LookupRef/Address.php
74-
75-
-
76-
message: '#^Parameter \#1 \$sheetName of static method PhpOffice\\PhpSpreadsheet\\Calculation\\LookupRef\\Address\:\:sheetName\(\) expects string, mixed given\.$#'
77-
identifier: argument.type
78-
count: 1
79-
path: src/PhpSpreadsheet/Calculation/LookupRef/Address.php
80-
81-
-
82-
message: '#^Parameter \#2 \$column of static method PhpOffice\\PhpSpreadsheet\\Calculation\\LookupRef\\Address\:\:formatAsA1\(\) expects int, mixed given\.$#'
83-
identifier: argument.type
84-
count: 1
85-
path: src/PhpSpreadsheet/Calculation/LookupRef/Address.php
86-
87-
-
88-
message: '#^Parameter \#2 \$column of static method PhpOffice\\PhpSpreadsheet\\Calculation\\LookupRef\\Address\:\:formatAsR1C1\(\) expects int, mixed given\.$#'
89-
identifier: argument.type
90-
count: 1
91-
path: src/PhpSpreadsheet/Calculation/LookupRef/Address.php
92-
93-
-
94-
message: '#^Parameter \#3 \$relativity of static method PhpOffice\\PhpSpreadsheet\\Calculation\\LookupRef\\Address\:\:formatAsA1\(\) expects int, mixed given\.$#'
95-
identifier: argument.type
96-
count: 1
97-
path: src/PhpSpreadsheet/Calculation/LookupRef/Address.php
98-
99-
-
100-
message: '#^Parameter \#3 \$relativity of static method PhpOffice\\PhpSpreadsheet\\Calculation\\LookupRef\\Address\:\:formatAsR1C1\(\) expects int, mixed given\.$#'
101-
identifier: argument.type
102-
count: 1
103-
path: src/PhpSpreadsheet/Calculation/LookupRef/Address.php
104-
105-
-
106-
message: '#^Cannot use \+\+ on mixed\.$#'
107-
identifier: preInc.type
108-
count: 1
109-
path: src/PhpSpreadsheet/Calculation/LookupRef/ExcelMatch.php
110-
111-
-
112-
message: '#^Method PhpOffice\\PhpSpreadsheet\\Calculation\\LookupRef\\Formula\:\:text\(\) should return string but returns mixed\.$#'
113-
identifier: return.type
114-
count: 1
115-
path: src/PhpSpreadsheet/Calculation/LookupRef/Formula.php
116-
117-
-
118-
message: '#^Parameter \#1 \$coordinate of method PhpOffice\\PhpSpreadsheet\\Worksheet\\Worksheet\:\:cellExists\(\) expects array\{int, int\}\|PhpOffice\\PhpSpreadsheet\\Cell\\CellAddress\|string, mixed given\.$#'
119-
identifier: argument.type
120-
count: 1
121-
path: src/PhpSpreadsheet/Calculation/LookupRef/Formula.php
122-
123-
-
124-
message: '#^Parameter \#1 \$coordinate of method PhpOffice\\PhpSpreadsheet\\Worksheet\\Worksheet\:\:getCell\(\) expects array\{int, int\}\|PhpOffice\\PhpSpreadsheet\\Cell\\CellAddress\|string, mixed given\.$#'
125-
identifier: argument.type
126-
count: 2
127-
path: src/PhpSpreadsheet/Calculation/LookupRef/Formula.php
128-
129-
-
130-
message: '#^Parameter \#2 \$subject of function preg_match expects string, mixed given\.$#'
131-
identifier: argument.type
132-
count: 1
133-
path: src/PhpSpreadsheet/Calculation/LookupRef/Formula.php
134-
135-
-
136-
message: '#^Cannot cast mixed to string\.$#'
137-
identifier: cast.string
138-
count: 1
139-
path: src/PhpSpreadsheet/Calculation/LookupRef/HLookup.php
140-
141-
-
142-
message: '#^Parameter \#1 \$lookupArray of static method PhpOffice\\PhpSpreadsheet\\Calculation\\LookupRef\\HLookup\:\:convertLiteralArray\(\) expects array, mixed given\.$#'
143-
identifier: argument.type
144-
count: 1
145-
path: src/PhpSpreadsheet/Calculation/LookupRef/HLookup.php
146-
147-
-
148-
message: '#^Parameter \#2 \$index_number of static method PhpOffice\\PhpSpreadsheet\\Calculation\\LookupRef\\LookupBase\:\:validateIndexLookup\(\) expects float\|int\|string, mixed given\.$#'
149-
identifier: argument.type
150-
count: 1
151-
path: src/PhpSpreadsheet/Calculation/LookupRef/HLookup.php
152-
1533
-
1544
message: '#^Method PhpOffice\\PhpSpreadsheet\\Calculation\\LookupRef\\Hyperlink\:\:set\(\) should return string but returns mixed\.$#'
1555
identifier: return.type

src/PhpSpreadsheet/Calculation/Functions.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PhpOffice\PhpSpreadsheet\Cell\Cell;
66
use PhpOffice\PhpSpreadsheet\Shared\Date;
7+
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
78

89
class Functions
910
{
@@ -130,16 +131,22 @@ public static function DUMMY(): string
130131

131132
public static function isMatrixValue(mixed $idx): bool
132133
{
134+
$idx = StringHelper::convertToString($idx);
135+
133136
return (substr_count($idx, '.') <= 1) || (preg_match('/\.[A-Z]/', $idx) > 0);
134137
}
135138

136139
public static function isValue(mixed $idx): bool
137140
{
141+
$idx = StringHelper::convertToString($idx);
142+
138143
return substr_count($idx, '.') === 0;
139144
}
140145

141146
public static function isCellValue(mixed $idx): bool
142147
{
148+
$idx = StringHelper::convertToString($idx);
149+
143150
return substr_count($idx, '.') > 1;
144151
}
145152

@@ -154,37 +161,41 @@ public static function ifCondition(mixed $condition): string
154161
$condition = self::operandSpecialHandling($condition);
155162
if (is_bool($condition)) {
156163
return '=' . ($condition ? 'TRUE' : 'FALSE');
157-
} elseif (!is_numeric($condition)) {
164+
}
165+
if (!is_numeric($condition)) {
158166
if ($condition !== '""') { // Not an empty string
159167
// Escape any quotes in the string value
160168
$condition = (string) preg_replace('/"/ui', '""', $condition);
161169
}
162170
$condition = Calculation::wrapResult(strtoupper($condition));
163171
}
164172

165-
return str_replace('""""', '""', '=' . $condition);
173+
return str_replace('""""', '""', '=' . StringHelper::convertToString($condition));
166174
}
167175
$operator = $operand = '';
168176
if (1 === preg_match('/(=|<[>=]?|>=?)(.*)/', $condition, $matches)) {
169177
[, $operator, $operand] = $matches;
170178
}
171179

172-
$operand = self::operandSpecialHandling($operand);
180+
$operand = (string) self::operandSpecialHandling($operand);
173181
if (is_numeric(trim($operand, '"'))) {
174182
$operand = trim($operand, '"');
175183
} elseif (!is_numeric($operand) && $operand !== 'FALSE' && $operand !== 'TRUE') {
176184
$operand = str_replace('"', '""', $operand);
177185
$operand = Calculation::wrapResult(strtoupper($operand));
186+
$operand = StringHelper::convertToString($operand);
178187
}
179188

180189
return str_replace('""""', '""', $operator . $operand);
181190
}
182191

183-
private static function operandSpecialHandling(mixed $operand): mixed
192+
private static function operandSpecialHandling(mixed $operand): bool|float|int|string
184193
{
185194
if (is_numeric($operand) || is_bool($operand)) {
186195
return $operand;
187-
} elseif (strtoupper($operand) === Calculation::getTRUE() || strtoupper($operand) === Calculation::getFALSE()) {
196+
}
197+
$operand = StringHelper::convertToString($operand);
198+
if (strtoupper($operand) === Calculation::getTRUE() || strtoupper($operand) === Calculation::getFALSE()) {
188199
return strtoupper($operand);
189200
}
190201

src/PhpSpreadsheet/Calculation/LookupRef/Address.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
77
use PhpOffice\PhpSpreadsheet\Cell\AddressHelper;
88
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
9+
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
910

1011
class Address
1112
{
@@ -63,14 +64,16 @@ public static function cell(mixed $row, mixed $column, mixed $relativity = 1, mi
6364
);
6465
}
6566

66-
$relativity = $relativity ?? 1;
67+
$relativity = ($relativity === null) ? 1 : (int) StringHelper::convertToString($relativity);
6768
$referenceStyle = $referenceStyle ?? true;
69+
$row = (int) StringHelper::convertToString($row);
70+
$column = (int) StringHelper::convertToString($column);
6871

6972
if (($row < 1) || ($column < 1)) {
7073
return ExcelError::VALUE();
7174
}
7275

73-
$sheetName = self::sheetName($sheetName);
76+
$sheetName = self::sheetName(StringHelper::convertToString($sheetName));
7477

7578
if (is_int($referenceStyle)) {
7679
$referenceStyle = (bool) $referenceStyle;

src/PhpSpreadsheet/Calculation/LookupRef/ExcelMatch.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public static function MATCH(mixed $lookupValue, mixed $lookupArray, mixed $matc
7070
};
7171

7272
if ($valueKey !== null) {
73-
return ++$valueKey;
73+
return ++$valueKey; //* @phpstan-ignore-line
7474
}
7575

7676
// Unsuccessful in finding a match, return #N/A error value

src/PhpSpreadsheet/Calculation/LookupRef/Formula.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
66
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
77
use PhpOffice\PhpSpreadsheet\Cell\Cell;
8+
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
89

910
class Formula
1011
{
@@ -21,6 +22,7 @@ public static function text(mixed $cellReference = '', ?Cell $cell = null): stri
2122
}
2223

2324
$worksheet = null;
25+
$cellReference = StringHelper::convertToString($cellReference);
2426
if (1 === preg_match('/^' . Calculation::CALCULATION_REGEXP_CELLREF . '$/i', $cellReference, $matches)) {
2527
$cellReference = $matches[6] . $matches[7];
2628
$worksheetName = trim($matches[3], "'");
@@ -37,6 +39,6 @@ public static function text(mixed $cellReference = '', ?Cell $cell = null): stri
3739
return ExcelError::NA();
3840
}
3941

40-
return $worksheet->getCell($cellReference)->getValue();
42+
return $worksheet->getCell($cellReference)->getValueString();
4143
}
4244
}

src/PhpSpreadsheet/Calculation/LookupRef/HLookup.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ class HLookup extends LookupBase
1818
* in the same column based on the index_number.
1919
*
2020
* @param mixed $lookupValue The value that you want to match in lookup_array
21-
* @param mixed $lookupArray The range of cells being searched
22-
* @param mixed $indexNumber The row number in table_array from which the matching value must be returned.
21+
* @param array $lookupArray The range of cells being searched
22+
* @param array|float|int|string $indexNumber The row number in table_array from which the matching value must be returned.
2323
* The first row is 1.
2424
* @param mixed $notExactMatch determines if you are looking for an exact match based on lookup_value
2525
*
2626
* @return mixed The value of the found cell
2727
*/
28-
public static function lookup(mixed $lookupValue, mixed $lookupArray, mixed $indexNumber, mixed $notExactMatch = true): mixed
28+
public static function lookup(mixed $lookupValue, $lookupArray, $indexNumber, mixed $notExactMatch = true): mixed
2929
{
3030
if (is_array($lookupValue) || is_array($indexNumber)) {
3131
return self::evaluateArrayArgumentsIgnore([self::class, __FUNCTION__], 1, $lookupValue, $lookupArray, $indexNumber, $notExactMatch);
@@ -66,7 +66,7 @@ public static function lookup(mixed $lookupValue, mixed $lookupArray, mixed $ind
6666
*/
6767
private static function hLookupSearch(mixed $lookupValue, array $lookupArray, $column, bool $notExactMatch): ?int
6868
{
69-
$lookupLower = StringHelper::strToLower((string) $lookupValue);
69+
$lookupLower = StringHelper::strToLower(StringHelper::convertToString($lookupValue));
7070

7171
$rowNumber = null;
7272
foreach ($lookupArray[$column] as $rowKey => $rowData) {

0 commit comments

Comments
 (0)