| Conditions | 10 |
| Paths | 288 |
| Total Lines | 100 |
| Code Lines | 80 |
| 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 |
||
| 114 | break; |
||
| 115 | |||
| 116 | case t('Reset'): |
||
| 117 | $_SESSION['mongodb_watchdog_overview_filter'] = array(); |
||
| 118 | break; |
||
| 119 | } |
||
| 120 | return 'admin/reports/mongodb'; |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Gets all available filter types. |
||
| 125 | * |
||
| 126 | * @return array |
||
| 127 | * An array of message type names. |
||
| 128 | */ |
||
| 129 | function _mongodb_watchdog_get_message_types() { |
||
| 130 | // As of version 1.0.1, the PHP driver doesn't expose the 'distinct' command. |
||
| 131 | $collection = mongodb_collection(variable_get('mongodb_watchdog', 'watchdog')); |
||
| 132 | $result = $collection->db->command(array('distinct' => $collection->getName(), 'key' => 'type')); |
||
| 133 | return $result['values']; |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Return form for mongodb_watchdog clear button. |
||
| 138 | * |
||
| 139 | * @ingroup forms |
||
| 140 | * @see dblog_clear_log_submit() |
||
| 141 | * |
||
| 142 | * @return array |
||
| 143 | * A form array. |
||
| 144 | */ |
||
| 145 | function mongodb_watchdog_clear_log_form($form) { |
||
| 146 | $form['mongodb_watchdog_clear'] = array( |
||
| 147 | '#type' => 'fieldset', |
||
| 148 | '#title' => t('Clear log messages'), |
||
| 149 | '#description' => t('This will permanently remove the log messages from the database.'), |
||
| 150 | '#collapsible' => TRUE, |
||
| 151 | '#collapsed' => TRUE, |
||
| 152 | ); |
||
| 153 | |||
| 154 | $form['mongodb_watchdog_clear']['clear'] = array( |
||
| 155 | '#type' => 'submit', |
||
| 156 | '#value' => t('Clear log messages'), |
||
| 157 | '#submit' => array('mongodb_watchdog_clear_log_submit'), |
||
| 158 | ); |
||
| 159 | |||
| 160 | return $form; |
||
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Submit callback: clear database with log messages. |
||
| 165 | */ |
||
| 166 | function mongodb_watchdog_clear_log_submit() { |
||
| 167 | try { |
||
| 168 | // Drop the watchdog collection. |
||
| 169 | $collection = mongodb_collection(variable_get('mongodb_watchdog', 'watchdog')); |
||
| 170 | $collection->db->dropCollection($collection->getName()); |
||
| 171 | |||
| 172 | // Recreate the indexes. |
||
| 173 | module_load_include('install', 'mongodb_watchdog'); |
||
| 174 | mongodb_watchdog_ensure_indexes(); |
||
| 175 | |||
| 176 | // Drop the event collections. |
||
| 177 | foreach ($collection->db->listCollections() as $table) { |
||
| 178 | $parts = explode('.', $table); |
||
| 179 | if (substr($parts[1], 0, 15) == 'watchdog_event_') { |
||
| 180 | $collection->db->dropCollection($table); |
||
| 181 | } |
||
| 182 | } |
||
| 183 | |||
| 184 | drupal_set_message(t('MongoDB log cleared.')); |
||
| 185 | } |
||
| 186 | catch (Exception $e) { |
||
| 187 | drupal_set_message(t('An error occured while clearing the MongoDB log.'), 'error'); |
||
| 188 | } |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Build a MongoDB query based on the selected filters. |
||
| 193 | * |
||
| 194 | * Refer to the @link https://jira.mongodb.org/browse/PHP-1051 Mongo Issue regarding the $in value @endlink |
||
| 195 | * Refer to the @link https://jira.mongodb.org/browse/PHP-104 Mongo Issue regarding numeric keys on objects @endlink |
||
| 196 | * @return array |
||
| 197 | * An array to build a MongoDB query. |
||
| 198 | */ |
||
| 199 | function mongodb_watchdog_build_filter_query() { |
||
| 200 | if (empty($_SESSION['mongodb_watchdog_overview_filter'])) { |
||
| 201 | return array(); |
||
| 202 | } |
||
| 203 | |||
| 204 | // Build query. |
||
| 205 | $where = $args = array(); |
||
| 206 | $types = $_SESSION['mongodb_watchdog_overview_filter']['type'] ? $_SESSION['mongodb_watchdog_overview_filter']['type'] : NULL; |
||
| 207 | $severities = $_SESSION['mongodb_watchdog_overview_filter']['severity'] ? $_SESSION['mongodb_watchdog_overview_filter']['severity'] : NULL; |
||
| 208 | |||
| 209 | $find = array(); |
||
| 210 | if ($types) { |
||
| 211 | $find['type'] = array('$in' => array_values($types)); |
||
| 212 | } |
||
| 213 | if ($severities) { |
||
| 214 | // MongoDB is picky about types, ensure the severities are all integers. |
||
| 219 |
This check looks for the generic type
arrayas a return type and suggests a more specific type. This type is inferred from the actual code.