Passed
Push — master ( 46fdc8...4b16f3 )
by Mark
15:13 queued 06:31
created

PageSetup   A

Complexity

Total Complexity 40

Size/Duplication

Total Lines 158
Duplicated Lines 0 %

Test Coverage

Coverage 98.84%

Importance

Changes 0
Metric Value
wmc 40
eloc 90
dl 0
loc 158
ccs 85
cts 86
cp 0.9884
rs 9.2
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A margins() 0 10 2
A load() 0 13 2
F pageSetup() 0 41 14
A rowBreaks() 0 5 3
A pageBreaks() 0 7 5
A columnBreaks() 0 7 3
B headerFooter() 0 44 10

How to fix   Complexity   

Complex Class

Complex classes like PageSetup often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use PageSetup, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Reader\Xlsx;
4
5
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
6
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
7
use SimpleXMLElement;
8
9
class PageSetup extends BaseParserClass
10
{
11
    /** @var Worksheet */
12
    private $worksheet;
13
14
    /** @var ?SimpleXMLElement */
15
    private $worksheetXml;
16
17 475
    public function __construct(Worksheet $workSheet, ?SimpleXMLElement $worksheetXml = null)
18
    {
19 475
        $this->worksheet = $workSheet;
20 475
        $this->worksheetXml = $worksheetXml;
21
    }
22
23 475
    public function load(array $unparsedLoadedData): array
24
    {
25 475
        $worksheetXml = $this->worksheetXml;
26 475
        if ($worksheetXml === null) {
27
            return $unparsedLoadedData;
28
        }
29
30 475
        $this->margins($worksheetXml, $this->worksheet);
31 475
        $unparsedLoadedData = $this->pageSetup($worksheetXml, $this->worksheet, $unparsedLoadedData);
32 475
        $this->headerFooter($worksheetXml, $this->worksheet);
33 475
        $this->pageBreaks($worksheetXml, $this->worksheet);
34
35 475
        return $unparsedLoadedData;
36
    }
37
38 475
    private function margins(SimpleXMLElement $xmlSheet, Worksheet $worksheet): void
39
    {
40 475
        if ($xmlSheet->pageMargins) {
41 469
            $docPageMargins = $worksheet->getPageMargins();
42 469
            $docPageMargins->setLeft((float) ($xmlSheet->pageMargins['left']));
43 469
            $docPageMargins->setRight((float) ($xmlSheet->pageMargins['right']));
44 469
            $docPageMargins->setTop((float) ($xmlSheet->pageMargins['top']));
45 469
            $docPageMargins->setBottom((float) ($xmlSheet->pageMargins['bottom']));
46 469
            $docPageMargins->setHeader((float) ($xmlSheet->pageMargins['header']));
47 469
            $docPageMargins->setFooter((float) ($xmlSheet->pageMargins['footer']));
48
        }
49
    }
50
51 475
    private function pageSetup(SimpleXMLElement $xmlSheet, Worksheet $worksheet, array $unparsedLoadedData): array
52
    {
53 475
        if ($xmlSheet->pageSetup) {
54 399
            $docPageSetup = $worksheet->getPageSetup();
55
56 399
            if (isset($xmlSheet->pageSetup['orientation'])) {
57 399
                $docPageSetup->setOrientation((string) $xmlSheet->pageSetup['orientation']);
58
            }
59 399
            if (isset($xmlSheet->pageSetup['paperSize'])) {
60 364
                $docPageSetup->setPaperSize((int) ($xmlSheet->pageSetup['paperSize']));
61
            }
62 399
            if (isset($xmlSheet->pageSetup['scale'])) {
63 160
                $docPageSetup->setScale((int) ($xmlSheet->pageSetup['scale']), false);
64
            }
65 399
            if (isset($xmlSheet->pageSetup['fitToHeight']) && (int) ($xmlSheet->pageSetup['fitToHeight']) >= 0) {
66 158
                $docPageSetup->setFitToHeight((int) ($xmlSheet->pageSetup['fitToHeight']), false);
67
            }
68 399
            if (isset($xmlSheet->pageSetup['fitToWidth']) && (int) ($xmlSheet->pageSetup['fitToWidth']) >= 0) {
69 157
                $docPageSetup->setFitToWidth((int) ($xmlSheet->pageSetup['fitToWidth']), false);
70
            }
71
            if (
72 399
                isset($xmlSheet->pageSetup['firstPageNumber'], $xmlSheet->pageSetup['useFirstPageNumber']) &&
73 399
                self::boolean((string) $xmlSheet->pageSetup['useFirstPageNumber'])
74
            ) {
75 4
                $docPageSetup->setFirstPageNumber((int) ($xmlSheet->pageSetup['firstPageNumber']));
76
            }
77 399
            if (isset($xmlSheet->pageSetup['pageOrder'])) {
78 156
                $docPageSetup->setPageOrder((string) $xmlSheet->pageSetup['pageOrder']);
79
            }
80
81 399
            $relAttributes = $xmlSheet->pageSetup->attributes(Namespaces::SCHEMA_OFFICE_DOCUMENT);
82 399
            if (isset($relAttributes['id'])) {
83 255
                $relid = (string) $relAttributes['id'];
84 255
                if (substr($relid, -2) !== 'ps') {
85 255
                    $relid .= 'ps';
86
                }
87 255
                $unparsedLoadedData['sheets'][$worksheet->getCodeName()]['pageSetupRelId'] = $relid;
88
            }
89
        }
90
91 475
        return $unparsedLoadedData;
92
    }
93
94 475
    private function headerFooter(SimpleXMLElement $xmlSheet, Worksheet $worksheet): void
95
    {
96 475
        if ($xmlSheet->headerFooter) {
97 170
            $docHeaderFooter = $worksheet->getHeaderFooter();
98
99
            if (
100 170
                isset($xmlSheet->headerFooter['differentOddEven']) &&
101 170
                self::boolean((string) $xmlSheet->headerFooter['differentOddEven'])
102
            ) {
103 5
                $docHeaderFooter->setDifferentOddEven(true);
104
            } else {
105 165
                $docHeaderFooter->setDifferentOddEven(false);
106
            }
107
            if (
108 170
                isset($xmlSheet->headerFooter['differentFirst']) &&
109 170
                self::boolean((string) $xmlSheet->headerFooter['differentFirst'])
110
            ) {
111 5
                $docHeaderFooter->setDifferentFirst(true);
112
            } else {
113 165
                $docHeaderFooter->setDifferentFirst(false);
114
            }
115
            if (
116 170
                isset($xmlSheet->headerFooter['scaleWithDoc']) &&
117 170
                !self::boolean((string) $xmlSheet->headerFooter['scaleWithDoc'])
118
            ) {
119 2
                $docHeaderFooter->setScaleWithDocument(false);
120
            } else {
121 170
                $docHeaderFooter->setScaleWithDocument(true);
122
            }
123
            if (
124 170
                isset($xmlSheet->headerFooter['alignWithMargins']) &&
125 170
                !self::boolean((string) $xmlSheet->headerFooter['alignWithMargins'])
126
            ) {
127 6
                $docHeaderFooter->setAlignWithMargins(false);
128
            } else {
129 164
                $docHeaderFooter->setAlignWithMargins(true);
130
            }
131
132 170
            $docHeaderFooter->setOddHeader((string) $xmlSheet->headerFooter->oddHeader);
133 170
            $docHeaderFooter->setOddFooter((string) $xmlSheet->headerFooter->oddFooter);
134 170
            $docHeaderFooter->setEvenHeader((string) $xmlSheet->headerFooter->evenHeader);
135 170
            $docHeaderFooter->setEvenFooter((string) $xmlSheet->headerFooter->evenFooter);
136 170
            $docHeaderFooter->setFirstHeader((string) $xmlSheet->headerFooter->firstHeader);
137 170
            $docHeaderFooter->setFirstFooter((string) $xmlSheet->headerFooter->firstFooter);
138
        }
139
    }
140
141 475
    private function pageBreaks(SimpleXMLElement $xmlSheet, Worksheet $worksheet): void
142
    {
143 475
        if ($xmlSheet->rowBreaks && $xmlSheet->rowBreaks->brk) {
144 2
            $this->rowBreaks($xmlSheet, $worksheet);
145
        }
146 475
        if ($xmlSheet->colBreaks && $xmlSheet->colBreaks->brk) {
147 5
            $this->columnBreaks($xmlSheet, $worksheet);
148
        }
149
    }
150
151 2
    private function rowBreaks(SimpleXMLElement $xmlSheet, Worksheet $worksheet): void
152
    {
153 2
        foreach ($xmlSheet->rowBreaks->brk as $brk) {
154 2
            if ($brk['man']) {
155 2
                $worksheet->setBreak("A{$brk['id']}", Worksheet::BREAK_ROW);
156
            }
157
        }
158
    }
159
160 5
    private function columnBreaks(SimpleXMLElement $xmlSheet, Worksheet $worksheet): void
161
    {
162 5
        foreach ($xmlSheet->colBreaks->brk as $brk) {
163 5
            if ($brk['man']) {
164 5
                $worksheet->setBreak(
165 5
                    Coordinate::stringFromColumnIndex(((int) $brk['id']) + 1) . '1',
166 5
                    Worksheet::BREAK_COLUMN
167 5
                );
168
            }
169
        }
170
    }
171
}
172