Completed
Push — develop ( 3ee9cc...870d86 )
by Adrien
29:45
created

DocProps::writeDocPropsApp()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 89
Code Lines 42

Duplication

Lines 5
Ratio 5.62 %

Code Coverage

Tests 40
CRAP Score 3.0001

Importance

Changes 0
Metric Value
cc 3
eloc 42
nc 4
nop 1
dl 5
loc 89
ccs 40
cts 41
cp 0.9756
crap 3.0001
rs 8.5731
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 52
    public function writeDocPropsApp(\PhpOffice\PhpSpreadsheet\SpreadSheet $spreadsheet)
39
    {
40
        // Create XML writer
41 52
        $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 52 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 52
            $objWriter = new \PhpOffice\PhpSpreadsheet\Shared\XMLWriter(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter::STORAGE_MEMORY);
46
        }
47
48
        // XML header
49 52
        $objWriter->startDocument('1.0', 'UTF-8', 'yes');
50
51
        // Properties
52 52
        $objWriter->startElement('Properties');
53 52
        $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/officeDocument/2006/extended-properties');
54 52
        $objWriter->writeAttribute('xmlns:vt', 'http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes');
55
56
        // Application
57 52
        $objWriter->writeElement('Application', 'Microsoft Excel');
58
59
        // DocSecurity
60 52
        $objWriter->writeElement('DocSecurity', '0');
61
62
        // ScaleCrop
63 52
        $objWriter->writeElement('ScaleCrop', 'false');
64
65
        // HeadingPairs
66 52
        $objWriter->startElement('HeadingPairs');
67
68
        // Vector
69 52
        $objWriter->startElement('vt:vector');
70 52
        $objWriter->writeAttribute('size', '2');
71 52
        $objWriter->writeAttribute('baseType', 'variant');
72
73
        // Variant
74 52
        $objWriter->startElement('vt:variant');
75 52
        $objWriter->writeElement('vt:lpstr', 'Worksheets');
76 52
        $objWriter->endElement();
77
78
        // Variant
79 52
        $objWriter->startElement('vt:variant');
80 52
        $objWriter->writeElement('vt:i4', $spreadsheet->getSheetCount());
81 52
        $objWriter->endElement();
82
83 52
        $objWriter->endElement();
84
85 52
        $objWriter->endElement();
86
87
        // TitlesOfParts
88 52
        $objWriter->startElement('TitlesOfParts');
89
90
        // Vector
91 52
        $objWriter->startElement('vt:vector');
92 52
        $objWriter->writeAttribute('size', $spreadsheet->getSheetCount());
93 52
        $objWriter->writeAttribute('baseType', 'lpstr');
94
95 52
        $sheetCount = $spreadsheet->getSheetCount();
96 52
        for ($i = 0; $i < $sheetCount; ++$i) {
97 52
            $objWriter->writeElement('vt:lpstr', $spreadsheet->getSheet($i)->getTitle());
98
        }
99
100 52
        $objWriter->endElement();
101
102 52
        $objWriter->endElement();
103
104
        // Company
105 52
        $objWriter->writeElement('Company', $spreadsheet->getProperties()->getCompany());
106
107
        // Company
108 52
        $objWriter->writeElement('Manager', $spreadsheet->getProperties()->getManager());
109
110
        // LinksUpToDate
111 52
        $objWriter->writeElement('LinksUpToDate', 'false');
112
113
        // SharedDoc
114 52
        $objWriter->writeElement('SharedDoc', 'false');
115
116
        // HyperlinksChanged
117 52
        $objWriter->writeElement('HyperlinksChanged', 'false');
118
119
        // AppVersion
120 52
        $objWriter->writeElement('AppVersion', '12.0000');
121
122 52
        $objWriter->endElement();
123
124
        // Return
125 52
        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 52
    public function writeDocPropsCore(\PhpOffice\PhpSpreadsheet\SpreadSheet $spreadsheet)
138
    {
139
        // Create XML writer
140 52
        $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 52 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 52
            $objWriter = new \PhpOffice\PhpSpreadsheet\Shared\XMLWriter(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter::STORAGE_MEMORY);
145
        }
146
147
        // XML header
148 52
        $objWriter->startDocument('1.0', 'UTF-8', 'yes');
149
150
        // cp:coreProperties
151 52
        $objWriter->startElement('cp:coreProperties');
152 52
        $objWriter->writeAttribute('xmlns:cp', 'http://schemas.openxmlformats.org/package/2006/metadata/core-properties');
153 52
        $objWriter->writeAttribute('xmlns:dc', 'http://purl.org/dc/elements/1.1/');
154 52
        $objWriter->writeAttribute('xmlns:dcterms', 'http://purl.org/dc/terms/');
155 52
        $objWriter->writeAttribute('xmlns:dcmitype', 'http://purl.org/dc/dcmitype/');
156 52
        $objWriter->writeAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
157
158
        // dc:creator
159 52
        $objWriter->writeElement('dc:creator', $spreadsheet->getProperties()->getCreator());
160
161
        // cp:lastModifiedBy
162 52
        $objWriter->writeElement('cp:lastModifiedBy', $spreadsheet->getProperties()->getLastModifiedBy());
163
164
        // dcterms:created
165 52
        $objWriter->startElement('dcterms:created');
166 52
        $objWriter->writeAttribute('xsi:type', 'dcterms:W3CDTF');
167 52
        $objWriter->writeRawData(date(DATE_W3C, $spreadsheet->getProperties()->getCreated()));
168 52
        $objWriter->endElement();
169
170
        // dcterms:modified
171 52
        $objWriter->startElement('dcterms:modified');
172 52
        $objWriter->writeAttribute('xsi:type', 'dcterms:W3CDTF');
173 52
        $objWriter->writeRawData(date(DATE_W3C, $spreadsheet->getProperties()->getModified()));
174 52
        $objWriter->endElement();
175
176
        // dc:title
177 52
        $objWriter->writeElement('dc:title', $spreadsheet->getProperties()->getTitle());
178
179
        // dc:description
180 52
        $objWriter->writeElement('dc:description', $spreadsheet->getProperties()->getDescription());
181
182
        // dc:subject
183 52
        $objWriter->writeElement('dc:subject', $spreadsheet->getProperties()->getSubject());
184
185
        // cp:keywords
186 52
        $objWriter->writeElement('cp:keywords', $spreadsheet->getProperties()->getKeywords());
187
188
        // cp:category
189 52
        $objWriter->writeElement('cp:category', $spreadsheet->getProperties()->getCategory());
190
191 52
        $objWriter->endElement();
192
193
        // Return
194 52
        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 52
    public function writeDocPropsCustom(\PhpOffice\PhpSpreadsheet\SpreadSheet $spreadsheet)
207
    {
208 52
        $customPropertyList = $spreadsheet->getProperties()->getCustomProperties();
209 52
        if (empty($customPropertyList)) {
210 51
            return;
211
        }
212
213
        // Create XML writer
214 1
        $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 1 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 1
            $objWriter = new \PhpOffice\PhpSpreadsheet\Shared\XMLWriter(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter::STORAGE_MEMORY);
219
        }
220
221
        // XML header
222 1
        $objWriter->startDocument('1.0', 'UTF-8', 'yes');
223
224
        // cp:coreProperties
225 1
        $objWriter->startElement('Properties');
226 1
        $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/officeDocument/2006/custom-properties');
227 1
        $objWriter->writeAttribute('xmlns:vt', 'http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes');
228
229 1
        foreach ($customPropertyList as $key => $customProperty) {
230 1
            $propertyValue = $spreadsheet->getProperties()->getCustomPropertyValue($customProperty);
231 1
            $propertyType = $spreadsheet->getProperties()->getCustomPropertyType($customProperty);
232
233 1
            $objWriter->startElement('property');
234 1
            $objWriter->writeAttribute('fmtid', '{D5CDD505-2E9C-101B-9397-08002B2CF9AE}');
235 1
            $objWriter->writeAttribute('pid', $key + 2);
236 1
            $objWriter->writeAttribute('name', $customProperty);
237
238
            switch ($propertyType) {
239 1
                case 'i':
240
                    $objWriter->writeElement('vt:i4', $propertyValue);
241
                    break;
242 1
                case 'f':
243
                    $objWriter->writeElement('vt:r8', $propertyValue);
244
                    break;
245 1
                case 'b':
246
                    $objWriter->writeElement('vt:bool', ($propertyValue) ? 'true' : 'false');
247
                    break;
248 1
                case 'd':
249
                    $objWriter->startElement('vt:filetime');
250
                    $objWriter->writeRawData(date(DATE_W3C, $propertyValue));
251
                    $objWriter->endElement();
252
                    break;
253
                default:
254 1
                    $objWriter->writeElement('vt:lpwstr', $propertyValue);
255 1
                    break;
256
            }
257
258 1
            $objWriter->endElement();
259
        }
260
261 1
        $objWriter->endElement();
262
263 1
        return $objWriter->getData();
264
    }
265
}
266