Completed
Push — develop ( 870d86...2ad559 )
by Adrien
42:24 queued 15:50
created

DocProps::writeDocPropsCustom()   C

Complexity

Conditions 9
Paths 13

Size

Total Lines 59
Code Lines 41

Duplication

Lines 5
Ratio 8.47 %

Code Coverage

Tests 35
CRAP Score 9.0397

Importance

Changes 0
Metric Value
cc 9
eloc 41
nc 13
nop 1
dl 5
loc 59
ccs 35
cts 38
cp 0.9211
crap 9.0397
rs 6.9133
c 0
b 0
f 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
/**
6
 * Copyright (c) 2006 - 2016 PhpSpreadsheet.
7
 *
8
 * This library is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU Lesser General Public
10
 * License as published by the Free Software Foundation; either
11
 * version 2.1 of the License, or (at your option) any later version.
12
 *
13
 * This library is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
 * Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with this library; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
 *
22
 * @category   PhpSpreadsheet
23
 *
24
 * @copyright  Copyright (c) 2006 - 2016 PhpSpreadsheet (https://github.com/PHPOffice/PhpSpreadsheet)
25
 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
26
 */
27
class DocProps extends WriterPart
28
{
29
    /**
30
     * Write docProps/app.xml to XML format.
31
     *
32
     * @param \PhpOffice\PhpSpreadsheet\SpreadSheet $spreadsheet
33
     *
34
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
35
     *
36
     * @return string XML Output
37
     */
38 55
    public function writeDocPropsApp(\PhpOffice\PhpSpreadsheet\SpreadSheet $spreadsheet)
39
    {
40
        // Create XML writer
41 55
        $objWriter = null;
0 ignored issues
show
Unused Code introduced by
$objWriter is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
42 55 View Code Duplication
        if ($this->getParentWriter()->getUseDiskCaching()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
43
            $objWriter = new \PhpOffice\PhpSpreadsheet\Shared\XMLWriter(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
44
        } else {
45 55
            $objWriter = new \PhpOffice\PhpSpreadsheet\Shared\XMLWriter(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter::STORAGE_MEMORY);
46
        }
47
48
        // XML header
49 55
        $objWriter->startDocument('1.0', 'UTF-8', 'yes');
50
51
        // Properties
52 55
        $objWriter->startElement('Properties');
53 55
        $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/officeDocument/2006/extended-properties');
54 55
        $objWriter->writeAttribute('xmlns:vt', 'http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes');
55
56
        // Application
57 55
        $objWriter->writeElement('Application', 'Microsoft Excel');
58
59
        // DocSecurity
60 55
        $objWriter->writeElement('DocSecurity', '0');
61
62
        // ScaleCrop
63 55
        $objWriter->writeElement('ScaleCrop', 'false');
64
65
        // HeadingPairs
66 55
        $objWriter->startElement('HeadingPairs');
67
68
        // Vector
69 55
        $objWriter->startElement('vt:vector');
70 55
        $objWriter->writeAttribute('size', '2');
71 55
        $objWriter->writeAttribute('baseType', 'variant');
72
73
        // Variant
74 55
        $objWriter->startElement('vt:variant');
75 55
        $objWriter->writeElement('vt:lpstr', 'Worksheets');
76 55
        $objWriter->endElement();
77
78
        // Variant
79 55
        $objWriter->startElement('vt:variant');
80 55
        $objWriter->writeElement('vt:i4', $spreadsheet->getSheetCount());
81 55
        $objWriter->endElement();
82
83 55
        $objWriter->endElement();
84
85 55
        $objWriter->endElement();
86
87
        // TitlesOfParts
88 55
        $objWriter->startElement('TitlesOfParts');
89
90
        // Vector
91 55
        $objWriter->startElement('vt:vector');
92 55
        $objWriter->writeAttribute('size', $spreadsheet->getSheetCount());
93 55
        $objWriter->writeAttribute('baseType', 'lpstr');
94
95 55
        $sheetCount = $spreadsheet->getSheetCount();
96 55
        for ($i = 0; $i < $sheetCount; ++$i) {
97 55
            $objWriter->writeElement('vt:lpstr', $spreadsheet->getSheet($i)->getTitle());
98
        }
99
100 55
        $objWriter->endElement();
101
102 55
        $objWriter->endElement();
103
104
        // Company
105 55
        $objWriter->writeElement('Company', $spreadsheet->getProperties()->getCompany());
106
107
        // Company
108 55
        $objWriter->writeElement('Manager', $spreadsheet->getProperties()->getManager());
109
110
        // LinksUpToDate
111 55
        $objWriter->writeElement('LinksUpToDate', 'false');
112
113
        // SharedDoc
114 55
        $objWriter->writeElement('SharedDoc', 'false');
115
116
        // HyperlinksChanged
117 55
        $objWriter->writeElement('HyperlinksChanged', 'false');
118
119
        // AppVersion
120 55
        $objWriter->writeElement('AppVersion', '12.0000');
121
122 55
        $objWriter->endElement();
123
124
        // Return
125 55
        return $objWriter->getData();
126
    }
127
128
    /**
129
     * Write docProps/core.xml to XML format.
130
     *
131
     * @param \PhpOffice\PhpSpreadsheet\SpreadSheet $spreadsheet
132
     *
133
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
134
     *
135
     * @return string XML Output
136
     */
137 55
    public function writeDocPropsCore(\PhpOffice\PhpSpreadsheet\SpreadSheet $spreadsheet)
138
    {
139
        // Create XML writer
140 55
        $objWriter = null;
0 ignored issues
show
Unused Code introduced by
$objWriter is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
141 55 View Code Duplication
        if ($this->getParentWriter()->getUseDiskCaching()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
142
            $objWriter = new \PhpOffice\PhpSpreadsheet\Shared\XMLWriter(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
143
        } else {
144 55
            $objWriter = new \PhpOffice\PhpSpreadsheet\Shared\XMLWriter(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter::STORAGE_MEMORY);
145
        }
146
147
        // XML header
148 55
        $objWriter->startDocument('1.0', 'UTF-8', 'yes');
149
150
        // cp:coreProperties
151 55
        $objWriter->startElement('cp:coreProperties');
152 55
        $objWriter->writeAttribute('xmlns:cp', 'http://schemas.openxmlformats.org/package/2006/metadata/core-properties');
153 55
        $objWriter->writeAttribute('xmlns:dc', 'http://purl.org/dc/elements/1.1/');
154 55
        $objWriter->writeAttribute('xmlns:dcterms', 'http://purl.org/dc/terms/');
155 55
        $objWriter->writeAttribute('xmlns:dcmitype', 'http://purl.org/dc/dcmitype/');
156 55
        $objWriter->writeAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
157
158
        // dc:creator
159 55
        $objWriter->writeElement('dc:creator', $spreadsheet->getProperties()->getCreator());
160
161
        // cp:lastModifiedBy
162 55
        $objWriter->writeElement('cp:lastModifiedBy', $spreadsheet->getProperties()->getLastModifiedBy());
163
164
        // dcterms:created
165 55
        $objWriter->startElement('dcterms:created');
166 55
        $objWriter->writeAttribute('xsi:type', 'dcterms:W3CDTF');
167 55
        $objWriter->writeRawData(date(DATE_W3C, $spreadsheet->getProperties()->getCreated()));
168 55
        $objWriter->endElement();
169
170
        // dcterms:modified
171 55
        $objWriter->startElement('dcterms:modified');
172 55
        $objWriter->writeAttribute('xsi:type', 'dcterms:W3CDTF');
173 55
        $objWriter->writeRawData(date(DATE_W3C, $spreadsheet->getProperties()->getModified()));
174 55
        $objWriter->endElement();
175
176
        // dc:title
177 55
        $objWriter->writeElement('dc:title', $spreadsheet->getProperties()->getTitle());
178
179
        // dc:description
180 55
        $objWriter->writeElement('dc:description', $spreadsheet->getProperties()->getDescription());
181
182
        // dc:subject
183 55
        $objWriter->writeElement('dc:subject', $spreadsheet->getProperties()->getSubject());
184
185
        // cp:keywords
186 55
        $objWriter->writeElement('cp:keywords', $spreadsheet->getProperties()->getKeywords());
187
188
        // cp:category
189 55
        $objWriter->writeElement('cp:category', $spreadsheet->getProperties()->getCategory());
190
191 55
        $objWriter->endElement();
192
193
        // Return
194 55
        return $objWriter->getData();
195
    }
196
197
    /**
198
     * Write docProps/custom.xml to XML format.
199
     *
200
     * @param \PhpOffice\PhpSpreadsheet\SpreadSheet $spreadsheet
201
     *
202
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
203
     *
204
     * @return string XML Output
205
     */
206 55
    public function writeDocPropsCustom(\PhpOffice\PhpSpreadsheet\SpreadSheet $spreadsheet)
207
    {
208 55
        $customPropertyList = $spreadsheet->getProperties()->getCustomProperties();
209 55
        if (empty($customPropertyList)) {
210 53
            return;
211
        }
212
213
        // Create XML writer
214 2
        $objWriter = null;
0 ignored issues
show
Unused Code introduced by
$objWriter is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
215 2 View Code Duplication
        if ($this->getParentWriter()->getUseDiskCaching()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
216
            $objWriter = new \PhpOffice\PhpSpreadsheet\Shared\XMLWriter(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
217
        } else {
218 2
            $objWriter = new \PhpOffice\PhpSpreadsheet\Shared\XMLWriter(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter::STORAGE_MEMORY);
219
        }
220
221
        // XML header
222 2
        $objWriter->startDocument('1.0', 'UTF-8', 'yes');
223
224
        // cp:coreProperties
225 2
        $objWriter->startElement('Properties');
226 2
        $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/officeDocument/2006/custom-properties');
227 2
        $objWriter->writeAttribute('xmlns:vt', 'http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes');
228
229 2
        foreach ($customPropertyList as $key => $customProperty) {
230 2
            $propertyValue = $spreadsheet->getProperties()->getCustomPropertyValue($customProperty);
231 2
            $propertyType = $spreadsheet->getProperties()->getCustomPropertyType($customProperty);
232
233 2
            $objWriter->startElement('property');
234 2
            $objWriter->writeAttribute('fmtid', '{D5CDD505-2E9C-101B-9397-08002B2CF9AE}');
235 2
            $objWriter->writeAttribute('pid', $key + 2);
236 2
            $objWriter->writeAttribute('name', $customProperty);
237
238
            switch ($propertyType) {
239 2
                case 'i':
240
                    $objWriter->writeElement('vt:i4', $propertyValue);
241
                    break;
242 2
                case 'f':
243 1
                    $objWriter->writeElement('vt:r8', $propertyValue);
244 1
                    break;
245 2
                case 'b':
246 1
                    $objWriter->writeElement('vt:bool', ($propertyValue) ? 'true' : 'false');
247 1
                    break;
248 2
                case 'd':
249 1
                    $objWriter->startElement('vt:filetime');
250 1
                    $objWriter->writeRawData(date(DATE_W3C, $propertyValue));
251 1
                    $objWriter->endElement();
252 1
                    break;
253
                default:
254 2
                    $objWriter->writeElement('vt:lpwstr', $propertyValue);
255 2
                    break;
256
            }
257
258 2
            $objWriter->endElement();
259
        }
260
261 2
        $objWriter->endElement();
262
263 2
        return $objWriter->getData();
264
    }
265
}
266