From 14fd9bddebf5f69c21352049d9d27aea2c87d92d Mon Sep 17 00:00:00 2001 From: oleibman <10341515+oleibman@users.noreply.github.com> Date: Sun, 5 Feb 2023 18:54:11 -0800 Subject: [PATCH] Resolve Phpstan Messages in Writer Ods (#3342) * Resolve Phpstan Messages in Writer Ods Reduce number of Phpstan messages by addressing their issues. * Minor Fix Phpstan reports one fewer error under Php8 than previously. --- phpstan-baseline.neon | 35 -------------------- phpstan-conditional.php | 2 +- src/PhpSpreadsheet/Writer/Ods.php | 10 +----- src/PhpSpreadsheet/Writer/Ods/Cell/Style.php | 9 ++--- src/PhpSpreadsheet/Writer/Ods/Content.php | 9 ++--- src/PhpSpreadsheet/Writer/Ods/Formula.php | 1 + 6 files changed, 11 insertions(+), 55 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 85ac63aa78..2eb5ad49ad 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -595,41 +595,6 @@ parameters: count: 1 path: src/PhpSpreadsheet/Shared/Trend/Trend.php - - - message: "#^Negated boolean expression is always false\\.$#" - count: 1 - path: src/PhpSpreadsheet/Writer/Ods.php - - - - message: "#^If condition is always true\\.$#" - count: 2 - path: src/PhpSpreadsheet/Writer/Ods/Cell/Style.php - - - - message: "#^Property PhpOffice\\\\PhpSpreadsheet\\\\Writer\\\\Ods\\\\Cell\\\\Style\\:\\:\\$writer has no type specified\\.$#" - count: 1 - path: src/PhpSpreadsheet/Writer/Ods/Cell/Style.php - - - - message: "#^Parameter \\#1 \\$range of static method PhpOffice\\\\PhpSpreadsheet\\\\Cell\\\\Coordinate\\:\\:splitRange\\(\\) expects string, string\\|false given\\.$#" - count: 1 - path: src/PhpSpreadsheet/Writer/Ods/Content.php - - - - message: "#^Parameter \\#2 \\$value of method XMLWriter\\:\\:writeAttribute\\(\\) expects string, int\\<2, max\\> given\\.$#" - count: 3 - path: src/PhpSpreadsheet/Writer/Ods/Content.php - - - - message: "#^Property PhpOffice\\\\PhpSpreadsheet\\\\Writer\\\\Ods\\\\Content\\:\\:\\$formulaConvertor has no type specified\\.$#" - count: 1 - path: src/PhpSpreadsheet/Writer/Ods/Content.php - - - - message: "#^Property PhpOffice\\\\PhpSpreadsheet\\\\Writer\\\\Ods\\\\Formula\\:\\:\\$definedNames has no type specified\\.$#" - count: 1 - path: src/PhpSpreadsheet/Writer/Ods/Formula.php - - message: "#^Cannot use array destructuring on array\\|false\\.$#" count: 1 diff --git a/phpstan-conditional.php b/phpstan-conditional.php index 1d0c15a9dd..9b1150b34c 100644 --- a/phpstan-conditional.php +++ b/phpstan-conditional.php @@ -79,7 +79,7 @@ $config['parameters']['ignoreErrors'][] = [ 'message' => '#^Binary operation "/" between float and array[|]float[|]int[|]string results in an error.#', 'path' => __DIR__ . '/src/PhpSpreadsheet/Calculation/MathTrig/Combinations.php', - 'count' => 2, + 'count' => 1, ]; } diff --git a/src/PhpSpreadsheet/Writer/Ods.php b/src/PhpSpreadsheet/Writer/Ods.php index 827c43b560..872be52dee 100644 --- a/src/PhpSpreadsheet/Writer/Ods.php +++ b/src/PhpSpreadsheet/Writer/Ods.php @@ -117,10 +117,6 @@ public function getWriterPartThumbnails(): Thumbnails */ public function save($filename, int $flags = 0): void { - if (!$this->spreadSheet) { - throw new WriterException('PhpSpreadsheet object unassigned.'); - } - $this->processFlags($flags); // garbage collect @@ -176,11 +172,7 @@ private function createZip() */ public function getSpreadsheet() { - if ($this->spreadSheet !== null) { - return $this->spreadSheet; - } - - throw new WriterException('No PhpSpreadsheet assigned.'); + return $this->spreadSheet; } /** diff --git a/src/PhpSpreadsheet/Writer/Ods/Cell/Style.php b/src/PhpSpreadsheet/Writer/Ods/Cell/Style.php index 1bf2c46342..4d46a5e140 100644 --- a/src/PhpSpreadsheet/Writer/Ods/Cell/Style.php +++ b/src/PhpSpreadsheet/Writer/Ods/Cell/Style.php @@ -19,6 +19,7 @@ class Style public const ROW_STYLE_PREFIX = 'ro'; public const TABLE_STYLE_PREFIX = 'ta'; + /** @var XMLWriter */ private $writer; public function __construct(XMLWriter $writer) @@ -97,9 +98,7 @@ private function writeCellProperties(CellStyle $style): void $this->writer->writeAttribute('style:rotation-align', 'none'); // Fill - if ($fill = $style->getFill()) { - $this->writeFillStyle($fill); - } + $this->writeFillStyle($style->getFill()); $this->writer->endElement(); @@ -142,9 +141,7 @@ protected function writeTextProperties(CellStyle $style): void $this->writer->writeAttribute('fo:font-style', 'italic'); } - if ($color = $font->getColor()) { - $this->writer->writeAttribute('fo:color', sprintf('#%s', $color->getRGB())); - } + $this->writer->writeAttribute('fo:color', sprintf('#%s', $font->getColor()->getRGB())); if ($family = $font->getName()) { $this->writer->writeAttribute('fo:font-family', $family); diff --git a/src/PhpSpreadsheet/Writer/Ods/Content.php b/src/PhpSpreadsheet/Writer/Ods/Content.php index 00ab0643d9..e931421afd 100644 --- a/src/PhpSpreadsheet/Writer/Ods/Content.php +++ b/src/PhpSpreadsheet/Writer/Ods/Content.php @@ -23,6 +23,7 @@ class Content extends WriterPart const NUMBER_COLS_REPEATED_MAX = 1024; const NUMBER_ROWS_REPEATED_MAX = 1048576; + /** @var Formula */ private $formulaConvertor; /** @@ -155,7 +156,7 @@ private function writeRows(XMLWriter $objWriter, Worksheet $sheet, int $sheetInd $objWriter->startElement('table:table-row'); if ($span_row) { if ($span_row > 1) { - $objWriter->writeAttribute('table:number-rows-repeated', $span_row); + $objWriter->writeAttribute('table:number-rows-repeated', (string) $span_row); } $objWriter->startElement('table:table-cell'); $objWriter->writeAttribute('table:number-columns-repeated', (string) self::NUMBER_COLS_REPEATED_MAX); @@ -254,7 +255,7 @@ private function writeCells(XMLWriter $objWriter, RowCellIterator $cells): void if ($numberColsRepeated > 0) { if ($numberColsRepeated > 1) { $objWriter->startElement('table:table-cell'); - $objWriter->writeAttribute('table:number-columns-repeated', $numberColsRepeated); + $objWriter->writeAttribute('table:number-columns-repeated', (string) $numberColsRepeated); $objWriter->endElement(); } else { $objWriter->writeElement('table:table-cell'); @@ -275,7 +276,7 @@ private function writeCellSpan(XMLWriter $objWriter, $curColumn, $prevColumn): v $objWriter->writeElement('table:table-cell'); } elseif ($diff > 1) { $objWriter->startElement('table:table-cell'); - $objWriter->writeAttribute('table:number-columns-repeated', $diff); + $objWriter->writeAttribute('table:number-columns-repeated', (string) $diff); $objWriter->endElement(); } } @@ -322,7 +323,7 @@ private function writeCellMerge(XMLWriter $objWriter, Cell $cell): void return; } - $mergeRange = Coordinate::splitRange($cell->getMergeRange()); + $mergeRange = Coordinate::splitRange((string) $cell->getMergeRange()); [$startCell, $endCell] = $mergeRange[0]; $start = Coordinate::coordinateFromString($startCell); $end = Coordinate::coordinateFromString($endCell); diff --git a/src/PhpSpreadsheet/Writer/Ods/Formula.php b/src/PhpSpreadsheet/Writer/Ods/Formula.php index db766fb42e..cc2d2f4351 100644 --- a/src/PhpSpreadsheet/Writer/Ods/Formula.php +++ b/src/PhpSpreadsheet/Writer/Ods/Formula.php @@ -7,6 +7,7 @@ class Formula { + /** @var array */ private $definedNames = []; /**