Merge pull request #4158 from oleibman/stanupgrade

Upgrade Phpstan
This commit is contained in:
oleibman
2024-10-07 16:08:07 +00:00
committed by GitHub
20 changed files with 85 additions and 55 deletions
Generated
+5 -5
View File
@@ -1793,16 +1793,16 @@
},
{
"name": "phpstan/phpstan",
"version": "1.11.8",
"version": "1.12.0",
"source": {
"type": "git",
"url": "https://github.com/phpstan/phpstan.git",
"reference": "6adbd118e6c0515dd2f36b06cde1d6da40f1b8ec"
"reference": "384af967d35b2162f69526c7276acadce534d0e1"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/phpstan/phpstan/zipball/6adbd118e6c0515dd2f36b06cde1d6da40f1b8ec",
"reference": "6adbd118e6c0515dd2f36b06cde1d6da40f1b8ec",
"url": "https://api.github.com/repos/phpstan/phpstan/zipball/384af967d35b2162f69526c7276acadce534d0e1",
"reference": "384af967d35b2162f69526c7276acadce534d0e1",
"shasum": ""
},
"require": {
@@ -1847,7 +1847,7 @@
"type": "github"
}
],
"time": "2024-07-24T07:01:22+00:00"
"time": "2024-08-27T09:18:05+00:00"
},
{
"name": "phpstan/phpstan-phpunit",
+20 -7
View File
@@ -4160,8 +4160,10 @@ class Calculation
}
}
} elseif ($expectedArgumentCount != '*') {
preg_match('/(\d*)([-+,])(\d*)/', $expectedArgumentCount, $argMatch);
switch ($argMatch[2] ?? '') {
if (1 !== preg_match('/(\d*)([-+,])(\d*)/', $expectedArgumentCount, $argMatch)) {
$argMatch = ['', '', '', ''];
}
switch ($argMatch[2]) {
case '+':
if ($argumentCount < $argMatch[1]) {
$argumentCountError = true;
@@ -4234,7 +4236,7 @@ class Calculation
// do we now have a function/variable/number?
$expectingOperator = true;
$expectingOperand = false;
$val = $match[1];
$val = $match[1] ?? '';
$length = strlen($val);
if (preg_match('/^' . self::CALCULATION_REGEXP_FUNCTION . '$/miu', $val, $matches)) {
@@ -4290,7 +4292,7 @@ class Calculation
$rangeStartCellRef = $output[count($output) - 2]['value'] ?? '';
}
preg_match('/^' . self::CALCULATION_REGEXP_CELLREF . '$/miu', $rangeStartCellRef, $rangeStartMatches);
if ($rangeStartMatches[2] !== $matches[2]) {
if (isset($rangeStartMatches[2]) && $rangeStartMatches[2] !== $matches[2]) {
return $this->raiseFormulaError('3D Range references are not yet supported');
}
}
@@ -4380,7 +4382,7 @@ class Calculation
$valx = $val;
$endRowColRef = ($refSheet !== null) ? $refSheet->getHighestDataColumn($valx) : AddressRange::MAX_COLUMN; // Max 16,384 columns for Excel2007
$val = "{$rangeWS2}{$endRowColRef}{$val}";
} elseif (ctype_alpha($val) && strlen($val ?? '') <= 3) {
} elseif (ctype_alpha($val) && is_string($val) && strlen($val) <= 3) {
// Column range
$stackItemType = 'Column Reference';
$endRowColRef = ($refSheet !== null) ? $refSheet->getHighestDataRow($val) : AddressRange::MAX_ROW; // Max 1,048,576 rows for Excel2007
@@ -4545,6 +4547,12 @@ class Calculation
return $operand;
}
private static int $matchIndex8 = 8;
private static int $matchIndex9 = 9;
private static int $matchIndex10 = 10;
/**
* @return array<int, mixed>|false
*/
@@ -4908,12 +4916,17 @@ class Calculation
} elseif (preg_match('/^' . self::CALCULATION_REGEXP_CELLREF . '$/i', $token ?? '', $matches)) {
$cellRef = null;
if (isset($matches[8])) {
/* Phpstan says matches[8/9/10] is never set,
and code coverage report seems to confirm.
Appease PhpStan for now;
probably delete this block later.
*/
if (isset($matches[self::$matchIndex8])) {
if ($cell === null) {
// We can't access the range, so return a REF error
$cellValue = ExcelError::REF();
} else {
$cellRef = $matches[6] . $matches[7] . ':' . $matches[9] . $matches[10];
$cellRef = $matches[6] . $matches[7] . ':' . $matches[self::$matchIndex9] . $matches[self::$matchIndex10];
if ($matches[2] > '') {
$matches[2] = trim($matches[2], "\"'");
if ((str_contains($matches[2], '[')) || (str_contains($matches[2], ']'))) {
+4 -2
View File
@@ -164,8 +164,10 @@ class Functions
return str_replace('""""', '""', '=' . $condition);
}
preg_match('/(=|<[>=]?|>=?)(.*)/', $condition, $matches);
[, $operator, $operand] = $matches;
$operator = $operand = '';
if (1 === preg_match('/(=|<[>=]?|>=?)(.*)/', $condition, $matches)) {
[, $operator, $operand] = $matches;
}
$operand = self::operandSpecialHandling($operand);
if (is_numeric(trim($operand, '"'))) {
@@ -210,10 +210,11 @@ class Value
$fullCellReference = Functions::trimTrailingRange($fullCellReference);
preg_match('/^' . Calculation::CALCULATION_REGEXP_CELLREF . '$/i', $fullCellReference, $matches);
$fullCellReference = $matches[6] . $matches[7];
$worksheetName = str_replace("''", "'", trim($matches[2], "'"));
$worksheetName = '';
if (1 == preg_match('/^' . Calculation::CALCULATION_REGEXP_CELLREF . '$/i', $fullCellReference, $matches)) {
$fullCellReference = $matches[6] . $matches[7];
$worksheetName = str_replace("''", "'", trim($matches[2], "'"));
}
$worksheet = (!empty($worksheetName))
? $cell->getWorksheet()->getParentOrThrow()->getSheetByName($worksheetName)
@@ -20,13 +20,14 @@ class Formula
return ExcelError::REF();
}
preg_match('/^' . Calculation::CALCULATION_REGEXP_CELLREF . '$/i', $cellReference, $matches);
$cellReference = $matches[6] . $matches[7];
$worksheetName = trim($matches[3], "'");
$worksheet = (!empty($worksheetName))
? $cell->getWorksheet()->getParentOrThrow()->getSheetByName($worksheetName)
: $cell->getWorksheet();
$worksheet = null;
if (1 === preg_match('/^' . Calculation::CALCULATION_REGEXP_CELLREF . '$/i', $cellReference, $matches)) {
$cellReference = $matches[6] . $matches[7];
$worksheetName = trim($matches[3], "'");
$worksheet = (!empty($worksheetName))
? $cell->getWorksheet()->getParentOrThrow()->getSheetByName($worksheetName)
: $cell->getWorksheet();
}
if (
$worksheet === null
+1 -3
View File
@@ -136,9 +136,7 @@ class AddressHelper
?int $currentRowNumber = null,
?int $currentColumnNumber = null
): string {
$validityCheck = preg_match(Coordinate::A1_COORDINATE_REGEX, $address, $cellReference);
if ($validityCheck === 0) {
if (1 !== preg_match(Coordinate::A1_COORDINATE_REGEX, $address, $cellReference)) {
throw new Exception('Invalid A1-format Cell Reference');
}
+3 -2
View File
@@ -495,9 +495,10 @@ class Cell implements Stringable
if (isset($matches[3])) {
$minCol = $matches[1];
$minRow = (int) $matches[2];
$maxCol = $matches[4];
// https://github.com/phpstan/phpstan/issues/11602
$maxCol = $matches[4]; // @phpstan-ignore-line
++$maxCol;
$maxRow = (int) $matches[5];
$maxRow = (int) $matches[5]; // @phpstan-ignore-line
for ($row = $minRow; $row <= $maxRow; ++$row) {
for ($col = $minCol; $col !== $maxCol; ++$col) {
if ("$col$row" !== $coordinate) {
+1 -2
View File
@@ -271,8 +271,7 @@ abstract class Coordinate
private static function validateReferenceAndGetData($reference): array
{
$data = [];
preg_match(self::FULL_REFERENCE_REGEX, $reference, $matches);
if (count($matches) === 0) {
if (1 !== preg_match(self::FULL_REFERENCE_REGEX, $reference, $matches)) {
return ['type' => 'invalid'];
}
+3 -3
View File
@@ -8,7 +8,7 @@ class Size implements Stringable
{
const REGEXP_SIZE_VALIDATION = '/^(?P<size>\d*\.?\d+)(?P<unit>pt|px|em)?$/i';
protected bool $valid;
protected bool $valid = false;
protected string $size = '';
@@ -16,8 +16,8 @@ class Size implements Stringable
public function __construct(string $size)
{
$this->valid = (bool) preg_match(self::REGEXP_SIZE_VALIDATION, $size, $matches);
if ($this->valid) {
if (1 === preg_match(self::REGEXP_SIZE_VALIDATION, $size, $matches)) {
$this->valid = true;
$this->size = $matches['size'];
$this->unit = $matches['unit'] ?? 'pt';
}
+3 -2
View File
@@ -2359,8 +2359,9 @@ class Xlsx extends BaseReader
$firstRow = $matches[2];
$firstCol = $matches[1];
if (array_key_exists(3, $matches)) {
$lastCol = $matches[4];
$lastRow = $matches[5];
// https://github.com/phpstan/phpstan/issues/11602
$lastCol = $matches[4]; // @phpstan-ignore-line
$lastRow = $matches[5]; // @phpstan-ignore-line
} else {
$lastCol = $firstCol;
$lastRow = $firstRow;
+1 -1
View File
@@ -759,7 +759,7 @@ class ReferenceHelper
$column = $columns[$splitCount][0];
$row = $rows[$splitCount][0];
if (!empty($column) && $column[0] !== '$') {
if ($column[0] !== '$') {
$column = ((Coordinate::columnIndexFromString($column) + $numberOfColumns) % AddressRange::MAX_COLUMN_INT) ?: AddressRange::MAX_COLUMN_INT;
$column = Coordinate::stringFromColumnIndex($column);
$rowOffset -= ($columnLength - strlen($column));
@@ -25,9 +25,9 @@ class FractionFormatter extends BaseFormatter
$decimalDivisor = 10 ** $decimalLength;
preg_match('/(#?.*\?)\/(\?+|\d+)/', $format, $matches);
$formatIntegerPart = $matches[1];
$formatIntegerPart = $matches[1] ?? '0';
if (is_numeric($matches[2])) {
if (isset($matches[2]) && is_numeric($matches[2])) {
$fractionDivisor = 100 / (int) $matches[2];
} else {
/** @var float $fractionDivisor */
@@ -60,7 +60,7 @@ abstract class NumberBase implements Stringable
['language' => $language, 'script' => $script, 'country' => $country] = $matches;
// Set case and separator to match standardised locale case
$language = strtolower($language ?? '');
$language = strtolower($language);
$script = ($script === null) ? null : ucfirst(strtolower($script));
$country = ($country === null) ? null : strtoupper($country);
+1 -1
View File
@@ -100,7 +100,7 @@ abstract class BaseWriter implements IWriter
*/
public function openFileHandle($filename): void
{
if (is_resource($filename)) {
if (!is_string($filename)) {
$this->fileHandle = $filename;
$this->shouldCloseFile = false;
+3 -2
View File
@@ -238,10 +238,11 @@ class Content extends WriterPart
$matrixColSpan = 1;
if (isset($matches[3])) {
$minRow = (int) $matches[2];
$maxRow = (int) $matches[5];
// https://github.com/phpstan/phpstan/issues/11602
$maxRow = (int) $matches[5]; // @phpstan-ignore-line
$matrixRowSpan = $maxRow - $minRow + 1;
$minCol = Coordinate::columnIndexFromString($matches[1]);
$maxCol = Coordinate::columnIndexFromString($matches[4]);
$maxCol = Coordinate::columnIndexFromString($matches[4]); // @phpstan-ignore-line
$matrixColSpan = $maxCol - $minCol + 1;
}
$objWriter->writeAttribute('table:number-matrix-columns-spanned', "$matrixColSpan");
+3 -3
View File
@@ -102,9 +102,9 @@ class Formula
}
$newRange .= '.';
if (!empty($column)) {
$newRange .= $column;
}
//if (!empty($column)) { // phpstan says always true
$newRange .= $column;
//}
if (!empty($row)) {
$newRange .= $row;
}
@@ -118,9 +118,9 @@ class NamedExpressions
$newRange = "'" . str_replace("'", "''", $worksheet) . "'.";
}
if (!empty($column)) {
$newRange .= $column;
}
//if (!empty($column)) { // phpstan says always true
$newRange .= $column;
//}
if (!empty($row)) {
$newRange .= $row;
}
+10 -2
View File
@@ -894,7 +894,11 @@ class Parser
*/
private function rangeToPackedRange(string $range): array
{
preg_match('/(\$)?(\d+)\:(\$)?(\d+)/', $range, $match);
if (preg_match('/(\$)?(\d+)\:(\$)?(\d+)/', $range, $match) !== 1) {
// @codeCoverageIgnoreStart
throw new WriterException('Regexp failure in rangeToPackedRange');
// @codeCoverageIgnoreEnd
}
// return absolute rows if there is a $ in the ref
$row1_rel = empty($match[1]) ? 1 : 0;
$row1 = $match[2];
@@ -933,7 +937,11 @@ class Parser
*/
private function cellToRowcol(string $cell): array
{
preg_match('/(\$)?([A-I]?[A-Z])(\$)?(\d+)/', $cell, $match);
if (preg_match('/(\$)?([A-I]?[A-Z])(\$)?(\d+)/', $cell, $match) !== 1) {
// @codeCoverageIgnoreStart
throw new WriterException('Regexp failure in cellToRowcol');
// @codeCoverageIgnoreEnd
}
// return absolute column if there is a $ in the ref
$col_rel = empty($match[1]) ? 1 : 0;
$col_ref = $match[2];
+5 -1
View File
@@ -501,7 +501,11 @@ class Drawing extends WriterPart
private function writeVMLHeaderFooterImage(XMLWriter $objWriter, string $reference, HeaderFooterDrawing $image): void
{
// Calculate object id
preg_match('{(\d+)}', md5($reference), $m);
if (preg_match('{(\d+)}', md5($reference), $m) !== 1) {
// @codeCoverageIgnoreStart
throw new WriterException('Regexp failure in writeVMLHeaderFooterImage');
// @codeCoverageIgnoreEnd
}
$id = 1500 + ((int) substr($m[1], 0, 2) * 1);
// Calculate offset
+3 -2
View File
@@ -1542,10 +1542,11 @@ class Worksheet extends WriterPart
return $coordinate;
}
$minRow = (int) $matches[2];
$maxRow = (int) $matches[5];
// https://github.com/phpstan/phpstan/issues/11602
$maxRow = (int) $matches[5]; // @phpstan-ignore-line
$rows = $maxRow - $minRow + 1;
$minCol = Coordinate::columnIndexFromString($matches[1]);
$maxCol = Coordinate::columnIndexFromString($matches[4]);
$maxCol = Coordinate::columnIndexFromString($matches[4]); // @phpstan-ignore-line
$cols = $maxCol - $minCol + 1;
$firstCellArray = Coordinate::indexesFromString($coordinate);
$lastRow = $firstCellArray[1] + $rows - 1;