| Conditions | 5 |
| Paths | 5 |
| Total Lines | 74 |
| Code Lines | 50 |
| 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 |
||
| 21 | function mongodb_watchdog_page_top($type) { |
||
| 22 | $ret = array(); |
||
| 23 | $type_param = array('%type' => $type); |
||
| 24 | $limit = 50; |
||
| 25 | |||
| 26 | // Safety net |
||
| 27 | $types = array( |
||
| 28 | 'page not found', |
||
| 29 | 'access denied', |
||
| 30 | ); |
||
| 31 | if (!in_array($type, $types)) { |
||
| 32 | drupal_set_message(t('Unknown top report type: %type', $type_param), 'error'); |
||
| 33 | watchdog('mongodb_watchdog', 'Unknown top report type: %type', $type_param, WATCHDOG_WARNING); |
||
| 34 | $ret = ''; |
||
| 35 | return $ret; |
||
| 36 | } |
||
| 37 | |||
| 38 | // Find _id for the error type. |
||
| 39 | $watchdog = mongodb_collection(variable_get('mongodb_watchdog', 'watchdog')); |
||
| 40 | $template = $watchdog->findOne(array('type' => $type), array('_id')); |
||
| 41 | |||
| 42 | // findOne() will return NULL if no row is found |
||
| 43 | if (empty($template)) { |
||
| 44 | $ret['empty'] = array( |
||
| 45 | '#markup' => t('No "%type" message found', $type_param), |
||
| 46 | '#prefix' => '<div class="mongodb-watchdog-message">', |
||
| 47 | '#suffix' => '</div>', |
||
| 48 | ); |
||
| 49 | $ret = drupal_render($ret); |
||
| 50 | return $ret; |
||
| 51 | } |
||
| 52 | |||
| 53 | // Find occurrences of error type. |
||
| 54 | $key = $template['_id']; |
||
| 55 | $event_collection = mongodb_collection('watchdog_event_' . $key); |
||
| 56 | $reduce = <<<EOT |
||
| 57 | function (doc, accumulator) { |
||
| 58 | accumulator.count++; |
||
| 59 | } |
||
| 60 | EOT; |
||
| 61 | |||
| 62 | $counts = $event_collection->group( |
||
| 63 | array('variables.@param' => 1), |
||
| 64 | array('count' => array()), |
||
| 65 | $reduce |
||
| 66 | ); |
||
| 67 | if (!$counts['ok']) { |
||
| 68 | drupal_set_message(t('No "%type" occurrence found', $type_param), 'error'); |
||
| 69 | $ret = ''; |
||
| 70 | return $ret; |
||
| 71 | } |
||
| 72 | $counts = $counts['retval']; |
||
| 73 | usort($counts, '_mongodb_watchdog_sort_top'); |
||
| 74 | $counts = array_slice($counts, 0, $limit); |
||
| 75 | |||
| 76 | $header = array( |
||
| 77 | t('#'), |
||
| 78 | t('Paths'), |
||
| 79 | ); |
||
| 80 | $rows = array(); |
||
| 81 | foreach ($counts as $count) { |
||
| 82 | $rows[] = array( |
||
| 83 | $count['variables.@param'], |
||
| 84 | $count['count'], |
||
| 85 | ); |
||
| 86 | } |
||
| 87 | |||
| 88 | $ret = array( |
||
| 89 | '#theme' => 'table', |
||
| 90 | '#header' => $header, |
||
| 91 | '#rows' => $rows, |
||
| 92 | ); |
||
| 93 | return $ret; |
||
| 94 | } |
||
| 95 | |||
| 111 |