| Conditions | 5 |
| Paths | 5 |
| Total Lines | 75 |
| Code Lines | 49 |
| 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 |
||
| 150 | protected function top($type) { |
||
| 151 | $ret = []; |
||
| 152 | $type_param = ['%type' => $type]; |
||
| 153 | $limit = 50; |
||
| 154 | |||
| 155 | // Safety net. |
||
| 156 | $types = [ |
||
| 157 | 'page not found', |
||
| 158 | 'access denied', |
||
| 159 | ]; |
||
| 160 | if (!in_array($type, $types)) { |
||
| 161 | $error = t('Unknown top report type: %type', $type_param); |
||
| 162 | drupal_set_message($error, 'error'); |
||
| 163 | $this->logger->warning('Unknown top report type: %type', $type_param); |
||
| 164 | $ret = [ |
||
| 165 | '#markup' => '', |
||
| 166 | ]; |
||
| 167 | return $ret; |
||
| 168 | } |
||
| 169 | |||
| 170 | // Find _id for the error type. |
||
| 171 | $template_collection = $this->watchdog->templateCollection(); |
||
| 172 | $template = $template_collection->findOne(['type' => $type], ['_id']); |
||
| 173 | |||
| 174 | // Method findOne() will return NULL if no row is found. |
||
| 175 | $ret['empty'] = array( |
||
| 176 | '#markup' => t('No "%type" message found', $type_param), |
||
| 177 | '#prefix' => '<div class="mongodb-watchdog-message">', |
||
| 178 | '#suffix' => '</div>', |
||
| 179 | ); |
||
| 180 | if (empty($template)) { |
||
| 181 | return $ret; |
||
| 182 | } |
||
| 183 | |||
| 184 | // Find occurrences of error type. |
||
| 185 | $collection_name = $template['_id']; |
||
| 186 | $event_collection = $this->watchdog->eventCollection($collection_name); |
||
| 187 | |||
| 188 | $key = ['variables.@uri' => 1]; |
||
| 189 | $cond = []; |
||
| 190 | $reduce = <<<EOT |
||
| 191 | function (doc, accumulator) { |
||
| 192 | accumulator.count++; |
||
| 193 | } |
||
| 194 | EOT; |
||
| 195 | $initial = ['count' => 0]; |
||
| 196 | $counts = $this->group($event_collection, $key, $cond, $reduce, $initial); |
||
| 197 | |||
| 198 | if (empty($counts['ok'])) { |
||
| 199 | return $ret; |
||
| 200 | } |
||
| 201 | |||
| 202 | $counts = $counts['retval']; |
||
| 203 | usort($counts, [$this, 'topSort']); |
||
| 204 | $counts = array_slice($counts, 0, $limit); |
||
| 205 | |||
| 206 | $header = array( |
||
| 207 | t('#'), |
||
| 208 | t('Paths'), |
||
| 209 | ); |
||
| 210 | $rows = array(); |
||
| 211 | foreach ($counts as $count) { |
||
| 212 | $rows[] = array( |
||
| 213 | $count['count'], |
||
| 214 | $count['variables.@uri'], |
||
| 215 | ); |
||
| 216 | } |
||
| 217 | |||
| 218 | $ret = array( |
||
| 219 | '#theme' => 'table', |
||
| 220 | '#header' => $header, |
||
| 221 | '#rows' => $rows, |
||
| 222 | ); |
||
| 223 | return $ret; |
||
| 224 | } |
||
| 225 | |||
| 242 |
Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.