| Conditions | 10 |
| Paths | 288 |
| Total Lines | 101 |
| Code Lines | 80 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 |
||
| 14 | function mongodb_watchdog_event($dblog) { |
||
| 15 | $severity = watchdog_severity_levels(); |
||
| 16 | $rows = array( |
||
| 17 | array( |
||
| 18 | array('data' => t('Type'), 'header' => TRUE), |
||
| 19 | t($dblog['type']), |
||
| 20 | ), |
||
| 21 | array( |
||
| 22 | array('data' => t('Severity'), 'header' => TRUE), |
||
| 23 | $severity[$dblog['severity']], |
||
| 24 | ), |
||
| 25 | array( |
||
| 26 | array('data' => t('Function'), 'header' => TRUE), |
||
| 27 | isset($dblog['function']) ? $dblog['function'] : '', |
||
| 28 | ), |
||
| 29 | array( |
||
| 30 | array('data' => t('File'), 'header' => TRUE), |
||
| 31 | isset($dblog['file']) ? $dblog['file'] : '', |
||
| 32 | ), |
||
| 33 | array( |
||
| 34 | array('data' => t('Line'), 'header' => TRUE), |
||
| 35 | isset($dblog['line']) ? $dblog['line'] : '', |
||
| 36 | ), |
||
| 37 | array( |
||
| 38 | array('data' => t('Count'), 'header' => TRUE), |
||
| 39 | isset($dblog['count']) ? $dblog['count'] : '', |
||
| 40 | ), |
||
| 41 | ); |
||
| 42 | $build['reports'] = array( |
||
|
|
|||
| 43 | '#type' => 'markup', |
||
| 44 | '#markup' => l(t('Return to log report'), 'admin/reports/mongodb'), |
||
| 45 | ); |
||
| 46 | $build['mongodb_watchdog_event_table']['header'] = array( |
||
| 47 | '#theme' => 'table', |
||
| 48 | '#rows' => $rows, |
||
| 49 | '#attributes' => array('class' => array('dblog-event')), |
||
| 50 | ); |
||
| 51 | // @todo: the count is unreliable, so just get the actual number of entries. |
||
| 52 | //$total = min($dblog['count'], variable_get('mongodb_watchdog_items', 10000)); |
||
| 53 | $collection = mongodb_collection(variable_get('mongodb_watchdog', 'watchdog')); |
||
| 54 | $collection = $collection->db->selectCollection('watchdog_event_' . $dblog['_id']); |
||
| 55 | $total = $collection->count(); |
||
| 56 | $limit = 20; |
||
| 57 | $pagenumber = mongodb_watchdog_pager_init(0, $limit, $total); |
||
| 58 | $result = $collection |
||
| 59 | ->find() |
||
| 60 | ->skip($pagenumber * $limit) |
||
| 61 | ->limit($limit) |
||
| 62 | ->sort(array('$natural' => -1)); |
||
| 63 | $severity = watchdog_severity_levels(); |
||
| 64 | $rows = array(); |
||
| 65 | $header = array( |
||
| 66 | array('data' => t('Date'), 'header' => TRUE), |
||
| 67 | array('data' => t('User'), 'header' => TRUE), |
||
| 68 | array('data' => t('Location'), 'header' => TRUE), |
||
| 69 | array('data' => t('Referrer'), 'header' => TRUE), |
||
| 70 | array('data' => t('Hostname'), 'header' => TRUE), |
||
| 71 | array('data' => t('Message'), 'header' => TRUE), |
||
| 72 | array('data' => t('Operations'), 'header' => TRUE), |
||
| 73 | ); |
||
| 74 | foreach ($result as $event) { |
||
| 75 | if (isset($event['wd-user'])) { |
||
| 76 | $account = $event['wd-user']; |
||
| 77 | unset($event['wd-user']); |
||
| 78 | $ip = $dblog['ip']; |
||
| 79 | $request_uri = $dblog['request_uri']; |
||
| 80 | $referer = $dblog['referer']; |
||
| 81 | $link = $dblog['link']; |
||
| 82 | $dblog['variables'] = $event; |
||
| 83 | } |
||
| 84 | else { |
||
| 85 | $account = $event['user']; |
||
| 86 | $ip = $event['ip']; |
||
| 87 | $request_uri = $event['request_uri']; |
||
| 88 | $referer = $event['referer']; |
||
| 89 | $link = $event['link']; |
||
| 90 | $dblog['variables'] = $event['variables']; |
||
| 91 | } |
||
| 92 | $rows[] = array( |
||
| 93 | format_date($event['timestamp'], 'short'), |
||
| 94 | l($account['name'], 'user/' . $account['uid']), |
||
| 95 | $request_uri ? l(truncate_utf8(basename(($request_uri)), 20), $request_uri) : '', |
||
| 96 | $referer ? l(truncate_utf8(basename(($referer)), 20), $referer) : '', |
||
| 97 | check_plain($ip), |
||
| 98 | _mongodb_watchdog_format_message($dblog), |
||
| 99 | $link, |
||
| 100 | ); |
||
| 101 | } |
||
| 102 | $build['mongodb_watchdog_event_table']['messages'] = array( |
||
| 103 | '#theme' => 'table', |
||
| 104 | '#header' => $header, |
||
| 105 | '#rows' => $rows, |
||
| 106 | ); |
||
| 107 | if ($total > $limit) { |
||
| 108 | $build['mongodb_watchdog_event_table']['pager'] = array( |
||
| 109 | '#theme' => 'pager', |
||
| 110 | ); |
||
| 111 | |||
| 112 | } |
||
| 113 | return $build; |
||
| 114 | } |
||
| 115 | |||
| 331 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.