GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Highcharts   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 6
dl 0
loc 62
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B run() 0 26 3
A registerAssets() 0 16 2
1
<?php
2
3
/**
4
 * Highcharts class file.
5
 *
6
 * @author Milo Schuman <[email protected]>
7
 * @link https://github.com/miloschuman/yii2-highcharts/
8
 * @license http://www.opensource.org/licenses/mit-license.php MIT License
9
 */
10
11
namespace miloschuman\highcharts;
12
13
use yii\base\Widget;
14
use yii\helpers\ArrayHelper;
15
use yii\helpers\Html;
16
use yii\helpers\Json;
17
use yii\web\View;
18
19
/**
20
 * Highcharts encapsulates the {@link http://www.highcharts.com/ Highcharts}
21
 * charting library's Chart object.
22
 *
23
 * To use this widget, you can insert the following code in a view:
24
 * ```php
25
 * echo \miloschuman\highcharts\Highcharts::widget([
26
 *     'options' => [
27
 *         'title' => ['text' => 'Fruit Consumption'],
28
 *         'xAxis' => [
29
 *             'categories' => ['Apples', 'Bananas', 'Oranges']
30
 *         ],
31
 *         'yAxis' => [
32
 *             'title' => ['text' => 'Fruit eaten']
33
 *         ],
34
 *         'series' => [
35
 *             ['name' => 'Jane', 'data' => [1, 0, 4]],
36
 *             ['name' => 'John', 'data' => [5, 7, 3]]
37
 *         ]
38
 *     ]
39
 * ]);
40
 * ```
41
 *
42
 * By configuring the {@link $options} property, you may specify the options
43
 * that need to be passed to the Highcharts JavaScript object. Please refer to
44
 * the demo gallery and documentation on the {@link https://www.highcharts.com/
45
 * Highcharts website} for possible options.
46
 *
47
 * Note: You do not need to specify the <code>chart->renderTo</code> option as
48
 * is shown in many of the examples on the Highcharts website. This value is
49
 * automatically populated with the id of the widget's container element. If you
50
 * wish to use a different container, feel free to specify a custom value.
51
 */
52
class Highcharts extends Widget
53
{
54
55
    protected $constr = 'Chart';
56
    protected $baseScript = 'highcharts';
57
    public $options = [];
58
    public $htmlOptions = [];
59
    public $setupOptions = [];
60
    public $scripts = [];
61
    public $callback = false;
62
63
    /**
64
     * Renders the widget.
65
     */
66
    public function run()
67
    {
68
        // determine the ID of the container element
69
        if (isset($this->htmlOptions['id'])) {
70
            $this->id = $this->htmlOptions['id'];
71
        } else {
72
            $this->id = $this->htmlOptions['id'] = $this->getId();
73
        }
74
75
        // render the container element
76
        echo Html::tag('div', '', $this->htmlOptions);
77
78
        // check if options parameter is a json string
79
        if (is_string($this->options)) {
80
            $this->options = Json::decode($this->options);
0 ignored issues
show
Documentation Bug introduced by
It seems like \yii\helpers\Json::decode($this->options) of type * is incompatible with the declared type array of property $options.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

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

Loading history...
81
        }
82
83
        // merge options with default values
84
        $defaultOptions = ['chart' => ['renderTo' => $this->id]];
85
        $this->options = ArrayHelper::merge($defaultOptions, $this->options);
86
        array_unshift($this->scripts, $this->baseScript);
87
88
        $this->registerAssets();
89
90
        parent::run();
91
    }
92
93
    /**
94
     * Registers required assets and the executing code block with the view
95
     */
96
    protected function registerAssets()
97
    {
98
        // register the necessary script files
99
        HighchartsAsset::register($this->view)->withScripts($this->scripts);
100
101
        // prepare and register JavaScript code block
102
        $jsOptions = Json::encode($this->options);
103
        $setupOptions = Json::encode($this->setupOptions);
104
        $js = "Highcharts.setOptions($setupOptions); new Highcharts.{$this->constr}($jsOptions);";
105
        $key = __CLASS__ . '#' . $this->id;
106
        if (is_string($this->callback)) {
107
            $js = "function {$this->callback}(data) {{$js}}";
108
        }
109
110
        $this->view->registerJs($js, View::POS_READY, $key);
111
    }
112
113
}
114