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

Xlsx Reader Theme Support Broken After 17.1 #2403

Merged
merged 2 commits into from
Nov 26, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
100 changes: 0 additions & 100 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -4460,106 +4460,6 @@ parameters:
count: 1
path: src/PhpSpreadsheet/Reader/Xlsx/SheetViewOptions.php

-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Reader\\\\Xlsx\\\\Styles\\:\\:dxfs\\(\\) has no return type specified\\.$#"
count: 1
path: src/PhpSpreadsheet/Reader/Xlsx/Styles.php

-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Reader\\\\Xlsx\\\\Styles\\:\\:dxfs\\(\\) has parameter \\$readDataOnly with no type specified\\.$#"
count: 1
path: src/PhpSpreadsheet/Reader/Xlsx/Styles.php

-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Reader\\\\Xlsx\\\\Styles\\:\\:getArrayItem\\(\\) has no return type specified\\.$#"
count: 1
path: src/PhpSpreadsheet/Reader/Xlsx/Styles.php

-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Reader\\\\Xlsx\\\\Styles\\:\\:getArrayItem\\(\\) has parameter \\$array with no type specified\\.$#"
count: 1
path: src/PhpSpreadsheet/Reader/Xlsx/Styles.php

-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Reader\\\\Xlsx\\\\Styles\\:\\:getArrayItem\\(\\) has parameter \\$key with no type specified\\.$#"
count: 1
path: src/PhpSpreadsheet/Reader/Xlsx/Styles.php

-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Reader\\\\Xlsx\\\\Styles\\:\\:readColor\\(\\) has no return type specified\\.$#"
count: 1
path: src/PhpSpreadsheet/Reader/Xlsx/Styles.php

-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Reader\\\\Xlsx\\\\Styles\\:\\:readColor\\(\\) has parameter \\$background with no type specified\\.$#"
count: 1
path: src/PhpSpreadsheet/Reader/Xlsx/Styles.php

-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Reader\\\\Xlsx\\\\Styles\\:\\:readColor\\(\\) has parameter \\$color with no type specified\\.$#"
count: 1
path: src/PhpSpreadsheet/Reader/Xlsx/Styles.php

-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Reader\\\\Xlsx\\\\Styles\\:\\:readProtectionHidden\\(\\) has parameter \\$style with no type specified\\.$#"
count: 1
path: src/PhpSpreadsheet/Reader/Xlsx/Styles.php

-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Reader\\\\Xlsx\\\\Styles\\:\\:readProtectionLocked\\(\\) has parameter \\$style with no type specified\\.$#"
count: 1
path: src/PhpSpreadsheet/Reader/Xlsx/Styles.php

-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Reader\\\\Xlsx\\\\Styles\\:\\:readStyle\\(\\) has parameter \\$style with no type specified\\.$#"
count: 1
path: src/PhpSpreadsheet/Reader/Xlsx/Styles.php

-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Reader\\\\Xlsx\\\\Styles\\:\\:setStyleBaseData\\(\\) has parameter \\$cellStyles with no type specified\\.$#"
count: 1
path: src/PhpSpreadsheet/Reader/Xlsx/Styles.php

-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Reader\\\\Xlsx\\\\Styles\\:\\:setStyleBaseData\\(\\) has parameter \\$styles with no type specified\\.$#"
count: 1
path: src/PhpSpreadsheet/Reader/Xlsx/Styles.php

-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Reader\\\\Xlsx\\\\Styles\\:\\:styles\\(\\) has no return type specified\\.$#"
count: 1
path: src/PhpSpreadsheet/Reader/Xlsx/Styles.php

-
message: "#^Parameter \\#1 \\$hexColourValue of static method PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\Color\\:\\:changeBrightness\\(\\) expects string, string\\|null given\\.$#"
count: 1
path: src/PhpSpreadsheet/Reader/Xlsx/Styles.php

-
message: "#^Parameter \\#2 \\$alignmentXml of static method PhpOffice\\\\PhpSpreadsheet\\\\Reader\\\\Xlsx\\\\Styles\\:\\:readAlignmentStyle\\(\\) expects SimpleXMLElement, object given\\.$#"
count: 1
path: src/PhpSpreadsheet/Reader/Xlsx/Styles.php

-
message: "#^Property PhpOffice\\\\PhpSpreadsheet\\\\Reader\\\\Xlsx\\\\Styles\\:\\:\\$cellStyles has no type specified\\.$#"
count: 1
path: src/PhpSpreadsheet/Reader/Xlsx/Styles.php

-
message: "#^Property PhpOffice\\\\PhpSpreadsheet\\\\Reader\\\\Xlsx\\\\Styles\\:\\:\\$styleXml has no type specified\\.$#"
count: 1
path: src/PhpSpreadsheet/Reader/Xlsx/Styles.php

-
message: "#^Property PhpOffice\\\\PhpSpreadsheet\\\\Reader\\\\Xlsx\\\\Styles\\:\\:\\$styles has no type specified\\.$#"
count: 1
path: src/PhpSpreadsheet/Reader/Xlsx/Styles.php

-
message: "#^Static property PhpOffice\\\\PhpSpreadsheet\\\\Reader\\\\Xlsx\\\\Styles\\:\\:\\$theme \\(PhpOffice\\\\PhpSpreadsheet\\\\Reader\\\\Xlsx\\\\Theme\\) does not accept PhpOffice\\\\PhpSpreadsheet\\\\Reader\\\\Xlsx\\\\Theme\\|null\\.$#"
count: 1
path: src/PhpSpreadsheet/Reader/Xlsx/Styles.php

-
message: "#^Parameter \\#1 \\$haystack of function strpos expects string, string\\|false given\\.$#"
count: 1
Expand Down
72 changes: 16 additions & 56 deletions src/PhpSpreadsheet/Reader/Xlsx.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use PhpOffice\PhpSpreadsheet\Reader\Xlsx\SheetViewOptions;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx\SheetViews;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx\Styles;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx\Theme;
use PhpOffice\PhpSpreadsheet\ReferenceHelper;
use PhpOffice\PhpSpreadsheet\RichText\RichText;
use PhpOffice\PhpSpreadsheet\Settings;
Expand All @@ -34,7 +35,6 @@
use PhpOffice\PhpSpreadsheet\Worksheet\HeaderFooterDrawing;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use SimpleXMLElement;
use stdClass;
use Throwable;
use XMLReader;
use ZipArchive;
Expand All @@ -50,18 +50,14 @@ class Xlsx extends BaseReader
*/
private $referenceHelper;

/**
* Xlsx\Theme instance.
*
* @var Xlsx\Theme
*/
private static $theme;

/**
* @var ZipArchive
*/
private $zip;

/** @var Styles */
private $styleReader;

/**
* Create a new Xlsx Reader instance.
*/
Expand Down Expand Up @@ -406,6 +402,8 @@ public function load(string $filename, int $flags = 0): Spreadsheet
// Read the theme first, because we need the colour scheme when reading the styles
[$workbookBasename, $xmlNamespaceBase] = $this->getWorkbookBaseName();
$wbRels = $this->loadZip("xl/_rels/${workbookBasename}.rels", Namespaces::RELATIONSHIPS);
$theme = null;
$this->styleReader = new Styles();
foreach ($wbRels->Relationship as $relx) {
$rel = self::getAttributes($relx);
$relTarget = (string) $rel['Target'];
Expand Down Expand Up @@ -438,7 +436,8 @@ public function load(string $filename, int $flags = 0): Spreadsheet
$themeColours[$themePos] = (string) $xmlColourData['val'];
}
}
self::$theme = new Xlsx\Theme($themeName, $colourSchemeName, $themeColours);
$theme = new Theme($themeName, $colourSchemeName, $themeColours);
$this->styleReader->setTheme($theme);

break;
}
Expand Down Expand Up @@ -599,7 +598,7 @@ public function load(string $filename, int $flags = 0): Spreadsheet

// add style to cellXf collection
$objStyle = new Style();
self::readStyle($objStyle, $style);
$this->styleReader->readStyle($objStyle, $style);
if ($addingFirstCellXf) {
$excel->removeCellXfByIndex(0); // remove the default style
$addingFirstCellXf = false;
Expand Down Expand Up @@ -634,18 +633,18 @@ public function load(string $filename, int $flags = 0): Spreadsheet

// add style to cellStyleXf collection
$objStyle = new Style();
self::readStyle($objStyle, $cellStyle);
$this->styleReader->readStyle($objStyle, $cellStyle);
if ($addingFirstCellStyleXf) {
$excel->removeCellStyleXfByIndex(0); // remove the default style
$addingFirstCellStyleXf = false;
}
$excel->addCellStyleXf($objStyle);
}
}
$styleReader = new Styles($xmlStyles);
$styleReader->setStyleBaseData(self::$theme, $styles, $cellStyles);
$dxfs = $styleReader->dxfs($this->readDataOnly);
$styles = $styleReader->styles();
$this->styleReader->setStyleXml($xmlStyles);
$this->styleReader->setStyleBaseData($theme, $styles, $cellStyles);
$dxfs = $this->styleReader->dxfs($this->readDataOnly);
$styles = $this->styleReader->styles();

$xmlWorkbook = $this->loadZipNoNamespace($relTarget, $mainNS);
$xmlWorkbookNS = $this->loadZip($relTarget, $mainNS);
Expand Down Expand Up @@ -718,7 +717,7 @@ public function load(string $filename, int $flags = 0): Spreadsheet
}

$sheetViewOptions = new SheetViewOptions($docSheet, $xmlSheet);
$sheetViewOptions->load($this->getReadDataOnly());
$sheetViewOptions->load($this->getReadDataOnly(), $this->styleReader);

(new ColumnAndRowAttributes($docSheet, $xmlSheet))
->load($this->getReadFilter(), $this->getReadDataOnly());
Expand Down Expand Up @@ -1618,45 +1617,6 @@ public function load(string $filename, int $flags = 0): Spreadsheet
return $excel;
}

/**
* @param SimpleXMLElement|stdClass $style
*/
private static function readStyle(Style $docStyle, $style): void
{
$docStyle->getNumberFormat()->setFormatCode($style->numFmt);

// font
if (isset($style->font)) {
Styles::readFontStyle($docStyle->getFont(), $style->font);
}

// fill
if (isset($style->fill)) {
Styles::readFillStyle($docStyle->getFill(), $style->fill);
}

// border
if (isset($style->border)) {
Styles::readBorderStyle($docStyle->getBorders(), $style->border);
}

// alignment
if (isset($style->alignment)) {
Styles::readAlignmentStyle($docStyle->getAlignment(), $style->alignment);
}

// protection
if (isset($style->protection)) {
Styles::readProtectionLocked($docStyle, $style);
Styles::readProtectionHidden($docStyle, $style);
}

// top-level style settings
if (isset($style->quotePrefix)) {
$docStyle->setQuotePrefix((bool) $style->quotePrefix);
}
}

/**
* @return RichText
*/
Expand Down Expand Up @@ -1685,7 +1645,7 @@ private function parseRichText(?SimpleXMLElement $is)
$objText->getFont()->setSize((float) $attr['val']);
}
if (isset($run->rPr->color)) {
$objText->getFont()->setColor(new Color(Styles::readColor($run->rPr->color)));
$objText->getFont()->setColor(new Color($this->styleReader->readColor($run->rPr->color)));
}
if (isset($run->rPr->b)) {
$attr = $run->rPr->b->attributes();
Expand Down
13 changes: 5 additions & 8 deletions src/PhpSpreadsheet/Reader/Xlsx/SheetViewOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,14 @@ public function __construct(Worksheet $workSheet, ?SimpleXMLElement $worksheetXm
$this->worksheetXml = $worksheetXml;
}

/**
* @param bool $readDataOnly
*/
public function load($readDataOnly = false): void
public function load(bool $readDataOnly, Styles $styleReader): void
{
if ($this->worksheetXml === null) {
return;
}

if (isset($this->worksheetXml->sheetPr)) {
$this->tabColor($this->worksheetXml->sheetPr);
$this->tabColor($this->worksheetXml->sheetPr, $styleReader);
$this->codeName($this->worksheetXml->sheetPr);
$this->outlines($this->worksheetXml->sheetPr);
$this->pageSetup($this->worksheetXml->sheetPr);
Expand All @@ -42,10 +39,10 @@ public function load($readDataOnly = false): void
}
}

private function tabColor(SimpleXMLElement $sheetPr): void
private function tabColor(SimpleXMLElement $sheetPr, Styles $styleReader): void
{
if (isset($sheetPr->tabColor, $sheetPr->tabColor['rgb'])) {
$this->worksheet->getTabColor()->setARGB((string) $sheetPr->tabColor['rgb']);
if (isset($sheetPr->tabColor)) {
$this->worksheet->getTabColor()->setARGB($styleReader->readColor($sheetPr->tabColor));
}
}

Expand Down
Loading