Skip to content

Commit

Permalink
Phpstan Differences from Php7 to Php8, Again (#2665)
Browse files Browse the repository at this point in the history
These changes have already been implemented twice, and been regressed twice. I'll try once more (with a different approach), then give up ...

As configured, Phpstan running under Php7 reports no errors. However, running under Php8, it reports 100 (!) errors. The vast majority of these are due to two reasons:
- renaming parameters in Php builtin functions in preparation for named parameters.
- using the new class GdImage rather than type resource as the argument type for many image-based functions.

Regardless of the cause, this will be a problem sooner or later. This PR is an attempt to get ahead of that problem. For source members, it mostly adds annotations or updates doc-blocks. Only 2 members have changes to executable code, and these are very minor - BitWise and Writer/Xlsx. For test members, all baseline errors are deleted and the code is fixed. Php7 and Php8 both report no errors with this configuration.
  • Loading branch information
oleibman authored Mar 12, 2022
1 parent c0ea894 commit 68158c8
Show file tree
Hide file tree
Showing 46 changed files with 199 additions and 544 deletions.
460 changes: 0 additions & 460 deletions phpstan-baseline.neon

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions src/PhpSpreadsheet/Calculation/Calculation.php
Original file line number Diff line number Diff line change
Expand Up @@ -3226,6 +3226,7 @@ public function _translateFormulaToEnglish($formula)
if (self::$functionReplaceToExcel === null) {
self::$functionReplaceToExcel = [];
foreach (array_keys(self::$localeFunctions) as $excelFunctionName) {
// @phpstan-ignore-next-line
self::$functionReplaceToExcel[] = '$1' . trim($excelFunctionName) . '$2';
}
foreach (array_keys(self::$localeBoolean) as $excelBoolean) {
Expand Down Expand Up @@ -4490,6 +4491,7 @@ private function processTokenStack($tokens, $cellID = null, ?Cell $cell = null)
if ($operand1Data['reference'] === null) {
if ((trim($operand1Data['value']) != '') && (is_numeric($operand1Data['value']))) {
$operand1Data['reference'] = $cell->getColumn() . $operand1Data['value'];
// @phpstan-ignore-next-line
} elseif (trim($operand1Data['reference']) == '') {
$operand1Data['reference'] = $cell->getCoordinate();
} else {
Expand All @@ -4499,6 +4501,7 @@ private function processTokenStack($tokens, $cellID = null, ?Cell $cell = null)
if ($operand2Data['reference'] === null) {
if ((trim($operand2Data['value']) != '') && (is_numeric($operand2Data['value']))) {
$operand2Data['reference'] = $cell->getColumn() . $operand2Data['value'];
// @phpstan-ignore-next-line
} elseif (trim($operand2Data['reference']) == '') {
$operand2Data['reference'] = $cell->getCoordinate();
} else {
Expand Down
3 changes: 2 additions & 1 deletion src/PhpSpreadsheet/Calculation/Engineering/BitWise.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,13 +211,14 @@ public static function BITRSHIFT($number, $shiftAmount)
*
* @param mixed $value
*
* @return float|int
* @return float
*/
private static function validateBitwiseArgument($value)
{
$value = self::nullFalseTrueToNumber($value);

if (is_numeric($value)) {
$value = (float) $value;
if ($value == floor($value)) {
if (($value > 2 ** 48 - 1) || ($value < 0)) {
throw new Exception(ExcelError::NAN());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ public static function ROW($cellAddress = null, ?Cell $cell = null)
function ($value) {
return [$value];
},
// @phpstan-ignore-next-line
range($startAddress, $endAddress)
);
}
Expand Down
3 changes: 2 additions & 1 deletion src/PhpSpreadsheet/Cell/Coordinate.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ public static function splitRange($range)
$exploded = explode(',', $range);
$counter = count($exploded);
for ($i = 0; $i < $counter; ++$i) {
// @phpstan-ignore-next-line
$exploded[$i] = explode(':', $exploded[$i]);
}

Expand Down Expand Up @@ -544,7 +545,7 @@ private static function getCellBlocksFromRangeString($rangeString)

// split range sets on intersection (space) or union (,) operators
$tokens = preg_split('/([ ,])/', $rangeString, -1, PREG_SPLIT_DELIM_CAPTURE);
// separate the range sets and the operators into arrays
/** @phpstan-ignore-next-line */
$split = array_chunk($tokens, 2);
$ranges = array_column($split, 0);
$operators = array_column($split, 1);
Expand Down
1 change: 1 addition & 0 deletions src/PhpSpreadsheet/Helper/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,7 @@ protected function handleCallback(DOMElement $element, $callbackTag, array $call
if (isset($callbacks[$callbackTag])) {
$elementHandler = $callbacks[$callbackTag];
if (method_exists($this, $elementHandler)) {
// @phpstan-ignore-next-line
call_user_func([$this, $elementHandler], $element);
}
}
Expand Down
1 change: 1 addition & 0 deletions src/PhpSpreadsheet/Reader/Xls/MD5.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public function getContext()
*/
public function add(string $data): void
{
/** @phpstan-ignore-next-line */
$words = array_values(unpack('V16', $data));

$A = $this->a;
Expand Down
10 changes: 10 additions & 0 deletions src/PhpSpreadsheet/Shared/Drawing.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,15 @@ public static function imagecreatefrombmp($bmpFilename)
{
// Load the image into a string
$file = fopen($bmpFilename, 'rb');
/** @phpstan-ignore-next-line */
$read = fread($file, 10);
// @phpstan-ignore-next-line
while (!feof($file) && ($read != '')) {
// @phpstan-ignore-next-line
$read .= fread($file, 1024);
}

/** @phpstan-ignore-next-line */
$temp = unpack('H*', $read);
$hex = $temp[1];
$header = substr($hex, 0, 108);
Expand Down Expand Up @@ -196,6 +200,8 @@ public static function imagecreatefrombmp($bmpFilename)
$y = 1;

// Create newimage

/** @phpstan-ignore-next-line */
$image = imagecreatetruecolor($width, $height);

// Grab the body from the image
Expand Down Expand Up @@ -241,7 +247,10 @@ public static function imagecreatefrombmp($bmpFilename)
$b = hexdec($body[$i_pos] . $body[$i_pos + 1]);

// Calculate and draw the pixel

/** @phpstan-ignore-next-line */
$color = imagecolorallocate($image, $r, $g, $b);
// @phpstan-ignore-next-line
imagesetpixel($image, $x, $height - $y, $color);

// Raise the horizontal position
Expand All @@ -252,6 +261,7 @@ public static function imagecreatefrombmp($bmpFilename)
unset($body);

// Return image-object
// @phpstan-ignore-next-line
return $image;
}
}
1 change: 1 addition & 0 deletions src/PhpSpreadsheet/Shared/JAMA/Matrix.php
Original file line number Diff line number Diff line change
Expand Up @@ -1134,6 +1134,7 @@ public function concat(...$args)
$this->checkMatrixDimensions($M);
for ($i = 0; $i < $this->m; ++$i) {
for ($j = 0; $j < $this->n; ++$j) {
// @phpstan-ignore-next-line
$this->A[$i][$j] = trim($this->A[$i][$j], '"') . trim($M->get($i, $j), '"');
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/PhpSpreadsheet/Shared/OLE.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ public function getStream($blockIdOrPps)
*/
private static function readInt1($fileHandle)
{
// @phpstan-ignore-next-line
[, $tmp] = unpack('c', fread($fileHandle, 1));

return $tmp;
Expand All @@ -265,6 +266,7 @@ private static function readInt1($fileHandle)
*/
private static function readInt2($fileHandle)
{
// @phpstan-ignore-next-line
[, $tmp] = unpack('v', fread($fileHandle, 2));

return $tmp;
Expand All @@ -279,6 +281,7 @@ private static function readInt2($fileHandle)
*/
private static function readInt4($fileHandle)
{
// @phpstan-ignore-next-line
[, $tmp] = unpack('V', fread($fileHandle, 4));

return $tmp;
Expand Down
1 change: 1 addition & 0 deletions src/PhpSpreadsheet/Shared/OLE/ChainedBlockStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ public function stream_seek($offset, $whence) // @codingStandardsIgnoreLine
$this->pos = $offset;
} elseif ($whence == SEEK_CUR && -$offset <= $this->pos) {
$this->pos += $offset;
// @phpstan-ignore-next-line
} elseif ($whence == SEEK_END && -$offset <= count($this->data)) {
$this->pos = strlen($this->data) + $offset;
} else {
Expand Down
1 change: 1 addition & 0 deletions src/PhpSpreadsheet/Shared/Trend/PolynomialBestFit.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ public function getSlope($dp = 0)

public function getCoefficients($dp = 0)
{
// @phpstan-ignore-next-line
return array_merge([$this->getIntersect($dp)], $this->getSlope($dp));
}

Expand Down
2 changes: 2 additions & 0 deletions src/PhpSpreadsheet/Style/NumberFormat/DateFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ public static function format($value, string $format): string
$format = preg_replace_callback('/(?:^|")([^"]*)(?:$|")/', $callable, $format);

// Only process the non-quoted blocks for date format characters

/** @phpstan-ignore-next-line */
$blocks = explode('"', $format);
foreach ($blocks as $key => &$block) {
if ($key % 2 == 0) {
Expand Down
2 changes: 1 addition & 1 deletion src/PhpSpreadsheet/Worksheet/AutoFilter/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ public function setAttributes($attributes)
* Set An AutoFilter Attribute.
*
* @param string $name Attribute Name
* @param string $value Attribute Value
* @param int|string $value Attribute Value
*
* @return $this
*/
Expand Down
5 changes: 5 additions & 0 deletions src/PhpSpreadsheet/Worksheet/PageSetup.php
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,7 @@ public function getPrintArea($index = 0)
if ($index == 0) {
return $this->printArea;
}
/** @phpstan-ignore-next-line */
$printAreas = explode(',', $this->printArea);
if (isset($printAreas[$index - 1])) {
return $printAreas[$index - 1];
Expand All @@ -663,6 +664,7 @@ public function isPrintAreaSet($index = 0)
if ($index == 0) {
return $this->printArea !== null;
}
/** @phpstan-ignore-next-line */
$printAreas = explode(',', $this->printArea);

return isset($printAreas[$index - 1]);
Expand All @@ -683,6 +685,7 @@ public function clearPrintArea($index = 0)
if ($index == 0) {
$this->printArea = null;
} else {
/** @phpstan-ignore-next-line */
$printAreas = explode(',', $this->printArea);
if (isset($printAreas[$index - 1])) {
unset($printAreas[$index - 1]);
Expand Down Expand Up @@ -731,6 +734,7 @@ public function setPrintArea($value, $index = 0, $method = self::SETPRINTRANGE_O
if ($index == 0) {
$this->printArea = $value;
} else {
/** @phpstan-ignore-next-line */
$printAreas = explode(',', $this->printArea);
if ($index < 0) {
$index = count($printAreas) - abs($index) + 1;
Expand All @@ -745,6 +749,7 @@ public function setPrintArea($value, $index = 0, $method = self::SETPRINTRANGE_O
if ($index == 0) {
$this->printArea = $this->printArea ? ($this->printArea . ',' . $value) : $value;
} else {
/** @phpstan-ignore-next-line */
$printAreas = explode(',', $this->printArea);
if ($index < 0) {
$index = abs($index) - 1;
Expand Down
3 changes: 3 additions & 0 deletions src/PhpSpreadsheet/Worksheet/Worksheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,7 @@ public function addChart(Chart $chart, $chartIndex = null)
$this->chartCollection[] = $chart;
} else {
// Insert the chart at the requested index
// @phpstan-ignore-next-line
array_splice($this->chartCollection, $chartIndex, 0, [$chart]);
}

Expand Down Expand Up @@ -1224,6 +1225,7 @@ private function getWorksheetAndCoordinate(string $coordinate): array
throw new Exception('Sheet not found for named range: ' . $namedRange->getName());
}

/** @phpstan-ignore-next-line */
$cellCoordinate = ltrim(substr($namedRange->getValue(), strrpos($namedRange->getValue(), '!')), '!');
$finalCoordinate = str_replace('$', '', $cellCoordinate);
}
Expand Down Expand Up @@ -2767,6 +2769,7 @@ public function namedRangeToArray(string $definedName, $nullValue = null, $calcu
{
$namedRange = $this->validateNamedRange($definedName);
$workSheet = $namedRange->getWorksheet();
/** @phpstan-ignore-next-line */
$cellRange = ltrim(substr($namedRange->getValue(), strrpos($namedRange->getValue(), '!')), '!');
$cellRange = str_replace('$', '', $cellRange);

Expand Down
4 changes: 3 additions & 1 deletion src/PhpSpreadsheet/Writer/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -697,10 +697,12 @@ private function writeImageInCell(Worksheet $worksheet, $coordinates)
$imageResource = $drawing->getImageResource();
if ($imageResource) {
ob_start(); // Let's start output buffering.
// @phpstan-ignore-next-line
imagepng($imageResource); // This will normally output the image, but because of ob_start(), it won't.
$contents = ob_get_contents(); // Instead, output above is saved to $contents
ob_end_clean(); // End the output buffer.

/** @phpstan-ignore-next-line */
$dataUri = 'data:image/jpeg;base64,' . base64_encode($contents);

// Because of the nature of tables, width is more important than height.
Expand Down Expand Up @@ -748,7 +750,7 @@ private function writeChartInCell(Worksheet $worksheet, string $coordinates): st
if ($fp = fopen($chartFileName, 'rb', 0)) {
$picture = fread($fp, filesize($chartFileName));
fclose($fp);
// base64 encode the binary data
/** @phpstan-ignore-next-line */
$base64 = base64_encode($picture);
$imageData = 'data:' . $imageDetails['mime'] . ';base64,' . $base64;

Expand Down
2 changes: 2 additions & 0 deletions src/PhpSpreadsheet/Writer/Xls.php
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,7 @@ private function processDrawing(BstoreContainer &$bstoreContainer, Drawing $draw
case 1: // GIF, not supported by BIFF8, we convert to PNG
$blipType = BSE::BLIPTYPE_PNG;
ob_start();
// @phpstan-ignore-next-line
imagepng(imagecreatefromgif($filename));
$blipData = ob_get_contents();
ob_end_clean();
Expand All @@ -452,6 +453,7 @@ private function processDrawing(BstoreContainer &$bstoreContainer, Drawing $draw
case 6: // Windows DIB (BMP), we convert to PNG
$blipType = BSE::BLIPTYPE_PNG;
ob_start();
// @phpstan-ignore-next-line
imagepng(SharedDrawing::imagecreatefrombmp($filename));
$blipData = ob_get_contents();
ob_end_clean();
Expand Down
5 changes: 5 additions & 0 deletions src/PhpSpreadsheet/Writer/Xls/Worksheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -989,6 +989,8 @@ public function writeUrlWeb($row1, $col1, $row2, $col2, $url): void
$options = pack('V', 0x03);

// Convert URL to a null terminated wchar string

/** @phpstan-ignore-next-line */
$url = implode("\0", preg_split("''", $url, -1, PREG_SPLIT_NO_EMPTY));
$url = $url . "\0\0\0";

Expand Down Expand Up @@ -2387,6 +2389,7 @@ public function processBitmapGd($image)
$data = pack('Vvvvv', 0x000c, $width, $height, 0x01, 0x18);
for ($j = $height; --$j;) {
for ($i = 0; $i < $width; ++$i) {
/** @phpstan-ignore-next-line */
$color = imagecolorsforindex($image, imagecolorat($image, $i, $j));
foreach (['red', 'green', 'blue'] as $key) {
$color[$key] = $color[$key] + (int) round((255 - $color[$key]) * $color['alpha'] / 127);
Expand Down Expand Up @@ -2427,6 +2430,8 @@ public function processBitmap($bitmap)
}

// The first 2 bytes are used to identify the bitmap.

/** @phpstan-ignore-next-line */
$identity = unpack('A2ident', $data);
if ($identity['ident'] != 'BM') {
throw new WriterException("$bitmap doesn't appear to be a valid bitmap image.\n");
Expand Down
4 changes: 3 additions & 1 deletion src/PhpSpreadsheet/Writer/Xlsx.php
Original file line number Diff line number Diff line change
Expand Up @@ -501,8 +501,10 @@ public function save($filename, int $flags = 0): void
$zipContent['xl/media/' . $this->getDrawingHashTable()->getByIndex($i)->getIndexedFilename()] = $imageContents;
} elseif ($this->getDrawingHashTable()->getByIndex($i) instanceof MemoryDrawing) {
ob_start();
/** @var callable */
$callable = $this->getDrawingHashTable()->getByIndex($i)->getRenderingFunction();
call_user_func(
$this->getDrawingHashTable()->getByIndex($i)->getRenderingFunction(),
$callable,
$this->getDrawingHashTable()->getByIndex($i)->getImageResource()
);
$imageContents = ob_get_contents();
Expand Down
7 changes: 6 additions & 1 deletion tests/PhpSpreadsheetTests/Calculation/Engine/RangeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

class RangeTest extends TestCase
{
/** @var string */
private $incompleteMessage = 'Must be revisited';

/**
* @var Spreadsheet
*/
Expand Down Expand Up @@ -140,7 +143,9 @@ public function providerUTF8NamedRangeEvaluation(): array
*/
public function testCompositeNamedRangeEvaluation(string $composite, int $expectedSum, int $expectedCount): void
{
self::markTestSkipped('must be revisited.');
if ($this->incompleteMessage !== '') {
self::markTestIncomplete($this->incompleteMessage);
}

$workSheet = $this->spreadSheet->getActiveSheet();
$this->spreadSheet->addNamedRange(new NamedRange('COMPOSITE', $workSheet, $composite));
Expand Down
4 changes: 3 additions & 1 deletion tests/PhpSpreadsheetTests/Collection/CellsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ public function testCollectionCell(): void
// Assert empty state
self::assertEquals([], $collection->getCoordinates(), 'cell list should be empty');
self::assertEquals([], $collection->getSortedCoordinates(), 'sorted cell list should be empty');
self::assertNull($collection->get('B2'), 'getting non-existing cell must return null');
$getB2 = $collection->get('B2');
self::assertNull($getB2, 'getting non-existing cell must return null');
self::assertFalse($collection->has('B2'), 'non-existing cell should be non-existent');

// Add one cell
Expand All @@ -44,6 +45,7 @@ public function testCollectionCell(): void
$collection2 = $collection->cloneCellCollection($sheet2);
self::assertTrue($collection2->has('A1'));
$copiedCell2 = $collection2->get('A1');
self::assertNotNull($copiedCell2);
self::assertNotSame($cell2, $copiedCell2, 'copied cell should not be the same object any more');
self::assertSame($collection2, $copiedCell2->getParent(), 'copied cell should be owned by the copied collection');
self::assertSame('A1', $copiedCell2->getCoordinate(), 'copied cell should keep attributes');
Expand Down
2 changes: 1 addition & 1 deletion tests/PhpSpreadsheetTests/Functional/ColumnWidthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function providerFormats(): array
/**
* @dataProvider providerFormats
*/
public function testReadColumnWidth($format): void
public function testReadColumnWidth(string $format): void
{
// create new sheet with column width
$spreadsheet = new Spreadsheet();
Expand Down
2 changes: 1 addition & 1 deletion tests/PhpSpreadsheetTests/Functional/CommentsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function providerFormats(): array
*
* @dataProvider providerFormats
*/
public function testComments($format): void
public function testComments(string $format): void
{
$spreadsheet = new Spreadsheet();

Expand Down
Loading

0 comments on commit 68158c8

Please sign in to comment.