Failed Conditions
Pull Request — master (#3962)
by Owen
11:35
created

Metadata::writeMetadata()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 113
Code Lines 93

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 93
dl 0
loc 113
rs 8.1527
c 1
b 0
f 0
cc 3
nc 3
nop 0

How to fix   Long Method   

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\Writer\Xlsx;
4
5
use PhpOffice\PhpSpreadsheet\Reader\Xlsx\Namespaces;
6
use PhpOffice\PhpSpreadsheet\Shared\XMLWriter;
7
8
class Metadata extends WriterPart
9
{
10
    /**
11
     * Write content types to XML format.
12
     *
13
     * @return string XML Output
14
     */
15
    public function writeMetadata(): string
16
    {
17
        if (!$this->getParentWriter()->useDynamicArrays()) {
18
            return '';
19
        }
20
        // Create XML writer
21
        $objWriter = null;
22
        if ($this->getParentWriter()->getUseDiskCaching()) {
23
            $objWriter = new XMLWriter(XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
24
        } else {
25
            $objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY);
26
        }
27
28
        // XML header
29
        $objWriter->startDocument('1.0', 'UTF-8', 'yes');
30
31
        // Types
32
        $objWriter->startElement('metadata');
33
        $objWriter->writeAttribute('xmlns', Namespaces::MAIN);
34
        $objWriter->writeAttribute('xmlns:xlrd', Namespaces::DYNAMIC_ARRAY_RICHDATA);
35
        $objWriter->writeAttribute('xmlns:xda', Namespaces::DYNAMIC_ARRAY);
36
37
        $objWriter->startElement('metadataTypes');
38
        $objWriter->writeAttribute('count', '2');
39
40
        $objWriter->startElement('metadataType');
41
        $objWriter->writeAttribute('name', 'XLDAPR');
42
        $objWriter->writeAttribute('minSupportedVersion', '120000');
43
        $objWriter->writeAttribute('copy', '1');
44
        $objWriter->writeAttribute('pasteAll', '1');
45
        $objWriter->writeAttribute('pasteValues', '1');
46
        $objWriter->writeAttribute('merge', '1');
47
        $objWriter->writeAttribute('splitFirst', '1');
48
        $objWriter->writeAttribute('rowColShift', '1');
49
        $objWriter->writeAttribute('clearFormats', '1');
50
        $objWriter->writeAttribute('clearComments', '1');
51
        $objWriter->writeAttribute('assign', '1');
52
        $objWriter->writeAttribute('coerce', '1');
53
        $objWriter->writeAttribute('cellMeta', '1');
54
        $objWriter->endElement(); // metadataType XLDAPR
55
56
        $objWriter->startElement('metadataType');
57
        $objWriter->writeAttribute('name', 'XLRICHVALUE');
58
        $objWriter->writeAttribute('minSupportedVersion', '120000');
59
        $objWriter->writeAttribute('copy', '1');
60
        $objWriter->writeAttribute('pasteAll', '1');
61
        $objWriter->writeAttribute('pasteValues', '1');
62
        $objWriter->writeAttribute('merge', '1');
63
        $objWriter->writeAttribute('splitFirst', '1');
64
        $objWriter->writeAttribute('rowColShift', '1');
65
        $objWriter->writeAttribute('clearFormats', '1');
66
        $objWriter->writeAttribute('clearComments', '1');
67
        $objWriter->writeAttribute('assign', '1');
68
        $objWriter->writeAttribute('coerce', '1');
69
        $objWriter->endElement(); // metadataType XLRICHVALUE
70
71
        $objWriter->endElement(); // metadataTypes
72
73
        $objWriter->startElement('futureMetadata');
74
        $objWriter->writeAttribute('name', 'XLDAPR');
75
        $objWriter->writeAttribute('count', '1');
76
        $objWriter->startElement('bk');
77
        $objWriter->startElement('extLst');
78
        $objWriter->startElement('ext');
79
        $objWriter->writeAttribute('uri', '{bdbb8cdc-fa1e-496e-a857-3c3f30c029c3}');
80
        $objWriter->startElement('xda:dynamicArrayProperties');
81
        $objWriter->writeAttribute('fDynamic', '1');
82
        $objWriter->writeAttribute('fCollapsed', '0');
83
        $objWriter->endElement(); // xda:dynamicArrayProperties
84
        $objWriter->endElement(); // ext
85
        $objWriter->endElement(); // extLst
86
        $objWriter->endElement(); // bk
87
        $objWriter->endElement(); // futureMetadata XLDAPR
88
89
        $objWriter->startElement('futureMetadata');
90
        $objWriter->writeAttribute('name', 'XLRICHVALUE');
91
        $objWriter->writeAttribute('count', '1');
92
        $objWriter->startElement('bk');
93
        $objWriter->startElement('extLst');
94
        $objWriter->startElement('ext');
95
        $objWriter->writeAttribute('uri', '{3e2802c4-a4d2-4d8b-9148-e3be6c30e623}');
96
        $objWriter->startElement('xlrd:rvb');
97
        $objWriter->writeAttribute('i', '0');
98
        $objWriter->endElement(); // xlrd:rvb
99
        $objWriter->endElement(); // ext
100
        $objWriter->endElement(); // extLst
101
        $objWriter->endElement(); // bk
102
        $objWriter->endElement(); // futureMetadata XLRICHVALUE
103
104
        $objWriter->startElement('cellMetadata');
105
        $objWriter->writeAttribute('count', '1');
106
        $objWriter->startElement('bk');
107
        $objWriter->startElement('rc');
108
        $objWriter->writeAttribute('t', '1');
109
        $objWriter->writeAttribute('v', '0');
110
        $objWriter->endElement(); // rc
111
        $objWriter->endElement(); // bk
112
        $objWriter->endElement(); // cellMetadata
113
114
        $objWriter->startElement('valueMetadata');
115
        $objWriter->writeAttribute('count', '1');
116
        $objWriter->startElement('bk');
117
        $objWriter->startElement('rc');
118
        $objWriter->writeAttribute('t', '2');
119
        $objWriter->writeAttribute('v', '0');
120
        $objWriter->endElement(); // rc
121
        $objWriter->endElement(); // bk
122
        $objWriter->endElement(); // valueMetadata
123
124
        $objWriter->endElement(); // metadata
125
126
        // Return
127
        return $objWriter->getData();
128
    }
129
}
130