Failed Conditions
Push — master ( 11f758...e55052 )
by Adrien
18:18 queued 05:56
created

Style::parseStyles()   C

Complexity

Conditions 13
Paths 13

Size

Total Lines 59
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 37
CRAP Score 13

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 13
eloc 37
c 1
b 0
f 0
nc 13
nop 2
dl 0
loc 59
ccs 37
cts 37
cp 1
crap 13
rs 6.6166

How to fix   Long Method    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\Xml;
4
5
use SimpleXMLElement;
6
7
class Style
8
{
9
    /**
10
     * Formats.
11
     *
12
     * @var array
13
     */
14
    protected $styles = [];
15
16 14
    public function parseStyles(SimpleXMLElement $xml, array $namespaces): array
17
    {
18 14
        if (!isset($xml->Styles)) {
19 1
            return [];
20
        }
21
22 13
        $alignmentStyleParser = new Style\Alignment();
23 13
        $borderStyleParser = new Style\Border();
24 13
        $fontStyleParser = new Style\Font();
25 13
        $fillStyleParser = new Style\Fill();
26 13
        $numberFormatStyleParser = new Style\NumberFormat();
27
28 13
        foreach ($xml->Styles[0] as $style) {
29 13
            $style_ss = self::getAttributes($style, $namespaces['ss']);
30 13
            $styleID = (string) $style_ss['ID'];
31 13
            $this->styles[$styleID] = $this->styles['Default'] ?? [];
32
33 13
            $alignment = $border = $font = $fill = $numberFormat = [];
34
35 13
            foreach ($style as $styleType => $styleDatax) {
36 13
                $styleData = $styleDatax ?? new SimpleXMLElement('<xml></xml>');
37 13
                $styleAttributes = $styleData->attributes($namespaces['ss']);
38
39
                switch ($styleType) {
40 13
                    case 'Alignment':
41 13
                        if ($styleAttributes) {
42 13
                            $alignment = $alignmentStyleParser->parseStyle($styleAttributes);
43
                        }
44
45 13
                        break;
46 13
                    case 'Borders':
47 13
                        $border = $borderStyleParser->parseStyle($styleData, $namespaces);
48
49 13
                        break;
50 13
                    case 'Font':
51 13
                        if ($styleAttributes) {
52 13
                            $font = $fontStyleParser->parseStyle($styleAttributes);
53
                        }
54
55 13
                        break;
56 13
                    case 'Interior':
57 12
                        if ($styleAttributes) {
58 9
                            $fill = $fillStyleParser->parseStyle($styleAttributes);
59
                        }
60
61 12
                        break;
62 13
                    case 'NumberFormat':
63 13
                        if ($styleAttributes) {
64 10
                            $numberFormat = $numberFormatStyleParser->parseStyle($styleAttributes);
65
                        }
66
67 13
                        break;
68
                }
69
            }
70
71 13
            $this->styles[$styleID] = array_merge($alignment, $border, $font, $fill, $numberFormat);
72
        }
73
74 13
        return $this->styles;
75
    }
76
77 13
    protected static function getAttributes(?SimpleXMLElement $simple, string $node): SimpleXMLElement
78
    {
79 13
        return ($simple === null)
80
            ? new SimpleXMLElement('<xml></xml>')
81 13
            : ($simple->attributes($node) ?? new SimpleXMLElement('<xml></xml>'));
82
    }
83
}
84