Traces::__toString()   A
last analyzed

Complexity

Conditions 3
Paths 5

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
c 0
b 0
f 0
rs 9.4285
cc 3
eloc 9
nc 5
nop 0
1
<?php
2
/**
3
 * Copyright 2017, Jean Traullé <[email protected]>
4
 *
5
 * Licensed under The MIT License
6
 * Redistributions of files must retain the above copyright notice.
7
 *
8
 * @copyright Copyright 2017, Jean Traullé <[email protected]>
9
 * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
10
 *
11
 */
12
13
namespace CakeCharts\Utility;
14
15
/**
16
 * Class Traces
17
 * @package CakeCharts\Utility
18
 */
19
class Traces
20
{
21
    /**
22
     * @var Trace[] Array of Trace objects
23
     * @see \CakeCharts\Utility\Trace
24
     */
25
    private $traces;
26
27
    /**
28
     * @var \Exception|null Encountered error, if any
29
     */
30
    private $error = null;
31
32
    /**
33
     * Traces constructor.
34
     *
35
     * @param array|null $traces Array of Trace objects
36
     * @see \CakeCharts\Utility\Trace
37
     */
38
    public function __construct(array $traces = null)
39
    {
40
        $this->traces = $traces;
0 ignored issues
show
Documentation Bug introduced by
It seems like $traces can be null. However, the property $traces is declared as array. Maybe change the type of the property to array|null or add a type check?

Our type inference engine has found an assignment of a scalar value (like a string, an integer or null) to a property which is an array.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.

To type hint that a parameter can be either an array or null, you can set a type hint of array and a default value of null. The PHP interpreter will then accept both an array or null for that parameter.

function aContainsB(array $needle = null, array  $haystack) {
    if (!$needle) {
        return false;
    }

    return array_intersect($haystack, $needle) == $haystack;
}

The function can be called with either null or an array for the parameter $needle but will only accept an array as $haystack.

Loading history...
41
    }
42
43
    /**
44
     * Add a Trace object to the list of traces
45
     *
46
     * @param Trace $trace A trace object
47
     * @see \CakeCharts\Utility\Trace
48
     * @return void
49
     */
50
    public function addTrace(Trace $trace)
51
    {
52
        $this->traces[] = $trace;
53
    }
54
55
    /**
56
     * Output series as plotly.js compliant data series
57
     *
58
     * @return string plotly.js compliant (JSON) data series
59
     */
60
    public function __toString()
61
    {
62
        try {
63
            $return = [];
64
            foreach ($this->traces as $trace) {
65
                $return[] = $trace->toArray();
66
            }
67
68
            return json_encode($return);
69
        } catch (\Exception $e) {
70
            $this->error = $e;
71
72
            return json_encode([]);
73
        }
74
    }
75
76
    /**
77
     * @return \Exception|string Encountered error, if any
78
     */
79
    public function getError()
80
    {
81
        return $this->error;
82
    }
83
}
84