Failed Conditions
Push — master ( 7f5e0f...e5409f )
by Adrien
268:34 queued 262:01
created

Styles::readFontStyle()   C

Complexity

Conditions 13
Paths 120

Size

Total Lines 29
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 17.563

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 13
eloc 19
c 1
b 0
f 0
nc 120
nop 2
dl 0
loc 29
ccs 14
cts 20
cp 0.7
crap 17.563
rs 6.45

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Reader\Xlsx;
4
5
use PhpOffice\PhpSpreadsheet\Style\Alignment;
6
use PhpOffice\PhpSpreadsheet\Style\Border;
7
use PhpOffice\PhpSpreadsheet\Style\Borders;
8
use PhpOffice\PhpSpreadsheet\Style\Color;
9
use PhpOffice\PhpSpreadsheet\Style\Fill;
10
use PhpOffice\PhpSpreadsheet\Style\Font;
11
use PhpOffice\PhpSpreadsheet\Style\Protection;
12
use PhpOffice\PhpSpreadsheet\Style\Style;
13
14
class Styles extends BaseParserClass
15
{
16
    /**
17
     * Theme instance.
18
     *
19
     * @var Theme
20
     */
21
    private static $theme = null;
22
23
    private $styles = [];
24
25
    private $cellStyles = [];
26
27
    private $styleXml;
28
29 48
    public function __construct(\SimpleXMLElement $styleXml)
30
    {
31 48
        $this->styleXml = $styleXml;
32 48
    }
33
34 48
    public function setStyleBaseData(Theme $theme = null, $styles = [], $cellStyles = [])
35
    {
36 48
        self::$theme = $theme;
37 48
        $this->styles = $styles;
38 48
        $this->cellStyles = $cellStyles;
39 48
    }
40
41 48
    private static function readFontStyle(Font $fontStyle, \SimpleXMLElement $fontStyleXml)
42
    {
43 48
        $fontStyle->setName((string) $fontStyleXml->name['val']);
44 48
        $fontStyle->setSize((float) $fontStyleXml->sz['val']);
45
46 48
        if (isset($fontStyleXml->b)) {
47 34
            $fontStyle->setBold(!isset($fontStyleXml->b['val']) || self::boolean((string) $fontStyleXml->b['val']));
48
        }
49 48
        if (isset($fontStyleXml->i)) {
50 34
            $fontStyle->setItalic(!isset($fontStyleXml->i['val']) || self::boolean((string) $fontStyleXml->i['val']));
51
        }
52 48
        if (isset($fontStyleXml->strike)) {
53 33
            $fontStyle->setStrikethrough(!isset($fontStyleXml->strike['val']) || self::boolean((string) $fontStyleXml->strike['val']));
54
        }
55 48
        $fontStyle->getColor()->setARGB(self::readColor($fontStyleXml->color));
56
57 48
        if (isset($fontStyleXml->u) && !isset($fontStyleXml->u['val'])) {
58
            $fontStyle->setUnderline(Font::UNDERLINE_SINGLE);
59 48
        } elseif (isset($fontStyleXml->u, $fontStyleXml->u['val'])) {
60 33
            $fontStyle->setUnderline((string) $fontStyleXml->u['val']);
61
        }
62
63 48
        if (isset($fontStyleXml->vertAlign, $fontStyleXml->vertAlign['val'])) {
64
            $verticalAlign = strtolower((string) $fontStyleXml->vertAlign['val']);
65
            if ($verticalAlign === 'superscript') {
66
                $fontStyle->setSuperscript(true);
67
            }
68
            if ($verticalAlign === 'subscript') {
69
                $fontStyle->setSubscript(true);
70
            }
71
        }
72 48
    }
73
74 48
    private static function readFillStyle(Fill $fillStyle, \SimpleXMLElement $fillStyleXml)
75
    {
76 48
        if ($fillStyleXml->gradientFill) {
77
            /** @var \SimpleXMLElement $gradientFill */
78
            $gradientFill = $fillStyleXml->gradientFill[0];
79
            if (!empty($gradientFill['type'])) {
80
                $fillStyle->setFillType((string) $gradientFill['type']);
81
            }
82
            $fillStyle->setRotation((float) ($gradientFill['degree']));
83
            $gradientFill->registerXPathNamespace('sml', 'http://schemas.openxmlformats.org/spreadsheetml/2006/main');
84
            $fillStyle->getStartColor()->setARGB(self::readColor(self::getArrayItem($gradientFill->xpath('sml:stop[@position=0]'))->color));
85
            $fillStyle->getEndColor()->setARGB(self::readColor(self::getArrayItem($gradientFill->xpath('sml:stop[@position=1]'))->color));
86 48
        } elseif ($fillStyleXml->patternFill) {
87 48
            $patternType = (string) $fillStyleXml->patternFill['patternType'] != '' ? (string) $fillStyleXml->patternFill['patternType'] : 'solid';
88 48
            $fillStyle->setFillType($patternType);
89 48
            if ($fillStyleXml->patternFill->fgColor) {
90
                $fillStyle->getStartColor()->setARGB(self::readColor($fillStyleXml->patternFill->fgColor, true));
91
            } else {
92 48
                $fillStyle->getStartColor()->setARGB('FF000000');
93
            }
94 48
            if ($fillStyleXml->patternFill->bgColor) {
95 1
                $fillStyle->getEndColor()->setARGB(self::readColor($fillStyleXml->patternFill->bgColor, true));
96
            }
97
        }
98 48
    }
99
100 48
    private static function readBorderStyle(Borders $borderStyle, \SimpleXMLElement $borderStyleXml)
101
    {
102 48
        $diagonalUp = self::boolean((string) $borderStyleXml['diagonalUp']);
103 48
        $diagonalDown = self::boolean((string) $borderStyleXml['diagonalDown']);
104 48
        if (!$diagonalUp && !$diagonalDown) {
105 48
            $borderStyle->setDiagonalDirection(Borders::DIAGONAL_NONE);
106
        } elseif ($diagonalUp && !$diagonalDown) {
107
            $borderStyle->setDiagonalDirection(Borders::DIAGONAL_UP);
108
        } elseif (!$diagonalUp && $diagonalDown) {
109
            $borderStyle->setDiagonalDirection(Borders::DIAGONAL_DOWN);
110
        } else {
111
            $borderStyle->setDiagonalDirection(Borders::DIAGONAL_BOTH);
112
        }
113
114 48
        self::readBorder($borderStyle->getLeft(), $borderStyleXml->left);
115 48
        self::readBorder($borderStyle->getRight(), $borderStyleXml->right);
116 48
        self::readBorder($borderStyle->getTop(), $borderStyleXml->top);
117 48
        self::readBorder($borderStyle->getBottom(), $borderStyleXml->bottom);
118 48
        self::readBorder($borderStyle->getDiagonal(), $borderStyleXml->diagonal);
119 48
    }
120
121 48
    private static function readBorder(Border $border, \SimpleXMLElement $borderXml)
122
    {
123 48
        if (isset($borderXml['style'])) {
124
            $border->setBorderStyle((string) $borderXml['style']);
125
        }
126 48
        if (isset($borderXml->color)) {
127
            $border->getColor()->setARGB(self::readColor($borderXml->color));
128
        }
129 48
    }
130
131
    private static function readAlignmentStyle(Alignment $alignment, \SimpleXMLElement $alignmentXml)
132
    {
133
        $alignment->setHorizontal((string) $alignmentXml->alignment['horizontal']);
134
        $alignment->setVertical((string) $alignmentXml->alignment['vertical']);
135
136
        $textRotation = 0;
137
        if ((int) $alignmentXml->alignment['textRotation'] <= 90) {
138
            $textRotation = (int) $alignmentXml->alignment['textRotation'];
139
        } elseif ((int) $alignmentXml->alignment['textRotation'] > 90) {
140
            $textRotation = 90 - (int) $alignmentXml->alignment['textRotation'];
141
        }
142
143
        $alignment->setTextRotation((int) $textRotation);
144
        $alignment->setWrapText(self::boolean((string) $alignmentXml->alignment['wrapText']));
145
        $alignment->setShrinkToFit(self::boolean((string) $alignmentXml->alignment['shrinkToFit']));
146
        $alignment->setIndent((int) ((string) $alignmentXml->alignment['indent']) > 0 ? (int) ((string) $alignmentXml->alignment['indent']) : 0);
147
        $alignment->setReadOrder((int) ((string) $alignmentXml->alignment['readingOrder']) > 0 ? (int) ((string) $alignmentXml->alignment['readingOrder']) : 0);
148
    }
149
150 48
    private function readStyle(Style $docStyle, $style)
151
    {
152 48
        $docStyle->getNumberFormat()->setFormatCode($style->numFmt);
153
154 48
        if (isset($style->font)) {
155 48
            self::readFontStyle($docStyle->getFont(), $style->font);
156
        }
157
158 48
        if (isset($style->fill)) {
159 48
            self::readFillStyle($docStyle->getFill(), $style->fill);
160
        }
161
162 48
        if (isset($style->border)) {
163 48
            self::readBorderStyle($docStyle->getBorders(), $style->border);
164
        }
165
166 48
        if (isset($style->alignment->alignment)) {
167
            self::readAlignmentStyle($docStyle->getAlignment(), $style->alignment);
168
        }
169
170
        // protection
171 48
        if (isset($style->protection)) {
172 48
            $this->readProtectionLocked($docStyle, $style);
173 48
            $this->readProtectionHidden($docStyle, $style);
174
        }
175
176
        // top-level style settings
177 48
        if (isset($style->quotePrefix)) {
178 48
            $docStyle->setQuotePrefix(true);
179
        }
180 48
    }
181
182 48
    private function readProtectionLocked(Style $docStyle, $style)
183
    {
184 48
        if (isset($style->protection['locked'])) {
185
            if (self::boolean((string) $style->protection['locked'])) {
186
                $docStyle->getProtection()->setLocked(Protection::PROTECTION_PROTECTED);
187
            } else {
188
                $docStyle->getProtection()->setLocked(Protection::PROTECTION_UNPROTECTED);
189
            }
190
        }
191 48
    }
192
193 48
    private function readProtectionHidden(Style $docStyle, $style)
194
    {
195 48
        if (isset($style->protection['hidden'])) {
196
            if (self::boolean((string) $style->protection['hidden'])) {
197
                $docStyle->getProtection()->setHidden(Protection::PROTECTION_PROTECTED);
198
            } else {
199
                $docStyle->getProtection()->setHidden(Protection::PROTECTION_UNPROTECTED);
200
            }
201
        }
202 48
    }
203
204 48
    private static function readColor($color, $background = false)
205
    {
206 48
        if (isset($color['rgb'])) {
207 37
            return (string) $color['rgb'];
208 13
        } elseif (isset($color['indexed'])) {
209 3
            return Color::indexedColor($color['indexed'] - 7, $background)->getARGB();
210 10
        } elseif (isset($color['theme'])) {
211 9
            if (self::$theme !== null) {
212 9
                $returnColour = self::$theme->getColourByIndex((int) $color['theme']);
213 9
                if (isset($color['tint'])) {
214
                    $tintAdjust = (float) $color['tint'];
215
                    $returnColour = Color::changeBrightness($returnColour, $tintAdjust);
216
                }
217
218 9
                return 'FF' . $returnColour;
219
            }
220
        }
221
222 1
        return ($background) ? 'FFFFFFFF' : 'FF000000';
223
    }
224
225 48
    public function dxfs($readDataOnly = false)
226
    {
227 48
        $dxfs = [];
228 48
        if (!$readDataOnly && $this->styleXml) {
229
            //    Conditional Styles
230 48
            if ($this->styleXml->dxfs) {
231 48
                foreach ($this->styleXml->dxfs->dxf as $dxf) {
232 2
                    $style = new Style(false, true);
233 2
                    $this->readStyle($style, $dxf);
234 2
                    $dxfs[] = $style;
235
                }
236
            }
237
            //    Cell Styles
238 48
            if ($this->styleXml->cellStyles) {
239 48
                foreach ($this->styleXml->cellStyles->cellStyle as $cellStyle) {
240 48
                    if ((int) ($cellStyle['builtinId']) == 0) {
241 48
                        if (isset($this->cellStyles[(int) ($cellStyle['xfId'])])) {
242
                            // Set default style
243 48
                            $style = new Style();
244 48
                            $this->readStyle($style, $this->cellStyles[(int) ($cellStyle['xfId'])]);
245
246
                            // normal style, currently not using it for anything
247
                        }
248
                    }
249
                }
250
            }
251
        }
252
253 48
        return $dxfs;
254
    }
255
256 48
    public function styles()
257
    {
258 48
        return $this->styles;
259
    }
260
261
    private static function getArrayItem($array, $key = 0)
262
    {
263
        return $array[$key] ?? null;
264
    }
265
}
266