Completed
Push — develop ( 714dc2...c2b38b )
by Adrien
31:27
created

Worksheet::writeDataValidations()   D

Complexity

Conditions 16
Paths 514

Size

Total Lines 58
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 33
CRAP Score 16

Importance

Changes 0
Metric Value
cc 16
eloc 32
nc 514
nop 2
dl 0
loc 58
ccs 33
cts 33
cp 1
crap 16
rs 4.1746
c 0
b 0
f 0

How to fix   Long Method    Complexity   

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
 *
26
 * @copyright  Copyright (c) 2006 - 2015 PhpSpreadsheet (https://github.com/PHPOffice/PhpSpreadsheet)
27
 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
28
 */
29
30
/**
31
 * @category   PhpSpreadsheet
32
 *
33
 * @copyright  Copyright (c) 2006 - 2015 PhpSpreadsheet (https://github.com/PHPOffice/PhpSpreadsheet)
34
 */
35
class Worksheet extends WriterPart
36
{
37
    /**
38
     * Write worksheet to XML format.
39
     *
40
     * @param \PhpOffice\PhpSpreadsheet\Worksheet $pSheet
41
     * @param string[] $pStringTable
42
     * @param bool $includeCharts Flag indicating if we should write charts
43
     *
44
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
45
     *
46
     * @return string XML Output
47
     */
48 55
    public function writeWorksheet($pSheet = null, $pStringTable = null, $includeCharts = false)
49
    {
50 55
        if (!is_null($pSheet)) {
51
            // Create XML writer
52 55
            $objWriter = null;
0 ignored issues
show
Unused Code introduced by
$objWriter is not used, you could remove the assignment.

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

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

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

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

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

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

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

Loading history...
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\Html, PhpOffice\PhpSpreadsheet\Writer\Ods, PhpOffice\PhpSpreadsheet\Writer\Pdf\Core, PhpOffice\PhpSpreadsheet\Writer\Pdf\DomPDF, PhpOffice\PhpSpreadsheet\Writer\Pdf\MPDF, PhpOffice\PhpSpreadsheet\Writer\Pdf\TcPDF, PhpOffice\PhpSpreadsheet\Writer\Xls, 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...
54
                $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\Html, PhpOffice\PhpSpreadsheet\Writer\Ods, PhpOffice\PhpSpreadsheet\Writer\Pdf\Core, PhpOffice\PhpSpreadsheet\Writer\Pdf\DomPDF, PhpOffice\PhpSpreadsheet\Writer\Pdf\MPDF, PhpOffice\PhpSpreadsheet\Writer\Pdf\TcPDF, PhpOffice\PhpSpreadsheet\Writer\Xls, 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...
55
            } else {
56 55
                $objWriter = new \PhpOffice\PhpSpreadsheet\Shared\XMLWriter(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter::STORAGE_MEMORY);
57
            }
58
59
            // XML header
60 55
            $objWriter->startDocument('1.0', 'UTF-8', 'yes');
61
62
            // Worksheet
63 55
            $objWriter->startElement('worksheet');
64 55
            $objWriter->writeAttribute('xml:space', 'preserve');
65 55
            $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/spreadsheetml/2006/main');
66 55
            $objWriter->writeAttribute('xmlns:r', 'http://schemas.openxmlformats.org/officeDocument/2006/relationships');
67
68
                // sheetPr
69 55
                $this->writeSheetPr($objWriter, $pSheet);
70
71
                // Dimension
72 55
                $this->writeDimension($objWriter, $pSheet);
73
74
                // sheetViews
75 55
                $this->writeSheetViews($objWriter, $pSheet);
76
77
                // sheetFormatPr
78 55
                $this->writeSheetFormatPr($objWriter, $pSheet);
79
80
                // cols
81 55
                $this->writeCols($objWriter, $pSheet);
82
83
                // sheetData
84 55
                $this->writeSheetData($objWriter, $pSheet, $pStringTable);
85
86
                // sheetProtection
87 55
                $this->writeSheetProtection($objWriter, $pSheet);
88
89
                // protectedRanges
90 55
                $this->writeProtectedRanges($objWriter, $pSheet);
91
92
                // autoFilter
93 55
                $this->writeAutoFilter($objWriter, $pSheet);
94
95
                // mergeCells
96 55
                $this->writeMergeCells($objWriter, $pSheet);
97
98
                // conditionalFormatting
99 55
                $this->writeConditionalFormatting($objWriter, $pSheet);
100
101
                // dataValidations
102 55
                $this->writeDataValidations($objWriter, $pSheet);
103
104
                // hyperlinks
105 55
                $this->writeHyperlinks($objWriter, $pSheet);
106
107
                // Print options
108 55
                $this->writePrintOptions($objWriter, $pSheet);
109
110
                // Page margins
111 55
                $this->writePageMargins($objWriter, $pSheet);
112
113
                // Page setup
114 55
                $this->writePageSetup($objWriter, $pSheet);
115
116
                // Header / footer
117 55
                $this->writeHeaderFooter($objWriter, $pSheet);
118
119
                // Breaks
120 55
                $this->writeBreaks($objWriter, $pSheet);
121
122
                // Drawings and/or Charts
123 55
                $this->writeDrawings($objWriter, $pSheet, $includeCharts);
124
125
                // LegacyDrawing
126 55
                $this->writeLegacyDrawing($objWriter, $pSheet);
127
128
                // LegacyDrawingHF
129 55
                $this->writeLegacyDrawingHF($objWriter, $pSheet);
130
131 55
            $objWriter->endElement();
132
133
            // Return
134 55
            return $objWriter->getData();
135
        }
136
        throw new \PhpOffice\PhpSpreadsheet\Writer\Exception('Invalid \\PhpOffice\\PhpSpreadsheet\\Worksheet object passed.');
137
    }
138
139
    /**
140
     * Write SheetPr.
141
     *
142
     * @param \PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter XML Writer
143
     * @param \PhpOffice\PhpSpreadsheet\Worksheet $pSheet Worksheet
144
     *
145
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
146
     */
147 55
    private function writeSheetPr(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter = null, \PhpOffice\PhpSpreadsheet\Worksheet $pSheet = null)
148
    {
149
        // sheetPr
150 55
        $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...
151 55
        if ($pSheet->getParent()->hasMacros()) {
152
            //if the workbook have macros, we need to have codeName for the sheet
153
            if ($pSheet->hasCodeName() == false) {
154
                $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...
155
            }
156
            $objWriter->writeAttribute('codeName', $pSheet->getCodeName());
157
        }
158 55
        $autoFilterRange = $pSheet->getAutoFilter()->getRange();
159 55
        if (!empty($autoFilterRange)) {
160 3
            $objWriter->writeAttribute('filterMode', 1);
161 3
            $pSheet->getAutoFilter()->showHideRows();
162
        }
163
164
        // tabColor
165 55
        if ($pSheet->isTabColorSet()) {
166 6
            $objWriter->startElement('tabColor');
167 6
            $objWriter->writeAttribute('rgb', $pSheet->getTabColor()->getARGB());
168 6
            $objWriter->endElement();
169
        }
170
171
        // outlinePr
172 55
        $objWriter->startElement('outlinePr');
173 55
        $objWriter->writeAttribute('summaryBelow', ($pSheet->getShowSummaryBelow() ? '1' : '0'));
174 55
        $objWriter->writeAttribute('summaryRight', ($pSheet->getShowSummaryRight() ? '1' : '0'));
175 55
        $objWriter->endElement();
176
177
        // pageSetUpPr
178 55
        if ($pSheet->getPageSetup()->getFitToPage()) {
179
            $objWriter->startElement('pageSetUpPr');
180
            $objWriter->writeAttribute('fitToPage', '1');
181
            $objWriter->endElement();
182
        }
183
184 55
        $objWriter->endElement();
185 55
    }
186
187
    /**
188
     * Write Dimension.
189
     *
190
     * @param \PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter XML Writer
191
     * @param \PhpOffice\PhpSpreadsheet\Worksheet $pSheet Worksheet
192
     *
193
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
194
     */
195 55
    private function writeDimension(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter = null, \PhpOffice\PhpSpreadsheet\Worksheet $pSheet = null)
196
    {
197
        // dimension
198 55
        $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...
199 55
        $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...
200 55
        $objWriter->endElement();
201 55
    }
202
203
    /**
204
     * Write SheetViews.
205
     *
206
     * @param \PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter XML Writer
207
     * @param \PhpOffice\PhpSpreadsheet\Worksheet $pSheet Worksheet
208
     *
209
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
210
     */
211 55
    private function writeSheetViews(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter = null, \PhpOffice\PhpSpreadsheet\Worksheet $pSheet = null)
212
    {
213
        // sheetViews
214 55
        $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...
215
216
        // Sheet selected?
217 55
        $sheetSelected = false;
218 55
        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\Ods, 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...
219 55
            $sheetSelected = true;
220
        }
221
222
        // sheetView
223 55
        $objWriter->startElement('sheetView');
224 55
        $objWriter->writeAttribute('tabSelected', $sheetSelected ? '1' : '0');
225 55
        $objWriter->writeAttribute('workbookViewId', '0');
226
227
        // Zoom scales
228 55
        if ($pSheet->getSheetView()->getZoomScale() != 100) {
229
            $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...
230
        }
231 55
        if ($pSheet->getSheetView()->getZoomScaleNormal() != 100) {
232
            $objWriter->writeAttribute('zoomScaleNormal', $pSheet->getSheetView()->getZoomScaleNormal());
233
        }
234
235
        // View Layout Type
236 55
        if ($pSheet->getSheetView()->getView() !== \PhpOffice\PhpSpreadsheet\Worksheet\SheetView::SHEETVIEW_NORMAL) {
237 1
            $objWriter->writeAttribute('view', $pSheet->getSheetView()->getView());
238
        }
239
240
        // Gridlines
241 55
        if ($pSheet->getShowGridlines()) {
242 55
            $objWriter->writeAttribute('showGridLines', 'true');
243
        } else {
244
            $objWriter->writeAttribute('showGridLines', 'false');
245
        }
246
247
        // Row and column headers
248 55
        if ($pSheet->getShowRowColHeaders()) {
249 55
            $objWriter->writeAttribute('showRowColHeaders', '1');
250
        } else {
251
            $objWriter->writeAttribute('showRowColHeaders', '0');
252
        }
253
254
        // Right-to-left
255 55
        if ($pSheet->getRightToLeft()) {
256
            $objWriter->writeAttribute('rightToLeft', 'true');
257
        }
258
259 55
        $activeCell = $pSheet->getActiveCell();
260
261
        // Pane
262 55
        $pane = '';
263 55
        $topLeftCell = $pSheet->getFreezePane();
264 55
        if (($topLeftCell != '') && ($topLeftCell != 'A1')) {
265 3
            $activeCell = $topLeftCell;
266
            // Calculate freeze coordinates
267 3
            $xSplit = $ySplit = 0;
268
269 3
            list($xSplit, $ySplit) = \PhpOffice\PhpSpreadsheet\Cell::coordinateFromString($topLeftCell);
270 3
            $xSplit = \PhpOffice\PhpSpreadsheet\Cell::columnIndexFromString($xSplit);
271
272
            // pane
273 3
            $pane = 'topRight';
274 3
            $objWriter->startElement('pane');
275 3
            if ($xSplit > 1) {
276
                $objWriter->writeAttribute('xSplit', $xSplit - 1);
277
            }
278 3
            if ($ySplit > 1) {
279 3
                $objWriter->writeAttribute('ySplit', $ySplit - 1);
280 3
                $pane = ($xSplit > 1) ? 'bottomRight' : 'bottomLeft';
281
            }
282 3
            $objWriter->writeAttribute('topLeftCell', $topLeftCell);
283 3
            $objWriter->writeAttribute('activePane', $pane);
284 3
            $objWriter->writeAttribute('state', 'frozen');
285 3
            $objWriter->endElement();
286
287 3
            if (($xSplit > 1) && ($ySplit > 1)) {
288
                //    Write additional selections if more than two panes (ie both an X and a Y split)
289
                $objWriter->startElement('selection');
290
                $objWriter->writeAttribute('pane', 'topRight');
291
                $objWriter->endElement();
292
                $objWriter->startElement('selection');
293
                $objWriter->writeAttribute('pane', 'bottomLeft');
294
                $objWriter->endElement();
295
            }
296
        }
297
298
        // 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...
299
//      if ($pane != '') {
300
        // Only need to write selection element if we have a split pane
301
        // We cheat a little by over-riding the active cell selection, setting it to the split cell
302 55
        $objWriter->startElement('selection');
303 55
        if ($pane != '') {
304 3
            $objWriter->writeAttribute('pane', $pane);
305
        }
306 55
        $objWriter->writeAttribute('activeCell', $activeCell);
307 55
        $objWriter->writeAttribute('sqref', $activeCell);
308 55
        $objWriter->endElement();
309
//      }
310
311 55
        $objWriter->endElement();
312
313 55
        $objWriter->endElement();
314 55
    }
315
316
    /**
317
     * Write SheetFormatPr.
318
     *
319
     * @param \PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter XML Writer
320
     * @param \PhpOffice\PhpSpreadsheet\Worksheet $pSheet Worksheet
321
     *
322
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
323
     */
324 55
    private function writeSheetFormatPr(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter = null, \PhpOffice\PhpSpreadsheet\Worksheet $pSheet = null)
325
    {
326
        // sheetFormatPr
327 55
        $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...
328
329
        // Default row height
330 55
        if ($pSheet->getDefaultRowDimension()->getRowHeight() >= 0) {
331 2
            $objWriter->writeAttribute('customHeight', 'true');
332 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...
333
        } else {
334 53
            $objWriter->writeAttribute('defaultRowHeight', '14.4');
335
        }
336
337
        // Set Zero Height row
338 55
        if ((string) $pSheet->getDefaultRowDimension()->getZeroHeight() == '1' ||
339 55
            strtolower((string) $pSheet->getDefaultRowDimension()->getZeroHeight()) == 'true') {
340
            $objWriter->writeAttribute('zeroHeight', '1');
341
        }
342
343
        // Default column width
344 55
        if ($pSheet->getDefaultColumnDimension()->getWidth() >= 0) {
345
            $objWriter->writeAttribute('defaultColWidth', \PhpOffice\PhpSpreadsheet\Shared\StringHelper::formatNumber($pSheet->getDefaultColumnDimension()->getWidth()));
346
        }
347
348
        // Outline level - row
349 55
        $outlineLevelRow = 0;
350 55
        foreach ($pSheet->getRowDimensions() as $dimension) {
351 11
            if ($dimension->getOutlineLevel() > $outlineLevelRow) {
352
                $outlineLevelRow = $dimension->getOutlineLevel();
353
            }
354
        }
355 55
        $objWriter->writeAttribute('outlineLevelRow', (int) $outlineLevelRow);
356
357
        // Outline level - column
358 55
        $outlineLevelCol = 0;
359 55
        foreach ($pSheet->getColumnDimensions() as $dimension) {
360 21
            if ($dimension->getOutlineLevel() > $outlineLevelCol) {
361 1
                $outlineLevelCol = $dimension->getOutlineLevel();
362
            }
363
        }
364 55
        $objWriter->writeAttribute('outlineLevelCol', (int) $outlineLevelCol);
365
366 55
        $objWriter->endElement();
367 55
    }
368
369
    /**
370
     * Write Cols.
371
     *
372
     * @param \PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter XML Writer
373
     * @param \PhpOffice\PhpSpreadsheet\Worksheet $pSheet Worksheet
374
     *
375
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
376
     */
377 55
    private function writeCols(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter = null, \PhpOffice\PhpSpreadsheet\Worksheet $pSheet = null)
378
    {
379
        // cols
380 55
        if (count($pSheet->getColumnDimensions()) > 0) {
381 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...
382
383 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...
384
385
            // Loop through column dimensions
386 21
            foreach ($pSheet->getColumnDimensions() as $colDimension) {
387
                // col
388 21
                $objWriter->startElement('col');
389 21
                $objWriter->writeAttribute('min', \PhpOffice\PhpSpreadsheet\Cell::columnIndexFromString($colDimension->getColumnIndex()));
390 21
                $objWriter->writeAttribute('max', \PhpOffice\PhpSpreadsheet\Cell::columnIndexFromString($colDimension->getColumnIndex()));
391
392 21
                if ($colDimension->getWidth() < 0) {
393
                    // No width set, apply default of 10
394 2
                    $objWriter->writeAttribute('width', '9.10');
395
                } else {
396
                    // Width set
397 21
                    $objWriter->writeAttribute('width', \PhpOffice\PhpSpreadsheet\Shared\StringHelper::formatNumber($colDimension->getWidth()));
398
                }
399
400
                // Column visibility
401 21
                if ($colDimension->getVisible() == false) {
402 3
                    $objWriter->writeAttribute('hidden', 'true');
403
                }
404
405
                // Auto size?
406 21
                if ($colDimension->getAutoSize()) {
407 8
                    $objWriter->writeAttribute('bestFit', 'true');
408
                }
409
410
                // Custom width?
411 21
                if ($colDimension->getWidth() != $pSheet->getDefaultColumnDimension()->getWidth()) {
412 21
                    $objWriter->writeAttribute('customWidth', 'true');
413
                }
414
415
                // Collapsed
416 21
                if ($colDimension->getCollapsed() == true) {
417 1
                    $objWriter->writeAttribute('collapsed', 'true');
418
                }
419
420
                // Outline level
421 21
                if ($colDimension->getOutlineLevel() > 0) {
422 1
                    $objWriter->writeAttribute('outlineLevel', $colDimension->getOutlineLevel());
423
                }
424
425
                // Style
426 21
                $objWriter->writeAttribute('style', $colDimension->getXfIndex());
427
428 21
                $objWriter->endElement();
429
            }
430
431 21
            $objWriter->endElement();
432
        }
433 55
    }
434
435
    /**
436
     * Write SheetProtection.
437
     *
438
     * @param \PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter XML Writer
439
     * @param \PhpOffice\PhpSpreadsheet\Worksheet $pSheet Worksheet
440
     *
441
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
442
     */
443 55
    private function writeSheetProtection(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter = null, \PhpOffice\PhpSpreadsheet\Worksheet $pSheet = null)
444
    {
445
        // sheetProtection
446 55
        $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...
447
448 55
        if ($pSheet->getProtection()->getPassword() != '') {
449 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...
450
        }
451
452 55
        $objWriter->writeAttribute('sheet', ($pSheet->getProtection()->getSheet() ? 'true' : 'false'));
453 55
        $objWriter->writeAttribute('objects', ($pSheet->getProtection()->getObjects() ? 'true' : 'false'));
454 55
        $objWriter->writeAttribute('scenarios', ($pSheet->getProtection()->getScenarios() ? 'true' : 'false'));
455 55
        $objWriter->writeAttribute('formatCells', ($pSheet->getProtection()->getFormatCells() ? 'true' : 'false'));
456 55
        $objWriter->writeAttribute('formatColumns', ($pSheet->getProtection()->getFormatColumns() ? 'true' : 'false'));
457 55
        $objWriter->writeAttribute('formatRows', ($pSheet->getProtection()->getFormatRows() ? 'true' : 'false'));
458 55
        $objWriter->writeAttribute('insertColumns', ($pSheet->getProtection()->getInsertColumns() ? 'true' : 'false'));
459 55
        $objWriter->writeAttribute('insertRows', ($pSheet->getProtection()->getInsertRows() ? 'true' : 'false'));
460 55
        $objWriter->writeAttribute('insertHyperlinks', ($pSheet->getProtection()->getInsertHyperlinks() ? 'true' : 'false'));
461 55
        $objWriter->writeAttribute('deleteColumns', ($pSheet->getProtection()->getDeleteColumns() ? 'true' : 'false'));
462 55
        $objWriter->writeAttribute('deleteRows', ($pSheet->getProtection()->getDeleteRows() ? 'true' : 'false'));
463 55
        $objWriter->writeAttribute('selectLockedCells', ($pSheet->getProtection()->getSelectLockedCells() ? 'true' : 'false'));
464 55
        $objWriter->writeAttribute('sort', ($pSheet->getProtection()->getSort() ? 'true' : 'false'));
465 55
        $objWriter->writeAttribute('autoFilter', ($pSheet->getProtection()->getAutoFilter() ? 'true' : 'false'));
466 55
        $objWriter->writeAttribute('pivotTables', ($pSheet->getProtection()->getPivotTables() ? 'true' : 'false'));
467 55
        $objWriter->writeAttribute('selectUnlockedCells', ($pSheet->getProtection()->getSelectUnlockedCells() ? 'true' : 'false'));
468 55
        $objWriter->endElement();
469 55
    }
470
471
    /**
472
     * Write ConditionalFormatting.
473
     *
474
     * @param \PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter XML Writer
475
     * @param \PhpOffice\PhpSpreadsheet\Worksheet $pSheet Worksheet
476
     *
477
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
478
     */
479 55
    private function writeConditionalFormatting(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter = null, \PhpOffice\PhpSpreadsheet\Worksheet $pSheet = null)
480
    {
481
        // Conditional id
482 55
        $id = 1;
483
484
        // Loop through styles in the current worksheet
485 55
        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...
486 2
            foreach ($conditionalStyles as $conditional) {
487
                // WHY was this again?
488
                // 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...
489
                //    continue;
490
                // }
491 2
                if ($conditional->getConditionType() != \PhpOffice\PhpSpreadsheet\Style\Conditional::CONDITION_NONE) {
492
                    // conditionalFormatting
493 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...
494 2
                    $objWriter->writeAttribute('sqref', $cellCoordinate);
495
496
                    // cfRule
497 2
                    $objWriter->startElement('cfRule');
498 2
                    $objWriter->writeAttribute('type', $conditional->getConditionType());
499 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...
500 2
                    $objWriter->writeAttribute('priority', $id++);
501
502 2
                    if (($conditional->getConditionType() == \PhpOffice\PhpSpreadsheet\Style\Conditional::CONDITION_CELLIS || $conditional->getConditionType() == \PhpOffice\PhpSpreadsheet\Style\Conditional::CONDITION_CONTAINSTEXT)
503 2
                        && $conditional->getOperatorType() != \PhpOffice\PhpSpreadsheet\Style\Conditional::OPERATOR_NONE) {
504 2
                        $objWriter->writeAttribute('operator', $conditional->getOperatorType());
505
                    }
506
507 2
                    if ($conditional->getConditionType() == \PhpOffice\PhpSpreadsheet\Style\Conditional::CONDITION_CONTAINSTEXT
508
                        && !is_null($conditional->getText())) {
509
                        $objWriter->writeAttribute('text', $conditional->getText());
510
                    }
511
512 2
                    if ($conditional->getConditionType() == \PhpOffice\PhpSpreadsheet\Style\Conditional::CONDITION_CONTAINSTEXT
513
                        && $conditional->getOperatorType() == \PhpOffice\PhpSpreadsheet\Style\Conditional::OPERATOR_CONTAINSTEXT
514
                        && !is_null($conditional->getText())) {
515
                        $objWriter->writeElement('formula', 'NOT(ISERROR(SEARCH("' . $conditional->getText() . '",' . $cellCoordinate . ')))');
516 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...
517
                        && $conditional->getOperatorType() == \PhpOffice\PhpSpreadsheet\Style\Conditional::OPERATOR_BEGINSWITH
518
                        && !is_null($conditional->getText())) {
519
                        $objWriter->writeElement('formula', 'LEFT(' . $cellCoordinate . ',' . strlen($conditional->getText()) . ')="' . $conditional->getText() . '"');
520 2
                    } elseif ($conditional->getConditionType() == \PhpOffice\PhpSpreadsheet\Style\Conditional::CONDITION_CONTAINSTEXT
521
                        && $conditional->getOperatorType() == \PhpOffice\PhpSpreadsheet\Style\Conditional::OPERATOR_ENDSWITH
522
                        && !is_null($conditional->getText())) {
523
                        $objWriter->writeElement('formula', 'RIGHT(' . $cellCoordinate . ',' . strlen($conditional->getText()) . ')="' . $conditional->getText() . '"');
524 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...
525
                        && $conditional->getOperatorType() == \PhpOffice\PhpSpreadsheet\Style\Conditional::OPERATOR_NOTCONTAINS
526
                        && !is_null($conditional->getText())) {
527
                        $objWriter->writeElement('formula', 'ISERROR(SEARCH("' . $conditional->getText() . '",' . $cellCoordinate . '))');
528 2
                    } elseif ($conditional->getConditionType() == \PhpOffice\PhpSpreadsheet\Style\Conditional::CONDITION_CELLIS
529
                        || $conditional->getConditionType() == \PhpOffice\PhpSpreadsheet\Style\Conditional::CONDITION_CONTAINSTEXT
530
                        || $conditional->getConditionType() == \PhpOffice\PhpSpreadsheet\Style\Conditional::CONDITION_EXPRESSION) {
531 2
                        foreach ($conditional->getConditions() as $formula) {
532
                            // Formula
533 2
                            $objWriter->writeElement('formula', $formula);
534
                        }
535
                    }
536
537 2
                    $objWriter->endElement();
538
539 2
                    $objWriter->endElement();
540
                }
541
            }
542
        }
543 55
    }
544
545
    /**
546
     * Write DataValidations.
547
     *
548
     * @param \PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter XML Writer
549
     * @param \PhpOffice\PhpSpreadsheet\Worksheet $pSheet Worksheet
550
     *
551
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
552
     */
553 55
    private function writeDataValidations(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter = null, \PhpOffice\PhpSpreadsheet\Worksheet $pSheet = null)
554
    {
555
        // Datavalidation collection
556 55
        $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...
557
558
        // Write data validations?
559 55
        if (!empty($dataValidationCollection)) {
560 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...
561 2
            $objWriter->writeAttribute('count', count($dataValidationCollection));
562
563 2
            foreach ($dataValidationCollection as $coordinate => $dv) {
564 2
                $objWriter->startElement('dataValidation');
565
566 2
                if ($dv->getType() != '') {
567 2
                    $objWriter->writeAttribute('type', $dv->getType());
568
                }
569
570 2
                if ($dv->getErrorStyle() != '') {
571 2
                    $objWriter->writeAttribute('errorStyle', $dv->getErrorStyle());
572
                }
573
574 2
                if ($dv->getOperator() != '') {
575 2
                    $objWriter->writeAttribute('operator', $dv->getOperator());
576
                }
577
578 2
                $objWriter->writeAttribute('allowBlank', ($dv->getAllowBlank() ? '1' : '0'));
579 2
                $objWriter->writeAttribute('showDropDown', (!$dv->getShowDropDown() ? '1' : '0'));
580 2
                $objWriter->writeAttribute('showInputMessage', ($dv->getShowInputMessage() ? '1' : '0'));
581 2
                $objWriter->writeAttribute('showErrorMessage', ($dv->getShowErrorMessage() ? '1' : '0'));
582
583 2
                if ($dv->getErrorTitle() !== '') {
584 2
                    $objWriter->writeAttribute('errorTitle', $dv->getErrorTitle());
585
                }
586 2
                if ($dv->getError() !== '') {
587 2
                    $objWriter->writeAttribute('error', $dv->getError());
588
                }
589 2
                if ($dv->getPromptTitle() !== '') {
590 2
                    $objWriter->writeAttribute('promptTitle', $dv->getPromptTitle());
591
                }
592 2
                if ($dv->getPrompt() !== '') {
593 2
                    $objWriter->writeAttribute('prompt', $dv->getPrompt());
594
                }
595
596 2
                $objWriter->writeAttribute('sqref', $coordinate);
597
598 2
                if ($dv->getFormula1() !== '') {
599 2
                    $objWriter->writeElement('formula1', $dv->getFormula1());
600
                }
601 2
                if ($dv->getFormula2() !== '') {
602 1
                    $objWriter->writeElement('formula2', $dv->getFormula2());
603
                }
604
605 2
                $objWriter->endElement();
606
            }
607
608 2
            $objWriter->endElement();
609
        }
610 55
    }
611
612
    /**
613
     * Write Hyperlinks.
614
     *
615
     * @param \PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter XML Writer
616
     * @param \PhpOffice\PhpSpreadsheet\Worksheet $pSheet Worksheet
617
     *
618
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
619
     */
620 55
    private function writeHyperlinks(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter = null, \PhpOffice\PhpSpreadsheet\Worksheet $pSheet = null)
621
    {
622
        // Hyperlink collection
623 55
        $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...
624
625
        // Relation ID
626 55
        $relationId = 1;
627
628
        // Write hyperlinks?
629 55
        if (!empty($hyperlinkCollection)) {
630 9
            $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...
631
632 9
            foreach ($hyperlinkCollection as $coordinate => $hyperlink) {
633 9
                $objWriter->startElement('hyperlink');
634
635 9
                $objWriter->writeAttribute('ref', $coordinate);
636 9
                if (!$hyperlink->isInternal()) {
637 9
                    $objWriter->writeAttribute('r:id', 'rId_hyperlink_' . $relationId);
638 9
                    ++$relationId;
639
                } else {
640 7
                    $objWriter->writeAttribute('location', str_replace('sheet://', '', $hyperlink->getUrl()));
641
                }
642
643 9
                if ($hyperlink->getTooltip() != '') {
644 6
                    $objWriter->writeAttribute('tooltip', $hyperlink->getTooltip());
645
                }
646
647 9
                $objWriter->endElement();
648
            }
649
650 9
            $objWriter->endElement();
651
        }
652 55
    }
653
654
    /**
655
     * Write ProtectedRanges.
656
     *
657
     * @param \PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter XML Writer
658
     * @param \PhpOffice\PhpSpreadsheet\Worksheet $pSheet Worksheet
659
     *
660
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
661
     */
662 55
    private function writeProtectedRanges(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter = null, \PhpOffice\PhpSpreadsheet\Worksheet $pSheet = null)
663
    {
664 55
        if (count($pSheet->getProtectedCells()) > 0) {
665
            // protectedRanges
666 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...
667
668
            // Loop protectedRanges
669 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...
670
                // protectedRange
671 6
                $objWriter->startElement('protectedRange');
672 6
                $objWriter->writeAttribute('name', 'p' . md5($protectedCell));
673 6
                $objWriter->writeAttribute('sqref', $protectedCell);
674 6
                if (!empty($passwordHash)) {
675 6
                    $objWriter->writeAttribute('password', $passwordHash);
676
                }
677 6
                $objWriter->endElement();
678
            }
679
680 6
            $objWriter->endElement();
681
        }
682 55
    }
683
684
    /**
685
     * Write MergeCells.
686
     *
687
     * @param \PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter XML Writer
688
     * @param \PhpOffice\PhpSpreadsheet\Worksheet $pSheet Worksheet
689
     *
690
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
691
     */
692 55
    private function writeMergeCells(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter = null, \PhpOffice\PhpSpreadsheet\Worksheet $pSheet = null)
693
    {
694 55
        if (count($pSheet->getMergeCells()) > 0) {
695
            // mergeCells
696 11
            $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...
697
698
            // Loop mergeCells
699 11
            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...
700
                // mergeCell
701 11
                $objWriter->startElement('mergeCell');
702 11
                $objWriter->writeAttribute('ref', $mergeCell);
703 11
                $objWriter->endElement();
704
            }
705
706 11
            $objWriter->endElement();
707
        }
708 55
    }
709
710
    /**
711
     * Write PrintOptions.
712
     *
713
     * @param \PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter XML Writer
714
     * @param \PhpOffice\PhpSpreadsheet\Worksheet $pSheet Worksheet
715
     *
716
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
717
     */
718 55
    private function writePrintOptions(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter = null, \PhpOffice\PhpSpreadsheet\Worksheet $pSheet = null)
719
    {
720
        // printOptions
721 55
        $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...
722
723 55
        $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...
724 55
        $objWriter->writeAttribute('gridLinesSet', 'true');
725
726 55
        if ($pSheet->getPageSetup()->getHorizontalCentered()) {
727
            $objWriter->writeAttribute('horizontalCentered', 'true');
728
        }
729
730 55
        if ($pSheet->getPageSetup()->getVerticalCentered()) {
731
            $objWriter->writeAttribute('verticalCentered', 'true');
732
        }
733
734 55
        $objWriter->endElement();
735 55
    }
736
737
    /**
738
     * Write PageMargins.
739
     *
740
     * @param \PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter XML Writer
741
     * @param \PhpOffice\PhpSpreadsheet\Worksheet $pSheet Worksheet
742
     *
743
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
744
     */
745 55
    private function writePageMargins(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter = null, \PhpOffice\PhpSpreadsheet\Worksheet $pSheet = null)
746
    {
747
        // pageMargins
748 55
        $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...
749 55
        $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...
750 55
        $objWriter->writeAttribute('right', \PhpOffice\PhpSpreadsheet\Shared\StringHelper::formatNumber($pSheet->getPageMargins()->getRight()));
751 55
        $objWriter->writeAttribute('top', \PhpOffice\PhpSpreadsheet\Shared\StringHelper::formatNumber($pSheet->getPageMargins()->getTop()));
752 55
        $objWriter->writeAttribute('bottom', \PhpOffice\PhpSpreadsheet\Shared\StringHelper::formatNumber($pSheet->getPageMargins()->getBottom()));
753 55
        $objWriter->writeAttribute('header', \PhpOffice\PhpSpreadsheet\Shared\StringHelper::formatNumber($pSheet->getPageMargins()->getHeader()));
754 55
        $objWriter->writeAttribute('footer', \PhpOffice\PhpSpreadsheet\Shared\StringHelper::formatNumber($pSheet->getPageMargins()->getFooter()));
755 55
        $objWriter->endElement();
756 55
    }
757
758
    /**
759
     * Write AutoFilter.
760
     *
761
     * @param \PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter XML Writer
762
     * @param \PhpOffice\PhpSpreadsheet\Worksheet $pSheet Worksheet
763
     *
764
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
765
     */
766 55
    private function writeAutoFilter(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter = null, \PhpOffice\PhpSpreadsheet\Worksheet $pSheet = null)
767
    {
768 55
        $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...
769 55
        if (!empty($autoFilterRange)) {
770
            // autoFilter
771 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...
772
773
            // Strip any worksheet reference from the filter coordinates
774 3
            $range = \PhpOffice\PhpSpreadsheet\Cell::splitRange($autoFilterRange);
775 3
            $range = $range[0];
776
            //    Strip any worksheet ref
777 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...
778
                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...
779
            }
780 3
            $range = implode(':', $range);
781
782 3
            $objWriter->writeAttribute('ref', str_replace('$', '', $range));
783
784 3
            $columns = $pSheet->getAutoFilter()->getColumns();
785 3
            if (count($columns > 0)) {
786 3
                foreach ($columns as $columnID => $column) {
787 2
                    $rules = $column->getRules();
788 2
                    if (count($rules) > 0) {
789 2
                        $objWriter->startElement('filterColumn');
790 2
                        $objWriter->writeAttribute('colId', $pSheet->getAutoFilter()->getColumnOffset($columnID));
791
792 2
                        $objWriter->startElement($column->getFilterType());
793 2
                        if ($column->getJoin() == \PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column::AUTOFILTER_COLUMN_JOIN_AND) {
794 1
                            $objWriter->writeAttribute('and', 1);
795
                        }
796
797 2
                        foreach ($rules as $rule) {
798 2
                            if (($column->getFilterType() === \PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column::AUTOFILTER_FILTERTYPE_FILTER) &&
799 2
                                ($rule->getOperator() === \PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule::AUTOFILTER_COLUMN_RULE_EQUAL) &&
800 2
                                ($rule->getValue() === '')) {
801
                                //    Filter rule for Blanks
802 1
                                $objWriter->writeAttribute('blank', 1);
803 2
                            } elseif ($rule->getRuleType() === \PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule::AUTOFILTER_RULETYPE_DYNAMICFILTER) {
804
                                //    Dynamic Filter Rule
805 1
                                $objWriter->writeAttribute('type', $rule->getGrouping());
806 1
                                $val = $column->getAttribute('val');
807 1
                                if ($val !== null) {
808 1
                                    $objWriter->writeAttribute('val', $val);
809
                                }
810 1
                                $maxVal = $column->getAttribute('maxVal');
811 1
                                if ($maxVal !== null) {
812 1
                                    $objWriter->writeAttribute('maxVal', $maxVal);
813
                                }
814 2
                            } elseif ($rule->getRuleType() === \PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule::AUTOFILTER_RULETYPE_TOPTENFILTER) {
815
                                //    Top 10 Filter Rule
816
                                $objWriter->writeAttribute('val', $rule->getValue());
817
                                $objWriter->writeAttribute('percent', (($rule->getOperator() === \PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule::AUTOFILTER_COLUMN_RULE_TOPTEN_PERCENT) ? '1' : '0'));
818
                                $objWriter->writeAttribute('top', (($rule->getGrouping() === \PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule::AUTOFILTER_COLUMN_RULE_TOPTEN_TOP) ? '1' : '0'));
819
                            } else {
820
                                //    Filter, DateGroupItem or CustomFilter
821 2
                                $objWriter->startElement($rule->getRuleType());
822
823 2
                                if ($rule->getOperator() !== \PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule::AUTOFILTER_COLUMN_RULE_EQUAL) {
824 1
                                    $objWriter->writeAttribute('operator', $rule->getOperator());
825
                                }
826 2
                                if ($rule->getRuleType() === \PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule::AUTOFILTER_RULETYPE_DATEGROUP) {
827
                                    // Date Group filters
828 1
                                    foreach ($rule->getValue() as $key => $value) {
829 1
                                        if ($value > '') {
830 1
                                            $objWriter->writeAttribute($key, $value);
831
                                        }
832
                                    }
833 1
                                    $objWriter->writeAttribute('dateTimeGrouping', $rule->getGrouping());
834
                                } else {
835 2
                                    $objWriter->writeAttribute('val', $rule->getValue());
836
                                }
837
838 2
                                $objWriter->endElement();
839
                            }
840
                        }
841
842 2
                        $objWriter->endElement();
843
844 2
                        $objWriter->endElement();
845
                    }
846
                }
847
            }
848 3
            $objWriter->endElement();
849
        }
850 55
    }
851
852
    /**
853
     * Write PageSetup.
854
     *
855
     * @param \PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter XML Writer
856
     * @param \PhpOffice\PhpSpreadsheet\Worksheet $pSheet Worksheet
857
     *
858
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
859
     */
860 55
    private function writePageSetup(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter = null, \PhpOffice\PhpSpreadsheet\Worksheet $pSheet = null)
861
    {
862
        // pageSetup
863 55
        $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...
864 55
        $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...
865 55
        $objWriter->writeAttribute('orientation', $pSheet->getPageSetup()->getOrientation());
866
867 55
        if (!is_null($pSheet->getPageSetup()->getScale())) {
868 55
            $objWriter->writeAttribute('scale', $pSheet->getPageSetup()->getScale());
869
        }
870 55
        if (!is_null($pSheet->getPageSetup()->getFitToHeight())) {
871 55
            $objWriter->writeAttribute('fitToHeight', $pSheet->getPageSetup()->getFitToHeight());
872
        } else {
873
            $objWriter->writeAttribute('fitToHeight', '0');
874
        }
875 55
        if (!is_null($pSheet->getPageSetup()->getFitToWidth())) {
876 55
            $objWriter->writeAttribute('fitToWidth', $pSheet->getPageSetup()->getFitToWidth());
877
        } else {
878
            $objWriter->writeAttribute('fitToWidth', '0');
879
        }
880 55
        if (!is_null($pSheet->getPageSetup()->getFirstPageNumber())) {
881
            $objWriter->writeAttribute('firstPageNumber', $pSheet->getPageSetup()->getFirstPageNumber());
882
            $objWriter->writeAttribute('useFirstPageNumber', '1');
883
        }
884
885 55
        $objWriter->endElement();
886 55
    }
887
888
    /**
889
     * Write Header / Footer.
890
     *
891
     * @param \PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter XML Writer
892
     * @param \PhpOffice\PhpSpreadsheet\Worksheet $pSheet Worksheet
893
     *
894
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
895
     */
896 55
    private function writeHeaderFooter(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter = null, \PhpOffice\PhpSpreadsheet\Worksheet $pSheet = null)
897
    {
898
        // headerFooter
899 55
        $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...
900 55
        $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...
901 55
        $objWriter->writeAttribute('differentFirst', ($pSheet->getHeaderFooter()->getDifferentFirst() ? 'true' : 'false'));
902 55
        $objWriter->writeAttribute('scaleWithDoc', ($pSheet->getHeaderFooter()->getScaleWithDocument() ? 'true' : 'false'));
903 55
        $objWriter->writeAttribute('alignWithMargins', ($pSheet->getHeaderFooter()->getAlignWithMargins() ? 'true' : 'false'));
904
905 55
        $objWriter->writeElement('oddHeader', $pSheet->getHeaderFooter()->getOddHeader());
906 55
        $objWriter->writeElement('oddFooter', $pSheet->getHeaderFooter()->getOddFooter());
907 55
        $objWriter->writeElement('evenHeader', $pSheet->getHeaderFooter()->getEvenHeader());
908 55
        $objWriter->writeElement('evenFooter', $pSheet->getHeaderFooter()->getEvenFooter());
909 55
        $objWriter->writeElement('firstHeader', $pSheet->getHeaderFooter()->getFirstHeader());
910 55
        $objWriter->writeElement('firstFooter', $pSheet->getHeaderFooter()->getFirstFooter());
911 55
        $objWriter->endElement();
912 55
    }
913
914
    /**
915
     * Write Breaks.
916
     *
917
     * @param \PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter XML Writer
918
     * @param \PhpOffice\PhpSpreadsheet\Worksheet $pSheet Worksheet
919
     *
920
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
921
     */
922 55
    private function writeBreaks(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter = null, \PhpOffice\PhpSpreadsheet\Worksheet $pSheet = null)
923
    {
924
        // Get row and column breaks
925 55
        $aRowBreaks = [];
926 55
        $aColumnBreaks = [];
927 55
        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...
928 1
            if ($breakType == \PhpOffice\PhpSpreadsheet\Worksheet::BREAK_ROW) {
929 1
                $aRowBreaks[] = $cell;
930
            } elseif ($breakType == \PhpOffice\PhpSpreadsheet\Worksheet::BREAK_COLUMN) {
931
                $aColumnBreaks[] = $cell;
932
            }
933
        }
934
935
        // rowBreaks
936 55 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...
937 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...
938 1
            $objWriter->writeAttribute('count', count($aRowBreaks));
939 1
            $objWriter->writeAttribute('manualBreakCount', count($aRowBreaks));
940
941 1
            foreach ($aRowBreaks as $cell) {
942 1
                $coords = \PhpOffice\PhpSpreadsheet\Cell::coordinateFromString($cell);
943
944 1
                $objWriter->startElement('brk');
945 1
                $objWriter->writeAttribute('id', $coords[1]);
946 1
                $objWriter->writeAttribute('man', '1');
947 1
                $objWriter->endElement();
948
            }
949
950 1
            $objWriter->endElement();
951
        }
952
953
        // Second, write column breaks
954 55 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...
955
            $objWriter->startElement('colBreaks');
956
            $objWriter->writeAttribute('count', count($aColumnBreaks));
957
            $objWriter->writeAttribute('manualBreakCount', count($aColumnBreaks));
958
959
            foreach ($aColumnBreaks as $cell) {
960
                $coords = \PhpOffice\PhpSpreadsheet\Cell::coordinateFromString($cell);
961
962
                $objWriter->startElement('brk');
963
                $objWriter->writeAttribute('id', \PhpOffice\PhpSpreadsheet\Cell::columnIndexFromString($coords[0]) - 1);
964
                $objWriter->writeAttribute('man', '1');
965
                $objWriter->endElement();
966
            }
967
968
            $objWriter->endElement();
969
        }
970 55
    }
971
972
    /**
973
     * Write SheetData.
974
     *
975
     * @param \PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter XML Writer
976
     * @param \PhpOffice\PhpSpreadsheet\Worksheet $pSheet Worksheet
977
     * @param string[] $pStringTable String table
978
     *
979
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
980
     */
981 55
    private function writeSheetData(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter = null, \PhpOffice\PhpSpreadsheet\Worksheet $pSheet = null, $pStringTable = null)
982
    {
983 55
        if (is_array($pStringTable)) {
984
            // Flipped stringtable, for faster index searching
985 55
            $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\Ods, 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...
986
987
            // sheetData
988 55
            $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...
989
990
            // Get column count
991 55
            $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...
992
993
            // Highest row number
994 55
            $highestRow = $pSheet->getHighestRow();
995
996
            // Loop through cells
997 55
            $cellsByRow = [];
998 55
            foreach ($pSheet->getCellCollection() as $cellID) {
999 55
                $cellAddress = \PhpOffice\PhpSpreadsheet\Cell::coordinateFromString($cellID);
1000 55
                $cellsByRow[$cellAddress[1]][] = $cellID;
1001
            }
1002
1003 55
            $currentRow = 0;
1004 55
            while ($currentRow++ < $highestRow) {
1005
                // Get row dimension
1006 55
                $rowDimension = $pSheet->getRowDimension($currentRow);
1007
1008
                // Write current row?
1009 55
                $writeCurrentRow = isset($cellsByRow[$currentRow]) || $rowDimension->getRowHeight() >= 0 || $rowDimension->getVisible() == false || $rowDimension->getCollapsed() == true || $rowDimension->getOutlineLevel() > 0 || $rowDimension->getXfIndex() !== null;
1010
1011 55
                if ($writeCurrentRow) {
1012
                    // Start a new row
1013 55
                    $objWriter->startElement('row');
1014 55
                    $objWriter->writeAttribute('r', $currentRow);
1015 55
                    $objWriter->writeAttribute('spans', '1:' . $colCount);
1016
1017
                    // Row dimensions
1018 55
                    if ($rowDimension->getRowHeight() >= 0) {
1019 5
                        $objWriter->writeAttribute('customHeight', '1');
1020 5
                        $objWriter->writeAttribute('ht', \PhpOffice\PhpSpreadsheet\Shared\StringHelper::formatNumber($rowDimension->getRowHeight()));
1021
                    }
1022
1023
                    // Row visibility
1024 55
                    if ($rowDimension->getVisible() == false) {
1025 2
                        $objWriter->writeAttribute('hidden', 'true');
1026
                    }
1027
1028
                    // Collapsed
1029 55
                    if ($rowDimension->getCollapsed() == true) {
1030
                        $objWriter->writeAttribute('collapsed', 'true');
1031
                    }
1032
1033
                    // Outline level
1034 55
                    if ($rowDimension->getOutlineLevel() > 0) {
1035
                        $objWriter->writeAttribute('outlineLevel', $rowDimension->getOutlineLevel());
1036
                    }
1037
1038
                    // Style
1039 55
                    if ($rowDimension->getXfIndex() !== null) {
1040
                        $objWriter->writeAttribute('s', $rowDimension->getXfIndex());
1041
                        $objWriter->writeAttribute('customFormat', '1');
1042
                    }
1043
1044
                    // Write cells
1045 55
                    if (isset($cellsByRow[$currentRow])) {
1046 55
                        foreach ($cellsByRow[$currentRow] as $cellAddress) {
1047
                            // Write cell
1048 55
                            $this->writeCell($objWriter, $pSheet, $cellAddress, $pStringTable, $aFlippedStringTable);
1049
                        }
1050
                    }
1051
1052
                    // End row
1053 55
                    $objWriter->endElement();
1054
                }
1055
            }
1056
1057 55
            $objWriter->endElement();
1058
        } else {
1059
            throw new \PhpOffice\PhpSpreadsheet\Writer\Exception('Invalid parameters passed.');
1060
        }
1061 55
    }
1062
1063
    /**
1064
     * Write Cell.
1065
     *
1066
     * @param \PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter XML Writer
1067
     * @param \PhpOffice\PhpSpreadsheet\Worksheet $pSheet Worksheet
1068
     * @param \PhpOffice\PhpSpreadsheet\Cell $pCellAddress Cell Address
1069
     * @param string[] $pStringTable String table
1070
     * @param string[] $pFlippedStringTable String table (flipped), for faster index searching
1071
     *
1072
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
1073
     */
1074 55
    private function writeCell(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter = null, \PhpOffice\PhpSpreadsheet\Worksheet $pSheet = null, $pCellAddress = null, $pStringTable = null, $pFlippedStringTable = null)
1075
    {
1076 55
        if (is_array($pStringTable) && is_array($pFlippedStringTable)) {
1077
            // Cell
1078 55
            $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...
1079 55
            $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...
1080 55
            $objWriter->writeAttribute('r', $pCellAddress);
1081
1082
            // Sheet styles
1083 55
            if ($pCell->getXfIndex() != '') {
1084 31
                $objWriter->writeAttribute('s', $pCell->getXfIndex());
1085
            }
1086
1087
            // If cell value is supplied, write cell value
1088 55
            $cellValue = $pCell->getValue();
1089 55
            if (is_object($cellValue) || $cellValue !== '') {
1090
                // Map type
1091 55
                $mappedType = $pCell->getDataType();
1092
1093
                // Write data type depending on its type
1094 55
                switch (strtolower($mappedType)) {
1095 55
                    case 'inlinestr':    // Inline string
1096 54
                    case 's':            // String
1097 47
                    case 'b':            // Boolean
1098 50
                        $objWriter->writeAttribute('t', $mappedType);
1099 50
                        break;
1100 45
                    case 'f':            // Formula
1101 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\Html, PhpOffice\PhpSpreadsheet\Writer\Ods, PhpOffice\PhpSpreadsheet\Writer\Pdf\Core, PhpOffice\PhpSpreadsheet\Writer\Pdf\DomPDF, PhpOffice\PhpSpreadsheet\Writer\Pdf\MPDF, PhpOffice\PhpSpreadsheet\Writer\Pdf\TcPDF, PhpOffice\PhpSpreadsheet\Writer\Xls, 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...
1102 18
                            $pCell->getCalculatedValue() : $cellValue;
1103 18
                        if (is_string($calculatedValue)) {
1104 13
                            $objWriter->writeAttribute('t', 'str');
1105
                        }
1106 18
                        break;
1107 44
                    case 'e':            // Error
1108
                        $objWriter->writeAttribute('t', $mappedType);
1109
                }
1110
1111
                // Write data depending on its type
1112 55
                switch (strtolower($mappedType)) {
1113 55
                    case 'inlinestr':    // Inline string
1114 8
                        if (!$cellValue instanceof \PhpOffice\PhpSpreadsheet\RichText) {
1115
                            $objWriter->writeElement('t', \PhpOffice\PhpSpreadsheet\Shared\StringHelper::controlCharacterPHP2OOXML(htmlspecialchars($cellValue)));
1116
                        } elseif ($cellValue instanceof \PhpOffice\PhpSpreadsheet\RichText) {
1117 8
                            $objWriter->startElement('is');
1118 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\Ods, 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...
1119 8
                            $objWriter->endElement();
1120
                        }
1121
1122 8
                        break;
1123 54
                    case 's':            // String
1124 49
                        if (!$cellValue instanceof \PhpOffice\PhpSpreadsheet\RichText) {
1125 49
                            if (isset($pFlippedStringTable[$cellValue])) {
1126 49
                                $objWriter->writeElement('v', $pFlippedStringTable[$cellValue]);
1127
                            }
1128
                        } elseif ($cellValue instanceof \PhpOffice\PhpSpreadsheet\RichText) {
1129 2
                            $objWriter->writeElement('v', $pFlippedStringTable[$cellValue->getHashCode()]);
1130
                        }
1131
1132 49
                        break;
1133 47
                    case 'f':            // Formula
1134 18
                        $attributes = $pCell->getFormulaAttributes();
1135 18
                        if ($attributes['t'] == 'array') {
1136
                            $objWriter->startElement('f');
1137
                            $objWriter->writeAttribute('t', 'array');
1138
                            $objWriter->writeAttribute('ref', $pCellAddress);
1139
                            $objWriter->writeAttribute('aca', '1');
1140
                            $objWriter->writeAttribute('ca', '1');
1141
                            $objWriter->text(substr($cellValue, 1));
1142
                            $objWriter->endElement();
1143
                        } else {
1144 18
                            $objWriter->writeElement('f', substr($cellValue, 1));
1145
                        }
1146 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...
1147 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\Html, PhpOffice\PhpSpreadsheet\Writer\Ods, PhpOffice\PhpSpreadsheet\Writer\Pdf\Core, PhpOffice\PhpSpreadsheet\Writer\Pdf\DomPDF, PhpOffice\PhpSpreadsheet\Writer\Pdf\MPDF, PhpOffice\PhpSpreadsheet\Writer\Pdf\TcPDF, PhpOffice\PhpSpreadsheet\Writer\Xls, 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...
1148 18
                                if (!is_array($calculatedValue) && substr($calculatedValue, 0, 1) != '#') {
1149 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...
1150
                                } else {
1151 4
                                    $objWriter->writeElement('v', '0');
1152
                                }
1153
                            } else {
1154
                                $objWriter->writeElement('v', '0');
1155
                            }
1156
                        }
1157 18
                        break;
1158 46
                    case 'n':            // Numeric
1159
                        // force point as decimal separator in case current locale uses comma
1160 39
                        $objWriter->writeElement('v', str_replace(',', '.', $cellValue));
1161 39
                        break;
1162 25
                    case 'b':            // Boolean
1163 8
                        $objWriter->writeElement('v', ($cellValue ? '1' : '0'));
1164 8
                        break;
1165 21
                    case 'e':            // Error
1166
                        if (substr($cellValue, 0, 1) == '=') {
1167
                            $objWriter->writeElement('f', substr($cellValue, 1));
1168
                            $objWriter->writeElement('v', substr($cellValue, 1));
1169
                        } else {
1170
                            $objWriter->writeElement('v', $cellValue);
1171
                        }
1172
1173
                        break;
1174
                }
1175
            }
1176
1177 55
            $objWriter->endElement();
1178
        } else {
1179
            throw new \PhpOffice\PhpSpreadsheet\Writer\Exception('Invalid parameters passed.');
1180
        }
1181 55
    }
1182
1183
    /**
1184
     * Write Drawings.
1185
     *
1186
     * @param \PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter XML Writer
1187
     * @param \PhpOffice\PhpSpreadsheet\Worksheet $pSheet Worksheet
1188
     * @param bool $includeCharts Flag indicating if we should include drawing details for charts
1189
     *
1190
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
1191
     */
1192 55
    private function writeDrawings(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter = null, \PhpOffice\PhpSpreadsheet\Worksheet $pSheet = null, $includeCharts = false)
1193
    {
1194 55
        $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...
1195
        // If sheet contains drawings, add the relationships
1196 55
        if (($pSheet->getDrawingCollection()->count() > 0) ||
1197 47
            ($chartCount > 0)) {
1198 22
            $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...
1199 22
            $objWriter->writeAttribute('r:id', 'rId1');
1200 22
            $objWriter->endElement();
1201
        }
1202 55
    }
1203
1204
    /**
1205
     * Write LegacyDrawing.
1206
     *
1207
     * @param \PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter XML Writer
1208
     * @param \PhpOffice\PhpSpreadsheet\Worksheet $pSheet Worksheet
1209
     *
1210
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
1211
     */
1212 55 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...
1213
    {
1214
        // If sheet contains comments, add the relationships
1215 55
        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...
1216 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...
1217 9
            $objWriter->writeAttribute('r:id', 'rId_comments_vml1');
1218 9
            $objWriter->endElement();
1219
        }
1220 55
    }
1221
1222
    /**
1223
     * Write LegacyDrawingHF.
1224
     *
1225
     * @param \PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter XML Writer
1226
     * @param \PhpOffice\PhpSpreadsheet\Worksheet $pSheet Worksheet
1227
     *
1228
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
1229
     */
1230 55 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...
1231
    {
1232
        // If sheet contains images, add the relationships
1233 55
        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...
1234 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...
1235 1
            $objWriter->writeAttribute('r:id', 'rId_headerfooter_vml1');
1236 1
            $objWriter->endElement();
1237
        }
1238 55
    }
1239
}
1240