Failed Conditions
Push — develop ( a6bb49...7a4cbd )
by Adrien
36:15
created

DataSeries::__construct()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
nc 8
nop 9
dl 0
loc 24
ccs 17
cts 17
cp 1
crap 6
rs 8.9137
c 0
b 0
f 0

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 PhpOffice\PhpSpreadsheet\Chart;
4
5
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
6
7
class DataSeries
8
{
9
    const TYPE_BARCHART = 'barChart';
10
    const TYPE_BARCHART_3D = 'bar3DChart';
11
    const TYPE_LINECHART = 'lineChart';
12
    const TYPE_LINECHART_3D = 'line3DChart';
13
    const TYPE_AREACHART = 'areaChart';
14
    const TYPE_AREACHART_3D = 'area3DChart';
15
    const TYPE_PIECHART = 'pieChart';
16
    const TYPE_PIECHART_3D = 'pie3DChart';
17
    const TYPE_DOUGHNUTCHART = 'doughnutChart';
18
    const TYPE_DONUTCHART = self::TYPE_DOUGHNUTCHART; // Synonym
19
    const TYPE_SCATTERCHART = 'scatterChart';
20
    const TYPE_SURFACECHART = 'surfaceChart';
21
    const TYPE_SURFACECHART_3D = 'surface3DChart';
22
    const TYPE_RADARCHART = 'radarChart';
23
    const TYPE_BUBBLECHART = 'bubbleChart';
24
    const TYPE_STOCKCHART = 'stockChart';
25
    const TYPE_CANDLECHART = self::TYPE_STOCKCHART; // Synonym
26
27
    const GROUPING_CLUSTERED = 'clustered';
28
    const GROUPING_STACKED = 'stacked';
29
    const GROUPING_PERCENT_STACKED = 'percentStacked';
30
    const GROUPING_STANDARD = 'standard';
31
32
    const DIRECTION_BAR = 'bar';
33
    const DIRECTION_HORIZONTAL = self::DIRECTION_BAR;
34
    const DIRECTION_COL = 'col';
35
    const DIRECTION_COLUMN = self::DIRECTION_COL;
36
    const DIRECTION_VERTICAL = self::DIRECTION_COL;
37
38
    const STYLE_LINEMARKER = 'lineMarker';
39
    const STYLE_SMOOTHMARKER = 'smoothMarker';
40
    const STYLE_MARKER = 'marker';
41
    const STYLE_FILLED = 'filled';
42
43
    /**
44
     * Series Plot Type.
45
     *
46
     * @var string
47
     */
48
    private $plotType;
49
50
    /**
51
     * Plot Grouping Type.
52
     *
53
     * @var string
54
     */
55
    private $plotGrouping;
56
57
    /**
58
     * Plot Direction.
59
     *
60
     * @var string
61
     */
62
    private $plotDirection;
63
64
    /**
65
     * Plot Style.
66
     *
67
     * @var null|string
68
     */
69
    private $plotStyle;
70
71
    /**
72
     * Order of plots in Series.
73
     *
74
     * @var array of integer
75
     */
76
    private $plotOrder = [];
77
78
    /**
79
     * Plot Label.
80
     *
81
     * @var array of DataSeriesValues
82
     */
83
    private $plotLabel = [];
84
85
    /**
86
     * Plot Category.
87
     *
88
     * @var array of DataSeriesValues
89
     */
90
    private $plotCategory = [];
91
92
    /**
93
     * Smooth Line.
94
     *
95
     * @var bool
96
     */
97
    private $smoothLine;
98
99
    /**
100
     * Plot Values.
101
     *
102
     * @var array of DataSeriesValues
103
     */
104
    private $plotValues = [];
105
106
    /**
107
     * Create a new DataSeries.
108
     *
109
     * @param null|mixed $plotType
110
     * @param null|mixed $plotGrouping
111
     * @param int[] $plotOrder
112
     * @param DataSeriesValues[] $plotLabel
113
     * @param DataSeriesValues[] $plotCategory
114
     * @param DataSeriesValues[] $plotValues
115
     * @param null|string $plotDirection
116
     * @param bool $smoothLine
117
     * @param null|string $plotStyle
118
     */
119 15
    public function __construct($plotType = null, $plotGrouping = null, array $plotOrder = [], array $plotLabel = [], array $plotCategory = [], array $plotValues = [], $plotDirection = null, $smoothLine = false, $plotStyle = null)
120
    {
121 15
        $this->plotType = $plotType;
122 15
        $this->plotGrouping = $plotGrouping;
123 15
        $this->plotOrder = $plotOrder;
124 15
        $keys = array_keys($plotValues);
125 15
        $this->plotValues = $plotValues;
126 15
        if ((count($plotLabel) == 0) || ($plotLabel[$keys[0]] === null)) {
127 2
            $plotLabel[$keys[0]] = new DataSeriesValues();
128
        }
129 15
        $this->plotLabel = $plotLabel;
130
131 15
        if ((count($plotCategory) == 0) || ($plotCategory[$keys[0]] === null)) {
132 3
            $plotCategory[$keys[0]] = new DataSeriesValues();
133
        }
134 15
        $this->plotCategory = $plotCategory;
135
136 15
        $this->smoothLine = $smoothLine;
137 15
        $this->plotStyle = $plotStyle;
138
139 15
        if ($plotDirection === null) {
140 13
            $plotDirection = self::DIRECTION_COL;
141
        }
142 15
        $this->plotDirection = $plotDirection;
143 15
    }
144
145
    /**
146
     * Get Plot Type.
147
     *
148
     * @return string
149
     */
150 14
    public function getPlotType()
151
    {
152 14
        return $this->plotType;
153
    }
154
155
    /**
156
     * Set Plot Type.
157
     *
158
     * @param string $plotType
159
     *
160
     * @return DataSeries
161
     */
162
    public function setPlotType($plotType)
163
    {
164
        $this->plotType = $plotType;
165
166
        return $this;
167
    }
168
169
    /**
170
     * Get Plot Grouping Type.
171
     *
172
     * @return string
173
     */
174 14
    public function getPlotGrouping()
175
    {
176 14
        return $this->plotGrouping;
177
    }
178
179
    /**
180
     * Set Plot Grouping Type.
181
     *
182
     * @param string $groupingType
183
     *
184
     * @return DataSeries
185
     */
186
    public function setPlotGrouping($groupingType)
187
    {
188
        $this->plotGrouping = $groupingType;
189
190
        return $this;
191
    }
192
193
    /**
194
     * Get Plot Direction.
195
     *
196
     * @return string
197
     */
198 8
    public function getPlotDirection()
199
    {
200 8
        return $this->plotDirection;
201
    }
202
203
    /**
204
     * Set Plot Direction.
205
     *
206
     * @param string $plotDirection
207
     *
208
     * @return DataSeries
209
     */
210 9
    public function setPlotDirection($plotDirection)
211
    {
212 9
        $this->plotDirection = $plotDirection;
213
214 9
        return $this;
215
    }
216
217
    /**
218
     * Get Plot Order.
219
     *
220
     * @return int[]
221
     */
222 14
    public function getPlotOrder()
223
    {
224 14
        return $this->plotOrder;
225
    }
226
227
    /**
228
     * Get Plot Labels.
229
     *
230
     * @return array of DataSeriesValues
231
     */
232
    public function getPlotLabels()
233
    {
234
        return $this->plotLabel;
235
    }
236
237
    /**
238
     * Get Plot Label by Index.
239
     *
240
     * @param mixed $index
241
     *
242
     * @return DataSeriesValues
243
     */
244 14
    public function getPlotLabelByIndex($index)
245
    {
246 14
        $keys = array_keys($this->plotLabel);
247 14
        if (in_array($index, $keys)) {
248 14
            return $this->plotLabel[$index];
249 2
        } elseif (isset($keys[$index])) {
250 2
            return $this->plotLabel[$keys[$index]];
251
        }
252
253 2
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type PhpOffice\PhpSpreadsheet\Chart\DataSeriesValues.
Loading history...
254
    }
255
256
    /**
257
     * Get Plot Categories.
258
     *
259
     * @return array of DataSeriesValues
260
     */
261
    public function getPlotCategories()
262
    {
263
        return $this->plotCategory;
264
    }
265
266
    /**
267
     * Get Plot Category by Index.
268
     *
269
     * @param mixed $index
270
     *
271
     * @return DataSeriesValues
272
     */
273 14
    public function getPlotCategoryByIndex($index)
274
    {
275 14
        $keys = array_keys($this->plotCategory);
276 14
        if (in_array($index, $keys)) {
277 14
            return $this->plotCategory[$index];
278 11
        } elseif (isset($keys[$index])) {
279 2
            return $this->plotCategory[$keys[$index]];
280
        }
281
282 10
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type PhpOffice\PhpSpreadsheet\Chart\DataSeriesValues.
Loading history...
283
    }
284
285
    /**
286
     * Get Plot Style.
287
     *
288
     * @return null|string
289
     */
290 14
    public function getPlotStyle()
291
    {
292 14
        return $this->plotStyle;
293
    }
294
295
    /**
296
     * Set Plot Style.
297
     *
298
     * @param null|string $plotStyle
299
     *
300
     * @return DataSeries
301
     */
302 2
    public function setPlotStyle($plotStyle)
303
    {
304 2
        $this->plotStyle = $plotStyle;
305
306 2
        return $this;
307
    }
308
309
    /**
310
     * Get Plot Values.
311
     *
312
     * @return array of DataSeriesValues
313
     */
314
    public function getPlotValues()
315
    {
316
        return $this->plotValues;
317
    }
318
319
    /**
320
     * Get Plot Values by Index.
321
     *
322
     * @param mixed $index
323
     *
324
     * @return DataSeriesValues
325
     */
326 14
    public function getPlotValuesByIndex($index)
327
    {
328 14
        $keys = array_keys($this->plotValues);
329 14
        if (in_array($index, $keys)) {
330 14
            return $this->plotValues[$index];
331 2
        } elseif (isset($keys[$index])) {
332 2
            return $this->plotValues[$keys[$index]];
333
        }
334
335
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type PhpOffice\PhpSpreadsheet\Chart\DataSeriesValues.
Loading history...
336
    }
337
338
    /**
339
     * Get Number of Plot Series.
340
     *
341
     * @return int
342
     */
343 1
    public function getPlotSeriesCount()
344
    {
345 1
        return count($this->plotValues);
346
    }
347
348
    /**
349
     * Get Smooth Line.
350
     *
351
     * @return bool
352
     */
353 3
    public function getSmoothLine()
354
    {
355 3
        return $this->smoothLine;
356
    }
357
358
    /**
359
     * Set Smooth Line.
360
     *
361
     * @param bool $smoothLine
362
     *
363
     * @return DataSeries
364
     */
365
    public function setSmoothLine($smoothLine)
366
    {
367
        $this->smoothLine = $smoothLine;
368
369
        return $this;
370
    }
371
372 14
    public function refresh(Worksheet $worksheet)
373
    {
374 14
        foreach ($this->plotValues as $plotValues) {
375 14
            if ($plotValues !== null) {
376 14
                $plotValues->refresh($worksheet, true);
377
            }
378
        }
379 14
        foreach ($this->plotLabel as $plotValues) {
380 14
            if ($plotValues !== null) {
381 14
                $plotValues->refresh($worksheet, true);
382
            }
383
        }
384 14
        foreach ($this->plotCategory as $plotValues) {
385 14
            if ($plotValues !== null) {
386 14
                $plotValues->refresh($worksheet, false);
387
            }
388
        }
389 14
    }
390
}
391