Skip to content

Commit 6b2767c

Browse files
authored
Merge pull request PHPOffice#4475 from oleibman/stan1008
Phpstan Level 10 Prep Penultimate
2 parents 85a9a39 + 645d9fe commit 6b2767c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+316
-104
lines changed

src/PhpSpreadsheet/Calculation/Engine/FormattedNumber.php

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,18 @@ class FormattedNumber
1616
// preg_quoted string for major currency symbols, with a %s for locale currency
1717
private const CURRENCY_CONVERSION_LIST = '\$€£¥%s';
1818

19-
private const STRING_CONVERSION_LIST = [
20-
[self::class, 'convertToNumberIfNumeric'],
21-
[self::class, 'convertToNumberIfFraction'],
22-
[self::class, 'convertToNumberIfPercent'],
23-
[self::class, 'convertToNumberIfCurrency'],
24-
];
25-
2619
/**
2720
* Identify whether a string contains a formatted numeric value,
2821
* and convert it to a numeric if it is.
2922
*
30-
* @param string $operand string value to test
23+
* @param float|string $operand string value to test
3124
*/
32-
public static function convertToNumberIfFormatted(string &$operand): bool
25+
public static function convertToNumberIfFormatted(float|string &$operand): bool
3326
{
34-
foreach (self::STRING_CONVERSION_LIST as $conversionMethod) {
35-
if ($conversionMethod($operand) === true) {
36-
return true;
37-
}
38-
}
39-
40-
return false;
27+
return self::convertToNumberIfNumeric($operand)
28+
|| self::convertToNumberIfFraction($operand)
29+
|| self::convertToNumberIfPercent($operand)
30+
|| self::convertToNumberIfCurrency($operand);
4131
}
4232

4333
/**
@@ -68,9 +58,9 @@ public static function convertToNumberIfNumeric(float|string &$operand): bool
6858
*
6959
* @param string $operand string value to test
7060
*/
71-
public static function convertToNumberIfFraction(string &$operand): bool
61+
public static function convertToNumberIfFraction(float|string &$operand): bool
7262
{
73-
if (preg_match(self::STRING_REGEXP_FRACTION, $operand, $match)) {
63+
if (is_string($operand) && preg_match(self::STRING_REGEXP_FRACTION, $operand, $match)) {
7464
$sign = ($match[1] === '-') ? '-' : '+';
7565
$wholePart = ($match[3] === '') ? '' : ($sign . $match[3]);
7666
$fractionFormula = '=' . $wholePart . $sign . $match[4];

src/PhpSpreadsheet/Calculation/Engine/Operands/StructuredReference.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,15 @@ final class StructuredReference implements Operand, Stringable
4747

4848
private ?int $totalsRow;
4949

50+
/** @var mixed[] */
5051
private array $columns;
5152

5253
public function __construct(string $structuredReference)
5354
{
5455
$this->value = $structuredReference;
5556
}
5657

58+
/** @param string[] $matches */
5759
public static function fromParser(string $formula, int $index, array $matches): self
5860
{
5961
$val = $matches[0];
@@ -171,6 +173,11 @@ private function getTableByName(Cell $cell): Table
171173
return $table;
172174
}
173175

176+
/**
177+
* @param array<array<int|string>> $tableRange
178+
*
179+
* @return mixed[]
180+
*/
174181
private function getColumns(Cell $cell, array $tableRange): array
175182
{
176183
$worksheet = $cell->getWorksheet();
@@ -179,6 +186,7 @@ private function getColumns(Cell $cell, array $tableRange): array
179186
$columns = [];
180187
$lastColumn = ++$tableRange[1][0];
181188
for ($column = $tableRange[0][0]; $column !== $lastColumn; ++$column) {
189+
/** @var string $column */
182190
$columns[$column] = $worksheet
183191
->getCell($column . ($this->headersRow ?? ($this->firstDataRow - 1)))
184192
->getCalculatedValue();
@@ -196,7 +204,7 @@ private function getRowReference(Cell $cell): string
196204
$reference = str_replace('[' . self::ITEM_SPECIFIER_THIS_ROW . '],', '', $reference);
197205

198206
foreach ($this->columns as $columnId => $columnName) {
199-
$columnName = str_replace("\u{a0}", ' ', $columnName);
207+
$columnName = str_replace("\u{a0}", ' ', $columnName); //* @phpstan-ignore-line
200208
$reference = $this->adjustRowReference($columnName, $reference, $cell, $columnId);
201209
}
202210

@@ -330,7 +338,7 @@ private function getColumnsForColumnReference(string $reference, int $startRow,
330338
{
331339
$columnsSelected = false;
332340
foreach ($this->columns as $columnId => $columnName) {
333-
$columnName = str_replace("\u{a0}", ' ', $columnName ?? '');
341+
$columnName = str_replace("\u{a0}", ' ', $columnName ?? ''); //* @phpstan-ignore-line
334342
$cellFrom = "{$columnId}{$startRow}";
335343
$cellTo = "{$columnId}{$endRow}";
336344
$cellReference = ($cellFrom === $cellTo) ? $cellFrom : "{$cellFrom}:{$cellTo}";

src/PhpSpreadsheet/Calculation/Logical/Operations.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,10 @@ public static function NOT(mixed $logical = false): array|bool|string
130130
return !$logical;
131131
}
132132

133-
/** @param mixed[] $args */
133+
/**
134+
* @param mixed[] $args
135+
* @param callable(int, int): bool $func
136+
*/
134137
private static function countTrueValues(array $args, callable $func): bool|string
135138
{
136139
$trueValueCount = 0;

src/PhpSpreadsheet/Calculation/LookupRef/Address.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class Address
4545
* @param mixed $sheetName Optional Name of worksheet to use
4646
* Or can be an array of values
4747
*
48-
* @return array|string If an array of values is passed as the $testValue argument, then the returned result will also be
48+
* @return mixed[]|string If an array of values is passed as the $testValue argument, then the returned result will also be
4949
* an array with the same dimensions
5050
*/
5151
public static function cell(mixed $row, mixed $column, mixed $relativity = 1, mixed $referenceStyle = true, mixed $sheetName = ''): array|string

src/PhpSpreadsheet/Calculation/LookupRef/ChooseRowsEtc.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ public static function chooseRows(mixed $input, mixed ...$args): array|string
8080
return $outputArray;
8181
}
8282

83+
/**
84+
* @param mixed[] $array
85+
*
86+
* @return mixed[]|string
87+
*/
8388
private static function dropRows(array $array, mixed $offset): array|string
8489
{
8590
if ($offset === null) {
@@ -134,6 +139,11 @@ public static function drop(mixed $input, mixed $rows = null, mixed $columns = n
134139
return self::transpose($outputArray3);
135140
}
136141

142+
/**
143+
* @param mixed[] $array
144+
*
145+
* @return mixed[]|string
146+
*/
137147
private static function takeRows(array $array, mixed $offset): array|string
138148
{
139149
if ($offset === null) {

src/PhpSpreadsheet/Calculation/LookupRef/ExcelMatch.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class ExcelMatch
3030
* @param mixed $matchType The number -1, 0, or 1. -1 means above, 0 means exact match, 1 means below.
3131
* If match_type is 1 or -1, the list has to be ordered.
3232
*
33-
* @return array|float|int|string The relative position of the found item
33+
* @return array<mixed>|float|int|string The relative position of the found item
3434
*/
3535
public static function MATCH(mixed $lookupValue, mixed $lookupArray, mixed $matchType = self::MATCHTYPE_LARGEST_VALUE): array|string|int|float
3636
{
@@ -77,6 +77,7 @@ public static function MATCH(mixed $lookupValue, mixed $lookupArray, mixed $matc
7777
return ExcelError::NA();
7878
}
7979

80+
/** @param mixed[] $lookupArray */
8081
private static function matchFirstValue(array $lookupArray, mixed $lookupValue): int|string|null
8182
{
8283
if (is_string($lookupValue)) {
@@ -113,6 +114,10 @@ private static function matchFirstValue(array $lookupArray, mixed $lookupValue):
113114
return null;
114115
}
115116

117+
/**
118+
* @param mixed[] $lookupArray
119+
* @param mixed[] $keySet
120+
*/
116121
private static function matchLargestValue(array $lookupArray, mixed $lookupValue, array $keySet): mixed
117122
{
118123
if (is_string($lookupValue)) {
@@ -147,6 +152,7 @@ private static function matchLargestValue(array $lookupArray, mixed $lookupValue
147152
return null;
148153
}
149154

155+
/** @param mixed[] $lookupArray */
150156
private static function matchSmallestValue(array $lookupArray, mixed $lookupValue): int|string|null
151157
{
152158
$valueKey = null;
@@ -215,6 +221,7 @@ private static function validateMatchType(mixed $matchType): int
215221
return self::MATCHTYPE_FIRST_VALUE;
216222
}
217223

224+
/** @param mixed[] $lookupArray */
218225
private static function validateLookupArray(array $lookupArray): void
219226
{
220227
// Lookup_array should not be empty
@@ -224,6 +231,11 @@ private static function validateLookupArray(array $lookupArray): void
224231
}
225232
}
226233

234+
/**
235+
* @param mixed[] $lookupArray
236+
*
237+
* @return mixed[]
238+
*/
227239
private static function prepareLookupArray(array $lookupArray, mixed $matchType): array
228240
{
229241
// Lookup_array should contain only number, text, or logical values, or empty (null) cells

src/PhpSpreadsheet/Calculation/LookupRef/Filter.php

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

77
class Filter
88
{
9+
/** @param mixed[] $lookupArray */
910
public static function filter(array $lookupArray, mixed $matchArray, mixed $ifEmpty = null): mixed
1011
{
1112
if (!is_array($matchArray)) {
@@ -21,10 +22,17 @@ public static function filter(array $lookupArray, mixed $matchArray, mixed $ifEm
2122
if (empty($result)) {
2223
return $ifEmpty ?? ExcelError::CALC();
2324
}
25+
/** @var callable(mixed): mixed */
26+
$func = 'array_values';
2427

25-
return array_values(array_map('array_values', $result));
28+
return array_values(array_map($func, $result));
2629
}
2730

31+
/**
32+
* @param mixed[] $sortArray
33+
*
34+
* @return mixed[]
35+
*/
2836
private static function enumerateArrayKeys(array $sortArray): array
2937
{
3038
array_walk(
@@ -39,6 +47,12 @@ function (&$columns): void {
3947
return array_values($sortArray);
4048
}
4149

50+
/**
51+
* @param mixed[] $lookupArray
52+
* @param mixed[] $matchArray
53+
*
54+
* @return mixed[]
55+
*/
4256
private static function filterByRow(array $lookupArray, array $matchArray): array
4357
{
4458
$matchArray = array_values(array_column($matchArray, 0)); // @phpstan-ignore-line
@@ -50,14 +64,20 @@ private static function filterByRow(array $lookupArray, array $matchArray): arra
5064
);
5165
}
5266

67+
/**
68+
* @param mixed[] $lookupArray
69+
* @param mixed[] $matchArray
70+
*
71+
* @return mixed[]
72+
*/
5373
private static function filterByColumn(array $lookupArray, array $matchArray): array
5474
{
5575
$lookupArray = Matrix::transpose($lookupArray);
5676

5777
if (count($matchArray) === 1) {
5878
$matchArray = array_pop($matchArray);
5979
}
60-
80+
/** @var mixed[] $matchArray */
6181
array_walk(
6282
$matchArray,
6383
function (&$value): void {

src/PhpSpreadsheet/Calculation/LookupRef/HLookup.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ 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 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.
21+
* @param mixed[][] $lookupArray The range of cells being searched
22+
* @param array<mixed>|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
*
@@ -49,6 +49,7 @@ public static function lookup(mixed $lookupValue, $lookupArray, $indexNumber, mi
4949

5050
$firstkey = $f[0] - 1;
5151
$returnColumn = $firstkey + $indexNumber;
52+
/** @var mixed[][] $lookupArray */
5253
$firstColumn = array_shift($f) ?? 1;
5354
$rowNumber = self::hLookupSearch($lookupValue, $lookupArray, $firstColumn, $notExactMatch);
5455

@@ -62,6 +63,7 @@ public static function lookup(mixed $lookupValue, $lookupArray, $indexNumber, mi
6263

6364
/**
6465
* @param mixed $lookupValue The value that you want to match in lookup_array
66+
* @param mixed[][] $lookupArray
6567
* @param int|string $column
6668
*/
6769
private static function hLookupSearch(mixed $lookupValue, array $lookupArray, $column, bool $notExactMatch): ?int
@@ -71,8 +73,10 @@ private static function hLookupSearch(mixed $lookupValue, array $lookupArray, $c
7173
$rowNumber = null;
7274
foreach ($lookupArray[$column] as $rowKey => $rowData) {
7375
// break if we have passed possible keys
76+
/** @var string $rowKey */
7477
$bothNumeric = is_numeric($lookupValue) && is_numeric($rowData);
7578
$bothNotNumeric = !is_numeric($lookupValue) && !is_numeric($rowData);
79+
/** @var scalar $rowData */
7680
$cellDataLower = StringHelper::strToLower((string) $rowData);
7781

7882
if (
@@ -96,6 +100,11 @@ private static function hLookupSearch(mixed $lookupValue, array $lookupArray, $c
96100
return $rowNumber;
97101
}
98102

103+
/**
104+
* @param mixed[] $lookupArray
105+
*
106+
* @return mixed[]
107+
*/
99108
private static function convertLiteralArray(array $lookupArray): array
100109
{
101110
if (array_key_exists(0, $lookupArray)) {

src/PhpSpreadsheet/Calculation/LookupRef/Helpers.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ private static function adjustSheetTitle(string &$sheetTitle, ?string $value): v
3535
}
3636
}
3737

38+
/** @return array{string, ?string, string} */
3839
public static function extractCellAddresses(string $cellAddress, bool $a1, Worksheet $sheet, string $sheetName = '', ?int $baseRow = null, ?int $baseCol = null): array
3940
{
4041
$cellAddress1 = $cellAddress;
@@ -57,6 +58,7 @@ public static function extractCellAddresses(string $cellAddress, bool $a1, Works
5758
return [$cellAddress1, $cellAddress2, $cellAddress];
5859
}
5960

61+
/** @return array{string, ?Worksheet, string} */
6062
public static function extractWorksheet(string $cellAddress, Cell $cell): array
6163
{
6264
$sheetName = '';

src/PhpSpreadsheet/Calculation/LookupRef/Indirect.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ private static function a1Format(mixed $a1fmt): bool
3434

3535
/**
3636
* Convert cellAddress to string, verify not null string.
37+
*
38+
* @param null|mixed[]|string $cellAddress
3739
*/
3840
private static function validateAddress(array|string|null $cellAddress): string
3941
{
@@ -54,12 +56,12 @@ private static function validateAddress(array|string|null $cellAddress): string
5456
* Excel Function:
5557
* =INDIRECT(cellAddress, bool) where the bool argument is optional
5658
*
57-
* @param array|string $cellAddress $cellAddress The cell address of the current cell (containing this formula)
59+
* @param mixed[]|string $cellAddress $cellAddress The cell address of the current cell (containing this formula)
5860
* @param mixed $a1fmt Expect bool Helpers::CELLADDRESS_USE_A1 or CELLADDRESS_USE_R1C1,
5961
* but can be provided as numeric which is cast to bool
6062
* @param Cell $cell The current cell (containing this formula)
6163
*
62-
* @return array|string An array containing a cell or range of cells, or a string on error
64+
* @return mixed[]|string An array containing a cell or range of cells, or a string on error
6365
*/
6466
public static function INDIRECT($cellAddress, mixed $a1fmt, Cell $cell): string|array
6567
{
@@ -99,7 +101,7 @@ public static function INDIRECT($cellAddress, mixed $a1fmt, Cell $cell): string|
99101
/**
100102
* Extract range values.
101103
*
102-
* @return array Array of values in range if range contains more than one element.
104+
* @return mixed[] Array of values in range if range contains more than one element.
103105
* Otherwise, a single value is returned.
104106
*/
105107
private static function extractRequiredCells(?Worksheet $worksheet, string $cellAddress): array

0 commit comments

Comments
 (0)