Conditions | 33 |
Paths | 865 |
Total Lines | 192 |
Code Lines | 89 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
Bugs | 2 | 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 |
||
249 | protected function populateState($ordering = null, $direction = null) |
||
250 | { |
||
251 | // If the context is set, assume that stateful lists are used. |
||
252 | if (!$this->context) |
||
253 | { |
||
254 | $this->setState('list.start', 0); |
||
255 | $this->setState('list.limit', 0); |
||
256 | |||
257 | return; |
||
258 | } |
||
259 | |||
260 | $app = JFactory::getApplication(); |
||
261 | $inputFilter = JFilterInput::getInstance(); |
||
262 | $params = null; |
||
263 | |||
264 | // Load the parameters for frontend. |
||
265 | if (version_compare(JVERSION, '3.7', '<') && $app->isSite()) |
||
266 | { |
||
267 | $params = $app->getParams(); |
||
268 | } |
||
269 | elseif (method_exists($app, 'isClient') && $app->isClient('site')) |
||
270 | { |
||
271 | $params = $app->getParams(); |
||
272 | } |
||
273 | |||
274 | // Receive & set filters |
||
275 | $filters = $app->getUserStateFromRequest($this->context . '.filter', 'filter', array(), 'array'); |
||
276 | |||
277 | if ($filters) |
||
278 | { |
||
279 | foreach ($filters as $name => $value) |
||
280 | { |
||
281 | // Exclude if blacklisted |
||
282 | if (!in_array($name, $this->filterBlacklist)) |
||
283 | { |
||
284 | $this->setState('filter.' . $name, $value); |
||
285 | } |
||
286 | } |
||
287 | } |
||
288 | |||
289 | $limit = 0; |
||
290 | |||
291 | // Receive & set list options |
||
292 | $list = $app->getUserStateFromRequest($this->context . '.list', 'list', array(), 'array'); |
||
293 | |||
294 | if ($list) |
||
295 | { |
||
296 | foreach ($list as $name => $value) |
||
297 | { |
||
298 | // Exclude if blacklisted |
||
299 | if (!in_array($name, $this->listBlacklist)) |
||
300 | { |
||
301 | // Extra validations |
||
302 | switch ($name) |
||
303 | { |
||
304 | case 'fullordering': |
||
305 | $orderingParts = explode(' ', $value); |
||
306 | |||
307 | if (count($orderingParts) >= 2) |
||
308 | { |
||
309 | // Latest part will be considered the direction |
||
310 | $fullDirection = end($orderingParts); |
||
311 | |||
312 | if (in_array(strtoupper($fullDirection), array('ASC', 'DESC', ''))) |
||
313 | { |
||
314 | $this->setState('list.direction', $fullDirection); |
||
315 | } |
||
316 | else |
||
317 | { |
||
318 | $this->setState('list.direction', $direction); |
||
319 | |||
320 | // Fallback to the default value |
||
321 | $value = $ordering . ' ' . $direction; |
||
322 | } |
||
323 | |||
324 | unset($orderingParts[count($orderingParts) - 1]); |
||
325 | |||
326 | // The rest will be the ordering |
||
327 | $fullOrdering = implode(' ', $orderingParts); |
||
328 | |||
329 | if (in_array($fullOrdering, $this->filter_fields)) |
||
330 | { |
||
331 | $this->setState('list.ordering', $fullOrdering); |
||
332 | } |
||
333 | else |
||
334 | { |
||
335 | $this->setState('list.ordering', $ordering); |
||
336 | |||
337 | // Fallback to the default value |
||
338 | $value = $ordering . ' ' . $direction; |
||
339 | } |
||
340 | } |
||
341 | else |
||
342 | { |
||
343 | $this->setState('list.ordering', $ordering); |
||
344 | $this->setState('list.direction', $direction); |
||
345 | |||
346 | // Fallback to the default value |
||
347 | $value = $ordering . ' ' . $direction; |
||
348 | } |
||
349 | break; |
||
350 | |||
351 | case 'ordering': |
||
352 | if (!in_array($value, $this->filter_fields)) |
||
353 | { |
||
354 | $value = $ordering; |
||
355 | } |
||
356 | break; |
||
357 | |||
358 | case 'direction': |
||
359 | if (!in_array(strtoupper($value), array('ASC', 'DESC', ''))) |
||
360 | { |
||
361 | $value = $direction; |
||
362 | } |
||
363 | break; |
||
364 | |||
365 | case $this->limitField: |
||
366 | $value = $inputFilter->clean($value, 'int'); |
||
367 | $this->setState('list.limit', $value); |
||
368 | $limit = $value; |
||
369 | break; |
||
370 | |||
371 | case $this->limitstartField: |
||
372 | $value = $inputFilter->clean($value, 'int'); |
||
373 | break; |
||
374 | |||
375 | case 'select': |
||
376 | $explodedValue = explode(',', $value); |
||
377 | |||
378 | foreach ($explodedValue as &$field) |
||
379 | { |
||
380 | $field = $inputFilter->clean($field, 'cmd'); |
||
381 | } |
||
382 | |||
383 | $value = implode(',', $explodedValue); |
||
384 | break; |
||
385 | } |
||
386 | |||
387 | $this->setState('list.' . $name, $value); |
||
388 | } |
||
389 | } |
||
390 | } |
||
391 | else |
||
392 | // Keep B/C for components previous to jform forms for filters |
||
393 | { |
||
394 | // Pre-fill the limits |
||
395 | $defaultLimit = ($params && $params->get('list_limit') >= 0) ? $params->get('list_limit', $app->get('list_limit')) : $app->get('list_limit'); |
||
396 | $limit = $app->getUserStateFromRequest('global.list.' . $this->limitField, $this->limitField, $defaultLimit, 'uint'); |
||
397 | $this->setState('list.limit', $limit); |
||
398 | |||
399 | // Check if the ordering field is in the white list, otherwise use the incoming value. |
||
400 | $value = $app->getUserStateFromRequest($this->context . '.ordercol', 'filter_order', $ordering); |
||
401 | |||
402 | if (!in_array($value, $this->filter_fields)) |
||
403 | { |
||
404 | $value = $ordering; |
||
405 | $app->setUserState($this->context . '.ordercol', $value); |
||
406 | } |
||
407 | |||
408 | $this->setState('list.ordering', $value); |
||
409 | |||
410 | // Check if the ordering direction is valid, otherwise use the incoming value. |
||
411 | $value = $app->getUserStateFromRequest($this->context . '.orderdirn', 'filter_order_Dir', $direction); |
||
412 | |||
413 | if (!in_array(strtoupper($value), array('ASC', 'DESC', ''))) |
||
414 | { |
||
415 | $value = $direction; |
||
416 | $app->setUserState($this->context . '.orderdirn', $value); |
||
417 | } |
||
418 | |||
419 | $this->setState('list.direction', $value); |
||
420 | } |
||
421 | |||
422 | // Support old ordering field |
||
423 | $oldOrdering = $app->input->get('filter_order'); |
||
424 | |||
425 | if (!empty($oldOrdering) && in_array($oldOrdering, $this->filter_fields)) |
||
426 | { |
||
427 | $this->setState('list.ordering', $oldOrdering); |
||
428 | } |
||
429 | |||
430 | // Support old direction field |
||
431 | $oldDirection = $app->input->get('filter_order_Dir'); |
||
432 | |||
433 | if (!empty($oldDirection) && in_array(strtoupper($oldDirection), array('ASC', 'DESC', ''))) |
||
434 | { |
||
435 | $this->setState('list.direction', $oldDirection); |
||
436 | } |
||
437 | |||
438 | $value = $app->getUserStateFromRequest($this->context . '.' . $this->limitstartField, $this->limitstartField, 0, 'int'); |
||
439 | $limitstart = ($limit != 0 ? (floor($value / $limit) * $limit) : 0); |
||
440 | $this->setState('list.start', $limitstart); |
||
441 | } |
||
618 |