| Conditions | 2 |
| Paths | 2 |
| Total Lines | 78 |
| Code Lines | 43 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 83 | public function registerAsset() |
||
| 84 | { |
||
| 85 | $view = $this->getView(); |
||
| 86 | FlotAsset::register($view); |
||
| 87 | $clientData = Json::encode($this->getClientData()); |
||
| 88 | $clientOptions = Json::encode($this->getClientOptions()); |
||
| 89 | $script = " |
||
| 90 | let plot_{$this->id} = $.plot('#{$this->id}', {$clientData}, {$clientOptions}); |
||
| 91 | "; |
||
| 92 | $view->registerJs($script); |
||
| 93 | |||
| 94 | // Realtime |
||
| 95 | if ($this->realtime['on'] === true) { |
||
| 96 | $dataUrl = $this->realtime['dataUrl']; |
||
| 97 | $btnGroupId = $this->realtime['btnGroupId']; |
||
| 98 | $btnDefault = $this->realtime['btnDefault']; |
||
| 99 | $updateInterval = $this->realtime['updateInterval']; |
||
| 100 | $script = " |
||
| 101 | let url_{$this->id} = '$dataUrl', |
||
| 102 | data_{$this->id} = (plot_{$this->id}.getData().length === 0) ? [plot_{$this->id}.getData()] : |
||
| 103 | plot_{$this->id}.getData()[0].data, |
||
| 104 | updateInterval_{$this->id} = {$updateInterval}, |
||
| 105 | realtime_{$this->id} = '{$btnDefault}', |
||
| 106 | btnRealtime_{$this->id} = $('#{$btnGroupId} .btn'); |
||
| 107 | |||
| 108 | // GET DATA AJAX |
||
| 109 | function getDataAjax_{$this->id}() { |
||
| 110 | $.ajax({ |
||
| 111 | url: url_{$this->id}, |
||
| 112 | dataType: 'json', |
||
| 113 | type: 'post', |
||
| 114 | data: {data: data_{$this->id}}, |
||
| 115 | }).done(function (response) { |
||
| 116 | data_{$this->id} = response.result; |
||
| 117 | }).fail(function (response) { |
||
| 118 | console.log(response.result); |
||
| 119 | }); |
||
| 120 | return data_{$this->id}; |
||
| 121 | } |
||
| 122 | |||
| 123 | // UPDATE |
||
| 124 | function update_{$this->id}() { |
||
| 125 | plot_{$this->id}.setData([getDataAjax_{$this->id}()]); |
||
| 126 | plot_{$this->id}.setupGrid(); |
||
| 127 | // Since the axes don't change, we don't need to call plot.setupGrid() |
||
| 128 | plot_{$this->id}.draw(); |
||
| 129 | if (realtime_{$this->id} === 'on') { |
||
| 130 | setTimeout(update_{$this->id}, updateInterval_{$this->id}); |
||
| 131 | } |
||
| 132 | } |
||
| 133 | |||
| 134 | // INITIALIZE REALTIME DATA FETCHING |
||
| 135 | if (realtime_{$this->id} === 'on') { |
||
| 136 | update_{$this->id}(); |
||
| 137 | } |
||
| 138 | |||
| 139 | // REALTIME TOGGLE |
||
| 140 | btnRealtime_{$this->id}.click(function () { |
||
| 141 | btnRealtime_{$this->id}.addClass('btn-default'); |
||
| 142 | $(this).removeClass('btn-default'); |
||
| 143 | if ($(this).data('toggle') === 'on') { |
||
| 144 | if(realtime_{$this->id} !== 'on') { |
||
| 145 | realtime_{$this->id} = 'on'; |
||
| 146 | btnRealtime_{$this->id}.removeClass('btn-danger'); |
||
| 147 | $(this).addClass('btn-success'); |
||
| 148 | update_{$this->id}(); |
||
| 149 | } |
||
| 150 | } else { |
||
| 151 | if(realtime_{$this->id} !== 'off') { |
||
| 152 | realtime_{$this->id} = 'off'; |
||
| 153 | btnRealtime_{$this->id}.removeClass('btn-success'); |
||
| 154 | $(this).addClass('btn-danger'); |
||
| 155 | update_{$this->id}(); |
||
| 156 | } |
||
| 157 | } |
||
| 158 | }); |
||
| 159 | "; |
||
| 160 | $view->registerJs($script); |
||
| 161 | } |
||
| 164 |