DataSetChart   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 73
Duplicated Lines 10.96 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 8
loc 73
c 0
b 0
f 0
wmc 10
lcom 1
cbo 2
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A data() 8 17 3
A getLabels() 0 3 1
A getDataSets() 0 3 1
A addDataSet() 0 4 1
A makeLabels() 0 11 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
}