Skip to content

Commit 6ac7d5f

Browse files
authored
Merge pull request #4158 from oleibman/stanupgrade
Upgrade Phpstan
2 parents 37bb153 + 95a83a1 commit 6ac7d5f

File tree

20 files changed

+85
-55
lines changed

20 files changed

+85
-55
lines changed

composer.lock

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/PhpSpreadsheet/Calculation/Calculation.php

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4160,8 +4160,10 @@ private function internalParseFormula(string $formula, ?Cell $cell = null): bool
41604160
}
41614161
}
41624162
} elseif ($expectedArgumentCount != '*') {
4163-
preg_match('/(\d*)([-+,])(\d*)/', $expectedArgumentCount, $argMatch);
4164-
switch ($argMatch[2] ?? '') {
4163+
if (1 !== preg_match('/(\d*)([-+,])(\d*)/', $expectedArgumentCount, $argMatch)) {
4164+
$argMatch = ['', '', '', ''];
4165+
}
4166+
switch ($argMatch[2]) {
41654167
case '+':
41664168
if ($argumentCount < $argMatch[1]) {
41674169
$argumentCountError = true;
@@ -4234,7 +4236,7 @@ private function internalParseFormula(string $formula, ?Cell $cell = null): bool
42344236
// do we now have a function/variable/number?
42354237
$expectingOperator = true;
42364238
$expectingOperand = false;
4237-
$val = $match[1];
4239+
$val = $match[1] ?? '';
42384240
$length = strlen($val);
42394241

42404242
if (preg_match('/^' . self::CALCULATION_REGEXP_FUNCTION . '$/miu', $val, $matches)) {
@@ -4290,7 +4292,7 @@ private function internalParseFormula(string $formula, ?Cell $cell = null): bool
42904292
$rangeStartCellRef = $output[count($output) - 2]['value'] ?? '';
42914293
}
42924294
preg_match('/^' . self::CALCULATION_REGEXP_CELLREF . '$/miu', $rangeStartCellRef, $rangeStartMatches);
4293-
if ($rangeStartMatches[2] !== $matches[2]) {
4295+
if (isset($rangeStartMatches[2]) && $rangeStartMatches[2] !== $matches[2]) {
42944296
return $this->raiseFormulaError('3D Range references are not yet supported');
42954297
}
42964298
}
@@ -4380,7 +4382,7 @@ private function internalParseFormula(string $formula, ?Cell $cell = null): bool
43804382
$valx = $val;
43814383
$endRowColRef = ($refSheet !== null) ? $refSheet->getHighestDataColumn($valx) : AddressRange::MAX_COLUMN; // Max 16,384 columns for Excel2007
43824384
$val = "{$rangeWS2}{$endRowColRef}{$val}";
4383-
} elseif (ctype_alpha($val) && strlen($val ?? '') <= 3) {
4385+
} elseif (ctype_alpha($val) && is_string($val) && strlen($val) <= 3) {
43844386
// Column range
43854387
$stackItemType = 'Column Reference';
43864388
$endRowColRef = ($refSheet !== null) ? $refSheet->getHighestDataRow($val) : AddressRange::MAX_ROW; // Max 1,048,576 rows for Excel2007
@@ -4545,6 +4547,12 @@ private static function dataTestReference(array &$operandData): mixed
45454547
return $operand;
45464548
}
45474549

4550+
private static int $matchIndex8 = 8;
4551+
4552+
private static int $matchIndex9 = 9;
4553+
4554+
private static int $matchIndex10 = 10;
4555+
45484556
/**
45494557
* @return array<int, mixed>|false
45504558
*/
@@ -4908,12 +4916,17 @@ private function processTokenStack(mixed $tokens, ?string $cellID = null, ?Cell
49084916
} elseif (preg_match('/^' . self::CALCULATION_REGEXP_CELLREF . '$/i', $token ?? '', $matches)) {
49094917
$cellRef = null;
49104918

4911-
if (isset($matches[8])) {
4919+
/* Phpstan says matches[8/9/10] is never set,
4920+
and code coverage report seems to confirm.
4921+
Appease PhpStan for now;
4922+
probably delete this block later.
4923+
*/
4924+
if (isset($matches[self::$matchIndex8])) {
49124925
if ($cell === null) {
49134926
// We can't access the range, so return a REF error
49144927
$cellValue = ExcelError::REF();
49154928
} else {
4916-
$cellRef = $matches[6] . $matches[7] . ':' . $matches[9] . $matches[10];
4929+
$cellRef = $matches[6] . $matches[7] . ':' . $matches[self::$matchIndex9] . $matches[self::$matchIndex10];
49174930
if ($matches[2] > '') {
49184931
$matches[2] = trim($matches[2], "\"'");
49194932
if ((str_contains($matches[2], '[')) || (str_contains($matches[2], ']'))) {

src/PhpSpreadsheet/Calculation/Functions.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,10 @@ public static function ifCondition(mixed $condition): string
164164

165165
return str_replace('""""', '""', '=' . $condition);
166166
}
167-
preg_match('/(=|<[>=]?|>=?)(.*)/', $condition, $matches);
168-
[, $operator, $operand] = $matches;
167+
$operator = $operand = '';
168+
if (1 === preg_match('/(=|<[>=]?|>=?)(.*)/', $condition, $matches)) {
169+
[, $operator, $operand] = $matches;
170+
}
169171

170172
$operand = self::operandSpecialHandling($operand);
171173
if (is_numeric(trim($operand, '"'))) {

src/PhpSpreadsheet/Calculation/Information/Value.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,11 @@ public static function isFormula(mixed $cellReference = '', ?Cell $cell = null):
210210

211211
$fullCellReference = Functions::trimTrailingRange($fullCellReference);
212212

213-
preg_match('/^' . Calculation::CALCULATION_REGEXP_CELLREF . '$/i', $fullCellReference, $matches);
214-
215-
$fullCellReference = $matches[6] . $matches[7];
216-
$worksheetName = str_replace("''", "'", trim($matches[2], "'"));
213+
$worksheetName = '';
214+
if (1 == preg_match('/^' . Calculation::CALCULATION_REGEXP_CELLREF . '$/i', $fullCellReference, $matches)) {
215+
$fullCellReference = $matches[6] . $matches[7];
216+
$worksheetName = str_replace("''", "'", trim($matches[2], "'"));
217+
}
217218

218219
$worksheet = (!empty($worksheetName))
219220
? $cell->getWorksheet()->getParentOrThrow()->getSheetByName($worksheetName)

src/PhpSpreadsheet/Calculation/LookupRef/Formula.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@ public static function text(mixed $cellReference = '', ?Cell $cell = null): stri
2020
return ExcelError::REF();
2121
}
2222

23-
preg_match('/^' . Calculation::CALCULATION_REGEXP_CELLREF . '$/i', $cellReference, $matches);
24-
25-
$cellReference = $matches[6] . $matches[7];
26-
$worksheetName = trim($matches[3], "'");
27-
$worksheet = (!empty($worksheetName))
28-
? $cell->getWorksheet()->getParentOrThrow()->getSheetByName($worksheetName)
29-
: $cell->getWorksheet();
23+
$worksheet = null;
24+
if (1 === preg_match('/^' . Calculation::CALCULATION_REGEXP_CELLREF . '$/i', $cellReference, $matches)) {
25+
$cellReference = $matches[6] . $matches[7];
26+
$worksheetName = trim($matches[3], "'");
27+
$worksheet = (!empty($worksheetName))
28+
? $cell->getWorksheet()->getParentOrThrow()->getSheetByName($worksheetName)
29+
: $cell->getWorksheet();
30+
}
3031

3132
if (
3233
$worksheet === null

src/PhpSpreadsheet/Cell/AddressHelper.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,7 @@ public static function convertToR1C1(
136136
?int $currentRowNumber = null,
137137
?int $currentColumnNumber = null
138138
): string {
139-
$validityCheck = preg_match(Coordinate::A1_COORDINATE_REGEX, $address, $cellReference);
140-
141-
if ($validityCheck === 0) {
139+
if (1 !== preg_match(Coordinate::A1_COORDINATE_REGEX, $address, $cellReference)) {
142140
throw new Exception('Invalid A1-format Cell Reference');
143141
}
144142

src/PhpSpreadsheet/Cell/Cell.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -495,9 +495,10 @@ public function getCalculatedValue(bool $resetLog = true): mixed
495495
if (isset($matches[3])) {
496496
$minCol = $matches[1];
497497
$minRow = (int) $matches[2];
498-
$maxCol = $matches[4];
498+
// https://github.com/phpstan/phpstan/issues/11602
499+
$maxCol = $matches[4]; // @phpstan-ignore-line
499500
++$maxCol;
500-
$maxRow = (int) $matches[5];
501+
$maxRow = (int) $matches[5]; // @phpstan-ignore-line
501502
for ($row = $minRow; $row <= $maxRow; ++$row) {
502503
for ($col = $minCol; $col !== $maxCol; ++$col) {
503504
if ("$col$row" !== $coordinate) {

src/PhpSpreadsheet/Cell/Coordinate.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,7 @@ public static function getRangeBoundaries(string $range): array
271271
private static function validateReferenceAndGetData($reference): array
272272
{
273273
$data = [];
274-
preg_match(self::FULL_REFERENCE_REGEX, $reference, $matches);
275-
if (count($matches) === 0) {
274+
if (1 !== preg_match(self::FULL_REFERENCE_REGEX, $reference, $matches)) {
276275
return ['type' => 'invalid'];
277276
}
278277

src/PhpSpreadsheet/Helper/Size.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@ class Size implements Stringable
88
{
99
const REGEXP_SIZE_VALIDATION = '/^(?P<size>\d*\.?\d+)(?P<unit>pt|px|em)?$/i';
1010

11-
protected bool $valid;
11+
protected bool $valid = false;
1212

1313
protected string $size = '';
1414

1515
protected string $unit = '';
1616

1717
public function __construct(string $size)
1818
{
19-
$this->valid = (bool) preg_match(self::REGEXP_SIZE_VALIDATION, $size, $matches);
20-
if ($this->valid) {
19+
if (1 === preg_match(self::REGEXP_SIZE_VALIDATION, $size, $matches)) {
20+
$this->valid = true;
2121
$this->size = $matches['size'];
2222
$this->unit = $matches['unit'] ?? 'pt';
2323
}

src/PhpSpreadsheet/Reader/Xlsx.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2359,8 +2359,9 @@ private function processIgnoredErrors(SimpleXMLElement $xml, Worksheet $sheet):
23592359
$firstRow = $matches[2];
23602360
$firstCol = $matches[1];
23612361
if (array_key_exists(3, $matches)) {
2362-
$lastCol = $matches[4];
2363-
$lastRow = $matches[5];
2362+
// https://github.com/phpstan/phpstan/issues/11602
2363+
$lastCol = $matches[4]; // @phpstan-ignore-line
2364+
$lastRow = $matches[5]; // @phpstan-ignore-line
23642365
} else {
23652366
$lastCol = $firstCol;
23662367
$lastRow = $firstRow;

0 commit comments

Comments
 (0)