GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( f8e5a3...1b3ca1 )
by Mewes
04:44
created

SheetWrapper::configureMappings()   B

Complexity

Conditions 3
Paths 1

Size

Total Lines 78
Code Lines 68

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 78
rs 8.9019
c 0
b 0
f 0
cc 3
eloc 68
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace MewesK\TwigSpreadsheetBundle\Wrapper;
4
5
use PhpOffice\PhpSpreadsheet\Exception;
6
use PhpOffice\PhpSpreadsheet\Worksheet;
7
use PhpOffice\PhpSpreadsheet\Worksheet\ColumnDimension;
8
use PhpOffice\PhpSpreadsheet\Worksheet\RowDimension;
9
10
/**
11
 * Class SheetWrapper.
12
 */
13
class SheetWrapper extends BaseWrapper
14
{
15
    /**
16
     * @var int
17
     */
18
    const COLUMN_DEFAULT = 0;
19
    /**
20
     * @var int
21
     */
22
    const ROW_DEFAULT = 1;
23
24
    /**
25
     * @var DocumentWrapper
26
     */
27
    protected $documentWrapper;
28
29
    /**
30
     * @var Worksheet
31
     */
32
    protected $object;
33
    /**
34
     * @var null|int
35
     */
36
    protected $row;
37
    /**
38
     * @var null|int
39
     */
40
    protected $column;
41
42
    /**
43
     * SheetWrapper constructor.
44
     *
45
     * @param array             $context
46
     * @param \Twig_Environment $environment
47
     * @param DocumentWrapper   $documentWrapper
48
     */
49
    public function __construct(array $context, \Twig_Environment $environment, DocumentWrapper $documentWrapper)
50
    {
51
        parent::__construct($context, $environment);
52
53
        $this->documentWrapper = $documentWrapper;
54
55
        $this->object = null;
56
        $this->row = null;
57
        $this->column = null;
58
    }
59
60
    /**
61
     * @param int|string|null $index
62
     * @param array           $properties
63
     *
64
     * @throws \PhpOffice\PhpSpreadsheet\Exception
65
     * @throws \RuntimeException
66
     */
67
    public function start($index, array $properties = [])
68
    {
69
        if (is_int($index) && $index < $this->documentWrapper->getObject()->getSheetCount()) {
70
            $this->object = $this->documentWrapper->getObject()->setActiveSheetIndex($index);
71
        } elseif (is_string($index)) {
72
            if (!$this->documentWrapper->getObject()->sheetNameExists($index)) {
73
                // create new sheet with a name
74
                $this->documentWrapper->getObject()->createSheet()->setTitle($index);
75
            }
76
            $this->object = $this->documentWrapper->getObject()->setActiveSheetIndexByName($index);
77
        } else {
78
            // create new sheet without a name
79
            $this->documentWrapper->getObject()->createSheet();
80
            $this->object = $this->documentWrapper->getObject()->setActiveSheetIndex(0);
81
        }
82
83
        $this->attributes['index'] = $index;
84
        $this->attributes['properties'] = $properties;
85
86
        $this->setProperties($properties, $this->mappings);
87
    }
88
89
    /**
90
     * @throws \PhpOffice\PhpSpreadsheet\Exception
91
     */
92
    public function end()
93
    {
94
        // auto-size columns
95
        if (
96
            isset($this->attributes['properties']['columnDimension']) &&
97
            is_array($this->attributes['properties']['columnDimension'])
98
        ) {
99
            /**
100
             * @var array $columnDimension
101
             */
102
            $columnDimension = $this->attributes['properties']['columnDimension'];
103
            foreach ($columnDimension as $key => $value) {
104
                if (isset($value['autoSize'])) {
105
                    if ('default' === $key) {
106
                        try {
107
                            $cellIterator = $this->object->getRowIterator()->current()->getCellIterator();
108
                            $cellIterator->setIterateOnlyExistingCells(true);
109
110
                            foreach ($cellIterator as $cell) {
111
                                $this->object->getColumnDimension($cell->getColumn())->setAutoSize($value['autoSize']);
112
                            }
113
                        } catch (Exception $e) {
114
                            // ignore exceptions thrown when no cells are defined
115
                        }
116
                    } else {
117
                        $this->object->getColumnDimension($key)->setAutoSize($value['autoSize']);
118
                    }
119
                }
120
            }
121
        }
122
123
        $this->object = null;
124
        $this->attributes = [];
125
        $this->row = null;
126
    }
127
128
    public function increaseRow()
129
    {
130
        $this->row = $this->row === null ? self::ROW_DEFAULT : $this->row + 1;
131
    }
132
133
    public function increaseColumn()
134
    {
135
        $this->column = $this->column === null ? self::COLUMN_DEFAULT : $this->column + 1;
136
    }
137
138
    /**
139
     * @return Worksheet
140
     */
141
    public function getObject(): Worksheet
142
    {
143
        return $this->object;
144
    }
145
146
    /**
147
     * @param Worksheet $object
148
     */
149
    public function setObject(Worksheet $object)
150
    {
151
        $this->object = $object;
152
    }
153
154
    /**
155
     * @return int|null
156
     */
157
    public function getRow()
158
    {
159
        return $this->row;
160
    }
161
162
    /**
163
     * @param int|null $row
164
     */
165
    public function setRow($row)
166
    {
167
        $this->row = $row;
168
    }
169
170
    /**
171
     * @return int|null
172
     */
173
    public function getColumn()
174
    {
175
        return $this->column;
176
    }
177
178
    /**
179
     * @param int|null $column
180
     */
181
    public function setColumn($column)
182
    {
183
        $this->column = $column;
184
    }
185
186
    /**
187
     * @return array
188
     */
189
    protected function configureMappings(): array
190
    {
191
        return [
192
            'autoFilter' => function ($value) { $this->object->setAutoFilter($value); },
193
            'columnDimension' => [
194
                '__multi' => function ($column = 'default'): ColumnDimension {
195
                    return $column === 'default' ?
196
                        $this->object->getDefaultColumnDimension() :
197
                        $this->object->getColumnDimension($column);
198
                },
199
                'autoSize' => function ($value, ColumnDimension $object) { $object->setAutoSize($value); },
200
                'collapsed' => function ($value, ColumnDimension $object) { $object->setCollapsed($value); },
201
                'columnIndex' => function ($value, ColumnDimension $object) { $object->setColumnIndex($value); },
202
                'outlineLevel' => function ($value, ColumnDimension $object) { $object->setOutlineLevel($value); },
203
                'visible' => function ($value, ColumnDimension $object) { $object->setVisible($value); },
204
                'width' => function ($value, ColumnDimension $object) { $object->setWidth($value); },
205
                'xfIndex' => function ($value, ColumnDimension $object) { $object->setXfIndex($value); },
206
            ],
207
            'pageMargins' => [
208
                'top' => function ($value) { $this->object->getPageMargins()->setTop($value); },
209
                'bottom' => function ($value) { $this->object->getPageMargins()->setBottom($value); },
210
                'left' => function ($value) { $this->object->getPageMargins()->setLeft($value); },
211
                'right' => function ($value) { $this->object->getPageMargins()->setRight($value); },
212
                'header' => function ($value) { $this->object->getPageMargins()->setHeader($value); },
213
                'footer' => function ($value) { $this->object->getPageMargins()->setFooter($value); },
214
            ],
215
            'pageSetup' => [
216
                'fitToHeight' => function ($value) { $this->object->getPageSetup()->setFitToHeight($value); },
217
                'fitToPage' => function ($value) { $this->object->getPageSetup()->setFitToPage($value); },
218
                'fitToWidth' => function ($value) { $this->object->getPageSetup()->setFitToWidth($value); },
219
                'horizontalCentered' => function ($value) { $this->object->getPageSetup()->setHorizontalCentered($value); },
220
                'orientation' => function ($value) { $this->object->getPageSetup()->setOrientation($value); },
221
                'paperSize' => function ($value) { $this->object->getPageSetup()->setPaperSize($value); },
222
                'printArea' => function ($value) { $this->object->getPageSetup()->setPrintArea($value); },
223
                'scale' => function ($value) { $this->object->getPageSetup()->setScale($value); },
224
                'verticalCentered' => function ($value) { $this->object->getPageSetup()->setVerticalCentered($value); },
225
            ],
226
            'printGridlines' => function ($value) { $this->object->setPrintGridlines($value); },
227
            'protection' => [
228
                'autoFilter' => function ($value) { $this->object->getProtection()->setAutoFilter($value); },
229
                'deleteColumns' => function ($value) { $this->object->getProtection()->setDeleteColumns($value); },
230
                'deleteRows' => function ($value) { $this->object->getProtection()->setDeleteRows($value); },
231
                'formatCells' => function ($value) { $this->object->getProtection()->setFormatCells($value); },
232
                'formatColumns' => function ($value) { $this->object->getProtection()->setFormatColumns($value); },
233
                'formatRows' => function ($value) { $this->object->getProtection()->setFormatRows($value); },
234
                'insertColumns' => function ($value) { $this->object->getProtection()->setInsertColumns($value); },
235
                'insertHyperlinks' => function ($value) { $this->object->getProtection()->setInsertHyperlinks($value); },
236
                'insertRows' => function ($value) { $this->object->getProtection()->setInsertRows($value); },
237
                'objects' => function ($value) { $this->object->getProtection()->setObjects($value); },
238
                'password' => function ($value) { $this->object->getProtection()->setPassword($value); },
239
                'pivotTables' => function ($value) { $this->object->getProtection()->setPivotTables($value); },
240
                'scenarios' => function ($value) { $this->object->getProtection()->setScenarios($value); },
241
                'selectLockedCells' => function ($value) { $this->object->getProtection()->setSelectLockedCells($value); },
242
                'selectUnlockedCells' => function ($value) { $this->object->getProtection()->setSelectUnlockedCells($value); },
243
                'sheet' => function ($value) { $this->object->getProtection()->setSheet($value); },
244
                'sort' => function ($value) { $this->object->getProtection()->setSort($value); },
245
            ],
246
            'rightToLeft' => function ($value) { $this->object->setRightToLeft($value); },
247
            'rowDimension' => [
248
                '__multi' => function ($column = 'default'): RowDimension {
249
                    return $column === 'default' ?
250
                        $this->object->getDefaultRowDimension() :
251
                        $this->object->getRowDimension($column);
252
                },
253
                'collapsed' => function ($value, RowDimension $object) { $object->setCollapsed($value); },
254
                'outlineLevel' => function ($value, RowDimension $object) { $object->setOutlineLevel($value); },
255
                'rowHeight' => function ($value, RowDimension $object) { $object->setRowHeight($value); },
256
                'rowIndex' => function ($value, RowDimension $object) { $object->setRowIndex($value); },
257
                'visible' => function ($value, RowDimension $object) { $object->setVisible($value); },
258
                'xfIndex' => function ($value, RowDimension $object) { $object->setXfIndex($value); },
259
                'zeroHeight' => function ($value, RowDimension $object) { $object->setZeroHeight($value); },
260
            ],
261
            'sheetState' => function ($value) { $this->object->setSheetState($value); },
262
            'showGridlines' => function ($value) { $this->object->setShowGridlines($value); },
263
            'tabColor' => function ($value) { $this->object->getTabColor()->setRGB($value); },
264
            'zoomScale' => function ($value) { $this->object->getSheetView()->setZoomScale($value); },
265
        ];
266
    }
267
}
268