Completed
Push — develop ( 2922a1...cfa1fe )
by Adrien
24:40
created

Worksheet::writePageSetup()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 18
nc 16
nop 2
dl 0
loc 27
rs 8.439
c 0
b 0
f 0
ccs 0
cts 24
cp 0
crap 30
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Writer\Excel2007;
4
5
/**
6
 * PhpSpreadsheet
7
 *
8
 * Copyright (c) 2006 - 2015 PhpSpreadsheet
9
 *
10
 * This library is free software; you can redistribute it and/or
11
 * modify it under the terms of the GNU Lesser General Public
12
 * License as published by the Free Software Foundation; either
13
 * version 2.1 of the License, or (at your option) any later version.
14
 *
15
 * This library is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18
 * Lesser General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Lesser General Public
21
 * License along with this library; if not, write to the Free Software
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
23
 *
24
 * @category   PhpSpreadsheet
25
 * @copyright  Copyright (c) 2006 - 2015 PhpSpreadsheet (https://github.com/PHPOffice/PhpSpreadsheet)
26
 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
27
 * @version    ##VERSION##, ##DATE##
28
 */
29
30
/**
31
 * @category   PhpSpreadsheet
32
 * @copyright  Copyright (c) 2006 - 2015 PhpSpreadsheet (https://github.com/PHPOffice/PhpSpreadsheet)
33
 */
34
class Worksheet extends WriterPart
35
{
36
    /**
37
     * Write worksheet to XML format
38
     *
39
     * @param    \PhpOffice\PhpSpreadsheet\Worksheet        $pSheet
40
     * @param    string[]                $pStringTable
41
     * @param    bool                    $includeCharts    Flag indicating if we should write charts
42
     * @throws    \PhpOffice\PhpSpreadsheet\Writer\Exception
43
     * @return    string                    XML Output
44
     */
45
    public function writeWorksheet($pSheet = null, $pStringTable = null, $includeCharts = false)
46
    {
47
        if (!is_null($pSheet)) {
48
            // Create XML writer
49
            $objWriter = null;
0 ignored issues
show
Unused Code introduced by
$objWriter is not used, you could remove the assignment.

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

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

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

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

Loading history...
50 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\Excel2007, PhpOffice\PhpSpreadsheet\Writer\Excel5, PhpOffice\PhpSpreadsheet\Writer\HTML, PhpOffice\PhpSpreadsheet\Writer\OpenDocument, PhpOffice\PhpSpreadsheet\Writer\PDF\Core, PhpOffice\PhpSpreadsheet\Writer\PDF\DomPDF, PhpOffice\PhpSpreadsheet\Writer\PDF\MPDF, PhpOffice\PhpSpreadsheet\Writer\PDF\TcPDF.

Let’s take a look at an example:

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

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

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

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

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

Available Fixes

  1. Change the type-hint for the parameter:

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

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

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

Let’s take a look at an example:

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

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

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

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

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

Available Fixes

  1. Change the type-hint for the parameter:

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

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

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

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

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

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

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

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

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

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

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

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

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
210
211
        // Sheet selected?
212
        $sheetSelected = false;
213
        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\Excel2007, PhpOffice\PhpSpreadsheet\Writer\OpenDocument.

Let’s take a look at an example:

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

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

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

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

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

Available Fixes

  1. Change the type-hint for the parameter:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
485
                    $objWriter->writeAttribute('sqref', $cellCoordinate);
486
487
                    // cfRule
488
                    $objWriter->startElement('cfRule');
489
                    $objWriter->writeAttribute('type', $conditional->getConditionType());
490
                    $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\Excel2007.

Let’s take a look at an example:

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

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

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

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

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

Available Fixes

  1. Change the type-hint for the parameter:

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

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

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
491
                    $objWriter->writeAttribute('priority', $id++);
492
493
                    if (($conditional->getConditionType() == \PhpOffice\PhpSpreadsheet\Style\Conditional::CONDITION_CELLIS || $conditional->getConditionType() == \PhpOffice\PhpSpreadsheet\Style\Conditional::CONDITION_CONTAINSTEXT)
494
                        && $conditional->getOperatorType() != \PhpOffice\PhpSpreadsheet\Style\Conditional::OPERATOR_NONE) {
495
                        $objWriter->writeAttribute('operator', $conditional->getOperatorType());
496
                    }
497
498
                    if ($conditional->getConditionType() == \PhpOffice\PhpSpreadsheet\Style\Conditional::CONDITION_CONTAINSTEXT
499
                        && !is_null($conditional->getText())) {
500
                        $objWriter->writeAttribute('text', $conditional->getText());
501
                    }
502
503
                    if ($conditional->getConditionType() == \PhpOffice\PhpSpreadsheet\Style\Conditional::CONDITION_CONTAINSTEXT
504
                        && $conditional->getOperatorType() == \PhpOffice\PhpSpreadsheet\Style\Conditional::OPERATOR_CONTAINSTEXT
505
                        && !is_null($conditional->getText())) {
506
                        $objWriter->writeElement('formula', 'NOT(ISERROR(SEARCH("' . $conditional->getText() . '",' . $cellCoordinate . ')))');
507 View Code Duplication
                    } elseif ($conditional->getConditionType() == \PhpOffice\PhpSpreadsheet\Style\Conditional::CONDITION_CONTAINSTEXT
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
508
                        && $conditional->getOperatorType() == \PhpOffice\PhpSpreadsheet\Style\Conditional::OPERATOR_BEGINSWITH
509
                        && !is_null($conditional->getText())) {
510
                        $objWriter->writeElement('formula', 'LEFT(' . $cellCoordinate . ',' . strlen($conditional->getText()) . ')="' . $conditional->getText() . '"');
511
                    } elseif ($conditional->getConditionType() == \PhpOffice\PhpSpreadsheet\Style\Conditional::CONDITION_CONTAINSTEXT
512
                        && $conditional->getOperatorType() == \PhpOffice\PhpSpreadsheet\Style\Conditional::OPERATOR_ENDSWITH
513
                        && !is_null($conditional->getText())) {
514
                        $objWriter->writeElement('formula', 'RIGHT(' . $cellCoordinate . ',' . strlen($conditional->getText()) . ')="' . $conditional->getText() . '"');
515 View Code Duplication
                    } elseif ($conditional->getConditionType() == \PhpOffice\PhpSpreadsheet\Style\Conditional::CONDITION_CONTAINSTEXT
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
516
                        && $conditional->getOperatorType() == \PhpOffice\PhpSpreadsheet\Style\Conditional::OPERATOR_NOTCONTAINS
517
                        && !is_null($conditional->getText())) {
518
                        $objWriter->writeElement('formula', 'ISERROR(SEARCH("' . $conditional->getText() . '",' . $cellCoordinate . '))');
519
                    } elseif ($conditional->getConditionType() == \PhpOffice\PhpSpreadsheet\Style\Conditional::CONDITION_CELLIS
520
                        || $conditional->getConditionType() == \PhpOffice\PhpSpreadsheet\Style\Conditional::CONDITION_CONTAINSTEXT
521
                        || $conditional->getConditionType() == \PhpOffice\PhpSpreadsheet\Style\Conditional::CONDITION_EXPRESSION) {
522
                        foreach ($conditional->getConditions() as $formula) {
523
                            // Formula
524
                            $objWriter->writeElement('formula', $formula);
525
                        }
526
                    }
527
528
                    $objWriter->endElement();
529
530
                    $objWriter->endElement();
531
                }
532
            }
533
        }
534
    }
535
536
    /**
537
     * Write DataValidations
538
     *
539
     * @param    \PhpOffice\PhpSpreadsheet\Shared\XMLWriter            $objWriter        XML Writer
540
     * @param    \PhpOffice\PhpSpreadsheet\Worksheet                    $pSheet            Worksheet
541
     * @throws    \PhpOffice\PhpSpreadsheet\Writer\Exception
542
     */
543
    private function writeDataValidations(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter = null, \PhpOffice\PhpSpreadsheet\Worksheet $pSheet = null)
544
    {
545
        // Datavalidation collection
546
        $dataValidationCollection = $pSheet->getDataValidationCollection();
0 ignored issues
show
Bug introduced by
It seems like $pSheet is not always an object, but can also be of type null. Maybe add an additional type check?

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
756
757
            // Strip any worksheet reference from the filter coordinates
758
            $range = \PhpOffice\PhpSpreadsheet\Cell::splitRange($autoFilterRange);
759
            $range = $range[0];
760
            //    Strip any worksheet ref
761 View Code Duplication
            if (strpos($range[0], '!') !== false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
762
                list($ws, $range[0]) = explode('!', $range[0]);
0 ignored issues
show
Unused Code introduced by
The assignment to $ws is unused. Consider omitting it like so list($first,,$third).

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

Consider the following code example.

<?php

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

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

print $a . " - " . $c;

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

Instead, the list call could have been.

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

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

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

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

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

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

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

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

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

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

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
909
            if ($breakType == \PhpOffice\PhpSpreadsheet\Worksheet::BREAK_ROW) {
910
                $aRowBreaks[] = $cell;
911
            } elseif ($breakType == \PhpOffice\PhpSpreadsheet\Worksheet::BREAK_COLUMN) {
912
                $aColumnBreaks[] = $cell;
913
            }
914
        }
915
916
        // rowBreaks
917 View Code Duplication
        if (!empty($aRowBreaks)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
918
            $objWriter->startElement('rowBreaks');
0 ignored issues
show
Bug introduced by
It seems like $objWriter is not always an object, but can also be of type null. Maybe add an additional type check?

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

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
919
            $objWriter->writeAttribute('count', count($aRowBreaks));
920
            $objWriter->writeAttribute('manualBreakCount', count($aRowBreaks));
921
922
            foreach ($aRowBreaks as $cell) {
923
                $coords = \PhpOffice\PhpSpreadsheet\Cell::coordinateFromString($cell);
924
925
                $objWriter->startElement('brk');
926
                $objWriter->writeAttribute('id', $coords[1]);
927
                $objWriter->writeAttribute('man', '1');
928
                $objWriter->endElement();
929
            }
930
931
            $objWriter->endElement();
932
        }
933
934
        // Second, write column breaks
935 View Code Duplication
        if (!empty($aColumnBreaks)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
936
            $objWriter->startElement('colBreaks');
937
            $objWriter->writeAttribute('count', count($aColumnBreaks));
938
            $objWriter->writeAttribute('manualBreakCount', count($aColumnBreaks));
939
940
            foreach ($aColumnBreaks as $cell) {
941
                $coords = \PhpOffice\PhpSpreadsheet\Cell::coordinateFromString($cell);
942
943
                $objWriter->startElement('brk');
944
                $objWriter->writeAttribute('id', \PhpOffice\PhpSpreadsheet\Cell::columnIndexFromString($coords[0]) - 1);
945
                $objWriter->writeAttribute('man', '1');
946
                $objWriter->endElement();
947
            }
948
949
            $objWriter->endElement();
950
        }
951
    }
952
953
    /**
954
     * Write SheetData
955
     *
956
     * @param    \PhpOffice\PhpSpreadsheet\Shared\XMLWriter        $objWriter        XML Writer
957
     * @param    \PhpOffice\PhpSpreadsheet\Worksheet                $pSheet            Worksheet
958
     * @param    string[]                        $pStringTable    String table
959
     * @throws    \PhpOffice\PhpSpreadsheet\Writer\Exception
960
     */
961
    private function writeSheetData(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter = null, \PhpOffice\PhpSpreadsheet\Worksheet $pSheet = null, $pStringTable = null)
962
    {
963
        if (is_array($pStringTable)) {
964
            // Flipped stringtable, for faster index searching
965
            $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\Excel2007, PhpOffice\PhpSpreadsheet\Writer\OpenDocument.

Let’s take a look at an example:

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

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

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

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

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

Available Fixes

  1. Change the type-hint for the parameter:

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

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

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

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

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

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

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

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

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
1059
            $objWriter->writeAttribute('r', $pCellAddress);
1060
1061
            // Sheet styles
1062
            if ($pCell->getXfIndex() != '') {
1063
                $objWriter->writeAttribute('s', $pCell->getXfIndex());
1064
            }
1065
1066
            // If cell value is supplied, write cell value
1067
            $cellValue = $pCell->getValue();
1068
            if (is_object($cellValue) || $cellValue !== '') {
1069
                // Map type
1070
                $mappedType = $pCell->getDataType();
1071
1072
                // Write data type depending on its type
1073
                switch (strtolower($mappedType)) {
1074
                    case 'inlinestr':    // Inline string
1075
                    case 's':            // String
1076
                    case 'b':            // Boolean
1077
                        $objWriter->writeAttribute('t', $mappedType);
1078
                        break;
1079
                    case 'f':            // Formula
1080
                        $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\Excel2007, PhpOffice\PhpSpreadsheet\Writer\Excel5, PhpOffice\PhpSpreadsheet\Writer\HTML, PhpOffice\PhpSpreadsheet\Writer\OpenDocument, PhpOffice\PhpSpreadsheet\Writer\PDF\Core, PhpOffice\PhpSpreadsheet\Writer\PDF\DomPDF, PhpOffice\PhpSpreadsheet\Writer\PDF\MPDF, PhpOffice\PhpSpreadsheet\Writer\PDF\TcPDF.

Let’s take a look at an example:

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

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

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

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

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

Available Fixes

  1. Change the type-hint for the parameter:

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

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

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
1081
                            $pCell->getCalculatedValue() : $cellValue;
1082
                        if (is_string($calculatedValue)) {
1083
                            $objWriter->writeAttribute('t', 'str');
1084
                        }
1085
                        break;
1086
                    case 'e':            // Error
1087
                        $objWriter->writeAttribute('t', $mappedType);
1088
                }
1089
1090
                // Write data depending on its type
1091
                switch (strtolower($mappedType)) {
1092
                    case 'inlinestr':    // Inline string
1093
                        if (!$cellValue instanceof \PhpOffice\PhpSpreadsheet\RichText) {
1094
                            $objWriter->writeElement('t', \PhpOffice\PhpSpreadsheet\Shared\StringHelper::controlCharacterPHP2OOXML(htmlspecialchars($cellValue)));
1095
                        } elseif ($cellValue instanceof \PhpOffice\PhpSpreadsheet\RichText) {
1096
                            $objWriter->startElement('is');
1097
                            $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\Excel2007, PhpOffice\PhpSpreadsheet\Writer\OpenDocument.

Let’s take a look at an example:

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

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

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

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

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

Available Fixes

  1. Change the type-hint for the parameter:

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

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

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
1098
                            $objWriter->endElement();
1099
                        }
1100
1101
                        break;
1102
                    case 's':            // String
1103
                        if (!$cellValue instanceof \PhpOffice\PhpSpreadsheet\RichText) {
1104
                            if (isset($pFlippedStringTable[$cellValue])) {
1105
                                $objWriter->writeElement('v', $pFlippedStringTable[$cellValue]);
1106
                            }
1107
                        } elseif ($cellValue instanceof \PhpOffice\PhpSpreadsheet\RichText) {
1108
                            $objWriter->writeElement('v', $pFlippedStringTable[$cellValue->getHashCode()]);
1109
                        }
1110
1111
                        break;
1112
                    case 'f':            // Formula
1113
                        $attributes = $pCell->getFormulaAttributes();
1114
                        if ($attributes['t'] == 'array') {
1115
                            $objWriter->startElement('f');
1116
                            $objWriter->writeAttribute('t', 'array');
1117
                            $objWriter->writeAttribute('ref', $pCellAddress);
1118
                            $objWriter->writeAttribute('aca', '1');
1119
                            $objWriter->writeAttribute('ca', '1');
1120
                            $objWriter->text(substr($cellValue, 1));
1121
                            $objWriter->endElement();
1122
                        } else {
1123
                            $objWriter->writeElement('f', substr($cellValue, 1));
1124
                        }
1125
                        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\Excel2007.

Let’s take a look at an example:

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

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

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

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

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

Available Fixes

  1. Change the type-hint for the parameter:

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

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

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
1126
                            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\Excel2007, PhpOffice\PhpSpreadsheet\Writer\Excel5, PhpOffice\PhpSpreadsheet\Writer\HTML, PhpOffice\PhpSpreadsheet\Writer\OpenDocument, PhpOffice\PhpSpreadsheet\Writer\PDF\Core, PhpOffice\PhpSpreadsheet\Writer\PDF\DomPDF, PhpOffice\PhpSpreadsheet\Writer\PDF\MPDF, PhpOffice\PhpSpreadsheet\Writer\PDF\TcPDF.

Let’s take a look at an example:

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

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

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

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

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

Available Fixes

  1. Change the type-hint for the parameter:

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

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

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

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

Let’s take a look at an example:

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

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

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

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

Available Fixes

  1. Check for existence of the variable explicitly:

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

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

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

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

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

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

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
1177
            $objWriter->writeAttribute('r:id', 'rId1');
1178
            $objWriter->endElement();
1179
        }
1180
    }
1181
1182
    /**
1183
     * Write LegacyDrawing
1184
     *
1185
     * @param    \PhpOffice\PhpSpreadsheet\Shared\XMLWriter        $objWriter        XML Writer
1186
     * @param    \PhpOffice\PhpSpreadsheet\Worksheet                $pSheet            Worksheet
1187
     * @throws    \PhpOffice\PhpSpreadsheet\Writer\Exception
1188
     */
1189 View Code Duplication
    private function writeLegacyDrawing(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter = null, \PhpOffice\PhpSpreadsheet\Worksheet $pSheet = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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

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

Loading history...
1190
    {
1191
        // If sheet contains comments, add the relationships
1192
        if (count($pSheet->getComments()) > 0) {
0 ignored issues
show
Bug introduced by
It seems like $pSheet is not always an object, but can also be of type null. Maybe add an additional type check?

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

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

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

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
1194
            $objWriter->writeAttribute('r:id', 'rId_comments_vml1');
1195
            $objWriter->endElement();
1196
        }
1197
    }
1198
1199
    /**
1200
     * Write LegacyDrawingHF
1201
     *
1202
     * @param    \PhpOffice\PhpSpreadsheet\Shared\XMLWriter        $objWriter        XML Writer
1203
     * @param    \PhpOffice\PhpSpreadsheet\Worksheet                $pSheet            Worksheet
1204
     * @throws    \PhpOffice\PhpSpreadsheet\Writer\Exception
1205
     */
1206 View Code Duplication
    private function writeLegacyDrawingHF(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter = null, \PhpOffice\PhpSpreadsheet\Worksheet $pSheet = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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

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

Loading history...
1207
    {
1208
        // If sheet contains images, add the relationships
1209
        if (count($pSheet->getHeaderFooter()->getImages()) > 0) {
0 ignored issues
show
Bug introduced by
It seems like $pSheet is not always an object, but can also be of type null. Maybe add an additional type check?

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

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

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

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