Completed
Push — develop ( 2b41bd...39b55d )
by Adrien
22:48
created

Worksheet::writeWorksheet()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 91
Code Lines 37

Duplication

Lines 5
Ratio 5.49 %

Code Coverage

Tests 33
CRAP Score 3.0016

Importance

Changes 0
Metric Value
cc 3
eloc 37
nc 3
nop 3
dl 5
loc 91
ccs 33
cts 35
cp 0.9429
crap 3.0016
rs 8.518
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
 * PhpSpreadsheet
7
 *
8
 * Copyright (c) 2006 - 2015 PhpSpreadsheet
9
 *
10
 * This library is free software; you can redistribute it and/or
11
 * modify it under the terms of the GNU Lesser General Public
12
 * License as published by the Free Software Foundation; either
13
 * version 2.1 of the License, or (at your option) any later version.
14
 *
15
 * This library is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18
 * Lesser General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Lesser General Public
21
 * License along with this library; if not, write to the Free Software
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
23
 *
24
 * @category   PhpSpreadsheet
25
 * @copyright  Copyright (c) 2006 - 2015 PhpSpreadsheet (https://github.com/PHPOffice/PhpSpreadsheet)
26
 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
27
 * @version    ##VERSION##, ##DATE##
28
 */
29
30
/**
31
 * @category   PhpSpreadsheet
32
 * @copyright  Copyright (c) 2006 - 2015 PhpSpreadsheet (https://github.com/PHPOffice/PhpSpreadsheet)
33
 */
34
class Worksheet extends WriterPart
35
{
36
    /**
37
     * Write worksheet to XML format
38
     *
39
     * @param    \PhpOffice\PhpSpreadsheet\Worksheet        $pSheet
40
     * @param    string[]                $pStringTable
41
     * @param    bool                    $includeCharts    Flag indicating if we should write charts
42
     * @throws    \PhpOffice\PhpSpreadsheet\Writer\Exception
43
     * @return    string                    XML Output
44
     */
45 56
    public function writeWorksheet($pSheet = null, $pStringTable = null, $includeCharts = false)
46
    {
47 56
        if (!is_null($pSheet)) {
48
            // Create XML writer
49 56
            $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...
50 56 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...
Bug introduced by
It seems like you code against a concrete implementation and not the interface PhpOffice\PhpSpreadsheet\Writer\IWriter as the method getUseDiskCaching() does only exist in the following implementations of said interface: PhpOffice\PhpSpreadsheet\Writer\BaseWriter, PhpOffice\PhpSpreadsheet\Writer\CSV, PhpOffice\PhpSpreadsheet\Writer\Excel5, PhpOffice\PhpSpreadsheet\Writer\HTML, PhpOffice\PhpSpreadsheet\Writer\OpenDocument, PhpOffice\PhpSpreadsheet\Writer\PDF\Core, PhpOffice\PhpSpreadsheet\Writer\PDF\DomPDF, PhpOffice\PhpSpreadsheet\Writer\PDF\MPDF, PhpOffice\PhpSpreadsheet\Writer\PDF\TcPDF, PhpOffice\PhpSpreadsheet\Writer\Xlsx.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
51
                $objWriter = new \PhpOffice\PhpSpreadsheet\Shared\XMLWriter(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface PhpOffice\PhpSpreadsheet\Writer\IWriter as the method getDiskCachingDirectory() does only exist in the following implementations of said interface: PhpOffice\PhpSpreadsheet\Writer\BaseWriter, PhpOffice\PhpSpreadsheet\Writer\CSV, PhpOffice\PhpSpreadsheet\Writer\Excel5, PhpOffice\PhpSpreadsheet\Writer\HTML, PhpOffice\PhpSpreadsheet\Writer\OpenDocument, PhpOffice\PhpSpreadsheet\Writer\PDF\Core, PhpOffice\PhpSpreadsheet\Writer\PDF\DomPDF, PhpOffice\PhpSpreadsheet\Writer\PDF\MPDF, PhpOffice\PhpSpreadsheet\Writer\PDF\TcPDF, PhpOffice\PhpSpreadsheet\Writer\Xlsx.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
52
            } else {
53 56
                $objWriter = new \PhpOffice\PhpSpreadsheet\Shared\XMLWriter(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter::STORAGE_MEMORY);
54
            }
55
56
            // XML header
57 56
            $objWriter->startDocument('1.0', 'UTF-8', 'yes');
58
59
            // Worksheet
60 56
            $objWriter->startElement('worksheet');
61 56
            $objWriter->writeAttribute('xml:space', 'preserve');
62 56
            $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/spreadsheetml/2006/main');
63 56
            $objWriter->writeAttribute('xmlns:r', 'http://schemas.openxmlformats.org/officeDocument/2006/relationships');
64
65
                // sheetPr
66 56
                $this->writeSheetPr($objWriter, $pSheet);
67
68
                // Dimension
69 56
                $this->writeDimension($objWriter, $pSheet);
70
71
                // sheetViews
72 56
                $this->writeSheetViews($objWriter, $pSheet);
73
74
                // sheetFormatPr
75 56
                $this->writeSheetFormatPr($objWriter, $pSheet);
76
77
                // cols
78 56
                $this->writeCols($objWriter, $pSheet);
79
80
                // sheetData
81 56
                $this->writeSheetData($objWriter, $pSheet, $pStringTable);
82
83
                // sheetProtection
84 56
                $this->writeSheetProtection($objWriter, $pSheet);
85
86
                // protectedRanges
87 56
                $this->writeProtectedRanges($objWriter, $pSheet);
88
89
                // autoFilter
90 56
                $this->writeAutoFilter($objWriter, $pSheet);
91
92
                // mergeCells
93 56
                $this->writeMergeCells($objWriter, $pSheet);
94
95
                // conditionalFormatting
96 56
                $this->writeConditionalFormatting($objWriter, $pSheet);
97
98
                // dataValidations
99 56
                $this->writeDataValidations($objWriter, $pSheet);
100
101
                // hyperlinks
102 56
                $this->writeHyperlinks($objWriter, $pSheet);
103
104
                // Print options
105 56
                $this->writePrintOptions($objWriter, $pSheet);
106
107
                // Page margins
108 56
                $this->writePageMargins($objWriter, $pSheet);
109
110
                // Page setup
111 56
                $this->writePageSetup($objWriter, $pSheet);
112
113
                // Header / footer
114 56
                $this->writeHeaderFooter($objWriter, $pSheet);
115
116
                // Breaks
117 56
                $this->writeBreaks($objWriter, $pSheet);
118
119
                // Drawings and/or Charts
120 56
                $this->writeDrawings($objWriter, $pSheet, $includeCharts);
121
122
                // LegacyDrawing
123 56
                $this->writeLegacyDrawing($objWriter, $pSheet);
124
125
                // LegacyDrawingHF
126 56
                $this->writeLegacyDrawingHF($objWriter, $pSheet);
127
128 56
            $objWriter->endElement();
129
130
            // Return
131 56
            return $objWriter->getData();
132
        } else {
133
            throw new \PhpOffice\PhpSpreadsheet\Writer\Exception('Invalid \\PhpOffice\\PhpSpreadsheet\\Worksheet object passed.');
134
        }
135
    }
136
137
    /**
138
     * Write SheetPr
139
     *
140
     * @param    \PhpOffice\PhpSpreadsheet\Shared\XMLWriter        $objWriter        XML Writer
141
     * @param    \PhpOffice\PhpSpreadsheet\Worksheet                $pSheet            Worksheet
142
     * @throws    \PhpOffice\PhpSpreadsheet\Writer\Exception
143
     */
144 56
    private function writeSheetPr(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter = null, \PhpOffice\PhpSpreadsheet\Worksheet $pSheet = null)
145
    {
146
        // sheetPr
147 56
        $objWriter->startElement('sheetPr');
0 ignored issues
show
Bug introduced by
It seems like $objWriter is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
148 56
        if ($pSheet->getParent()->hasMacros()) {
149
            //if the workbook have macros, we need to have codeName for the sheet
150
            if ($pSheet->hasCodeName() == false) {
151
                $pSheet->setCodeName($pSheet->getTitle());
0 ignored issues
show
Bug introduced by
It seems like $pSheet is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
152
            }
153
            $objWriter->writeAttribute('codeName', $pSheet->getCodeName());
154
        }
155 56
        $autoFilterRange = $pSheet->getAutoFilter()->getRange();
156 56
        if (!empty($autoFilterRange)) {
157 3
            $objWriter->writeAttribute('filterMode', 1);
158 3
            $pSheet->getAutoFilter()->showHideRows();
159
        }
160
161
        // tabColor
162 56
        if ($pSheet->isTabColorSet()) {
163 6
            $objWriter->startElement('tabColor');
164 6
            $objWriter->writeAttribute('rgb', $pSheet->getTabColor()->getARGB());
165 6
            $objWriter->endElement();
166
        }
167
168
        // outlinePr
169 56
        $objWriter->startElement('outlinePr');
170 56
        $objWriter->writeAttribute('summaryBelow', ($pSheet->getShowSummaryBelow() ? '1' : '0'));
171 56
        $objWriter->writeAttribute('summaryRight', ($pSheet->getShowSummaryRight() ? '1' : '0'));
172 56
        $objWriter->endElement();
173
174
        // pageSetUpPr
175 56
        if ($pSheet->getPageSetup()->getFitToPage()) {
176
            $objWriter->startElement('pageSetUpPr');
177
            $objWriter->writeAttribute('fitToPage', '1');
178
            $objWriter->endElement();
179
        }
180
181 56
        $objWriter->endElement();
182 56
    }
183
184
    /**
185
     * Write Dimension
186
     *
187
     * @param    \PhpOffice\PhpSpreadsheet\Shared\XMLWriter    $objWriter        XML Writer
188
     * @param    \PhpOffice\PhpSpreadsheet\Worksheet            $pSheet            Worksheet
189
     * @throws    \PhpOffice\PhpSpreadsheet\Writer\Exception
190
     */
191 56
    private function writeDimension(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter = null, \PhpOffice\PhpSpreadsheet\Worksheet $pSheet = null)
192
    {
193
        // dimension
194 56
        $objWriter->startElement('dimension');
0 ignored issues
show
Bug introduced by
It seems like $objWriter is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
195 56
        $objWriter->writeAttribute('ref', $pSheet->calculateWorksheetDimension());
0 ignored issues
show
Bug introduced by
It seems like $pSheet is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
196 56
        $objWriter->endElement();
197 56
    }
198
199
    /**
200
     * Write SheetViews
201
     *
202
     * @param    \PhpOffice\PhpSpreadsheet\Shared\XMLWriter            $objWriter        XML Writer
203
     * @param    \PhpOffice\PhpSpreadsheet\Worksheet                    $pSheet            Worksheet
204
     * @throws    \PhpOffice\PhpSpreadsheet\Writer\Exception
205
     */
206 56
    private function writeSheetViews(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter = null, \PhpOffice\PhpSpreadsheet\Worksheet $pSheet = null)
207
    {
208
        // sheetViews
209 56
        $objWriter->startElement('sheetViews');
0 ignored issues
show
Bug introduced by
It seems like $objWriter is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
210
211
        // Sheet selected?
212 56
        $sheetSelected = false;
213 56
        if ($this->getParentWriter()->getSpreadsheet()->getIndex($pSheet) == $this->getParentWriter()->getSpreadsheet()->getActiveSheetIndex()) {
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface PhpOffice\PhpSpreadsheet\Writer\IWriter as the method getSpreadsheet() does only exist in the following implementations of said interface: PhpOffice\PhpSpreadsheet\Writer\OpenDocument, PhpOffice\PhpSpreadsheet\Writer\Xlsx.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
214 56
            $sheetSelected = true;
215
        }
216
217
        // sheetView
218 56
        $objWriter->startElement('sheetView');
219 56
        $objWriter->writeAttribute('tabSelected', $sheetSelected ? '1' : '0');
220 56
        $objWriter->writeAttribute('workbookViewId', '0');
221
222
        // Zoom scales
223 56
        if ($pSheet->getSheetView()->getZoomScale() != 100) {
224
            $objWriter->writeAttribute('zoomScale', $pSheet->getSheetView()->getZoomScale());
0 ignored issues
show
Bug introduced by
It seems like $pSheet is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
225
        }
226 56
        if ($pSheet->getSheetView()->getZoomScaleNormal() != 100) {
227
            $objWriter->writeAttribute('zoomScaleNormal', $pSheet->getSheetView()->getZoomScaleNormal());
228
        }
229
230
        // View Layout Type
231 56
        if ($pSheet->getSheetView()->getView() !== \PhpOffice\PhpSpreadsheet\Worksheet\SheetView::SHEETVIEW_NORMAL) {
232 1
            $objWriter->writeAttribute('view', $pSheet->getSheetView()->getView());
233
        }
234
235
        // Gridlines
236 56
        if ($pSheet->getShowGridlines()) {
237 56
            $objWriter->writeAttribute('showGridLines', 'true');
238
        } else {
239
            $objWriter->writeAttribute('showGridLines', 'false');
240
        }
241
242
        // Row and column headers
243 56
        if ($pSheet->getShowRowColHeaders()) {
244 56
            $objWriter->writeAttribute('showRowColHeaders', '1');
245
        } else {
246
            $objWriter->writeAttribute('showRowColHeaders', '0');
247
        }
248
249
        // Right-to-left
250 56
        if ($pSheet->getRightToLeft()) {
251
            $objWriter->writeAttribute('rightToLeft', 'true');
252
        }
253
254 56
        $activeCell = $pSheet->getActiveCell();
255
256
        // Pane
257 56
        $pane = '';
258 56
        $topLeftCell = $pSheet->getFreezePane();
259 56
        if (($topLeftCell != '') && ($topLeftCell != 'A1')) {
260 3
            $activeCell = $topLeftCell;
261
            // Calculate freeze coordinates
262 3
            $xSplit = $ySplit = 0;
263
264 3
            list($xSplit, $ySplit) = \PhpOffice\PhpSpreadsheet\Cell::coordinateFromString($topLeftCell);
265 3
            $xSplit = \PhpOffice\PhpSpreadsheet\Cell::columnIndexFromString($xSplit);
266
267
            // pane
268 3
            $pane = 'topRight';
269 3
            $objWriter->startElement('pane');
270 3
            if ($xSplit > 1) {
271
                $objWriter->writeAttribute('xSplit', $xSplit - 1);
272
            }
273 3
            if ($ySplit > 1) {
274 3
                $objWriter->writeAttribute('ySplit', $ySplit - 1);
275 3
                $pane = ($xSplit > 1) ? 'bottomRight' : 'bottomLeft';
276
            }
277 3
            $objWriter->writeAttribute('topLeftCell', $topLeftCell);
278 3
            $objWriter->writeAttribute('activePane', $pane);
279 3
            $objWriter->writeAttribute('state', 'frozen');
280 3
            $objWriter->endElement();
281
282 3
            if (($xSplit > 1) && ($ySplit > 1)) {
283
                //    Write additional selections if more than two panes (ie both an X and a Y split)
284
                $objWriter->startElement('selection');
285
                $objWriter->writeAttribute('pane', 'topRight');
286
                $objWriter->endElement();
287
                $objWriter->startElement('selection');
288
                $objWriter->writeAttribute('pane', 'bottomLeft');
289
                $objWriter->endElement();
290
            }
291
        }
292
293
        // Selection
0 ignored issues
show
Unused Code Comprehensibility introduced by
44% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
294
//      if ($pane != '') {
295
        // Only need to write selection element if we have a split pane
296
        // We cheat a little by over-riding the active cell selection, setting it to the split cell
297 56
        $objWriter->startElement('selection');
298 56
        if ($pane != '') {
299 3
            $objWriter->writeAttribute('pane', $pane);
300
        }
301 56
        $objWriter->writeAttribute('activeCell', $activeCell);
302 56
        $objWriter->writeAttribute('sqref', $activeCell);
303 56
        $objWriter->endElement();
304
//      }
305
306 56
        $objWriter->endElement();
307
308 56
        $objWriter->endElement();
309 56
    }
310
311
    /**
312
     * Write SheetFormatPr
313
     *
314
     * @param    \PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter        XML Writer
315
     * @param    \PhpOffice\PhpSpreadsheet\Worksheet          $pSheet            Worksheet
316
     * @throws    \PhpOffice\PhpSpreadsheet\Writer\Exception
317
     */
318 56
    private function writeSheetFormatPr(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter = null, \PhpOffice\PhpSpreadsheet\Worksheet $pSheet = null)
319
    {
320
        // sheetFormatPr
321 56
        $objWriter->startElement('sheetFormatPr');
0 ignored issues
show
Bug introduced by
It seems like $objWriter is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
322
323
        // Default row height
324 56
        if ($pSheet->getDefaultRowDimension()->getRowHeight() >= 0) {
325 2
            $objWriter->writeAttribute('customHeight', 'true');
326 2
            $objWriter->writeAttribute('defaultRowHeight', \PhpOffice\PhpSpreadsheet\Shared\StringHelper::formatNumber($pSheet->getDefaultRowDimension()->getRowHeight()));
0 ignored issues
show
Bug introduced by
It seems like $pSheet is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
327
        } else {
328 54
            $objWriter->writeAttribute('defaultRowHeight', '14.4');
329
        }
330
331
        // Set Zero Height row
332 56
        if ((string) $pSheet->getDefaultRowDimension()->getZeroHeight() == '1' ||
333 56
            strtolower((string) $pSheet->getDefaultRowDimension()->getZeroHeight()) == 'true') {
334
            $objWriter->writeAttribute('zeroHeight', '1');
335
        }
336
337
        // Default column width
338 56
        if ($pSheet->getDefaultColumnDimension()->getWidth() >= 0) {
339
            $objWriter->writeAttribute('defaultColWidth', \PhpOffice\PhpSpreadsheet\Shared\StringHelper::formatNumber($pSheet->getDefaultColumnDimension()->getWidth()));
340
        }
341
342
        // Outline level - row
343 56
        $outlineLevelRow = 0;
344 56
        foreach ($pSheet->getRowDimensions() as $dimension) {
345 12
            if ($dimension->getOutlineLevel() > $outlineLevelRow) {
346 12
                $outlineLevelRow = $dimension->getOutlineLevel();
347
            }
348
        }
349 56
        $objWriter->writeAttribute('outlineLevelRow', (int) $outlineLevelRow);
350
351
        // Outline level - column
352 56
        $outlineLevelCol = 0;
353 56
        foreach ($pSheet->getColumnDimensions() as $dimension) {
354 21
            if ($dimension->getOutlineLevel() > $outlineLevelCol) {
355 21
                $outlineLevelCol = $dimension->getOutlineLevel();
356
            }
357
        }
358 56
        $objWriter->writeAttribute('outlineLevelCol', (int) $outlineLevelCol);
359
360 56
        $objWriter->endElement();
361 56
    }
362
363
    /**
364
     * Write Cols
365
     *
366
     * @param    \PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter XML Writer
367
     * @param    \PhpOffice\PhpSpreadsheet\Worksheet $pSheet Worksheet
368
     * @throws    \PhpOffice\PhpSpreadsheet\Writer\Exception
369
     */
370 56
    private function writeCols(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter = null, \PhpOffice\PhpSpreadsheet\Worksheet $pSheet = null)
371
    {
372
        // cols
373 56
        if (count($pSheet->getColumnDimensions()) > 0) {
374 21
            $objWriter->startElement('cols');
0 ignored issues
show
Bug introduced by
It seems like $objWriter is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
375
376 21
            $pSheet->calculateColumnWidths();
0 ignored issues
show
Bug introduced by
It seems like $pSheet is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
377
378
            // Loop through column dimensions
379 21
            foreach ($pSheet->getColumnDimensions() as $colDimension) {
380
                // col
381 21
                $objWriter->startElement('col');
382 21
                $objWriter->writeAttribute('min', \PhpOffice\PhpSpreadsheet\Cell::columnIndexFromString($colDimension->getColumnIndex()));
383 21
                $objWriter->writeAttribute('max', \PhpOffice\PhpSpreadsheet\Cell::columnIndexFromString($colDimension->getColumnIndex()));
384
385 21
                if ($colDimension->getWidth() < 0) {
386
                    // No width set, apply default of 10
387 2
                    $objWriter->writeAttribute('width', '9.10');
388
                } else {
389
                    // Width set
390 21
                    $objWriter->writeAttribute('width', \PhpOffice\PhpSpreadsheet\Shared\StringHelper::formatNumber($colDimension->getWidth()));
391
                }
392
393
                // Column visibility
394 21
                if ($colDimension->getVisible() == false) {
395 3
                    $objWriter->writeAttribute('hidden', 'true');
396
                }
397
398
                // Auto size?
399 21
                if ($colDimension->getAutoSize()) {
400 8
                    $objWriter->writeAttribute('bestFit', 'true');
401
                }
402
403
                // Custom width?
404 21
                if ($colDimension->getWidth() != $pSheet->getDefaultColumnDimension()->getWidth()) {
405 21
                    $objWriter->writeAttribute('customWidth', 'true');
406
                }
407
408
                // Collapsed
409 21
                if ($colDimension->getCollapsed() == true) {
410 1
                    $objWriter->writeAttribute('collapsed', 'true');
411
                }
412
413
                // Outline level
414 21
                if ($colDimension->getOutlineLevel() > 0) {
415 1
                    $objWriter->writeAttribute('outlineLevel', $colDimension->getOutlineLevel());
416
                }
417
418
                // Style
419 21
                $objWriter->writeAttribute('style', $colDimension->getXfIndex());
420
421 21
                $objWriter->endElement();
422
            }
423
424 21
            $objWriter->endElement();
425
        }
426 56
    }
427
428
    /**
429
     * Write SheetProtection
430
     *
431
     * @param    \PhpOffice\PhpSpreadsheet\Shared\XMLWriter            $objWriter        XML Writer
432
     * @param    \PhpOffice\PhpSpreadsheet\Worksheet                    $pSheet            Worksheet
433
     * @throws    \PhpOffice\PhpSpreadsheet\Writer\Exception
434
     */
435 56
    private function writeSheetProtection(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter = null, \PhpOffice\PhpSpreadsheet\Worksheet $pSheet = null)
436
    {
437
        // sheetProtection
438 56
        $objWriter->startElement('sheetProtection');
0 ignored issues
show
Bug introduced by
It seems like $objWriter is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
439
440 56
        if ($pSheet->getProtection()->getPassword() != '') {
441 1
            $objWriter->writeAttribute('password', $pSheet->getProtection()->getPassword());
0 ignored issues
show
Bug introduced by
It seems like $pSheet is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
442
        }
443
444 56
        $objWriter->writeAttribute('sheet', ($pSheet->getProtection()->getSheet() ? 'true' : 'false'));
445 56
        $objWriter->writeAttribute('objects', ($pSheet->getProtection()->getObjects() ? 'true' : 'false'));
446 56
        $objWriter->writeAttribute('scenarios', ($pSheet->getProtection()->getScenarios() ? 'true' : 'false'));
447 56
        $objWriter->writeAttribute('formatCells', ($pSheet->getProtection()->getFormatCells() ? 'true' : 'false'));
448 56
        $objWriter->writeAttribute('formatColumns', ($pSheet->getProtection()->getFormatColumns() ? 'true' : 'false'));
449 56
        $objWriter->writeAttribute('formatRows', ($pSheet->getProtection()->getFormatRows() ? 'true' : 'false'));
450 56
        $objWriter->writeAttribute('insertColumns', ($pSheet->getProtection()->getInsertColumns() ? 'true' : 'false'));
451 56
        $objWriter->writeAttribute('insertRows', ($pSheet->getProtection()->getInsertRows() ? 'true' : 'false'));
452 56
        $objWriter->writeAttribute('insertHyperlinks', ($pSheet->getProtection()->getInsertHyperlinks() ? 'true' : 'false'));
453 56
        $objWriter->writeAttribute('deleteColumns', ($pSheet->getProtection()->getDeleteColumns() ? 'true' : 'false'));
454 56
        $objWriter->writeAttribute('deleteRows', ($pSheet->getProtection()->getDeleteRows() ? 'true' : 'false'));
455 56
        $objWriter->writeAttribute('selectLockedCells', ($pSheet->getProtection()->getSelectLockedCells() ? 'true' : 'false'));
456 56
        $objWriter->writeAttribute('sort', ($pSheet->getProtection()->getSort() ? 'true' : 'false'));
457 56
        $objWriter->writeAttribute('autoFilter', ($pSheet->getProtection()->getAutoFilter() ? 'true' : 'false'));
458 56
        $objWriter->writeAttribute('pivotTables', ($pSheet->getProtection()->getPivotTables() ? 'true' : 'false'));
459 56
        $objWriter->writeAttribute('selectUnlockedCells', ($pSheet->getProtection()->getSelectUnlockedCells() ? 'true' : 'false'));
460 56
        $objWriter->endElement();
461 56
    }
462
463
    /**
464
     * Write ConditionalFormatting
465
     *
466
     * @param    \PhpOffice\PhpSpreadsheet\Shared\XMLWriter            $objWriter        XML Writer
467
     * @param    \PhpOffice\PhpSpreadsheet\Worksheet                    $pSheet            Worksheet
468
     * @throws    \PhpOffice\PhpSpreadsheet\Writer\Exception
469
     */
470 56
    private function writeConditionalFormatting(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter = null, \PhpOffice\PhpSpreadsheet\Worksheet $pSheet = null)
471
    {
472
        // Conditional id
473 56
        $id = 1;
474
475
        // Loop through styles in the current worksheet
476 56
        foreach ($pSheet->getConditionalStylesCollection() as $cellCoordinate => $conditionalStyles) {
0 ignored issues
show
Bug introduced by
It seems like $pSheet is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
477 2
            foreach ($conditionalStyles as $conditional) {
478
                // WHY was this again?
479
                // if ($this->getParentWriter()->getStylesConditionalHashTable()->getIndexForHashCode($conditional->getHashCode()) == '') {
0 ignored issues
show
Unused Code Comprehensibility introduced by
66% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
480
                //    continue;
481
                // }
482 2
                if ($conditional->getConditionType() != \PhpOffice\PhpSpreadsheet\Style\Conditional::CONDITION_NONE) {
483
                    // conditionalFormatting
484 2
                    $objWriter->startElement('conditionalFormatting');
0 ignored issues
show
Bug introduced by
It seems like $objWriter is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
485 2
                    $objWriter->writeAttribute('sqref', $cellCoordinate);
486
487
                    // cfRule
488 2
                    $objWriter->startElement('cfRule');
489 2
                    $objWriter->writeAttribute('type', $conditional->getConditionType());
490 2
                    $objWriter->writeAttribute('dxfId', $this->getParentWriter()->getStylesConditionalHashTable()->getIndexForHashCode($conditional->getHashCode()));
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface PhpOffice\PhpSpreadsheet\Writer\IWriter as the method getStylesConditionalHashTable() does only exist in the following implementations of said interface: PhpOffice\PhpSpreadsheet\Writer\Xlsx.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
491 2
                    $objWriter->writeAttribute('priority', $id++);
492
493 2
                    if (($conditional->getConditionType() == \PhpOffice\PhpSpreadsheet\Style\Conditional::CONDITION_CELLIS || $conditional->getConditionType() == \PhpOffice\PhpSpreadsheet\Style\Conditional::CONDITION_CONTAINSTEXT)
494 2
                        && $conditional->getOperatorType() != \PhpOffice\PhpSpreadsheet\Style\Conditional::OPERATOR_NONE) {
495 2
                        $objWriter->writeAttribute('operator', $conditional->getOperatorType());
496
                    }
497
498 2
                    if ($conditional->getConditionType() == \PhpOffice\PhpSpreadsheet\Style\Conditional::CONDITION_CONTAINSTEXT
499 2
                        && !is_null($conditional->getText())) {
500
                        $objWriter->writeAttribute('text', $conditional->getText());
501
                    }
502
503 2
                    if ($conditional->getConditionType() == \PhpOffice\PhpSpreadsheet\Style\Conditional::CONDITION_CONTAINSTEXT
504 2
                        && $conditional->getOperatorType() == \PhpOffice\PhpSpreadsheet\Style\Conditional::OPERATOR_CONTAINSTEXT
505 2
                        && !is_null($conditional->getText())) {
506
                        $objWriter->writeElement('formula', 'NOT(ISERROR(SEARCH("' . $conditional->getText() . '",' . $cellCoordinate . ')))');
507 2 View Code Duplication
                    } elseif ($conditional->getConditionType() == \PhpOffice\PhpSpreadsheet\Style\Conditional::CONDITION_CONTAINSTEXT
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...
508 2
                        && $conditional->getOperatorType() == \PhpOffice\PhpSpreadsheet\Style\Conditional::OPERATOR_BEGINSWITH
509 2
                        && !is_null($conditional->getText())) {
510
                        $objWriter->writeElement('formula', 'LEFT(' . $cellCoordinate . ',' . strlen($conditional->getText()) . ')="' . $conditional->getText() . '"');
511 2
                    } elseif ($conditional->getConditionType() == \PhpOffice\PhpSpreadsheet\Style\Conditional::CONDITION_CONTAINSTEXT
512 2
                        && $conditional->getOperatorType() == \PhpOffice\PhpSpreadsheet\Style\Conditional::OPERATOR_ENDSWITH
513 2
                        && !is_null($conditional->getText())) {
514
                        $objWriter->writeElement('formula', 'RIGHT(' . $cellCoordinate . ',' . strlen($conditional->getText()) . ')="' . $conditional->getText() . '"');
515 2 View Code Duplication
                    } elseif ($conditional->getConditionType() == \PhpOffice\PhpSpreadsheet\Style\Conditional::CONDITION_CONTAINSTEXT
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...
516 2
                        && $conditional->getOperatorType() == \PhpOffice\PhpSpreadsheet\Style\Conditional::OPERATOR_NOTCONTAINS
517 2
                        && !is_null($conditional->getText())) {
518
                        $objWriter->writeElement('formula', 'ISERROR(SEARCH("' . $conditional->getText() . '",' . $cellCoordinate . '))');
519 2
                    } elseif ($conditional->getConditionType() == \PhpOffice\PhpSpreadsheet\Style\Conditional::CONDITION_CELLIS
520
                        || $conditional->getConditionType() == \PhpOffice\PhpSpreadsheet\Style\Conditional::CONDITION_CONTAINSTEXT
521 2
                        || $conditional->getConditionType() == \PhpOffice\PhpSpreadsheet\Style\Conditional::CONDITION_EXPRESSION) {
522 2
                        foreach ($conditional->getConditions() as $formula) {
523
                            // Formula
524 2
                            $objWriter->writeElement('formula', $formula);
525
                        }
526
                    }
527
528 2
                    $objWriter->endElement();
529
530 2
                    $objWriter->endElement();
531
                }
532
            }
533
        }
534 56
    }
535
536
    /**
537
     * Write DataValidations
538
     *
539
     * @param    \PhpOffice\PhpSpreadsheet\Shared\XMLWriter            $objWriter        XML Writer
540
     * @param    \PhpOffice\PhpSpreadsheet\Worksheet                    $pSheet            Worksheet
541
     * @throws    \PhpOffice\PhpSpreadsheet\Writer\Exception
542
     */
543 56
    private function writeDataValidations(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter = null, \PhpOffice\PhpSpreadsheet\Worksheet $pSheet = null)
544
    {
545
        // Datavalidation collection
546 56
        $dataValidationCollection = $pSheet->getDataValidationCollection();
0 ignored issues
show
Bug introduced by
It seems like $pSheet is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
547
548
        // Write data validations?
549 56
        if (!empty($dataValidationCollection)) {
550 2
            $objWriter->startElement('dataValidations');
0 ignored issues
show
Bug introduced by
It seems like $objWriter is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
551 2
            $objWriter->writeAttribute('count', count($dataValidationCollection));
552
553 2
            foreach ($dataValidationCollection as $coordinate => $dv) {
554 2
                $objWriter->startElement('dataValidation');
555
556 2
                if ($dv->getType() != '') {
557 2
                    $objWriter->writeAttribute('type', $dv->getType());
558
                }
559
560 2
                if ($dv->getErrorStyle() != '') {
561 2
                    $objWriter->writeAttribute('errorStyle', $dv->getErrorStyle());
562
                }
563
564 2
                if ($dv->getOperator() != '') {
565
                    $objWriter->writeAttribute('operator', $dv->getOperator());
566
                }
567
568 2
                $objWriter->writeAttribute('allowBlank', ($dv->getAllowBlank() ? '1' : '0'));
569 2
                $objWriter->writeAttribute('showDropDown', (!$dv->getShowDropDown() ? '1' : '0'));
570 2
                $objWriter->writeAttribute('showInputMessage', ($dv->getShowInputMessage() ? '1' : '0'));
571 2
                $objWriter->writeAttribute('showErrorMessage', ($dv->getShowErrorMessage() ? '1' : '0'));
572
573 2
                if ($dv->getErrorTitle() !== '') {
574 2
                    $objWriter->writeAttribute('errorTitle', $dv->getErrorTitle());
575
                }
576 2
                if ($dv->getError() !== '') {
577 2
                    $objWriter->writeAttribute('error', $dv->getError());
578
                }
579 2
                if ($dv->getPromptTitle() !== '') {
580 2
                    $objWriter->writeAttribute('promptTitle', $dv->getPromptTitle());
581
                }
582 2
                if ($dv->getPrompt() !== '') {
583 2
                    $objWriter->writeAttribute('prompt', $dv->getPrompt());
584
                }
585
586 2
                $objWriter->writeAttribute('sqref', $coordinate);
587
588 2
                if ($dv->getFormula1() !== '') {
589 2
                    $objWriter->writeElement('formula1', $dv->getFormula1());
590
                }
591 2
                if ($dv->getFormula2() !== '') {
592 1
                    $objWriter->writeElement('formula2', $dv->getFormula2());
593
                }
594
595 2
                $objWriter->endElement();
596
            }
597
598 2
            $objWriter->endElement();
599
        }
600 56
    }
601
602
    /**
603
     * Write Hyperlinks
604
     *
605
     * @param    \PhpOffice\PhpSpreadsheet\Shared\XMLWriter            $objWriter        XML Writer
606
     * @param    \PhpOffice\PhpSpreadsheet\Worksheet                    $pSheet            Worksheet
607
     * @throws    \PhpOffice\PhpSpreadsheet\Writer\Exception
608
     */
609 56
    private function writeHyperlinks(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter = null, \PhpOffice\PhpSpreadsheet\Worksheet $pSheet = null)
610
    {
611
        // Hyperlink collection
612 56
        $hyperlinkCollection = $pSheet->getHyperlinkCollection();
0 ignored issues
show
Bug introduced by
It seems like $pSheet is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
613
614
        // Relation ID
615 56
        $relationId = 1;
616
617
        // Write hyperlinks?
618 56
        if (!empty($hyperlinkCollection)) {
619 8
            $objWriter->startElement('hyperlinks');
0 ignored issues
show
Bug introduced by
It seems like $objWriter is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
620
621 8
            foreach ($hyperlinkCollection as $coordinate => $hyperlink) {
622 8
                $objWriter->startElement('hyperlink');
623
624 8
                $objWriter->writeAttribute('ref', $coordinate);
625 8
                if (!$hyperlink->isInternal()) {
626 8
                    $objWriter->writeAttribute('r:id', 'rId_hyperlink_' . $relationId);
627 8
                    ++$relationId;
628
                } else {
629 7
                    $objWriter->writeAttribute('location', str_replace('sheet://', '', $hyperlink->getUrl()));
630
                }
631
632 8
                if ($hyperlink->getTooltip() != '') {
633 6
                    $objWriter->writeAttribute('tooltip', $hyperlink->getTooltip());
634
                }
635
636 8
                $objWriter->endElement();
637
            }
638
639 8
            $objWriter->endElement();
640
        }
641 56
    }
642
643
    /**
644
     * Write ProtectedRanges
645
     *
646
     * @param    \PhpOffice\PhpSpreadsheet\Shared\XMLWriter            $objWriter        XML Writer
647
     * @param    \PhpOffice\PhpSpreadsheet\Worksheet                    $pSheet            Worksheet
648
     * @throws    \PhpOffice\PhpSpreadsheet\Writer\Exception
649
     */
650 56
    private function writeProtectedRanges(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter = null, \PhpOffice\PhpSpreadsheet\Worksheet $pSheet = null)
651
    {
652 56
        if (count($pSheet->getProtectedCells()) > 0) {
653
            // protectedRanges
654 6
            $objWriter->startElement('protectedRanges');
0 ignored issues
show
Bug introduced by
It seems like $objWriter is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
655
656
            // Loop protectedRanges
657 6
            foreach ($pSheet->getProtectedCells() as $protectedCell => $passwordHash) {
0 ignored issues
show
Bug introduced by
It seems like $pSheet is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
658
                // protectedRange
659 6
                $objWriter->startElement('protectedRange');
660 6
                $objWriter->writeAttribute('name', 'p' . md5($protectedCell));
661 6
                $objWriter->writeAttribute('sqref', $protectedCell);
662 6
                if (!empty($passwordHash)) {
663 6
                    $objWriter->writeAttribute('password', $passwordHash);
664
                }
665 6
                $objWriter->endElement();
666
            }
667
668 6
            $objWriter->endElement();
669
        }
670 56
    }
671
672
    /**
673
     * Write MergeCells
674
     *
675
     * @param    \PhpOffice\PhpSpreadsheet\Shared\XMLWriter            $objWriter        XML Writer
676
     * @param    \PhpOffice\PhpSpreadsheet\Worksheet                    $pSheet            Worksheet
677
     * @throws    \PhpOffice\PhpSpreadsheet\Writer\Exception
678
     */
679 56
    private function writeMergeCells(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter = null, \PhpOffice\PhpSpreadsheet\Worksheet $pSheet = null)
680
    {
681 56
        if (count($pSheet->getMergeCells()) > 0) {
682
            // mergeCells
683 12
            $objWriter->startElement('mergeCells');
0 ignored issues
show
Bug introduced by
It seems like $objWriter is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
684
685
            // Loop mergeCells
686 12
            foreach ($pSheet->getMergeCells() as $mergeCell) {
0 ignored issues
show
Bug introduced by
It seems like $pSheet is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
687
                // mergeCell
688 12
                $objWriter->startElement('mergeCell');
689 12
                $objWriter->writeAttribute('ref', $mergeCell);
690 12
                $objWriter->endElement();
691
            }
692
693 12
            $objWriter->endElement();
694
        }
695 56
    }
696
697
    /**
698
     * Write PrintOptions
699
     *
700
     * @param    \PhpOffice\PhpSpreadsheet\Shared\XMLWriter            $objWriter        XML Writer
701
     * @param    \PhpOffice\PhpSpreadsheet\Worksheet                    $pSheet            Worksheet
702
     * @throws    \PhpOffice\PhpSpreadsheet\Writer\Exception
703
     */
704 56
    private function writePrintOptions(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter = null, \PhpOffice\PhpSpreadsheet\Worksheet $pSheet = null)
705
    {
706
        // printOptions
707 56
        $objWriter->startElement('printOptions');
0 ignored issues
show
Bug introduced by
It seems like $objWriter is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
708
709 56
        $objWriter->writeAttribute('gridLines', ($pSheet->getPrintGridlines() ? 'true' : 'false'));
0 ignored issues
show
Bug introduced by
It seems like $pSheet is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
710 56
        $objWriter->writeAttribute('gridLinesSet', 'true');
711
712 56
        if ($pSheet->getPageSetup()->getHorizontalCentered()) {
713
            $objWriter->writeAttribute('horizontalCentered', 'true');
714
        }
715
716 56
        if ($pSheet->getPageSetup()->getVerticalCentered()) {
717
            $objWriter->writeAttribute('verticalCentered', 'true');
718
        }
719
720 56
        $objWriter->endElement();
721 56
    }
722
723
    /**
724
     * Write PageMargins
725
     *
726
     * @param    \PhpOffice\PhpSpreadsheet\Shared\XMLWriter                $objWriter        XML Writer
727
     * @param    \PhpOffice\PhpSpreadsheet\Worksheet                        $pSheet            Worksheet
728
     * @throws    \PhpOffice\PhpSpreadsheet\Writer\Exception
729
     */
730 56
    private function writePageMargins(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter = null, \PhpOffice\PhpSpreadsheet\Worksheet $pSheet = null)
731
    {
732
        // pageMargins
733 56
        $objWriter->startElement('pageMargins');
0 ignored issues
show
Bug introduced by
It seems like $objWriter is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
734 56
        $objWriter->writeAttribute('left', \PhpOffice\PhpSpreadsheet\Shared\StringHelper::formatNumber($pSheet->getPageMargins()->getLeft()));
0 ignored issues
show
Bug introduced by
It seems like $pSheet is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
735 56
        $objWriter->writeAttribute('right', \PhpOffice\PhpSpreadsheet\Shared\StringHelper::formatNumber($pSheet->getPageMargins()->getRight()));
736 56
        $objWriter->writeAttribute('top', \PhpOffice\PhpSpreadsheet\Shared\StringHelper::formatNumber($pSheet->getPageMargins()->getTop()));
737 56
        $objWriter->writeAttribute('bottom', \PhpOffice\PhpSpreadsheet\Shared\StringHelper::formatNumber($pSheet->getPageMargins()->getBottom()));
738 56
        $objWriter->writeAttribute('header', \PhpOffice\PhpSpreadsheet\Shared\StringHelper::formatNumber($pSheet->getPageMargins()->getHeader()));
739 56
        $objWriter->writeAttribute('footer', \PhpOffice\PhpSpreadsheet\Shared\StringHelper::formatNumber($pSheet->getPageMargins()->getFooter()));
740 56
        $objWriter->endElement();
741 56
    }
742
743
    /**
744
     * Write AutoFilter
745
     *
746
     * @param    \PhpOffice\PhpSpreadsheet\Shared\XMLWriter                $objWriter        XML Writer
747
     * @param    \PhpOffice\PhpSpreadsheet\Worksheet                        $pSheet            Worksheet
748
     * @throws    \PhpOffice\PhpSpreadsheet\Writer\Exception
749
     */
750 56
    private function writeAutoFilter(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter = null, \PhpOffice\PhpSpreadsheet\Worksheet $pSheet = null)
751
    {
752 56
        $autoFilterRange = $pSheet->getAutoFilter()->getRange();
0 ignored issues
show
Bug introduced by
It seems like $pSheet is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
753 56
        if (!empty($autoFilterRange)) {
754
            // autoFilter
755 3
            $objWriter->startElement('autoFilter');
0 ignored issues
show
Bug introduced by
It seems like $objWriter is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
756
757
            // Strip any worksheet reference from the filter coordinates
758 3
            $range = \PhpOffice\PhpSpreadsheet\Cell::splitRange($autoFilterRange);
759 3
            $range = $range[0];
760
            //    Strip any worksheet ref
761 3 View Code Duplication
            if (strpos($range[0], '!') !== false) {
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...
762
                list($ws, $range[0]) = explode('!', $range[0]);
0 ignored issues
show
Unused Code introduced by
The assignment to $ws is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
763
            }
764 3
            $range = implode(':', $range);
765
766 3
            $objWriter->writeAttribute('ref', str_replace('$', '', $range));
767
768 3
            $columns = $pSheet->getAutoFilter()->getColumns();
769 3
            if (count($columns > 0)) {
770 3
                foreach ($columns as $columnID => $column) {
771 2
                    $rules = $column->getRules();
772 2
                    if (count($rules) > 0) {
773 2
                        $objWriter->startElement('filterColumn');
774 2
                        $objWriter->writeAttribute('colId', $pSheet->getAutoFilter()->getColumnOffset($columnID));
775
776 2
                        $objWriter->startElement($column->getFilterType());
777 2
                        if ($column->getJoin() == \PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column::AUTOFILTER_COLUMN_JOIN_AND) {
778 1
                            $objWriter->writeAttribute('and', 1);
779
                        }
780
781 2
                        foreach ($rules as $rule) {
782 2
                            if (($column->getFilterType() === \PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column::AUTOFILTER_FILTERTYPE_FILTER) &&
783 2
                                ($rule->getOperator() === \PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule::AUTOFILTER_COLUMN_RULE_EQUAL) &&
784 2
                                ($rule->getValue() === '')) {
785
                                //    Filter rule for Blanks
786 1
                                $objWriter->writeAttribute('blank', 1);
787 2
                            } elseif ($rule->getRuleType() === \PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule::AUTOFILTER_RULETYPE_DYNAMICFILTER) {
788
                                //    Dynamic Filter Rule
789 1
                                $objWriter->writeAttribute('type', $rule->getGrouping());
790 1
                                $val = $column->getAttribute('val');
791 1
                                if ($val !== null) {
792 1
                                    $objWriter->writeAttribute('val', $val);
793
                                }
794 1
                                $maxVal = $column->getAttribute('maxVal');
795 1
                                if ($maxVal !== null) {
796 1
                                    $objWriter->writeAttribute('maxVal', $maxVal);
797
                                }
798 2
                            } elseif ($rule->getRuleType() === \PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule::AUTOFILTER_RULETYPE_TOPTENFILTER) {
799
                                //    Top 10 Filter Rule
800
                                $objWriter->writeAttribute('val', $rule->getValue());
801
                                $objWriter->writeAttribute('percent', (($rule->getOperator() === \PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule::AUTOFILTER_COLUMN_RULE_TOPTEN_PERCENT) ? '1' : '0'));
802
                                $objWriter->writeAttribute('top', (($rule->getGrouping() === \PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule::AUTOFILTER_COLUMN_RULE_TOPTEN_TOP) ? '1' : '0'));
803
                            } else {
804
                                //    Filter, DateGroupItem or CustomFilter
805 2
                                $objWriter->startElement($rule->getRuleType());
806
807 2
                                if ($rule->getOperator() !== \PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule::AUTOFILTER_COLUMN_RULE_EQUAL) {
808 1
                                    $objWriter->writeAttribute('operator', $rule->getOperator());
809
                                }
810 2
                                if ($rule->getRuleType() === \PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule::AUTOFILTER_RULETYPE_DATEGROUP) {
811
                                    // Date Group filters
812 1
                                    foreach ($rule->getValue() as $key => $value) {
813 1
                                        if ($value > '') {
814 1
                                            $objWriter->writeAttribute($key, $value);
815
                                        }
816
                                    }
817 1
                                    $objWriter->writeAttribute('dateTimeGrouping', $rule->getGrouping());
818
                                } else {
819 2
                                    $objWriter->writeAttribute('val', $rule->getValue());
820
                                }
821
822 2
                                $objWriter->endElement();
823
                            }
824
                        }
825
826 2
                        $objWriter->endElement();
827
828 2
                        $objWriter->endElement();
829
                    }
830
                }
831
            }
832 3
            $objWriter->endElement();
833
        }
834 56
    }
835
836
    /**
837
     * Write PageSetup
838
     *
839
     * @param    \PhpOffice\PhpSpreadsheet\Shared\XMLWriter            $objWriter        XML Writer
840
     * @param    \PhpOffice\PhpSpreadsheet\Worksheet                    $pSheet            Worksheet
841
     * @throws    \PhpOffice\PhpSpreadsheet\Writer\Exception
842
     */
843 56
    private function writePageSetup(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter = null, \PhpOffice\PhpSpreadsheet\Worksheet $pSheet = null)
844
    {
845
        // pageSetup
846 56
        $objWriter->startElement('pageSetup');
0 ignored issues
show
Bug introduced by
It seems like $objWriter is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
847 56
        $objWriter->writeAttribute('paperSize', $pSheet->getPageSetup()->getPaperSize());
0 ignored issues
show
Bug introduced by
It seems like $pSheet is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
848 56
        $objWriter->writeAttribute('orientation', $pSheet->getPageSetup()->getOrientation());
849
850 56
        if (!is_null($pSheet->getPageSetup()->getScale())) {
851 56
            $objWriter->writeAttribute('scale', $pSheet->getPageSetup()->getScale());
852
        }
853 56
        if (!is_null($pSheet->getPageSetup()->getFitToHeight())) {
854 56
            $objWriter->writeAttribute('fitToHeight', $pSheet->getPageSetup()->getFitToHeight());
855
        } else {
856
            $objWriter->writeAttribute('fitToHeight', '0');
857
        }
858 56
        if (!is_null($pSheet->getPageSetup()->getFitToWidth())) {
859 56
            $objWriter->writeAttribute('fitToWidth', $pSheet->getPageSetup()->getFitToWidth());
860
        } else {
861
            $objWriter->writeAttribute('fitToWidth', '0');
862
        }
863 56
        if (!is_null($pSheet->getPageSetup()->getFirstPageNumber())) {
864
            $objWriter->writeAttribute('firstPageNumber', $pSheet->getPageSetup()->getFirstPageNumber());
865
            $objWriter->writeAttribute('useFirstPageNumber', '1');
866
        }
867
868 56
        $objWriter->endElement();
869 56
    }
870
871
    /**
872
     * Write Header / Footer
873
     *
874
     * @param    \PhpOffice\PhpSpreadsheet\Shared\XMLWriter        $objWriter        XML Writer
875
     * @param    \PhpOffice\PhpSpreadsheet\Worksheet                $pSheet            Worksheet
876
     * @throws    \PhpOffice\PhpSpreadsheet\Writer\Exception
877
     */
878 56
    private function writeHeaderFooter(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter = null, \PhpOffice\PhpSpreadsheet\Worksheet $pSheet = null)
879
    {
880
        // headerFooter
881 56
        $objWriter->startElement('headerFooter');
0 ignored issues
show
Bug introduced by
It seems like $objWriter is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
882 56
        $objWriter->writeAttribute('differentOddEven', ($pSheet->getHeaderFooter()->getDifferentOddEven() ? 'true' : 'false'));
0 ignored issues
show
Bug introduced by
It seems like $pSheet is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
883 56
        $objWriter->writeAttribute('differentFirst', ($pSheet->getHeaderFooter()->getDifferentFirst() ? 'true' : 'false'));
884 56
        $objWriter->writeAttribute('scaleWithDoc', ($pSheet->getHeaderFooter()->getScaleWithDocument() ? 'true' : 'false'));
885 56
        $objWriter->writeAttribute('alignWithMargins', ($pSheet->getHeaderFooter()->getAlignWithMargins() ? 'true' : 'false'));
886
887 56
        $objWriter->writeElement('oddHeader', $pSheet->getHeaderFooter()->getOddHeader());
888 56
        $objWriter->writeElement('oddFooter', $pSheet->getHeaderFooter()->getOddFooter());
889 56
        $objWriter->writeElement('evenHeader', $pSheet->getHeaderFooter()->getEvenHeader());
890 56
        $objWriter->writeElement('evenFooter', $pSheet->getHeaderFooter()->getEvenFooter());
891 56
        $objWriter->writeElement('firstHeader', $pSheet->getHeaderFooter()->getFirstHeader());
892 56
        $objWriter->writeElement('firstFooter', $pSheet->getHeaderFooter()->getFirstFooter());
893 56
        $objWriter->endElement();
894 56
    }
895
896
    /**
897
     * Write Breaks
898
     *
899
     * @param    \PhpOffice\PhpSpreadsheet\Shared\XMLWriter        $objWriter        XML Writer
900
     * @param    \PhpOffice\PhpSpreadsheet\Worksheet                $pSheet            Worksheet
901
     * @throws    \PhpOffice\PhpSpreadsheet\Writer\Exception
902
     */
903 56
    private function writeBreaks(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter = null, \PhpOffice\PhpSpreadsheet\Worksheet $pSheet = null)
904
    {
905
        // Get row and column breaks
906 56
        $aRowBreaks = [];
907 56
        $aColumnBreaks = [];
908 56
        foreach ($pSheet->getBreaks() as $cell => $breakType) {
0 ignored issues
show
Bug introduced by
It seems like $pSheet is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
909 1
            if ($breakType == \PhpOffice\PhpSpreadsheet\Worksheet::BREAK_ROW) {
910 1
                $aRowBreaks[] = $cell;
911
            } elseif ($breakType == \PhpOffice\PhpSpreadsheet\Worksheet::BREAK_COLUMN) {
912 1
                $aColumnBreaks[] = $cell;
913
            }
914
        }
915
916
        // rowBreaks
917 56 View Code Duplication
        if (!empty($aRowBreaks)) {
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...
918 1
            $objWriter->startElement('rowBreaks');
0 ignored issues
show
Bug introduced by
It seems like $objWriter is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
919 1
            $objWriter->writeAttribute('count', count($aRowBreaks));
920 1
            $objWriter->writeAttribute('manualBreakCount', count($aRowBreaks));
921
922 1
            foreach ($aRowBreaks as $cell) {
923 1
                $coords = \PhpOffice\PhpSpreadsheet\Cell::coordinateFromString($cell);
924
925 1
                $objWriter->startElement('brk');
926 1
                $objWriter->writeAttribute('id', $coords[1]);
927 1
                $objWriter->writeAttribute('man', '1');
928 1
                $objWriter->endElement();
929
            }
930
931 1
            $objWriter->endElement();
932
        }
933
934
        // Second, write column breaks
935 56 View Code Duplication
        if (!empty($aColumnBreaks)) {
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...
936
            $objWriter->startElement('colBreaks');
937
            $objWriter->writeAttribute('count', count($aColumnBreaks));
938
            $objWriter->writeAttribute('manualBreakCount', count($aColumnBreaks));
939
940
            foreach ($aColumnBreaks as $cell) {
941
                $coords = \PhpOffice\PhpSpreadsheet\Cell::coordinateFromString($cell);
942
943
                $objWriter->startElement('brk');
944
                $objWriter->writeAttribute('id', \PhpOffice\PhpSpreadsheet\Cell::columnIndexFromString($coords[0]) - 1);
945
                $objWriter->writeAttribute('man', '1');
946
                $objWriter->endElement();
947
            }
948
949
            $objWriter->endElement();
950
        }
951 56
    }
952
953
    /**
954
     * Write SheetData
955
     *
956
     * @param    \PhpOffice\PhpSpreadsheet\Shared\XMLWriter        $objWriter        XML Writer
957
     * @param    \PhpOffice\PhpSpreadsheet\Worksheet                $pSheet            Worksheet
958
     * @param    string[]                        $pStringTable    String table
959
     * @throws    \PhpOffice\PhpSpreadsheet\Writer\Exception
960
     */
961 56
    private function writeSheetData(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter = null, \PhpOffice\PhpSpreadsheet\Worksheet $pSheet = null, $pStringTable = null)
962
    {
963 56
        if (is_array($pStringTable)) {
964
            // Flipped stringtable, for faster index searching
965 56
            $aFlippedStringTable = $this->getParentWriter()->getWriterPart('stringtable')->flipStringTable($pStringTable);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface PhpOffice\PhpSpreadsheet\Writer\IWriter as the method getWriterPart() does only exist in the following implementations of said interface: PhpOffice\PhpSpreadsheet\Writer\OpenDocument, PhpOffice\PhpSpreadsheet\Writer\Xlsx.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
966
967
            // sheetData
968 56
            $objWriter->startElement('sheetData');
0 ignored issues
show
Bug introduced by
It seems like $objWriter is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
969
970
            // Get column count
971 56
            $colCount = \PhpOffice\PhpSpreadsheet\Cell::columnIndexFromString($pSheet->getHighestColumn());
0 ignored issues
show
Bug introduced by
It seems like $pSheet is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
972
973
            // Highest row number
974 56
            $highestRow = $pSheet->getHighestRow();
975
976
            // Loop through cells
977 56
            $cellsByRow = [];
978 56
            foreach ($pSheet->getCellCollection() as $cellID) {
979 56
                $cellAddress = \PhpOffice\PhpSpreadsheet\Cell::coordinateFromString($cellID);
980 56
                $cellsByRow[$cellAddress[1]][] = $cellID;
981
            }
982
983 56
            $currentRow = 0;
984 56
            while ($currentRow++ < $highestRow) {
985
                // Get row dimension
986 56
                $rowDimension = $pSheet->getRowDimension($currentRow);
987
988
                // Write current row?
989 56
                $writeCurrentRow = isset($cellsByRow[$currentRow]) || $rowDimension->getRowHeight() >= 0 || $rowDimension->getVisible() == false || $rowDimension->getCollapsed() == true || $rowDimension->getOutlineLevel() > 0 || $rowDimension->getXfIndex() !== null;
990
991 56
                if ($writeCurrentRow) {
992
                    // Start a new row
993 56
                    $objWriter->startElement('row');
994 56
                    $objWriter->writeAttribute('r', $currentRow);
995 56
                    $objWriter->writeAttribute('spans', '1:' . $colCount);
996
997
                    // Row dimensions
998 56
                    if ($rowDimension->getRowHeight() >= 0) {
999 5
                        $objWriter->writeAttribute('customHeight', '1');
1000 5
                        $objWriter->writeAttribute('ht', \PhpOffice\PhpSpreadsheet\Shared\StringHelper::formatNumber($rowDimension->getRowHeight()));
1001
                    }
1002
1003
                    // Row visibility
1004 56
                    if ($rowDimension->getVisible() == false) {
1005 2
                        $objWriter->writeAttribute('hidden', 'true');
1006
                    }
1007
1008
                    // Collapsed
1009 56
                    if ($rowDimension->getCollapsed() == true) {
1010
                        $objWriter->writeAttribute('collapsed', 'true');
1011
                    }
1012
1013
                    // Outline level
1014 56
                    if ($rowDimension->getOutlineLevel() > 0) {
1015
                        $objWriter->writeAttribute('outlineLevel', $rowDimension->getOutlineLevel());
1016
                    }
1017
1018
                    // Style
1019 56
                    if ($rowDimension->getXfIndex() !== null) {
1020
                        $objWriter->writeAttribute('s', $rowDimension->getXfIndex());
1021
                        $objWriter->writeAttribute('customFormat', '1');
1022
                    }
1023
1024
                    // Write cells
1025 56
                    if (isset($cellsByRow[$currentRow])) {
1026 56
                        foreach ($cellsByRow[$currentRow] as $cellAddress) {
1027
                            // Write cell
1028 56
                            $this->writeCell($objWriter, $pSheet, $cellAddress, $pStringTable, $aFlippedStringTable);
1029
                        }
1030
                    }
1031
1032
                    // End row
1033 56
                    $objWriter->endElement();
1034
                }
1035
            }
1036
1037 56
            $objWriter->endElement();
1038
        } else {
1039
            throw new \PhpOffice\PhpSpreadsheet\Writer\Exception('Invalid parameters passed.');
1040
        }
1041 56
    }
1042
1043
    /**
1044
     * Write Cell
1045
     *
1046
     * @param    \PhpOffice\PhpSpreadsheet\Shared\XMLWriter    $objWriter                XML Writer
1047
     * @param    \PhpOffice\PhpSpreadsheet\Worksheet            $pSheet                    Worksheet
1048
     * @param    \PhpOffice\PhpSpreadsheet\Cell                $pCellAddress            Cell Address
1049
     * @param    string[]                    $pStringTable            String table
1050
     * @param    string[]                    $pFlippedStringTable    String table (flipped), for faster index searching
1051
     * @throws    \PhpOffice\PhpSpreadsheet\Writer\Exception
1052
     */
1053 56
    private function writeCell(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter = null, \PhpOffice\PhpSpreadsheet\Worksheet $pSheet = null, $pCellAddress = null, $pStringTable = null, $pFlippedStringTable = null)
1054
    {
1055 56
        if (is_array($pStringTable) && is_array($pFlippedStringTable)) {
1056
            // Cell
1057 56
            $pCell = $pSheet->getCell($pCellAddress);
0 ignored issues
show
Documentation introduced by
$pCellAddress is of type object<PhpOffice\PhpSpreadsheet\Cell>|null, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Bug introduced by
It seems like $pSheet is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
1058 56
            $objWriter->startElement('c');
0 ignored issues
show
Bug introduced by
It seems like $objWriter is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
1059 56
            $objWriter->writeAttribute('r', $pCellAddress);
1060
1061
            // Sheet styles
1062 56
            if ($pCell->getXfIndex() != '') {
1063 32
                $objWriter->writeAttribute('s', $pCell->getXfIndex());
1064
            }
1065
1066
            // If cell value is supplied, write cell value
1067 56
            $cellValue = $pCell->getValue();
1068 56
            if (is_object($cellValue) || $cellValue !== '') {
1069
                // Map type
1070 56
                $mappedType = $pCell->getDataType();
1071
1072
                // Write data type depending on its type
1073 56
                switch (strtolower($mappedType)) {
1074 56
                    case 'inlinestr':    // Inline string
1075 55
                    case 's':            // String
1076 47
                    case 'b':            // Boolean
1077 51
                        $objWriter->writeAttribute('t', $mappedType);
1078 51
                        break;
1079 45
                    case 'f':            // Formula
1080 18
                        $calculatedValue = ($this->getParentWriter()->getPreCalculateFormulas()) ?
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface PhpOffice\PhpSpreadsheet\Writer\IWriter as the method getPreCalculateFormulas() does only exist in the following implementations of said interface: PhpOffice\PhpSpreadsheet\Writer\BaseWriter, PhpOffice\PhpSpreadsheet\Writer\CSV, PhpOffice\PhpSpreadsheet\Writer\Excel5, PhpOffice\PhpSpreadsheet\Writer\HTML, PhpOffice\PhpSpreadsheet\Writer\OpenDocument, PhpOffice\PhpSpreadsheet\Writer\PDF\Core, PhpOffice\PhpSpreadsheet\Writer\PDF\DomPDF, PhpOffice\PhpSpreadsheet\Writer\PDF\MPDF, PhpOffice\PhpSpreadsheet\Writer\PDF\TcPDF, PhpOffice\PhpSpreadsheet\Writer\Xlsx.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
1081 18
                            $pCell->getCalculatedValue() : $cellValue;
1082 18
                        if (is_string($calculatedValue)) {
1083 13
                            $objWriter->writeAttribute('t', 'str');
1084
                        }
1085 18
                        break;
1086 44
                    case 'e':            // Error
1087
                        $objWriter->writeAttribute('t', $mappedType);
1088
                }
1089
1090
                // Write data depending on its type
1091 56
                switch (strtolower($mappedType)) {
1092 56
                    case 'inlinestr':    // Inline string
1093 8
                        if (!$cellValue instanceof \PhpOffice\PhpSpreadsheet\RichText) {
1094
                            $objWriter->writeElement('t', \PhpOffice\PhpSpreadsheet\Shared\StringHelper::controlCharacterPHP2OOXML(htmlspecialchars($cellValue)));
1095
                        } elseif ($cellValue instanceof \PhpOffice\PhpSpreadsheet\RichText) {
1096 8
                            $objWriter->startElement('is');
1097 8
                            $this->getParentWriter()->getWriterPart('stringtable')->writeRichText($objWriter, $cellValue);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface PhpOffice\PhpSpreadsheet\Writer\IWriter as the method getWriterPart() does only exist in the following implementations of said interface: PhpOffice\PhpSpreadsheet\Writer\OpenDocument, PhpOffice\PhpSpreadsheet\Writer\Xlsx.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
1098 8
                            $objWriter->endElement();
1099
                        }
1100
1101 8
                        break;
1102 55
                    case 's':            // String
1103 50
                        if (!$cellValue instanceof \PhpOffice\PhpSpreadsheet\RichText) {
1104 50
                            if (isset($pFlippedStringTable[$cellValue])) {
1105 50
                                $objWriter->writeElement('v', $pFlippedStringTable[$cellValue]);
1106
                            }
1107
                        } elseif ($cellValue instanceof \PhpOffice\PhpSpreadsheet\RichText) {
1108 2
                            $objWriter->writeElement('v', $pFlippedStringTable[$cellValue->getHashCode()]);
1109
                        }
1110
1111 50
                        break;
1112 47
                    case 'f':            // Formula
1113 18
                        $attributes = $pCell->getFormulaAttributes();
1114 18
                        if ($attributes['t'] == 'array') {
1115
                            $objWriter->startElement('f');
1116
                            $objWriter->writeAttribute('t', 'array');
1117
                            $objWriter->writeAttribute('ref', $pCellAddress);
1118
                            $objWriter->writeAttribute('aca', '1');
1119
                            $objWriter->writeAttribute('ca', '1');
1120
                            $objWriter->text(substr($cellValue, 1));
1121
                            $objWriter->endElement();
1122
                        } else {
1123 18
                            $objWriter->writeElement('f', substr($cellValue, 1));
1124
                        }
1125 18
                        if ($this->getParentWriter()->getOffice2003Compatibility() === false) {
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface PhpOffice\PhpSpreadsheet\Writer\IWriter as the method getOffice2003Compatibility() does only exist in the following implementations of said interface: PhpOffice\PhpSpreadsheet\Writer\Xlsx.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
1126 18
                            if ($this->getParentWriter()->getPreCalculateFormulas()) {
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface PhpOffice\PhpSpreadsheet\Writer\IWriter as the method getPreCalculateFormulas() does only exist in the following implementations of said interface: PhpOffice\PhpSpreadsheet\Writer\BaseWriter, PhpOffice\PhpSpreadsheet\Writer\CSV, PhpOffice\PhpSpreadsheet\Writer\Excel5, PhpOffice\PhpSpreadsheet\Writer\HTML, PhpOffice\PhpSpreadsheet\Writer\OpenDocument, PhpOffice\PhpSpreadsheet\Writer\PDF\Core, PhpOffice\PhpSpreadsheet\Writer\PDF\DomPDF, PhpOffice\PhpSpreadsheet\Writer\PDF\MPDF, PhpOffice\PhpSpreadsheet\Writer\PDF\TcPDF, PhpOffice\PhpSpreadsheet\Writer\Xlsx.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
1127 18
                                if (!is_array($calculatedValue) && substr($calculatedValue, 0, 1) != '#') {
1128 18
                                    $objWriter->writeElement('v', \PhpOffice\PhpSpreadsheet\Shared\StringHelper::formatNumber($calculatedValue));
0 ignored issues
show
Bug introduced by
The variable $calculatedValue does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
1129
                                } else {
1130 18
                                    $objWriter->writeElement('v', '0');
1131
                                }
1132
                            } else {
1133
                                $objWriter->writeElement('v', '0');
1134
                            }
1135
                        }
1136 18
                        break;
1137 46
                    case 'n':            // Numeric
1138
                        // force point as decimal separator in case current locale uses comma
1139 39
                        $objWriter->writeElement('v', str_replace(',', '.', $cellValue));
1140 39
                        break;
1141 25
                    case 'b':            // Boolean
1142 8
                        $objWriter->writeElement('v', ($cellValue ? '1' : '0'));
1143 8
                        break;
1144 21
                    case 'e':            // Error
1145
                        if (substr($cellValue, 0, 1) == '=') {
1146
                            $objWriter->writeElement('f', substr($cellValue, 1));
1147
                            $objWriter->writeElement('v', substr($cellValue, 1));
1148
                        } else {
1149
                            $objWriter->writeElement('v', $cellValue);
1150
                        }
1151
1152
                        break;
1153
                }
1154
            }
1155
1156 56
            $objWriter->endElement();
1157
        } else {
1158
            throw new \PhpOffice\PhpSpreadsheet\Writer\Exception('Invalid parameters passed.');
1159
        }
1160 56
    }
1161
1162
    /**
1163
     * Write Drawings
1164
     *
1165
     * @param    \PhpOffice\PhpSpreadsheet\Shared\XMLWriter    $objWriter        XML Writer
1166
     * @param    \PhpOffice\PhpSpreadsheet\Worksheet            $pSheet            Worksheet
1167
     * @param    bool                        $includeCharts    Flag indicating if we should include drawing details for charts
1168
     * @throws    \PhpOffice\PhpSpreadsheet\Writer\Exception
1169
     */
1170 56
    private function writeDrawings(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter = null, \PhpOffice\PhpSpreadsheet\Worksheet $pSheet = null, $includeCharts = false)
1171
    {
1172 56
        $chartCount = ($includeCharts) ? $pSheet->getChartCollection()->count() : 0;
0 ignored issues
show
Bug introduced by
It seems like $pSheet is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
1173
        // If sheet contains drawings, add the relationships
1174 56
        if (($pSheet->getDrawingCollection()->count() > 0) ||
1175 56
            ($chartCount > 0)) {
1176 21
            $objWriter->startElement('drawing');
0 ignored issues
show
Bug introduced by
It seems like $objWriter is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
1177 21
            $objWriter->writeAttribute('r:id', 'rId1');
1178 21
            $objWriter->endElement();
1179
        }
1180 56
    }
1181
1182
    /**
1183
     * Write LegacyDrawing
1184
     *
1185
     * @param    \PhpOffice\PhpSpreadsheet\Shared\XMLWriter        $objWriter        XML Writer
1186
     * @param    \PhpOffice\PhpSpreadsheet\Worksheet                $pSheet            Worksheet
1187
     * @throws    \PhpOffice\PhpSpreadsheet\Writer\Exception
1188
     */
1189 56 View Code Duplication
    private function writeLegacyDrawing(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter = null, \PhpOffice\PhpSpreadsheet\Worksheet $pSheet = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
1190
    {
1191
        // If sheet contains comments, add the relationships
1192 56
        if (count($pSheet->getComments()) > 0) {
0 ignored issues
show
Bug introduced by
It seems like $pSheet is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
1193 9
            $objWriter->startElement('legacyDrawing');
0 ignored issues
show
Bug introduced by
It seems like $objWriter is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
1194 9
            $objWriter->writeAttribute('r:id', 'rId_comments_vml1');
1195 9
            $objWriter->endElement();
1196
        }
1197 56
    }
1198
1199
    /**
1200
     * Write LegacyDrawingHF
1201
     *
1202
     * @param    \PhpOffice\PhpSpreadsheet\Shared\XMLWriter        $objWriter        XML Writer
1203
     * @param    \PhpOffice\PhpSpreadsheet\Worksheet                $pSheet            Worksheet
1204
     * @throws    \PhpOffice\PhpSpreadsheet\Writer\Exception
1205
     */
1206 56 View Code Duplication
    private function writeLegacyDrawingHF(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter = null, \PhpOffice\PhpSpreadsheet\Worksheet $pSheet = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
1207
    {
1208
        // If sheet contains images, add the relationships
1209 56
        if (count($pSheet->getHeaderFooter()->getImages()) > 0) {
0 ignored issues
show
Bug introduced by
It seems like $pSheet is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
1210 1
            $objWriter->startElement('legacyDrawingHF');
0 ignored issues
show
Bug introduced by
It seems like $objWriter is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
1211 1
            $objWriter->writeAttribute('r:id', 'rId_headerfooter_vml1');
1212 1
            $objWriter->endElement();
1213
        }
1214 56
    }
1215
}
1216