Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recent Scrutinizer Messages #3740

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/PhpSpreadsheet/Calculation/TextData/Replace.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,6 @@ private static function executeSubstitution(string $text, string $fromText, stri
--$instance;
}

return Functions::scalar(self::REPLACE($text, ++$pos, StringHelper::countCharacters($fromText), $toText));
return (string) Functions::scalar(self::REPLACE($text, ++$pos, StringHelper::countCharacters($fromText), $toText));
}
}
4 changes: 2 additions & 2 deletions src/PhpSpreadsheet/Chart/Layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -486,9 +486,9 @@ public function setLabelFontColor(?ChartColor $chartColor): self
{
if ($this->labelFont === null) {
$this->labelFont = new Font();
$this->labelFont->setSize(null, true);
$this->labelFont->/** @scrutinizer ignore-call */ setSize(null, true);
}
$this->labelFont->setChartColorFromObject($chartColor);
$this->labelFont->/** @scrutinizer ignore-call */ setChartColorFromObject($chartColor);

return $this;
}
Expand Down
5 changes: 3 additions & 2 deletions src/PhpSpreadsheet/Helper/Downloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ public function __construct(string $folder, string $filename, ?string $filetype
}

$filetype ??= pathinfo($filename, PATHINFO_EXTENSION);
if (array_key_exists(strtolower($filetype), self::CONTENT_TYPES) === false) {
$filetypeLower = strtolower(/** @scrutinizer ignore-type */ $filetype);
if (array_key_exists($filetypeLower, self::CONTENT_TYPES) === false) {
throw new Exception("Invalid filetype: {$filetype} cannot be downloaded");
}
$this->filetype = strtolower($filetype);
$this->filetype = $filetypeLower;
}

public function download(): void
Expand Down
2 changes: 1 addition & 1 deletion src/PhpSpreadsheet/Helper/Sample.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public function write(Spreadsheet $spreadsheet, $filename, array $writers = ['Xl
$writer->save($path);
$this->logWrite($writer, $path, /** @scrutinizer ignore-type */ $callStartTime);
if ($this->isCli() === false) {
echo '<a href="/download.php?type=' . pathinfo($path, PATHINFO_EXTENSION) . '&name=' . basename($path) . '">Download ' . basename($path) . '</a><br />';
echo '<a href="/download.php?type=' . /** @scrutinizer ignore-type */ pathinfo($path, PATHINFO_EXTENSION) . '&name=' . basename($path) . '">Download ' . basename($path) . '</a><br />';
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/PhpSpreadsheet/IOFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public static function createReaderForFile(string $filename, ?array $readers = n

// First, lucky guess by inspecting file extension
$guessedReader = self::getReaderTypeFromExtension($filename);
if (($guessedReader !== null) && array_key_exists($guessedReader, $testReaders)) {
if (array_key_exists($guessedReader, $testReaders)) {
$reader = self::createReader($guessedReader);

// Let's see if we are lucky
Expand All @@ -173,11 +173,11 @@ public static function createReaderForFile(string $filename, ?array $readers = n
/**
* Guess a reader type from the file extension, if any.
*/
private static function getReaderTypeFromExtension(string $filename): ?string
private static function getReaderTypeFromExtension(string $filename): string
{
$pathinfo = pathinfo($filename);
if (!isset($pathinfo['extension'])) {
return null;
return '';
}

return match (strtolower($pathinfo['extension'])) {
Expand Down Expand Up @@ -205,8 +205,8 @@ private static function getReaderTypeFromExtension(string $filename): ?string
// Do nothing
// We must not try to use CSV reader since it loads
// all files including Excel files etc.
'csv' => null,
default => null,
'csv' => '',
default => '',
};
}

Expand Down
2 changes: 1 addition & 1 deletion src/PhpSpreadsheet/Reader/Ods/DefinedNames.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ protected function readDefinedExpressions(DOMElement $workbookData): void
/**
* Assess scope and store the Defined Name.
*
* @param non-empty-string $baseAddress
* @param non-empty-string $baseAddress Scrutinizer does not understand this, but it helps Phpstan analyze call to extractSheetTitle better
*/
private function addDefinedName(string $baseAddress, string $definedName, string $value): void
{
Expand Down
2 changes: 1 addition & 1 deletion src/PhpSpreadsheet/Shared/Drawing.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public static function angleToDegrees($angle): int
public static function imagecreatefrombmp($bmpFilename): GdImage
{
$retVal = @imagecreatefrombmp($bmpFilename);
if ($retVal === false) {
if (!($retVal instanceof GdImage)) {
throw new ReaderException("Unable to create image from $bmpFilename");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ protected function processDuplicatesComparison(Conditional $conditional): bool
self::COMPARISON_DUPLICATES_OPERATORS[$conditional->getConditionType()],
$worksheetName,
$this->conditionalRange,
$this->cellConditionCheck($this->cell->getCalculatedValue())
$this->cellConditionCheck(/** @scrutinizer ignore-type */ $this->cell->getCalculatedValue())
);

return $this->evaluateExpression($expression);
Expand Down
4 changes: 2 additions & 2 deletions src/PhpSpreadsheet/Worksheet/BaseDrawing.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,8 @@ public function setWorksheet(?Worksheet $worksheet = null, bool $overrideOld = f
// Add drawing to \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet
if ($worksheet !== null) {
$this->worksheet = $worksheet;
$this->worksheet->getCell($this->coordinates);
$this->worksheet->getDrawingCollection()->append($this);
$worksheet->getCell($this->coordinates);
$worksheet->getDrawingCollection()->append($this);
}
} else {
if ($overrideOld) {
Expand Down
7 changes: 4 additions & 3 deletions src/PhpSpreadsheet/Worksheet/MemoryDrawing.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ public function __construct()
public function __destruct()
{
if ($this->imageResource) {
@imagedestroy($this->imageResource);
$this->imageResource = null;
$nullValue = null;
$rc = @imagedestroy($this->imageResource);
$this->imageResource = $rc ? null : $nullValue;
}
$this->worksheet = null;
}
Expand Down Expand Up @@ -156,7 +157,7 @@ public static function fromString(string $imageString): self
$renderingFunction = self::identifyRenderingFunction($mimeType);

$drawing = new self();
$drawing->setImageResource($gdImage);
$drawing->setImageResource(/** @scrutinizer ignore-type */ $gdImage);
$drawing->setRenderingFunction($renderingFunction);
$drawing->setMimeType($mimeType);

Expand Down
2 changes: 1 addition & 1 deletion src/PhpSpreadsheet/Worksheet/Worksheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -3442,7 +3442,7 @@ public function getHashCode()
* @param string $range Range to extract title from
* @param bool $returnRange Return range? (see example)
*
* @return ($range is non-empty-string ? ($returnRange is true ? array{0: string, 1: string} : string) : ($returnRange is true ? array{0: null, 1: null} : null))
* @return ($range is non-empty-string ? ($returnRange is true ? array{0: string, 1: string} : string) : ($returnRange is true ? array{0: null, 1: null} : null)) Scrutinizer does not understand this but it helps Phpstan
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @return ($range is non-empty-string ? ($returnRange is true ? array{0: string, 1: string} : string) : ($returnRange is true ? array{0: null, 1: null} : null)) Scrutinizer does not understand this but it helps Phpstan
* @return ($range is non-empty-string ? ($returnRange is true ? array{0: string, 1: string} : string) : ($returnRange is true ? array{0: null, 1: null} : null))

Don't think we should have those kind of comment that end up in end-user documentation.

*/
public static function extractSheetTitle($range, $returnRange = false): array|null|string
{
Expand Down
15 changes: 7 additions & 8 deletions src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -1311,15 +1311,14 @@ private function writeCellFormula(XMLWriter $objWriter, string $cellValue, Cell
$objWriter->endElement();
} else {
$objWriter->writeElement('f', FunctionPrefix::addFunctionPrefixStripEquals($cellValue));
self::writeElementIf(
$objWriter,
$this->getParentWriter()->getOffice2003Compatibility() === false
if (
$calculatedValue !== null
&& !is_array($calculatedValue)
&& $this->getParentWriter()->getOffice2003Compatibility() === false
&& $this->getParentWriter()->getPreCalculateFormulas()
&& $calculatedValue !== null,
'v',
(!is_array($calculatedValue) && !str_starts_with($calculatedValue ?? '', '#'))
? StringHelper::formatNumber($calculatedValue) : '0'
);
) {
$objWriter->writeElement('v', StringHelper::formatNumber($calculatedValue));
}
}
}

Expand Down