| Conditions | 8 |
| Paths | 25 |
| Total Lines | 63 |
| Code Lines | 47 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| 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 |
||
| 77 | function mongodb_watchdog_watchdog(array $log_entry) { |
||
| 78 | $watchdog_limit = variable_get('watchdog_limit', WATCHDOG_DEBUG); |
||
| 79 | if (isset($log_entry['severity']) && $log_entry['severity'] > $watchdog_limit) { |
||
| 80 | return; |
||
| 81 | } |
||
| 82 | |||
| 83 | static $checked_ids = array(); |
||
| 84 | |||
| 85 | // Find the function that generated this error. |
||
| 86 | $log_entry = (array) $log_entry; |
||
| 87 | _mongodb_watchdog_enhance_log_entry($log_entry, debug_backtrace()); |
||
| 88 | $account = $log_entry['user']; |
||
| 89 | // Special handling for core bug #904994: |
||
| 90 | if (!isset($log_entry['variables'])) { |
||
| 91 | $special_messages = array( |
||
| 92 | 'page not found' => 'Page not found: @param', |
||
| 93 | 'access denied' => 'Access denied: @param', |
||
| 94 | ); |
||
| 95 | $type = $log_entry['type']; |
||
| 96 | $log_entry['variables'] = array('@param' => $log_entry['message']); |
||
| 97 | $log_entry['message'] = isset($special_messages[$type]) |
||
| 98 | ? $special_messages[$log_entry['type']] |
||
| 99 | : '@param'; |
||
| 100 | } |
||
| 101 | |||
| 102 | $event = array( |
||
| 103 | 'variables' => $log_entry['variables'], |
||
| 104 | 'timestamp' => $log_entry['timestamp'], |
||
| 105 | 'user' => array( |
||
| 106 | 'name' => isset($account->name) ? $account->name : variable_get('anonymous', t('Anonymous')), |
||
| 107 | 'uid' => $log_entry['uid'], |
||
| 108 | ), |
||
| 109 | 'ip' => $log_entry['ip'], |
||
| 110 | 'request_uri' => $log_entry['request_uri'], |
||
| 111 | 'referer' => $log_entry['referer'], |
||
| 112 | 'link' => $log_entry['link'], |
||
| 113 | ); |
||
| 114 | unset($log_entry['variables'], $log_entry['user'], $log_entry['ip'], $log_entry['request_uri'], $log_entry['referer'], $log_entry['link']); |
||
| 115 | |||
| 116 | $newobj = array( |
||
| 117 | '$set' => $log_entry, |
||
| 118 | '$inc' => array('count' => 1), |
||
| 119 | ); |
||
| 120 | $collection = mongodb_collection(variable_get('mongodb_watchdog', 'watchdog')); |
||
| 121 | $id = md5($log_entry['function'] . ':' . $log_entry['line'] . ':' . $log_entry['severity'] . ':' . $log_entry['type'] . ':' . $log_entry['message']); |
||
| 122 | if (!isset($checked_ids[$id])) { |
||
| 123 | $checked_ids[$id] = $collection->findOne(array('_id' => $id), array('_id' => 1)); |
||
| 124 | } |
||
| 125 | $collection->update(array('_id' => $id), $newobj, array('upsert' => TRUE) + mongodb_default_write_options(FALSE)); |
||
| 126 | $collection = $collection->db->selectCollection('watchdog_event_' . $id); |
||
| 127 | if (empty($checked_ids[$id])) { |
||
| 128 | $max = variable_get('mongodb_watchdog_items', 10000); |
||
| 129 | $command = array( |
||
| 130 | 'create' => $collection->getName(), |
||
| 131 | 'capped' => TRUE, |
||
| 132 | 'size' => $max * 1000, |
||
| 133 | "max" => $max, |
||
| 134 | ); |
||
| 135 | $collection->db->command($command); |
||
| 136 | $checked_ids[$id] = TRUE; |
||
| 137 | } |
||
| 138 | $collection->insert($event, mongodb_default_write_options(FALSE)); |
||
| 139 | } |
||
| 140 | |||
| 179 |
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.