| Conditions | 17 |
| Paths | 5140 |
| Total Lines | 96 |
| Code Lines | 66 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 181 | public function jsonTasksList() { |
||
| 182 | $controller = new JsonController(); |
||
| 183 | $controller |
||
| 184 | ->restrictAccess(Auth::isAdmin()); |
||
| 185 | |||
| 186 | // Generate an AJAX/JSON response for datatables to load a block of rows |
||
| 187 | $search = Filter::postArray('search'); |
||
| 188 | if($search) $search = $search['value']; |
||
|
|
|||
| 189 | $start = Filter::postInteger('start'); |
||
| 190 | $length = Filter::postInteger('length'); |
||
| 191 | $order = Filter::postArray('order'); |
||
| 192 | |||
| 193 | $order_by_name = false; |
||
| 194 | foreach($order as $key => &$value) { |
||
| 195 | switch($value['column']) { |
||
| 196 | case 3: |
||
| 197 | $order_by_name = true; |
||
| 198 | unset($order[$key]); |
||
| 199 | break; |
||
| 200 | case 4; |
||
| 201 | $value['column'] = 'majat_last_run'; |
||
| 202 | break; |
||
| 203 | case 4; |
||
| 204 | $value['column'] = 'majat_last_result'; |
||
| 205 | break; |
||
| 206 | default: |
||
| 207 | unset($order[$key]); |
||
| 208 | } |
||
| 209 | } |
||
| 210 | |||
| 211 | $list = $this->provider->getFilteredTasksList($search, $order, $start, $length); |
||
| 212 | if($order_by_name) { |
||
| 213 | usort($list, function(AbstractTask $a, AbstractTask $b) { return I18N::strcasecmp($a->getTitle(), $b->getTitle()); }); |
||
| 214 | } |
||
| 215 | $recordsFiltered = count($list); |
||
| 216 | $recordsTotal = $this->provider->getTasksCount(); |
||
| 217 | |||
| 218 | $data = array(); |
||
| 219 | foreach($list as $task) { |
||
| 220 | $datum = array(); |
||
| 221 | |||
| 222 | $datum[0] = ' |
||
| 223 | <div class="btn-group"> |
||
| 224 | <button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown" aria-expanded="false"> |
||
| 225 | <i class="fa fa-pencil"></i><span class="caret"></span> |
||
| 226 | </button> |
||
| 227 | <ul class="dropdown-menu" role="menu"> |
||
| 228 | <li> |
||
| 229 | <a href="#" onclick="return set_admintask_status(\''. $task->getName().'\', '.($task->isEnabled() ? 'false' : 'true').');"> |
||
| 230 | <i class="fa fa-fw '.($task->isEnabled() ? 'fa-times' : 'fa-check').'"></i> ' . ($task->isEnabled() ? I18N::translate('Disable') : I18N::translate('Enable')) . ' |
||
| 231 | </a> |
||
| 232 | </li> |
||
| 233 | <li> |
||
| 234 | <a href="module.php?mod='.$this->module->getName().'&mod_action=Task@edit&task='. $task->getName().'"> |
||
| 235 | <i class="fa fa-fw fa-pencil"></i> ' . I18N::translate('Edit') . ' |
||
| 236 | </a> |
||
| 237 | </li> |
||
| 238 | </ul> |
||
| 239 | </div>'; |
||
| 240 | $datum[1] = $task->getName(); |
||
| 241 | $datum[2] = $task->isEnabled() ? |
||
| 242 | '<i class="fa fa-check"></i><span class="sr-only">'.I18N::translate('Enabled').'</span>' : |
||
| 243 | '<i class="fa fa-times"></i><span class="sr-only">'.I18N::translate('Disabled').'</span>'; |
||
| 244 | $datum[3] = $task->getTitle(); |
||
| 245 | $date_format = str_replace('%', '', I18N::dateFormat()) . ' H:i:s'; |
||
| 246 | $datum[4] = $task->getLastUpdated()->format($date_format); |
||
| 247 | $datum[5] = $task->isLastRunSuccess() ? |
||
| 248 | '<i class="fa fa-check"></i><span class="sr-only">'.I18N::translate('Yes').'</span>' : |
||
| 249 | '<i class="fa fa-times"></i><span class="sr-only">'.I18N::translate('No').'</span>'; |
||
| 250 | $dtF = new \DateTime('@0'); |
||
| 251 | $dtT = new \DateTime('@' . ($task->getFrequency() * 60)); |
||
| 252 | $datum[6] = $dtF->diff($dtT)->format(I18N::translate('%a d %h h %i m')); |
||
| 253 | $datum[7] = $task->getRemainingOccurrences() > 0 ? I18N::number($task->getRemainingOccurrences()) : I18N::translate('Unlimited'); |
||
| 254 | $datum[8] = $task->isRunning() ? |
||
| 255 | '<i class="fa fa-cog fa-spin fa-fw"></i><span class="sr-only">'.I18N::translate('Running').'</span>' : |
||
| 256 | '<i class="fa fa-times"></i><span class="sr-only">'.I18N::translate('Not running').'</span>'; |
||
| 257 | if($task->isEnabled() && !$task->isRunning()) { |
||
| 258 | $datum[9] = ' |
||
| 259 | <button id="bt_runtask_'. $task->getName() .'" class="btn btn-primary" href="#" onclick="return run_admintask(\''. $task->getName() .'\')"> |
||
| 260 | <div id="bt_runtasktext_'. $task->getName() .'"><i class="fa fa-cog fa-fw" ></i>' . I18N::translate('Run') . '</div> |
||
| 261 | </button>'; |
||
| 262 | } |
||
| 263 | else { |
||
| 264 | $datum[9] = ''; |
||
| 265 | } |
||
| 266 | |||
| 267 | $data[] = $datum; |
||
| 268 | } |
||
| 269 | |||
| 270 | $controller->pageHeader(); |
||
| 271 | |||
| 272 | $controller->encode(array( |
||
| 273 | 'draw' => Filter::getInteger('draw'), |
||
| 274 | 'recordsTotal' => $recordsTotal, |
||
| 275 | 'recordsFiltered' => $recordsFiltered, |
||
| 276 | 'data' => $data |
||
| 277 | )); |
||
| 304 | } |
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.