Completed
Push — develop ( 39b55d...e2e982 )
by Adrien
19:14
created

Content::writeCellSpan()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 8

Duplication

Lines 7
Ratio 63.64 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 3
dl 7
loc 11
ccs 0
cts 11
cp 0
crap 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Writer\Ods;
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
 * @author     Alexander Pervakov <[email protected]>
34
 */
35
class Content extends WriterPart
36
{
37
    const NUMBER_COLS_REPEATED_MAX = 1024;
38
    const NUMBER_ROWS_REPEATED_MAX = 1048576;
39
40
    /**
41
     * Write content.xml to XML format
42
     *
43
     * @param   \PhpOffice\PhpSpreadsheet\Spreadsheet                   $spreadsheet
44
     * @throws  \PhpOffice\PhpSpreadsheet\Writer\Exception
45
     * @return  string                     XML Output
46
     */
47
    public function write(\PhpOffice\PhpSpreadsheet\SpreadSheet $spreadsheet = null)
48
    {
49
        if (!$spreadsheet) {
50
            $spreadsheet = $this->getParentWriter()->getSpreadsheet(); /* @var $spreadsheet PhpSpreadsheet */
0 ignored issues
show
Unused Code introduced by
$spreadsheet 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...
Bug introduced by
It seems like you code against a concrete implementation and not the interface PhpOffice\PhpSpreadsheet\Writer\IWriter as the method getSpreadsheet() does only exist in the following implementations of said interface: PhpOffice\PhpSpreadsheet\Writer\Ods, PhpOffice\PhpSpreadsheet\Writer\Xlsx.

Let’s take a look at an example:

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

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

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

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

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

Available Fixes

  1. Change the type-hint for the parameter:

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

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

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
51
        }
52
53
        $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...
54 View Code Duplication
        if ($this->getParentWriter()->getUseDiskCaching()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
Bug introduced by
It seems like you code against a concrete implementation and not the interface PhpOffice\PhpSpreadsheet\Writer\IWriter as the method getUseDiskCaching() does only exist in the following implementations of said interface: PhpOffice\PhpSpreadsheet\Writer\BaseWriter, PhpOffice\PhpSpreadsheet\Writer\CSV, PhpOffice\PhpSpreadsheet\Writer\HTML, PhpOffice\PhpSpreadsheet\Writer\Ods, PhpOffice\PhpSpreadsheet\Writer\PDF\Core, PhpOffice\PhpSpreadsheet\Writer\PDF\DomPDF, PhpOffice\PhpSpreadsheet\Writer\PDF\MPDF, PhpOffice\PhpSpreadsheet\Writer\PDF\TcPDF, PhpOffice\PhpSpreadsheet\Writer\Xls, PhpOffice\PhpSpreadsheet\Writer\Xlsx.

Let’s take a look at an example:

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

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

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

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

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

Available Fixes

  1. Change the type-hint for the parameter:

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

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

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

Let’s take a look at an example:

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

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

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

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

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

Available Fixes

  1. Change the type-hint for the parameter:

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

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

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
56
        } else {
57
            $objWriter = new \PhpOffice\PhpSpreadsheet\Shared\XMLWriter(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter::STORAGE_MEMORY);
58
        }
59
60
        // XML header
61
        $objWriter->startDocument('1.0', 'UTF-8');
62
63
        // Content
64
        $objWriter->startElement('office:document-content');
65
        $objWriter->writeAttribute('xmlns:office', 'urn:oasis:names:tc:opendocument:xmlns:office:1.0');
66
        $objWriter->writeAttribute('xmlns:style', 'urn:oasis:names:tc:opendocument:xmlns:style:1.0');
67
        $objWriter->writeAttribute('xmlns:text', 'urn:oasis:names:tc:opendocument:xmlns:text:1.0');
68
        $objWriter->writeAttribute('xmlns:table', 'urn:oasis:names:tc:opendocument:xmlns:table:1.0');
69
        $objWriter->writeAttribute('xmlns:draw', 'urn:oasis:names:tc:opendocument:xmlns:drawing:1.0');
70
        $objWriter->writeAttribute('xmlns:fo', 'urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0');
71
        $objWriter->writeAttribute('xmlns:xlink', 'http://www.w3.org/1999/xlink');
72
        $objWriter->writeAttribute('xmlns:dc', 'http://purl.org/dc/elements/1.1/');
73
        $objWriter->writeAttribute('xmlns:meta', 'urn:oasis:names:tc:opendocument:xmlns:meta:1.0');
74
        $objWriter->writeAttribute('xmlns:number', 'urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0');
75
        $objWriter->writeAttribute('xmlns:presentation', 'urn:oasis:names:tc:opendocument:xmlns:presentation:1.0');
76
        $objWriter->writeAttribute('xmlns:svg', 'urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0');
77
        $objWriter->writeAttribute('xmlns:chart', 'urn:oasis:names:tc:opendocument:xmlns:chart:1.0');
78
        $objWriter->writeAttribute('xmlns:dr3d', 'urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0');
79
        $objWriter->writeAttribute('xmlns:math', 'http://www.w3.org/1998/Math/MathML');
80
        $objWriter->writeAttribute('xmlns:form', 'urn:oasis:names:tc:opendocument:xmlns:form:1.0');
81
        $objWriter->writeAttribute('xmlns:script', 'urn:oasis:names:tc:opendocument:xmlns:script:1.0');
82
        $objWriter->writeAttribute('xmlns:ooo', 'http://openoffice.org/2004/office');
83
        $objWriter->writeAttribute('xmlns:ooow', 'http://openoffice.org/2004/writer');
84
        $objWriter->writeAttribute('xmlns:oooc', 'http://openoffice.org/2004/calc');
85
        $objWriter->writeAttribute('xmlns:dom', 'http://www.w3.org/2001/xml-events');
86
        $objWriter->writeAttribute('xmlns:xforms', 'http://www.w3.org/2002/xforms');
87
        $objWriter->writeAttribute('xmlns:xsd', 'http://www.w3.org/2001/XMLSchema');
88
        $objWriter->writeAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
89
        $objWriter->writeAttribute('xmlns:rpt', 'http://openoffice.org/2005/report');
90
        $objWriter->writeAttribute('xmlns:of', 'urn:oasis:names:tc:opendocument:xmlns:of:1.2');
91
        $objWriter->writeAttribute('xmlns:xhtml', 'http://www.w3.org/1999/xhtml');
92
        $objWriter->writeAttribute('xmlns:grddl', 'http://www.w3.org/2003/g/data-view#');
93
        $objWriter->writeAttribute('xmlns:tableooo', 'http://openoffice.org/2009/table');
94
        $objWriter->writeAttribute('xmlns:field', 'urn:openoffice:names:experimental:ooo-ms-interop:xmlns:field:1.0');
95
        $objWriter->writeAttribute('xmlns:formx', 'urn:openoffice:names:experimental:ooxml-odf-interop:xmlns:form:1.0');
96
        $objWriter->writeAttribute('xmlns:css3t', 'http://www.w3.org/TR/css3-text/');
97
        $objWriter->writeAttribute('office:version', '1.2');
98
99
        $objWriter->writeElement('office:scripts');
100
        $objWriter->writeElement('office:font-face-decls');
101
        $objWriter->writeElement('office:automatic-styles');
102
103
        $objWriter->startElement('office:body');
104
        $objWriter->startElement('office:spreadsheet');
105
        $objWriter->writeElement('table:calculation-settings');
106
        $this->writeSheets($objWriter);
107
        $objWriter->writeElement('table:named-expressions');
108
        $objWriter->endElement();
109
        $objWriter->endElement();
110
        $objWriter->endElement();
111
112
        return $objWriter->getData();
113
    }
114
115
    /**
116
     * Write sheets
117
     *
118
     * @param \PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter
119
     */
120
    private function writeSheets(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter)
121
    {
122
        $spreadsheet = $this->getParentWriter()->getSpreadsheet(); /* @var $spreadsheet PhpSpreadsheet */
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface PhpOffice\PhpSpreadsheet\Writer\IWriter as the method getSpreadsheet() does only exist in the following implementations of said interface: PhpOffice\PhpSpreadsheet\Writer\Ods, PhpOffice\PhpSpreadsheet\Writer\Xlsx.

Let’s take a look at an example:

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

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

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

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

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

Available Fixes

  1. Change the type-hint for the parameter:

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

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

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
123
124
        $sheet_count = $spreadsheet->getSheetCount();
125
        for ($i = 0; $i < $sheet_count; ++$i) {
126
            $objWriter->startElement('table:table');
127
            $objWriter->writeAttribute('table:name', $spreadsheet->getSheet($i)->getTitle());
128
            $objWriter->writeElement('office:forms');
129
            $objWriter->startElement('table:table-column');
130
            $objWriter->writeAttribute('table:number-columns-repeated', self::NUMBER_COLS_REPEATED_MAX);
131
            $objWriter->endElement();
132
            $this->writeRows($objWriter, $spreadsheet->getSheet($i));
133
            $objWriter->endElement();
134
        }
135
    }
136
137
    /**
138
     * Write rows of the specified sheet
139
     *
140
     * @param \PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter
141
     * @param \PhpOffice\PhpSpreadsheet\Worksheet $sheet
142
     */
143
    private function writeRows(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter, \PhpOffice\PhpSpreadsheet\Worksheet $sheet)
144
    {
145
        $number_rows_repeated = self::NUMBER_ROWS_REPEATED_MAX;
146
        $span_row = 0;
147
        $rows = $sheet->getRowIterator();
148
        while ($rows->valid()) {
149
            --$number_rows_repeated;
150
            $row = $rows->current();
151
            if ($row->getCellIterator()->valid()) {
152
                if ($span_row) {
153
                    $objWriter->startElement('table:table-row');
154
                    if ($span_row > 1) {
155
                        $objWriter->writeAttribute('table:number-rows-repeated', $span_row);
156
                    }
157
                    $objWriter->startElement('table:table-cell');
158
                    $objWriter->writeAttribute('table:number-columns-repeated', self::NUMBER_COLS_REPEATED_MAX);
159
                    $objWriter->endElement();
160
                    $objWriter->endElement();
161
                    $span_row = 0;
162
                }
163
                $objWriter->startElement('table:table-row');
164
                $this->writeCells($objWriter, $row);
165
                $objWriter->endElement();
166
            } else {
167
                ++$span_row;
168
            }
169
            $rows->next();
170
        }
171
    }
172
173
    /**
174
     * Write cells of the specified row
175
     *
176
     * @param \PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter
177
     * @param \PhpOffice\PhpSpreadsheet\Worksheet\Row $row
178
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
179
     */
180
    private function writeCells(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter, \PhpOffice\PhpSpreadsheet\Worksheet\Row $row)
181
    {
182
        $number_cols_repeated = self::NUMBER_COLS_REPEATED_MAX;
183
        $prev_column = -1;
184
        $cells = $row->getCellIterator();
185
        while ($cells->valid()) {
186
            $cell = $cells->current();
187
            $column = \PhpOffice\PhpSpreadsheet\Cell::columnIndexFromString($cell->getColumn()) - 1;
188
189
            $this->writeCellSpan($objWriter, $column, $prev_column);
190
            $objWriter->startElement('table:table-cell');
191
192
            switch ($cell->getDataType()) {
193 View Code Duplication
                case \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_BOOL:
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...
194
                    $objWriter->writeAttribute('office:value-type', 'boolean');
195
                    $objWriter->writeAttribute('office:value', $cell->getValue());
196
                    $objWriter->writeElement('text:p', $cell->getValue());
197
                    break;
198
199
                case \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_ERROR:
200
                    throw new \PhpOffice\PhpSpreadsheet\Writer\Exception('Writing of error not implemented yet.');
201
                    break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
202
203
                case \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_FORMULA:
204
                    try {
205
                        $formula_value = $cell->getCalculatedValue();
206
                    } catch (Exception $e) {
0 ignored issues
show
Bug introduced by
The class PhpOffice\PhpSpreadsheet\Writer\Ods\Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
207
                        $formula_value = $cell->getValue();
208
                    }
209
                    $objWriter->writeAttribute('table:formula', 'of:' . $cell->getValue());
210
                    if (is_numeric($formula_value)) {
211
                        $objWriter->writeAttribute('office:value-type', 'float');
212
                    } else {
213
                        $objWriter->writeAttribute('office:value-type', 'string');
214
                    }
215
                    $objWriter->writeAttribute('office:value', $formula_value);
216
                    $objWriter->writeElement('text:p', $formula_value);
217
                    break;
218
219
                case \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_INLINE:
220
                    throw new \PhpOffice\PhpSpreadsheet\Writer\Exception('Writing of inline not implemented yet.');
221
                    break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
222
223 View Code Duplication
                case \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_NUMERIC:
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...
224
                    $objWriter->writeAttribute('office:value-type', 'float');
225
                    $objWriter->writeAttribute('office:value', $cell->getValue());
226
                    $objWriter->writeElement('text:p', $cell->getValue());
227
                    break;
228
229
                case \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING:
230
                    $objWriter->writeAttribute('office:value-type', 'string');
231
                    $objWriter->writeElement('text:p', $cell->getValue());
232
                    break;
233
            }
234
            Cell\Comment::write($objWriter, $cell);
0 ignored issues
show
Bug introduced by
It seems like $cell defined by $cells->current() on line 186 can be null; however, PhpOffice\PhpSpreadsheet...s\Cell\Comment::write() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
235
            $objWriter->endElement();
236
            $prev_column = $column;
237
            $cells->next();
238
        }
239
        $number_cols_repeated = $number_cols_repeated - $prev_column - 1;
240 View Code Duplication
        if ($number_cols_repeated > 0) {
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...
241
            if ($number_cols_repeated > 1) {
242
                $objWriter->startElement('table:table-cell');
243
                $objWriter->writeAttribute('table:number-columns-repeated', $number_cols_repeated);
244
                $objWriter->endElement();
245
            } else {
246
                $objWriter->writeElement('table:table-cell');
247
            }
248
        }
249
    }
250
251
    /**
252
     * Write span
253
     *
254
     * @param \PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter
255
     * @param int $curColumn
256
     * @param int $prevColumn
257
     */
258
    private function writeCellSpan(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter, $curColumn, $prevColumn)
259
    {
260
        $diff = $curColumn - $prevColumn - 1;
261 View Code Duplication
        if (1 === $diff) {
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...
262
            $objWriter->writeElement('table:table-cell');
263
        } elseif ($diff > 1) {
264
            $objWriter->startElement('table:table-cell');
265
            $objWriter->writeAttribute('table:number-columns-repeated', $diff);
266
            $objWriter->endElement();
267
        }
268
    }
269
}
270