Completed
Push — develop ( 870d86...2ad559 )
by Adrien
42:24 queued 15:50
created

DataSeriesValues::refresh()   C

Complexity

Conditions 12
Paths 8

Size

Total Lines 46
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 30
CRAP Score 12

Importance

Changes 0
Metric Value
cc 12
eloc 31
nc 8
nop 2
dl 0
loc 46
rs 5.15
c 0
b 0
f 0
ccs 30
cts 30
cp 1
crap 12

How to fix   Complexity   

Long Method

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

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

Commonly applied refactorings include:

1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Chart;
4
5
/**
6
 * Copyright (c) 2006 - 2016 PhpSpreadsheet.
7
 *
8
 * This library is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU Lesser General Public
10
 * License as published by the Free Software Foundation; either
11
 * version 2.1 of the License, or (at your option) any later version.
12
 *
13
 * This library is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
 * Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with this library; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
 *
22
 * @category    PhpSpreadsheet
23
 *
24
 * @copyright    Copyright (c) 2006 - 2016 PhpSpreadsheet (https://github.com/PHPOffice/PhpSpreadsheet)
25
 * @license        http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
26
 */
27
class DataSeriesValues
28
{
29
    const DATASERIES_TYPE_STRING = 'String';
30
    const DATASERIES_TYPE_NUMBER = 'Number';
31
32
    private static $dataTypeValues = [
33
        self::DATASERIES_TYPE_STRING,
34
        self::DATASERIES_TYPE_NUMBER,
35
    ];
36
37
    /**
38
     * Series Data Type.
39
     *
40
     * @var string
41
     */
42
    private $dataType;
43
44
    /**
45
     * Series Data Source.
46
     *
47
     * @var string
48
     */
49
    private $dataSource;
50
51
    /**
52
     * Format Code.
53
     *
54
     * @var string
55
     */
56
    private $formatCode;
57
58
    /**
59
     * Series Point Marker.
60
     *
61
     * @var string
62
     */
63
    private $pointMarker;
64
65
    /**
66
     * Point Count (The number of datapoints in the dataseries).
67
     *
68
     * @var int
69
     */
70
    private $pointCount = 0;
71
72
    /**
73
     * Data Values.
74
     *
75
     * @var array of mixed
76
     */
77
    private $dataValues = [];
78
79
    /**
80
     * Create a new DataSeriesValues object.
81
     *
82
     * @param mixed $dataType
83
     * @param string $dataSource
84
     * @param null|mixed $formatCode
85
     * @param mixed $pointCount
86
     * @param mixed $dataValues
87
     * @param null|mixed $marker
88
     */
89 17
    public function __construct($dataType = self::DATASERIES_TYPE_NUMBER, $dataSource = null, $formatCode = null, $pointCount = 0, $dataValues = [], $marker = null)
90
    {
91 17
        $this->setDataType($dataType);
92 17
        $this->dataSource = $dataSource;
93 17
        $this->formatCode = $formatCode;
94 17
        $this->pointCount = $pointCount;
95 17
        $this->dataValues = $dataValues;
0 ignored issues
show
Documentation Bug introduced by
It seems like $dataValues of type * is incompatible with the declared type array of property $dataValues.

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...
96 17
        $this->pointMarker = $marker;
97 17
    }
98
99
    /**
100
     * Get Series Data Type.
101
     *
102
     * @return string
103
     */
104 1
    public function getDataType()
105
    {
106 1
        return $this->dataType;
107
    }
108
109
    /**
110
     * Set Series Data Type.
111
     *
112
     * @param string $dataType Datatype of this data series
113
     *                                Typical values are:
114
     *                                    \PhpOffice\PhpSpreadsheet\Chart\DataSeriesValues::DATASERIES_TYPE_STRING
115
     *                                        Normally used for axis point values
116
     *                                    \PhpOffice\PhpSpreadsheet\Chart\DataSeriesValues::DATASERIES_TYPE_NUMBER
117
     *                                        Normally used for chart data values
118
     *
119
     * @throws Exception
120
     *
121
     * @return DataSeriesValues
122
     */
123 17
    public function setDataType($dataType)
124
    {
125 17
        if (!in_array($dataType, self::$dataTypeValues)) {
126 1
            throw new Exception('Invalid datatype for chart data series values');
127
        }
128 17
        $this->dataType = $dataType;
129
130 17
        return $this;
131
    }
132
133
    /**
134
     * Get Series Data Source (formula).
135
     *
136
     * @return string
137
     */
138 13
    public function getDataSource()
139
    {
140 13
        return $this->dataSource;
141
    }
142
143
    /**
144
     * Set Series Data Source (formula).
145
     *
146
     * @param string $dataSource
147
     *
148
     * @return DataSeriesValues
149
     */
150
    public function setDataSource($dataSource)
151
    {
152
        $this->dataSource = $dataSource;
153
154
        return $this;
155
    }
156
157
    /**
158
     * Get Point Marker.
159
     *
160
     * @return string
161
     */
162 13
    public function getPointMarker()
163
    {
164 13
        return $this->pointMarker;
165
    }
166
167
    /**
168
     * Set Point Marker.
169
     *
170
     * @param string $marker
171
     *
172
     * @return DataSeriesValues
173
     */
174
    public function setPointMarker($marker)
175
    {
176
        $this->pointMarker = $marker;
177
178
        return $this;
179
    }
180
181
    /**
182
     * Get Series Format Code.
183
     *
184
     * @return string
185
     */
186 12
    public function getFormatCode()
187
    {
188 12
        return $this->formatCode;
189
    }
190
191
    /**
192
     * Set Series Format Code.
193
     *
194
     * @param string $formatCode
195
     *
196
     * @return DataSeriesValues
197
     */
198
    public function setFormatCode($formatCode)
199
    {
200
        $this->formatCode = $formatCode;
201
202
        return $this;
203
    }
204
205
    /**
206
     * Get Series Point Count.
207
     *
208
     * @return int
209
     */
210 13
    public function getPointCount()
211
    {
212 13
        return $this->pointCount;
213
    }
214
215
    /**
216
     * Identify if the Data Series is a multi-level or a simple series.
217
     *
218
     * @return bool|null
219
     */
220 13
    public function isMultiLevelSeries()
221
    {
222 13
        if (count($this->dataValues) > 0) {
223 13
            return is_array($this->dataValues[0]);
224
        }
225
226
        return null;
227
    }
228
229
    /**
230
     * Return the level count of a multi-level Data Series.
231
     *
232
     * @return int
233
     */
234 2
    public function multiLevelCount()
235
    {
236 2
        $levelCount = 0;
237 2
        foreach ($this->dataValues as $dataValueSet) {
238 2
            $levelCount = max($levelCount, count($dataValueSet));
239
        }
240
241 2
        return $levelCount;
242
    }
243
244
    /**
245
     * Get Series Data Values.
246
     *
247
     * @return array of mixed
248
     */
249 13
    public function getDataValues()
250
    {
251 13
        return $this->dataValues;
252
    }
253
254
    /**
255
     * Get the first Series Data value.
256
     *
257
     * @return mixed
258
     */
259
    public function getDataValue()
260
    {
261
        $count = count($this->dataValues);
262
        if ($count == 0) {
263
            return null;
264
        } elseif ($count == 1) {
265
            return $this->dataValues[0];
266
        }
267
268
        return $this->dataValues;
269
    }
270
271
    /**
272
     * Set Series Data Values.
273
     *
274
     * @param array $dataValues
275
     *
276
     * @return DataSeriesValues
277
     */
278
    public function setDataValues($dataValues)
279
    {
280
        $this->dataValues = \PhpOffice\PhpSpreadsheet\Calculation\Functions::flattenArray($dataValues);
281
        $this->pointCount = count($dataValues);
282
283
        return $this;
284
    }
285
286 13
    public function refresh(\PhpOffice\PhpSpreadsheet\Worksheet $worksheet, $flatten = true)
287
    {
288 13
        if ($this->dataSource !== null) {
289 13
            $calcEngine = \PhpOffice\PhpSpreadsheet\Calculation::getInstance($worksheet->getParent());
290 13
            $newDataValues = \PhpOffice\PhpSpreadsheet\Calculation::unwrapResult(
291 13
                $calcEngine->_calculateFormulaValue(
292 13
                    '=' . $this->dataSource,
293 13
                    null,
294 13
                    $worksheet->getCell('A1')
295
                )
296
            );
297 13
            if ($flatten) {
298 13
                $this->dataValues = \PhpOffice\PhpSpreadsheet\Calculation\Functions::flattenArray($newDataValues);
299 13
                foreach ($this->dataValues as &$dataValue) {
300 13
                    if ((!empty($dataValue)) && ($dataValue[0] == '#')) {
301 13
                        $dataValue = 0.0;
302
                    }
303
                }
304 13
                unset($dataValue);
305
            } else {
306 13
                $cellRange = explode('!', $this->dataSource);
307 13
                if (count($cellRange) > 1) {
308 13
                    list(, $cellRange) = $cellRange;
309
                }
310
311 13
                $dimensions = \PhpOffice\PhpSpreadsheet\Cell::rangeDimension(str_replace('$', '', $cellRange));
312 13
                if (($dimensions[0] == 1) || ($dimensions[1] == 1)) {
313 12
                    $this->dataValues = \PhpOffice\PhpSpreadsheet\Calculation\Functions::flattenArray($newDataValues);
314
                } else {
315 2
                    $newArray = array_values(array_shift($newDataValues));
316 2
                    foreach ($newArray as $i => $newDataSet) {
317 2
                        $newArray[$i] = [$newDataSet];
318
                    }
319
320 2
                    foreach ($newDataValues as $newDataSet) {
321 2
                        $i = 0;
322 2
                        foreach ($newDataSet as $newDataVal) {
323 2
                            array_unshift($newArray[$i++], $newDataVal);
324
                        }
325
                    }
326 2
                    $this->dataValues = $newArray;
327
                }
328
            }
329 13
            $this->pointCount = count($this->dataValues);
330
        }
331 13
    }
332
}
333