Skip to content

Commit

Permalink
Eliminate a big chunck of duplicated code for reading styles (#2021)
Browse files Browse the repository at this point in the history
* Eliminate a big chunk of duplicated code for reading styles
  • Loading branch information
Mark Baker authored Apr 24, 2021
1 parent b05dc31 commit 55a41e8
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 181 deletions.
25 changes: 0 additions & 25 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -3940,31 +3940,6 @@ parameters:
count: 1
path: src/PhpSpreadsheet/Reader/Xlsx.php

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

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

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

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

-
message: "#^Parameter \\#1 \\$pValue of method PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\Font\\:\\:setSize\\(\\) expects float, string given\\.$#"
count: 1
path: src/PhpSpreadsheet/Reader/Xlsx.php

-
message: "#^Cannot access property \\$r on SimpleXMLElement\\|null\\.$#"
count: 2
Expand Down
154 changes: 15 additions & 139 deletions src/PhpSpreadsheet/Reader/Xlsx.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,8 @@
use PhpOffice\PhpSpreadsheet\Shared\Font;
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Style\Border;
use PhpOffice\PhpSpreadsheet\Style\Borders;
use PhpOffice\PhpSpreadsheet\Style\Color;
use PhpOffice\PhpSpreadsheet\Style\Fill;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
use PhpOffice\PhpSpreadsheet\Style\Protection;
use PhpOffice\PhpSpreadsheet\Style\Style;
use PhpOffice\PhpSpreadsheet\Worksheet\HeaderFooterDrawing;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
Expand Down Expand Up @@ -1570,31 +1566,6 @@ public function load($pFilename)
return $excel;
}

private static function readColor($color, $background = false)
{
if (isset($color['rgb'])) {
return (string) $color['rgb'];
} elseif (isset($color['indexed'])) {
return Color::indexedColor($color['indexed'] - 7, $background)->getARGB();
} elseif (isset($color['theme'])) {
if (self::$theme !== null) {
$returnColour = self::$theme->getColourByIndex((int) $color['theme']);
if (isset($color['tint'])) {
$tintAdjust = (float) $color['tint'];
$returnColour = Color::changeBrightness($returnColour, $tintAdjust);
}

return 'FF' . $returnColour;
}
}

if ($background) {
return 'FFFFFFFF';
}

return 'FF000000';
}

/**
* @param SimpleXMLElement|stdClass $style
*/
Expand All @@ -1604,116 +1575,28 @@ private static function readStyle(Style $docStyle, $style): void

// font
if (isset($style->font)) {
$docStyle->getFont()->setName((string) $style->font->name['val']);
$docStyle->getFont()->setSize((string) $style->font->sz['val']);
if (isset($style->font->b)) {
$docStyle->getFont()->setBold(!isset($style->font->b['val']) || self::boolean((string) $style->font->b['val']));
}
if (isset($style->font->i)) {
$docStyle->getFont()->setItalic(!isset($style->font->i['val']) || self::boolean((string) $style->font->i['val']));
}
if (isset($style->font->strike)) {
$docStyle->getFont()->setStrikethrough(!isset($style->font->strike['val']) || self::boolean((string) $style->font->strike['val']));
}
$docStyle->getFont()->getColor()->setARGB(self::readColor($style->font->color));

if (isset($style->font->u) && !isset($style->font->u['val'])) {
$docStyle->getFont()->setUnderline(\PhpOffice\PhpSpreadsheet\Style\Font::UNDERLINE_SINGLE);
} elseif (isset($style->font->u, $style->font->u['val'])) {
$docStyle->getFont()->setUnderline((string) $style->font->u['val']);
}

if (isset($style->font->vertAlign, $style->font->vertAlign['val'])) {
$vertAlign = strtolower((string) $style->font->vertAlign['val']);
if ($vertAlign == 'superscript') {
$docStyle->getFont()->setSuperscript(true);
}
if ($vertAlign == 'subscript') {
$docStyle->getFont()->setSubscript(true);
}
}
Styles::readFontStyle($docStyle->getFont(), $style->font);
}

// fill
if (isset($style->fill)) {
if ($style->fill->gradientFill) {
/** @var SimpleXMLElement $gradientFill */
$gradientFill = $style->fill->gradientFill[0];
if (!empty($gradientFill['type'])) {
$docStyle->getFill()->setFillType((string) $gradientFill['type']);
}
$docStyle->getFill()->setRotation((float) ($gradientFill['degree']));
$gradientFill->registerXPathNamespace('sml', 'http://schemas.openxmlformats.org/spreadsheetml/2006/main');
$docStyle->getFill()->getStartColor()->setARGB(self::readColor(self::getArrayItem($gradientFill->xpath('sml:stop[@position=0]'))->color));
$docStyle->getFill()->getEndColor()->setARGB(self::readColor(self::getArrayItem($gradientFill->xpath('sml:stop[@position=1]'))->color));
} elseif ($style->fill->patternFill) {
$patternType = (string) $style->fill->patternFill['patternType'] != '' ? (string) $style->fill->patternFill['patternType'] : Fill::FILL_NONE;
$docStyle->getFill()->setFillType($patternType);
if ($style->fill->patternFill->fgColor) {
$docStyle->getFill()->getStartColor()->setARGB(self::readColor($style->fill->patternFill->fgColor, true));
}
if ($style->fill->patternFill->bgColor) {
$docStyle->getFill()->getEndColor()->setARGB(self::readColor($style->fill->patternFill->bgColor, true));
}
}
Styles::readFillStyle($docStyle->getFill(), $style->fill);
}

// border
if (isset($style->border)) {
$diagonalUp = self::boolean((string) $style->border['diagonalUp']);
$diagonalDown = self::boolean((string) $style->border['diagonalDown']);
if (!$diagonalUp && !$diagonalDown) {
$docStyle->getBorders()->setDiagonalDirection(Borders::DIAGONAL_NONE);
} elseif ($diagonalUp && !$diagonalDown) {
$docStyle->getBorders()->setDiagonalDirection(Borders::DIAGONAL_UP);
} elseif (!$diagonalUp && $diagonalDown) {
$docStyle->getBorders()->setDiagonalDirection(Borders::DIAGONAL_DOWN);
} else {
$docStyle->getBorders()->setDiagonalDirection(Borders::DIAGONAL_BOTH);
}
self::readBorder($docStyle->getBorders()->getLeft(), $style->border->left);
self::readBorder($docStyle->getBorders()->getRight(), $style->border->right);
self::readBorder($docStyle->getBorders()->getTop(), $style->border->top);
self::readBorder($docStyle->getBorders()->getBottom(), $style->border->bottom);
self::readBorder($docStyle->getBorders()->getDiagonal(), $style->border->diagonal);
Styles::readBorderStyle($docStyle->getBorders(), $style->border);
}

// alignment
if (isset($style->alignment)) {
$docStyle->getAlignment()->setHorizontal((string) $style->alignment['horizontal']);
$docStyle->getAlignment()->setVertical((string) $style->alignment['vertical']);

$textRotation = 0;
if ((int) $style->alignment['textRotation'] <= 90) {
$textRotation = (int) $style->alignment['textRotation'];
} elseif ((int) $style->alignment['textRotation'] > 90) {
$textRotation = 90 - (int) $style->alignment['textRotation'];
}

$docStyle->getAlignment()->setTextRotation((int) $textRotation);
$docStyle->getAlignment()->setWrapText(self::boolean((string) $style->alignment['wrapText']));
$docStyle->getAlignment()->setShrinkToFit(self::boolean((string) $style->alignment['shrinkToFit']));
$docStyle->getAlignment()->setIndent((int) ((string) $style->alignment['indent']) > 0 ? (int) ((string) $style->alignment['indent']) : 0);
$docStyle->getAlignment()->setReadOrder((int) ((string) $style->alignment['readingOrder']) > 0 ? (int) ((string) $style->alignment['readingOrder']) : 0);
Styles::readAlignmentStyle($docStyle->getAlignment(), $style->alignment);
}

// protection
if (isset($style->protection)) {
if (isset($style->protection['locked'])) {
if (self::boolean((string) $style->protection['locked'])) {
$docStyle->getProtection()->setLocked(Protection::PROTECTION_PROTECTED);
} else {
$docStyle->getProtection()->setLocked(Protection::PROTECTION_UNPROTECTED);
}
}

if (isset($style->protection['hidden'])) {
if (self::boolean((string) $style->protection['hidden'])) {
$docStyle->getProtection()->setHidden(Protection::PROTECTION_PROTECTED);
} else {
$docStyle->getProtection()->setHidden(Protection::PROTECTION_UNPROTECTED);
}
}
Styles::readProtectionLocked($docStyle, $style->protection);
Styles::readProtectionHidden($docStyle, $style->protection);
}

// top-level style settings
Expand All @@ -1722,19 +1605,6 @@ private static function readStyle(Style $docStyle, $style): void
}
}

/**
* @param SimpleXMLElement $eleBorder
*/
private static function readBorder(Border $docBorder, $eleBorder): void
{
if (isset($eleBorder['style'])) {
$docBorder->setBorderStyle((string) $eleBorder['style']);
}
if (isset($eleBorder->color)) {
$docBorder->getColor()->setARGB(self::readColor($eleBorder->color));
}
}

/**
* @param SimpleXMLElement | null $is
*
Expand Down Expand Up @@ -1763,7 +1633,7 @@ private function parseRichText(?SimpleXMLElement $is)
$objText->getFont()->setSize((float) $run->rPr->sz['val']);
}
if (isset($run->rPr->color)) {
$objText->getFont()->setColor(new Color(self::readColor($run->rPr->color)));
$objText->getFont()->setColor(new Color(Styles::readColor($run->rPr->color)));
}
if (
(isset($run->rPr->b['val']) && self::boolean((string) $run->rPr->b['val'])) ||
Expand Down Expand Up @@ -1949,11 +1819,17 @@ private function readProtection(Spreadsheet $excel, SimpleXMLElement $xmlWorkboo
}

if ($xmlWorkbook->workbookProtection['revisionsPassword']) {
$excel->getSecurity()->setRevisionsPassword((string) $xmlWorkbook->workbookProtection['revisionsPassword'], true);
$excel->getSecurity()->setRevisionsPassword(
(string) $xmlWorkbook->workbookProtection['revisionsPassword'],
true
);
}

if ($xmlWorkbook->workbookProtection['workbookPassword']) {
$excel->getSecurity()->setWorkbookPassword((string) $xmlWorkbook->workbookProtection['workbookPassword'], true);
$excel->getSecurity()->setWorkbookPassword(
(string) $xmlWorkbook->workbookProtection['workbookPassword'],
true
);
}
}

Expand Down
45 changes: 28 additions & 17 deletions src/PhpSpreadsheet/Reader/Xlsx/Styles.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function setStyleBaseData(?Theme $theme = null, $styles = [], $cellStyles
$this->cellStyles = $cellStyles;
}

private static function readFontStyle(Font $fontStyle, SimpleXMLElement $fontStyleXml): void
public static function readFontStyle(Font $fontStyle, SimpleXMLElement $fontStyleXml): void
{
$fontStyle->setName((string) $fontStyleXml->name['val']);
$fontStyle->setSize((float) $fontStyleXml->sz['val']);
Expand All @@ -52,7 +52,9 @@ private static function readFontStyle(Font $fontStyle, SimpleXMLElement $fontSty
$fontStyle->setItalic(!isset($fontStyleXml->i['val']) || self::boolean((string) $fontStyleXml->i['val']));
}
if (isset($fontStyleXml->strike)) {
$fontStyle->setStrikethrough(!isset($fontStyleXml->strike['val']) || self::boolean((string) $fontStyleXml->strike['val']));
$fontStyle->setStrikethrough(
!isset($fontStyleXml->strike['val']) || self::boolean((string) $fontStyleXml->strike['val'])
);
}
$fontStyle->getColor()->setARGB(self::readColor($fontStyleXml->color));

Expand Down Expand Up @@ -84,7 +86,7 @@ private static function readNumberFormat(NumberFormat $numfmtStyle, SimpleXMLEle
}
}

private static function readFillStyle(Fill $fillStyle, SimpleXMLElement $fillStyleXml): void
public static function readFillStyle(Fill $fillStyle, SimpleXMLElement $fillStyleXml): void
{
if ($fillStyleXml->gradientFill) {
/** @var SimpleXMLElement $gradientFill */
Expand All @@ -94,23 +96,28 @@ private static function readFillStyle(Fill $fillStyle, SimpleXMLElement $fillSty
}
$fillStyle->setRotation((float) ($gradientFill['degree']));
$gradientFill->registerXPathNamespace('sml', 'http://schemas.openxmlformats.org/spreadsheetml/2006/main');
$fillStyle->getStartColor()->setARGB(self::readColor(self::getArrayItem($gradientFill->xpath('sml:stop[@position=0]'))->color));
$fillStyle->getEndColor()->setARGB(self::readColor(self::getArrayItem($gradientFill->xpath('sml:stop[@position=1]'))->color));
$fillStyle->getStartColor()->setARGB(
self::readColor(self::getArrayItem($gradientFill->xpath('sml:stop[@position=0]'))->color)
);
$fillStyle->getEndColor()->setARGB(
self::readColor(self::getArrayItem($gradientFill->xpath('sml:stop[@position=1]'))->color)
);
} elseif ($fillStyleXml->patternFill) {
$patternType = (string) $fillStyleXml->patternFill['patternType'] != '' ? (string) $fillStyleXml->patternFill['patternType'] : Fill::FILL_NONE;
$patternType = (string) $fillStyleXml->patternFill['patternType'] != ''
? (string) $fillStyleXml->patternFill['patternType']
: Fill::FILL_NONE;

$fillStyle->setFillType($patternType);
if ($fillStyleXml->patternFill->fgColor) {
$fillStyle->getStartColor()->setARGB(self::readColor($fillStyleXml->patternFill->fgColor, true));
} else {
$fillStyle->getStartColor()->setARGB('FF000000');
}
if ($fillStyleXml->patternFill->bgColor) {
$fillStyle->getEndColor()->setARGB(self::readColor($fillStyleXml->patternFill->bgColor, true));
}
}
}

private static function readBorderStyle(Borders $borderStyle, SimpleXMLElement $borderStyleXml): void
public static function readBorderStyle(Borders $borderStyle, SimpleXMLElement $borderStyleXml): void
{
$diagonalUp = self::boolean((string) $borderStyleXml['diagonalUp']);
$diagonalDown = self::boolean((string) $borderStyleXml['diagonalDown']);
Expand Down Expand Up @@ -141,7 +148,7 @@ private static function readBorder(Border $border, SimpleXMLElement $borderXml):
}
}

private static function readAlignmentStyle(Alignment $alignment, SimpleXMLElement $alignmentXml): void
public static function readAlignmentStyle(Alignment $alignment, SimpleXMLElement $alignmentXml): void
{
$alignment->setHorizontal((string) $alignmentXml['horizontal']);
$alignment->setVertical((string) $alignmentXml['vertical']);
Expand All @@ -156,8 +163,12 @@ private static function readAlignmentStyle(Alignment $alignment, SimpleXMLElemen
$alignment->setTextRotation((int) $textRotation);
$alignment->setWrapText(self::boolean((string) $alignmentXml['wrapText']));
$alignment->setShrinkToFit(self::boolean((string) $alignmentXml['shrinkToFit']));
$alignment->setIndent((int) ((string) $alignmentXml['indent']) > 0 ? (int) ((string) $alignmentXml['indent']) : 0);
$alignment->setReadOrder((int) ((string) $alignmentXml['readingOrder']) > 0 ? (int) ((string) $alignmentXml['readingOrder']) : 0);
$alignment->setIndent(
(int) ((string) $alignmentXml['indent']) > 0 ? (int) ((string) $alignmentXml['indent']) : 0
);
$alignment->setReadOrder(
(int) ((string) $alignmentXml['readingOrder']) > 0 ? (int) ((string) $alignmentXml['readingOrder']) : 0
);
}

private function readStyle(Style $docStyle, $style): void
Expand Down Expand Up @@ -186,8 +197,8 @@ private function readStyle(Style $docStyle, $style): void

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

// top-level style settings
Expand All @@ -196,7 +207,7 @@ private function readStyle(Style $docStyle, $style): void
}
}

private function readProtectionLocked(Style $docStyle, $style): void
public static function readProtectionLocked(Style $docStyle, $style): void
{
if (isset($style->protection['locked'])) {
if (self::boolean((string) $style->protection['locked'])) {
Expand All @@ -207,7 +218,7 @@ private function readProtectionLocked(Style $docStyle, $style): void
}
}

private function readProtectionHidden(Style $docStyle, $style): void
public static function readProtectionHidden(Style $docStyle, $style): void
{
if (isset($style->protection['hidden'])) {
if (self::boolean((string) $style->protection['hidden'])) {
Expand All @@ -218,7 +229,7 @@ private function readProtectionHidden(Style $docStyle, $style): void
}
}

private static function readColor($color, $background = false)
public static function readColor($color, $background = false)
{
if (isset($color['rgb'])) {
return (string) $color['rgb'];
Expand Down

0 comments on commit 55a41e8

Please sign in to comment.