Failed Conditions
Pull Request — master (#715)
by
unknown
02:29
created

WorksheetManager::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 22
c 0
b 0
f 0
ccs 13
cts 13
cp 1
rs 9.568
cc 1
nc 1
nop 8
crap 1

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace Box\Spout\Writer\XLSX\Manager;
4
5
use Box\Spout\Common\Entity\Cell;
6
use Box\Spout\Common\Entity\Row;
7
use Box\Spout\Common\Entity\Style\Style;
8
use Box\Spout\Common\Exception\InvalidArgumentException;
9
use Box\Spout\Common\Exception\IOException;
10
use Box\Spout\Common\Helper\Escaper\XLSX as XLSXEscaper;
11
use Box\Spout\Common\Helper\StringHelper;
12
use Box\Spout\Common\Manager\OptionsManagerInterface;
13
use Box\Spout\Writer\Common\Creator\InternalEntityFactory;
14
use Box\Spout\Writer\Common\Entity\Options;
15
use Box\Spout\Writer\Common\Entity\Worksheet;
16
use Box\Spout\Writer\Common\Helper\CellHelper;
17
use Box\Spout\Writer\Common\Manager\ManagesCellSize;
18
use Box\Spout\Writer\Common\Manager\RowManager;
19
use Box\Spout\Writer\Common\Manager\Style\StyleMerger;
20
use Box\Spout\Writer\Common\Manager\WorksheetManagerInterface;
21
use Box\Spout\Writer\XLSX\Manager\Style\StyleManager;
22
23
/**
24
 * Class WorksheetManager
25
 * XLSX worksheet manager, providing the interfaces to work with XLSX worksheets.
26
 */
27
class WorksheetManager implements WorksheetManagerInterface
28
{
29
    use ManagesCellSize;
30
31
    /**
32
     * Maximum number of characters a cell can contain
33
     * @see https://support.office.com/en-us/article/Excel-specifications-and-limits-16c69c74-3d6a-4aaf-ba35-e6eb276e8eaa [Excel 2007]
34
     * @see https://support.office.com/en-us/article/Excel-specifications-and-limits-1672b34d-7043-467e-8e27-269d656771c3 [Excel 2010]
35
     * @see https://support.office.com/en-us/article/Excel-specifications-and-limits-ca36e2dc-1f09-4620-b726-67c00b05040f [Excel 2013/2016]
36
     */
37
    const MAX_CHARACTERS_PER_CELL = 32767;
38
39
    const SHEET_XML_FILE_HEADER = <<<'EOD'
40
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
41
<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
42
EOD;
43
44
    /** @var bool Whether inline or shared strings should be used */
45
    protected $shouldUseInlineStrings;
46
47
    /** @var RowManager Manages rows */
48
    private $rowManager;
49
50
    /** @var StyleManager Manages styles */
51
    private $styleManager;
52
53
    /** @var StyleMerger Helper to merge styles together */
54
    private $styleMerger;
55
56
    /** @var SharedStringsManager Helper to write shared strings */
57
    private $sharedStringsManager;
58
59
    /** @var XLSXEscaper Strings escaper */
60
    private $stringsEscaper;
61
62
    /** @var StringHelper String helper */
63
    private $stringHelper;
64
65
    /** @var InternalEntityFactory Factory to create entities */
66
    private $entityFactory;
67
68
    /**
69
     * WorksheetManager constructor.
70
     *
71
     * @param OptionsManagerInterface $optionsManager
72
     * @param RowManager $rowManager
73
     * @param StyleManager $styleManager
74
     * @param StyleMerger $styleMerger
75
     * @param SharedStringsManager $sharedStringsManager
76
     * @param XLSXEscaper $stringsEscaper
77
     * @param StringHelper $stringHelper
78
     * @param InternalEntityFactory $entityFactory
79
     */
80 46
    public function __construct(
81
        OptionsManagerInterface $optionsManager,
82
        RowManager $rowManager,
83
        StyleManager $styleManager,
84
        StyleMerger $styleMerger,
85
        SharedStringsManager $sharedStringsManager,
86
        XLSXEscaper $stringsEscaper,
87
        StringHelper $stringHelper,
88
        InternalEntityFactory $entityFactory
89
    ) {
90 46
        $this->shouldUseInlineStrings = $optionsManager->getOption(Options::SHOULD_USE_INLINE_STRINGS);
91 46
        $this->setDefaultColumnWidth($optionsManager->getOption(Options::DEFAULT_COLUMN_WIDTH));
92 46
        $this->setDefaultRowHeight($optionsManager->getOption(Options::DEFAULT_ROW_HEIGHT));
93 46
        $this->columnWidths = $optionsManager->getOption(Options::COLUMN_WIDTHS) ?? [];
0 ignored issues
show
Documentation Bug introduced by
It seems like $optionsManager->getOpti...LUMN_WIDTHS) ?? array() of type * is incompatible with the declared type array of property $columnWidths.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
94 46
        $this->rowManager = $rowManager;
95 46
        $this->styleManager = $styleManager;
96 46
        $this->styleMerger = $styleMerger;
97 46
        $this->sharedStringsManager = $sharedStringsManager;
98 46
        $this->stringsEscaper = $stringsEscaper;
99 46
        $this->stringHelper = $stringHelper;
100 46
        $this->entityFactory = $entityFactory;
101 46
    }
102
103
    /**
104
     * @return SharedStringsManager
105
     */
106 42
    public function getSharedStringsManager()
107
    {
108 42
        return $this->sharedStringsManager;
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114 46
    public function startSheet(Worksheet $worksheet)
115
    {
116 46
        $sheetFilePointer = \fopen($worksheet->getFilePath(), 'w');
117 46
        $this->throwIfSheetFilePointerIsNotAvailable($sheetFilePointer);
118
119 46
        $worksheet->setFilePointer($sheetFilePointer);
120
121 46
        \fwrite($sheetFilePointer, self::SHEET_XML_FILE_HEADER);
122 46
    }
123
124
    /**
125
     * Writes the sheet data header
126
     *
127
     * @param Worksheet $worksheet The worksheet to add the row to
128
     * @return void
129
     */
130 42
    private function ensureSheetDataStated(Worksheet $worksheet)
131
    {
132 42
        if (!$worksheet->getSheetDataStarted()) {
133 42
            $worksheetFilePointer = $worksheet->getFilePointer();
134 42
            \fwrite($worksheetFilePointer, $this->getXMLFragmentForDefaultCellSizing());
135 42
            \fwrite($worksheetFilePointer, $this->getXMLFragmentForColumnWidths());
136 42
            \fwrite($worksheetFilePointer, '<sheetData>');
137 42
            $worksheet->setSheetDataStarted(true);
138
        }
139 42
    }
140
141
    /**
142
     * Checks if the sheet has been sucessfully created. Throws an exception if not.
143
     *
144
     * @param bool|resource $sheetFilePointer Pointer to the sheet data file or FALSE if unable to open the file
145
     * @throws IOException If the sheet data file cannot be opened for writing
146
     * @return void
147
     */
148 46
    private function throwIfSheetFilePointerIsNotAvailable($sheetFilePointer)
149
    {
150 46
        if (!$sheetFilePointer) {
151
            throw new IOException('Unable to open sheet for writing.');
152
        }
153 46
    }
154
155
    /**
156
     * {@inheritdoc}
157
     */
158 39
    public function addRow(Worksheet $worksheet, Row $row)
159
    {
160 39
        if (!$this->rowManager->isEmpty($row)) {
161 39
            $this->addNonEmptyRow($worksheet, $row);
162
        }
163
164 38
        $worksheet->setLastWrittenRowIndex($worksheet->getLastWrittenRowIndex() + 1);
165 38
    }
166
167
    /**
168
     * Adds non empty row to the worksheet.
169
     *
170
     * @param Worksheet $worksheet The worksheet to add the row to
171
     * @param Row $row The row to be written
172
     * @throws InvalidArgumentException If a cell value's type is not supported
173
     * @throws IOException If the data cannot be written
174
     * @return void
175
     */
176 39
    private function addNonEmptyRow(Worksheet $worksheet, Row $row)
177
    {
178 39
        $this->ensureSheetDataStated($worksheet);
179 39
        $sheetFilePointer = $worksheet->getFilePointer();
180 39
        $rowStyle = $row->getStyle();
181 39
        $rowIndexOneBased = $worksheet->getLastWrittenRowIndex() + 1;
182 39
        $numCells = $row->getNumCells();
183
184 39
        $hasCustomHeight = $this->defaultRowHeight > 0 ? '1' : '0';
185 39
        $rowXML = "<row r=\"{$rowIndexOneBased}\" spans=\"1:{$numCells}\" customHeight=\"{$hasCustomHeight}\">";
186
187 39
        foreach ($row->getCells() as $columnIndexZeroBased => $cell) {
188 39
            $rowXML .= $this->applyStyleAndGetCellXML($cell, $rowStyle, $rowIndexOneBased, $columnIndexZeroBased);
189
        }
190
191 38
        $rowXML .= '</row>';
192
193 38
        $wasWriteSuccessful = \fwrite($sheetFilePointer, $rowXML);
194 38
        if ($wasWriteSuccessful === false) {
195
            throw new IOException("Unable to write data in {$worksheet->getFilePath()}");
196
        }
197 38
    }
198
199
    /**
200
     * Applies styles to the given style, merging the cell's style with its row's style
201
     * Then builds and returns xml for the cell.
202
     *
203
     * @param Cell $cell
204
     * @param Style $rowStyle
205
     * @param int $rowIndexOneBased
206
     * @param int $columnIndexZeroBased
207
     *
208
     * @throws InvalidArgumentException If the given value cannot be processed
209
     * @return string
210
     */
211 39
    private function applyStyleAndGetCellXML(Cell $cell, Style $rowStyle, $rowIndexOneBased, $columnIndexZeroBased)
212
    {
213
        // Apply row and extra styles
214 39
        $mergedCellAndRowStyle = $this->styleMerger->merge($cell->getStyle(), $rowStyle);
215 39
        $cell->setStyle($mergedCellAndRowStyle);
216 39
        $newCellStyle = $this->styleManager->applyExtraStylesIfNeeded($cell);
217
218 39
        $registeredStyle = $this->styleManager->registerStyle($newCellStyle);
219
220 39
        return $this->getCellXML($rowIndexOneBased, $columnIndexZeroBased, $cell, $registeredStyle->getId());
221
    }
222
223
    /**
224
     * Builds and returns xml for a single cell.
225
     *
226
     * @param int $rowIndexOneBased
227
     * @param int $columnIndexZeroBased
228
     * @param Cell $cell
229
     * @param int $styleId
230
     *
231
     * @throws InvalidArgumentException If the given value cannot be processed
232
     * @return string
233
     */
234 39
    private function getCellXML($rowIndexOneBased, $columnIndexZeroBased, Cell $cell, $styleId)
235
    {
236 39
        $columnLetters = CellHelper::getColumnLettersFromColumnIndex($columnIndexZeroBased);
237 39
        $cellXML = '<c r="' . $columnLetters . $rowIndexOneBased . '"';
238 39
        $cellXML .= ' s="' . $styleId . '"';
239
240 39
        if ($cell->isString()) {
241 36
            $cellXML .= $this->getCellXMLFragmentForNonEmptyString($cell->getValue());
242 6
        } elseif ($cell->isBoolean()) {
243 1
            $cellXML .= ' t="b"><v>' . (int) ($cell->getValue()) . '</v></c>';
244 6
        } elseif ($cell->isNumeric()) {
245 2
            $cellXML .= '><v>' . $cell->getValue() . '</v></c>';
246 5
        } elseif ($cell->isError() && is_string($cell->getValueEvenIfError())) {
247
            // only writes the error value if it's a string
248 1
            $cellXML .= ' t="e"><v>' . $cell->getValueEvenIfError() . '</v></c>';
249 4
        } elseif ($cell->isEmpty()) {
250 2
            if ($this->styleManager->shouldApplyStyleOnEmptyCell($styleId)) {
251 1
                $cellXML .= '/>';
252
            } else {
253
                // don't write empty cells that do no need styling
254
                // NOTE: not appending to $cellXML is the right behavior!!
255 2
                $cellXML = '';
256
            }
257
        } else {
258 2
            throw new InvalidArgumentException('Trying to add a value with an unsupported type: ' . \gettype($cell->getValue()));
259
        }
260
261 38
        return $cellXML;
262
    }
263
264
    /**
265
     * Returns the XML fragment for a cell containing a non empty string
266
     *
267
     * @param string $cellValue The cell value
268
     * @throws InvalidArgumentException If the string exceeds the maximum number of characters allowed per cell
269
     * @return string The XML fragment representing the cell
270
     */
271 36
    private function getCellXMLFragmentForNonEmptyString($cellValue)
272
    {
273 36
        if ($this->stringHelper->getStringLength($cellValue) > self::MAX_CHARACTERS_PER_CELL) {
274
            throw new InvalidArgumentException('Trying to add a value that exceeds the maximum number of characters allowed in a cell (32,767)');
275
        }
276
277 36
        if ($this->shouldUseInlineStrings) {
278 33
            $cellXMLFragment = ' t="inlineStr"><is><t>' . $this->stringsEscaper->escape($cellValue) . '</t></is></c>';
279
        } else {
280 3
            $sharedStringId = $this->sharedStringsManager->writeString($cellValue);
281 3
            $cellXMLFragment = ' t="s"><v>' . $sharedStringId . '</v></c>';
282
        }
283
284 36
        return $cellXMLFragment;
285
    }
286
287
    /**
288
     * Construct column width references xml to inject into worksheet xml file
289
     *
290
     * @return string
291
     */
292 42
    public function getXMLFragmentForColumnWidths()
293
    {
294 42
        if (empty($this->columnWidths)) {
295 38
            return '';
296
        }
297 4
        $xml = '<cols>';
298 4
        foreach ($this->columnWidths as $entry) {
299 4
            $xml .= '<col min="' . $entry[0] . '" max="' . $entry[1] . '" width="' . $entry[2] . '" customWidth="true"/>';
300
        }
301 4
        $xml .= '</cols>';
302
303 4
        return $xml;
304
    }
305
306
    /**
307
     * Constructs default row height and width xml to inject into worksheet xml file
308
     *
309
     * @return string
310
     */
311 42
    public function getXMLFragmentForDefaultCellSizing()
312
    {
313 42
        $rowHeightXml = empty($this->defaultRowHeight) ? '' : " defaultRowHeight=\"{$this->defaultRowHeight}\"";
314 42
        $colWidthXml = empty($this->defaultColumnWidth) ? '' : " defaultColWidth=\"{$this->defaultColumnWidth}\"";
315 42
        if (empty($colWidthXml) && empty($rowHeightXml)) {
316 42
            return '';
317
        }
318
        // Ensure that the required defaultRowHeight is set
319
        $rowHeightXml = empty($rowHeightXml) ? ' defaultRowHeight="0"' : $rowHeightXml;
320
321
        return "<sheetFormatPr{$colWidthXml}{$rowHeightXml}/>";
322
    }
323
324
    /**
325
     * {@inheritdoc}
326
     */
327 42
    public function close(Worksheet $worksheet)
328
    {
329 42
        $worksheetFilePointer = $worksheet->getFilePointer();
330
331 42
        if (!\is_resource($worksheetFilePointer)) {
332
            return;
333
        }
334 42
        $this->ensureSheetDataStated($worksheet);
335 42
        \fwrite($worksheetFilePointer, '</sheetData>');
336 42
        \fwrite($worksheetFilePointer, '</worksheet>');
337 42
        \fclose($worksheetFilePointer);
338 42
    }
339
}
340