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