Passed
Pull Request — master (#4378)
by Owen
15:19
created

DataSeries::__construct()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 33
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 33
ccs 17
cts 17
cp 1
rs 9.7333
c 0
b 0
f 0
cc 4
nc 8
nop 9
crap 4

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
    const EMPTY_AS_GAP = 'gap';
44
    const EMPTY_AS_ZERO = 'zero';
45
    const EMPTY_AS_SPAN = 'span';
46
47
    /**
48
     * Series Plot Type.
49
     */
50
    private ?string $plotType;
51
52
    /**
53
     * Plot Grouping Type.
54
     */
55
    private ?string $plotGrouping;
56
57
    /**
58
     * Plot Direction.
59
     */
60
    private string $plotDirection;
61
62
    /**
63
     * Plot Style.
64
     */
65
    private ?string $plotStyle;
66
67
    /**
68
     * Order of plots in Series.
69
     *
70
     * @var int[]
71
     */
72
    private array $plotOrder;
73
74
    /**
75
     * Plot Label.
76
     *
77
     * @var DataSeriesValues[]
78
     */
79
    private array $plotLabel;
80
81
    /**
82
     * Plot Category.
83
     *
84
     * @var DataSeriesValues[]
85
     */
86
    private array $plotCategory;
87
88
    /**
89
     * Smooth Line. Must be specified for both DataSeries and DataSeriesValues.
90
     */
91
    private bool $smoothLine;
92
93
    /**
94
     * Plot Values.
95
     *
96
     * @var DataSeriesValues[]
97
     */
98
    private array $plotValues;
99
100
    /**
101
     * Plot Bubble Sizes.
102
     *
103
     * @var DataSeriesValues[]
104
     */
105
    private array $plotBubbleSizes = [];
106
107
    /**
108
     * Create a new DataSeries.
109
     *
110
     * @param int[] $plotOrder
111
     * @param DataSeriesValues[] $plotLabel
112
     * @param DataSeriesValues[] $plotCategory
113
     * @param DataSeriesValues[] $plotValues
114
     */
115 107
    public function __construct(
116
        null|string $plotType = null,
117
        null|string $plotGrouping = null,
118
        array $plotOrder = [],
119
        array $plotLabel = [],
120
        array $plotCategory = [],
121
        array $plotValues = [],
122
        ?string $plotDirection = null,
123
        bool $smoothLine = false,
124
        ?string $plotStyle = null
125
    ) {
126 107
        $this->plotType = $plotType;
127 107
        $this->plotGrouping = $plotGrouping;
128 107
        $this->plotOrder = $plotOrder;
129 107
        $keys = array_keys($plotValues);
130 107
        $this->plotValues = $plotValues;
131 107
        if (!isset($plotLabel[$keys[0]])) {
132 21
            $plotLabel[$keys[0]] = new DataSeriesValues();
133
        }
134 107
        $this->plotLabel = $plotLabel;
135
136 107
        if (!isset($plotCategory[$keys[0]])) {
137 17
            $plotCategory[$keys[0]] = new DataSeriesValues();
138
        }
139 107
        $this->plotCategory = $plotCategory;
140
141 107
        $this->smoothLine = (bool) $smoothLine;
142 107
        $this->plotStyle = $plotStyle;
143
144 107
        if ($plotDirection === null) {
145 107
            $plotDirection = self::DIRECTION_COL;
146
        }
147 107
        $this->plotDirection = $plotDirection;
148
    }
149
150
    /**
151
     * Get Plot Type.
152
     */
153 94
    public function getPlotType(): ?string
154
    {
155 94
        return $this->plotType;
156
    }
157
158
    /**
159
     * Set Plot Type.
160
     *
161
     * @return $this
162
     */
163 2
    public function setPlotType(string $plotType): static
164
    {
165 2
        $this->plotType = $plotType;
166
167 2
        return $this;
168
    }
169
170
    /**
171
     * Get Plot Grouping Type.
172
     */
173 93
    public function getPlotGrouping(): ?string
174
    {
175 93
        return $this->plotGrouping;
176
    }
177
178
    /**
179
     * Set Plot Grouping Type.
180
     *
181
     * @return $this
182
     */
183 1
    public function setPlotGrouping(string $groupingType): static
184
    {
185 1
        $this->plotGrouping = $groupingType;
186
187 1
        return $this;
188
    }
189
190
    /**
191
     * Get Plot Direction.
192
     */
193 30
    public function getPlotDirection(): string
194
    {
195 30
        return $this->plotDirection;
196
    }
197
198
    /**
199
     * Set Plot Direction.
200
     *
201
     * @return $this
202
     */
203 34
    public function setPlotDirection(string $plotDirection): static
204
    {
205 34
        $this->plotDirection = $plotDirection;
206
207 34
        return $this;
208
    }
209
210
    /**
211
     * Get Plot Order.
212
     *
213
     * @return int[]
214
     */
215 93
    public function getPlotOrder(): array
216
    {
217 93
        return $this->plotOrder;
218
    }
219
220
    /**
221
     * Get Plot Labels.
222
     *
223
     * @return DataSeriesValues[]
224
     */
225 4
    public function getPlotLabels(): array
226
    {
227 4
        return $this->plotLabel;
228
    }
229
230
    /**
231
     * Get Plot Label by Index.
232
     *
233
     * @return DataSeriesValues|false
234
     */
235 93
    public function getPlotLabelByIndex(int $index): bool|DataSeriesValues
236
    {
237 93
        $keys = array_keys($this->plotLabel);
238 93
        if (in_array($index, $keys)) {
239 93
            return $this->plotLabel[$index];
240
        }
241
242 7
        return false;
243
    }
244
245
    /**
246
     * Get Plot Categories.
247
     *
248
     * @return DataSeriesValues[]
249
     */
250 2
    public function getPlotCategories(): array
251
    {
252 2
        return $this->plotCategory;
253
    }
254
255
    /**
256
     * Get Plot Category by Index.
257
     *
258
     * @return DataSeriesValues|false
259
     */
260 93
    public function getPlotCategoryByIndex(int $index): bool|DataSeriesValues
261
    {
262 93
        $keys = array_keys($this->plotCategory);
263 93
        if (in_array($index, $keys)) {
264 93
            return $this->plotCategory[$index];
265 43
        } elseif (isset($keys[$index])) {
266 5
            return $this->plotCategory[$keys[$index]];
267
        }
268
269 42
        return false;
270
    }
271
272
    /**
273
     * Get Plot Style.
274
     */
275 93
    public function getPlotStyle(): ?string
276
    {
277 93
        return $this->plotStyle;
278
    }
279
280
    /**
281
     * Set Plot Style.
282
     *
283
     * @return $this
284
     */
285 30
    public function setPlotStyle(?string $plotStyle): static
286
    {
287 30
        $this->plotStyle = $plotStyle;
288
289 30
        return $this;
290
    }
291
292
    /**
293
     * Get Plot Values.
294
     *
295
     * @return DataSeriesValues[]
296
     */
297 15
    public function getPlotValues(): array
298
    {
299 15
        return $this->plotValues;
300
    }
301
302
    /**
303
     * Get Plot Values by Index.
304
     *
305
     * @return DataSeriesValues|false
306
     */
307 93
    public function getPlotValuesByIndex(int $index): bool|DataSeriesValues
308
    {
309 93
        $keys = array_keys($this->plotValues);
310 93
        if (in_array($index, $keys)) {
311 93
            return $this->plotValues[$index];
312
        }
313
314 2
        return false;
315
    }
316
317
    /**
318
     * Get Plot Bubble Sizes.
319
     *
320
     * @return DataSeriesValues[]
321
     */
322 4
    public function getPlotBubbleSizes(): array
323
    {
324 4
        return $this->plotBubbleSizes;
325
    }
326
327
    /**
328
     * Set Plot Bubble Sizes.
329
     *
330
     * @param DataSeriesValues[] $plotBubbleSizes
331
     */
332 70
    public function setPlotBubbleSizes(array $plotBubbleSizes): self
333
    {
334 70
        $this->plotBubbleSizes = $plotBubbleSizes;
335
336 70
        return $this;
337
    }
338
339
    /**
340
     * Get Number of Plot Series.
341
     */
342 6
    public function getPlotSeriesCount(): int
343
    {
344 6
        return count($this->plotValues);
345
    }
346
347
    /**
348
     * Get Smooth Line.
349
     */
350 20
    public function getSmoothLine(): bool
351
    {
352 20
        return $this->smoothLine;
353
    }
354
355
    /**
356
     * Set Smooth Line.
357
     *
358
     * @return $this
359
     */
360 1
    public function setSmoothLine(bool $smoothLine): static
361
    {
362 1
        $this->smoothLine = $smoothLine;
363
364 1
        return $this;
365
    }
366
367 94
    public function refresh(Worksheet $worksheet): void
368
    {
369 94
        foreach ($this->plotValues as $plotValues) {
370 94
            $plotValues->refresh($worksheet, true);
371 94
        }
372
        foreach ($this->plotLabel as $plotValues) {
373
            $plotValues->refresh($worksheet, true);
374 94
        }
375 94
        foreach ($this->plotCategory as $plotValues) {
376 94
            $plotValues->refresh($worksheet, false);
377
        }
378
    }
379 94
380 94
    /**
381 94
     * Implement PHP __clone to create a deep clone, not just a shallow copy.
382
     */
383
    public function __clone()
384
    {
385
        $plotLabels = $this->plotLabel;
386
        $this->plotLabel = [];
387
        foreach ($plotLabels as $plotLabel) {
388
            $this->plotLabel[] = $plotLabel;
389 5
        }
390
        $plotCategories = $this->plotCategory;
391 5
        $this->plotCategory = [];
392 5
        foreach ($plotCategories as $plotCategory) {
393 5
            $this->plotCategory[] = clone $plotCategory;
394 5
        }
395
        $plotValues = $this->plotValues;
396 5
        $this->plotValues = [];
397 5
        foreach ($plotValues as $plotValue) {
398 5
            $this->plotValues[] = clone $plotValue;
399 5
        }
400
        $plotBubbleSizes = $this->plotBubbleSizes;
401 5
        $this->plotBubbleSizes = [];
402 5
        foreach ($plotBubbleSizes as $plotBubbleSize) {
403 5
            $this->plotBubbleSizes[] = clone $plotBubbleSize;
404 5
        }
405
    }
406
}
407