DataSetChart::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
1
<?php
2
namespace rtens\domin\delivery\web\renderers\charting\charts;
3
4
use rtens\domin\delivery\web\renderers\charting\Chart;
5
use rtens\domin\delivery\web\renderers\charting\data\DataSet;
6
7
abstract class DataSetChart extends Chart {
8
9
    /** @var null|string[] */
10
    private $labels;
11
12
    /** @var DataSet[] */
13
    private $dataSets = [];
14
15
    /**
16
     * LineChart constructor.
17
     * @param null|string[] $labels
18
     * @param DataSet[] $dataSets
19
     */
20
    public function __construct(array $labels = null, array $dataSets = []) {
21
        parent::__construct();
22
        $this->labels = $labels;
23
        $this->dataSets = $dataSets;
24
    }
25
26
    public function data() {
27
        $dataSets = $this->getDataSets();
28
        if (!$dataSets) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $dataSets of type rtens\domin\delivery\web...charting\data\DataSet[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
29
            return [];
30
        }
31
        return [
32
            "labels" => $this->makeLabels(),
33 View Code Duplication
            "datasets" => array_map(function (DataSet $set) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
34
                return array_merge(
35
                    [
36
                        "label" => $set->getLabel(),
37
                        "data" => $set->getValues()
38
                    ],
39
                    $this->makePalette($set->getColor() ?: $this->provideColor()));
40
            }, $dataSets)
41
        ];
42
    }
43
44
    /**
45
     * @return null|string[]
46
     */
47
    public function getLabels() {
48
        return $this->labels;
49
    }
50
51
    /**
52
     * @return DataSet[]
53
     */
54
    public function getDataSets() {
55
        return $this->dataSets;
56
    }
57
58
    /**
59
     * @param DataSet $set
60
     * @return $this
61
     */
62
    public function addDataSet(DataSet $set) {
63
        $this->dataSets[] = $set;
64
        return $this;
65
    }
66
67
    private function makeLabels() {
68
        if ($this->labels) {
69
            return $this->labels;
70
        }
71
        $count = 0;
72
        foreach ($this->dataSets as $set) {
73
            $count = max($count, count($set->getValues()));
74
        }
75
76
        return range(1, $count);
77
    }
78
79
}