Skip to content

Commit 55a41e8

Browse files
author
Mark Baker
authored
Eliminate a big chunck of duplicated code for reading styles (#2021)
* Eliminate a big chunk of duplicated code for reading styles
1 parent b05dc31 commit 55a41e8

File tree

3 files changed

+43
-181
lines changed

3 files changed

+43
-181
lines changed

phpstan-baseline.neon

-25
Original file line numberDiff line numberDiff line change
@@ -3940,31 +3940,6 @@ parameters:
39403940
count: 1
39413941
path: src/PhpSpreadsheet/Reader/Xlsx.php
39423942

3943-
-
3944-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Reader\\\\Xlsx\\:\\:readColor\\(\\) has no return typehint specified\\.$#"
3945-
count: 1
3946-
path: src/PhpSpreadsheet/Reader/Xlsx.php
3947-
3948-
-
3949-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Reader\\\\Xlsx\\:\\:readColor\\(\\) has parameter \\$background with no typehint specified\\.$#"
3950-
count: 1
3951-
path: src/PhpSpreadsheet/Reader/Xlsx.php
3952-
3953-
-
3954-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Reader\\\\Xlsx\\:\\:readColor\\(\\) has parameter \\$color with no typehint specified\\.$#"
3955-
count: 1
3956-
path: src/PhpSpreadsheet/Reader/Xlsx.php
3957-
3958-
-
3959-
message: "#^Parameter \\#1 \\$hex of static method PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\Color\\:\\:changeBrightness\\(\\) expects string, string\\|null given\\.$#"
3960-
count: 1
3961-
path: src/PhpSpreadsheet/Reader/Xlsx.php
3962-
3963-
-
3964-
message: "#^Parameter \\#1 \\$pValue of method PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\Font\\:\\:setSize\\(\\) expects float, string given\\.$#"
3965-
count: 1
3966-
path: src/PhpSpreadsheet/Reader/Xlsx.php
3967-
39683943
-
39693944
message: "#^Cannot access property \\$r on SimpleXMLElement\\|null\\.$#"
39703945
count: 2

src/PhpSpreadsheet/Reader/Xlsx.php

+15-139
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,8 @@
2727
use PhpOffice\PhpSpreadsheet\Shared\Font;
2828
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
2929
use PhpOffice\PhpSpreadsheet\Spreadsheet;
30-
use PhpOffice\PhpSpreadsheet\Style\Border;
31-
use PhpOffice\PhpSpreadsheet\Style\Borders;
3230
use PhpOffice\PhpSpreadsheet\Style\Color;
33-
use PhpOffice\PhpSpreadsheet\Style\Fill;
3431
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
35-
use PhpOffice\PhpSpreadsheet\Style\Protection;
3632
use PhpOffice\PhpSpreadsheet\Style\Style;
3733
use PhpOffice\PhpSpreadsheet\Worksheet\HeaderFooterDrawing;
3834
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
@@ -1570,31 +1566,6 @@ public function load($pFilename)
15701566
return $excel;
15711567
}
15721568

1573-
private static function readColor($color, $background = false)
1574-
{
1575-
if (isset($color['rgb'])) {
1576-
return (string) $color['rgb'];
1577-
} elseif (isset($color['indexed'])) {
1578-
return Color::indexedColor($color['indexed'] - 7, $background)->getARGB();
1579-
} elseif (isset($color['theme'])) {
1580-
if (self::$theme !== null) {
1581-
$returnColour = self::$theme->getColourByIndex((int) $color['theme']);
1582-
if (isset($color['tint'])) {
1583-
$tintAdjust = (float) $color['tint'];
1584-
$returnColour = Color::changeBrightness($returnColour, $tintAdjust);
1585-
}
1586-
1587-
return 'FF' . $returnColour;
1588-
}
1589-
}
1590-
1591-
if ($background) {
1592-
return 'FFFFFFFF';
1593-
}
1594-
1595-
return 'FF000000';
1596-
}
1597-
15981569
/**
15991570
* @param SimpleXMLElement|stdClass $style
16001571
*/
@@ -1604,116 +1575,28 @@ private static function readStyle(Style $docStyle, $style): void
16041575

16051576
// font
16061577
if (isset($style->font)) {
1607-
$docStyle->getFont()->setName((string) $style->font->name['val']);
1608-
$docStyle->getFont()->setSize((string) $style->font->sz['val']);
1609-
if (isset($style->font->b)) {
1610-
$docStyle->getFont()->setBold(!isset($style->font->b['val']) || self::boolean((string) $style->font->b['val']));
1611-
}
1612-
if (isset($style->font->i)) {
1613-
$docStyle->getFont()->setItalic(!isset($style->font->i['val']) || self::boolean((string) $style->font->i['val']));
1614-
}
1615-
if (isset($style->font->strike)) {
1616-
$docStyle->getFont()->setStrikethrough(!isset($style->font->strike['val']) || self::boolean((string) $style->font->strike['val']));
1617-
}
1618-
$docStyle->getFont()->getColor()->setARGB(self::readColor($style->font->color));
1619-
1620-
if (isset($style->font->u) && !isset($style->font->u['val'])) {
1621-
$docStyle->getFont()->setUnderline(\PhpOffice\PhpSpreadsheet\Style\Font::UNDERLINE_SINGLE);
1622-
} elseif (isset($style->font->u, $style->font->u['val'])) {
1623-
$docStyle->getFont()->setUnderline((string) $style->font->u['val']);
1624-
}
1625-
1626-
if (isset($style->font->vertAlign, $style->font->vertAlign['val'])) {
1627-
$vertAlign = strtolower((string) $style->font->vertAlign['val']);
1628-
if ($vertAlign == 'superscript') {
1629-
$docStyle->getFont()->setSuperscript(true);
1630-
}
1631-
if ($vertAlign == 'subscript') {
1632-
$docStyle->getFont()->setSubscript(true);
1633-
}
1634-
}
1578+
Styles::readFontStyle($docStyle->getFont(), $style->font);
16351579
}
16361580

16371581
// fill
16381582
if (isset($style->fill)) {
1639-
if ($style->fill->gradientFill) {
1640-
/** @var SimpleXMLElement $gradientFill */
1641-
$gradientFill = $style->fill->gradientFill[0];
1642-
if (!empty($gradientFill['type'])) {
1643-
$docStyle->getFill()->setFillType((string) $gradientFill['type']);
1644-
}
1645-
$docStyle->getFill()->setRotation((float) ($gradientFill['degree']));
1646-
$gradientFill->registerXPathNamespace('sml', 'http://schemas.openxmlformats.org/spreadsheetml/2006/main');
1647-
$docStyle->getFill()->getStartColor()->setARGB(self::readColor(self::getArrayItem($gradientFill->xpath('sml:stop[@position=0]'))->color));
1648-
$docStyle->getFill()->getEndColor()->setARGB(self::readColor(self::getArrayItem($gradientFill->xpath('sml:stop[@position=1]'))->color));
1649-
} elseif ($style->fill->patternFill) {
1650-
$patternType = (string) $style->fill->patternFill['patternType'] != '' ? (string) $style->fill->patternFill['patternType'] : Fill::FILL_NONE;
1651-
$docStyle->getFill()->setFillType($patternType);
1652-
if ($style->fill->patternFill->fgColor) {
1653-
$docStyle->getFill()->getStartColor()->setARGB(self::readColor($style->fill->patternFill->fgColor, true));
1654-
}
1655-
if ($style->fill->patternFill->bgColor) {
1656-
$docStyle->getFill()->getEndColor()->setARGB(self::readColor($style->fill->patternFill->bgColor, true));
1657-
}
1658-
}
1583+
Styles::readFillStyle($docStyle->getFill(), $style->fill);
16591584
}
16601585

16611586
// border
16621587
if (isset($style->border)) {
1663-
$diagonalUp = self::boolean((string) $style->border['diagonalUp']);
1664-
$diagonalDown = self::boolean((string) $style->border['diagonalDown']);
1665-
if (!$diagonalUp && !$diagonalDown) {
1666-
$docStyle->getBorders()->setDiagonalDirection(Borders::DIAGONAL_NONE);
1667-
} elseif ($diagonalUp && !$diagonalDown) {
1668-
$docStyle->getBorders()->setDiagonalDirection(Borders::DIAGONAL_UP);
1669-
} elseif (!$diagonalUp && $diagonalDown) {
1670-
$docStyle->getBorders()->setDiagonalDirection(Borders::DIAGONAL_DOWN);
1671-
} else {
1672-
$docStyle->getBorders()->setDiagonalDirection(Borders::DIAGONAL_BOTH);
1673-
}
1674-
self::readBorder($docStyle->getBorders()->getLeft(), $style->border->left);
1675-
self::readBorder($docStyle->getBorders()->getRight(), $style->border->right);
1676-
self::readBorder($docStyle->getBorders()->getTop(), $style->border->top);
1677-
self::readBorder($docStyle->getBorders()->getBottom(), $style->border->bottom);
1678-
self::readBorder($docStyle->getBorders()->getDiagonal(), $style->border->diagonal);
1588+
Styles::readBorderStyle($docStyle->getBorders(), $style->border);
16791589
}
16801590

16811591
// alignment
16821592
if (isset($style->alignment)) {
1683-
$docStyle->getAlignment()->setHorizontal((string) $style->alignment['horizontal']);
1684-
$docStyle->getAlignment()->setVertical((string) $style->alignment['vertical']);
1685-
1686-
$textRotation = 0;
1687-
if ((int) $style->alignment['textRotation'] <= 90) {
1688-
$textRotation = (int) $style->alignment['textRotation'];
1689-
} elseif ((int) $style->alignment['textRotation'] > 90) {
1690-
$textRotation = 90 - (int) $style->alignment['textRotation'];
1691-
}
1692-
1693-
$docStyle->getAlignment()->setTextRotation((int) $textRotation);
1694-
$docStyle->getAlignment()->setWrapText(self::boolean((string) $style->alignment['wrapText']));
1695-
$docStyle->getAlignment()->setShrinkToFit(self::boolean((string) $style->alignment['shrinkToFit']));
1696-
$docStyle->getAlignment()->setIndent((int) ((string) $style->alignment['indent']) > 0 ? (int) ((string) $style->alignment['indent']) : 0);
1697-
$docStyle->getAlignment()->setReadOrder((int) ((string) $style->alignment['readingOrder']) > 0 ? (int) ((string) $style->alignment['readingOrder']) : 0);
1593+
Styles::readAlignmentStyle($docStyle->getAlignment(), $style->alignment);
16981594
}
16991595

17001596
// protection
17011597
if (isset($style->protection)) {
1702-
if (isset($style->protection['locked'])) {
1703-
if (self::boolean((string) $style->protection['locked'])) {
1704-
$docStyle->getProtection()->setLocked(Protection::PROTECTION_PROTECTED);
1705-
} else {
1706-
$docStyle->getProtection()->setLocked(Protection::PROTECTION_UNPROTECTED);
1707-
}
1708-
}
1709-
1710-
if (isset($style->protection['hidden'])) {
1711-
if (self::boolean((string) $style->protection['hidden'])) {
1712-
$docStyle->getProtection()->setHidden(Protection::PROTECTION_PROTECTED);
1713-
} else {
1714-
$docStyle->getProtection()->setHidden(Protection::PROTECTION_UNPROTECTED);
1715-
}
1716-
}
1598+
Styles::readProtectionLocked($docStyle, $style->protection);
1599+
Styles::readProtectionHidden($docStyle, $style->protection);
17171600
}
17181601

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

1725-
/**
1726-
* @param SimpleXMLElement $eleBorder
1727-
*/
1728-
private static function readBorder(Border $docBorder, $eleBorder): void
1729-
{
1730-
if (isset($eleBorder['style'])) {
1731-
$docBorder->setBorderStyle((string) $eleBorder['style']);
1732-
}
1733-
if (isset($eleBorder->color)) {
1734-
$docBorder->getColor()->setARGB(self::readColor($eleBorder->color));
1735-
}
1736-
}
1737-
17381608
/**
17391609
* @param SimpleXMLElement | null $is
17401610
*
@@ -1763,7 +1633,7 @@ private function parseRichText(?SimpleXMLElement $is)
17631633
$objText->getFont()->setSize((float) $run->rPr->sz['val']);
17641634
}
17651635
if (isset($run->rPr->color)) {
1766-
$objText->getFont()->setColor(new Color(self::readColor($run->rPr->color)));
1636+
$objText->getFont()->setColor(new Color(Styles::readColor($run->rPr->color)));
17671637
}
17681638
if (
17691639
(isset($run->rPr->b['val']) && self::boolean((string) $run->rPr->b['val'])) ||
@@ -1949,11 +1819,17 @@ private function readProtection(Spreadsheet $excel, SimpleXMLElement $xmlWorkboo
19491819
}
19501820

19511821
if ($xmlWorkbook->workbookProtection['revisionsPassword']) {
1952-
$excel->getSecurity()->setRevisionsPassword((string) $xmlWorkbook->workbookProtection['revisionsPassword'], true);
1822+
$excel->getSecurity()->setRevisionsPassword(
1823+
(string) $xmlWorkbook->workbookProtection['revisionsPassword'],
1824+
true
1825+
);
19531826
}
19541827

19551828
if ($xmlWorkbook->workbookProtection['workbookPassword']) {
1956-
$excel->getSecurity()->setWorkbookPassword((string) $xmlWorkbook->workbookProtection['workbookPassword'], true);
1829+
$excel->getSecurity()->setWorkbookPassword(
1830+
(string) $xmlWorkbook->workbookProtection['workbookPassword'],
1831+
true
1832+
);
19571833
}
19581834
}
19591835

src/PhpSpreadsheet/Reader/Xlsx/Styles.php

+28-17
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function setStyleBaseData(?Theme $theme = null, $styles = [], $cellStyles
4040
$this->cellStyles = $cellStyles;
4141
}
4242

43-
private static function readFontStyle(Font $fontStyle, SimpleXMLElement $fontStyleXml): void
43+
public static function readFontStyle(Font $fontStyle, SimpleXMLElement $fontStyleXml): void
4444
{
4545
$fontStyle->setName((string) $fontStyleXml->name['val']);
4646
$fontStyle->setSize((float) $fontStyleXml->sz['val']);
@@ -52,7 +52,9 @@ private static function readFontStyle(Font $fontStyle, SimpleXMLElement $fontSty
5252
$fontStyle->setItalic(!isset($fontStyleXml->i['val']) || self::boolean((string) $fontStyleXml->i['val']));
5353
}
5454
if (isset($fontStyleXml->strike)) {
55-
$fontStyle->setStrikethrough(!isset($fontStyleXml->strike['val']) || self::boolean((string) $fontStyleXml->strike['val']));
55+
$fontStyle->setStrikethrough(
56+
!isset($fontStyleXml->strike['val']) || self::boolean((string) $fontStyleXml->strike['val'])
57+
);
5658
}
5759
$fontStyle->getColor()->setARGB(self::readColor($fontStyleXml->color));
5860

@@ -84,7 +86,7 @@ private static function readNumberFormat(NumberFormat $numfmtStyle, SimpleXMLEle
8486
}
8587
}
8688

87-
private static function readFillStyle(Fill $fillStyle, SimpleXMLElement $fillStyleXml): void
89+
public static function readFillStyle(Fill $fillStyle, SimpleXMLElement $fillStyleXml): void
8890
{
8991
if ($fillStyleXml->gradientFill) {
9092
/** @var SimpleXMLElement $gradientFill */
@@ -94,23 +96,28 @@ private static function readFillStyle(Fill $fillStyle, SimpleXMLElement $fillSty
9496
}
9597
$fillStyle->setRotation((float) ($gradientFill['degree']));
9698
$gradientFill->registerXPathNamespace('sml', 'http://schemas.openxmlformats.org/spreadsheetml/2006/main');
97-
$fillStyle->getStartColor()->setARGB(self::readColor(self::getArrayItem($gradientFill->xpath('sml:stop[@position=0]'))->color));
98-
$fillStyle->getEndColor()->setARGB(self::readColor(self::getArrayItem($gradientFill->xpath('sml:stop[@position=1]'))->color));
99+
$fillStyle->getStartColor()->setARGB(
100+
self::readColor(self::getArrayItem($gradientFill->xpath('sml:stop[@position=0]'))->color)
101+
);
102+
$fillStyle->getEndColor()->setARGB(
103+
self::readColor(self::getArrayItem($gradientFill->xpath('sml:stop[@position=1]'))->color)
104+
);
99105
} elseif ($fillStyleXml->patternFill) {
100-
$patternType = (string) $fillStyleXml->patternFill['patternType'] != '' ? (string) $fillStyleXml->patternFill['patternType'] : Fill::FILL_NONE;
106+
$patternType = (string) $fillStyleXml->patternFill['patternType'] != ''
107+
? (string) $fillStyleXml->patternFill['patternType']
108+
: Fill::FILL_NONE;
109+
101110
$fillStyle->setFillType($patternType);
102111
if ($fillStyleXml->patternFill->fgColor) {
103112
$fillStyle->getStartColor()->setARGB(self::readColor($fillStyleXml->patternFill->fgColor, true));
104-
} else {
105-
$fillStyle->getStartColor()->setARGB('FF000000');
106113
}
107114
if ($fillStyleXml->patternFill->bgColor) {
108115
$fillStyle->getEndColor()->setARGB(self::readColor($fillStyleXml->patternFill->bgColor, true));
109116
}
110117
}
111118
}
112119

113-
private static function readBorderStyle(Borders $borderStyle, SimpleXMLElement $borderStyleXml): void
120+
public static function readBorderStyle(Borders $borderStyle, SimpleXMLElement $borderStyleXml): void
114121
{
115122
$diagonalUp = self::boolean((string) $borderStyleXml['diagonalUp']);
116123
$diagonalDown = self::boolean((string) $borderStyleXml['diagonalDown']);
@@ -141,7 +148,7 @@ private static function readBorder(Border $border, SimpleXMLElement $borderXml):
141148
}
142149
}
143150

144-
private static function readAlignmentStyle(Alignment $alignment, SimpleXMLElement $alignmentXml): void
151+
public static function readAlignmentStyle(Alignment $alignment, SimpleXMLElement $alignmentXml): void
145152
{
146153
$alignment->setHorizontal((string) $alignmentXml['horizontal']);
147154
$alignment->setVertical((string) $alignmentXml['vertical']);
@@ -156,8 +163,12 @@ private static function readAlignmentStyle(Alignment $alignment, SimpleXMLElemen
156163
$alignment->setTextRotation((int) $textRotation);
157164
$alignment->setWrapText(self::boolean((string) $alignmentXml['wrapText']));
158165
$alignment->setShrinkToFit(self::boolean((string) $alignmentXml['shrinkToFit']));
159-
$alignment->setIndent((int) ((string) $alignmentXml['indent']) > 0 ? (int) ((string) $alignmentXml['indent']) : 0);
160-
$alignment->setReadOrder((int) ((string) $alignmentXml['readingOrder']) > 0 ? (int) ((string) $alignmentXml['readingOrder']) : 0);
166+
$alignment->setIndent(
167+
(int) ((string) $alignmentXml['indent']) > 0 ? (int) ((string) $alignmentXml['indent']) : 0
168+
);
169+
$alignment->setReadOrder(
170+
(int) ((string) $alignmentXml['readingOrder']) > 0 ? (int) ((string) $alignmentXml['readingOrder']) : 0
171+
);
161172
}
162173

163174
private function readStyle(Style $docStyle, $style): void
@@ -186,8 +197,8 @@ private function readStyle(Style $docStyle, $style): void
186197

187198
// protection
188199
if (isset($style->protection)) {
189-
$this->readProtectionLocked($docStyle, $style);
190-
$this->readProtectionHidden($docStyle, $style);
200+
self::readProtectionLocked($docStyle, $style);
201+
self::readProtectionHidden($docStyle, $style);
191202
}
192203

193204
// top-level style settings
@@ -196,7 +207,7 @@ private function readStyle(Style $docStyle, $style): void
196207
}
197208
}
198209

199-
private function readProtectionLocked(Style $docStyle, $style): void
210+
public static function readProtectionLocked(Style $docStyle, $style): void
200211
{
201212
if (isset($style->protection['locked'])) {
202213
if (self::boolean((string) $style->protection['locked'])) {
@@ -207,7 +218,7 @@ private function readProtectionLocked(Style $docStyle, $style): void
207218
}
208219
}
209220

210-
private function readProtectionHidden(Style $docStyle, $style): void
221+
public static function readProtectionHidden(Style $docStyle, $style): void
211222
{
212223
if (isset($style->protection['hidden'])) {
213224
if (self::boolean((string) $style->protection['hidden'])) {
@@ -218,7 +229,7 @@ private function readProtectionHidden(Style $docStyle, $style): void
218229
}
219230
}
220231

221-
private static function readColor($color, $background = false)
232+
public static function readColor($color, $background = false)
222233
{
223234
if (isset($color['rgb'])) {
224235
return (string) $color['rgb'];

0 commit comments

Comments
 (0)