| Conditions | 5 |
| Paths | 5 |
| Total Lines | 70 |
| Code Lines | 46 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 1 |
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 |
||
| 149 | /** @var \MongoDB\Database $database */ |
||
| 150 | $database = $container->get('mongodb.watchdog_storage'); |
||
| 151 | |||
| 152 | return new static($logger, $watchdog, $config, $database); |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Obtain the data from the logger. |
||
| 157 | * |
||
| 158 | * @param \Symfony\Component\HttpFoundation\Request $request |
||
| 159 | * The current request. Needed for paging. |
||
| 160 | * @param string $type |
||
| 161 | * The type of top list to retrieve. |
||
| 162 | * |
||
| 163 | * @return array |
||
| 164 | * The data array. |
||
| 165 | */ |
||
| 166 | protected function getRowData(Request $request, $type) { |
||
| 167 | // Find _id for the error type. |
||
| 168 | $templateCollection = $this->watchdog->templateCollection(); |
||
| 169 | $template = $templateCollection->findOne(['type' => $type], ['_id']); |
||
| 170 | if (empty($template)) { |
||
| 171 | return []; |
||
| 172 | } |
||
| 173 | |||
| 174 | // Find occurrences of error type. |
||
| 175 | $collectionName = $template['_id']; |
||
| 176 | $eventCollection = $this->watchdog->eventCollection($collectionName); |
||
| 177 | |||
| 178 | $key = ['variables.@uri' => 1]; |
||
| 179 | $cond = []; |
||
| 180 | $reducer = <<<JAVASCRIPT |
||
| 181 | function reducer(doc, accumulator) { |
||
| 182 | accumulator.count++; |
||
| 183 | } |
||
| 184 | JAVASCRIPT; |
||
| 185 | |||
| 186 | $initial = ['count' => 0]; |
||
| 187 | $counts = $this->group($eventCollection, $key, $cond, $reducer, $initial); |
||
| 188 | if (empty($counts['ok'])) { |
||
| 189 | return []; |
||
| 190 | } |
||
| 191 | |||
| 192 | $counts = $counts['retval']; |
||
| 193 | usort($counts, [$this, 'topSort']); |
||
| 194 | |||
| 195 | $page = $this->setupPager($request, count($counts)); |
||
| 196 | $skip = $page * $this->itemsPerPage; |
||
| 197 | $counts = array_slice($counts, $skip, $this->itemsPerPage); |
||
| 198 | |||
| 199 | return $counts; |
||
| 200 | } |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Command wrapper for missing MongoDB group() implementation in PHPlib. |
||
| 204 | * |
||
| 205 | * @param \MongoDB\Collection $collection |
||
| 206 | * The collection on which to perform the command. |
||
| 207 | * @param object $key |
||
| 208 | * The grouping key. |
||
| 209 | * @param object $cond |
||
| 210 | * The condition. |
||
| 211 | * @param string $reduce |
||
| 212 | * The reducer function: must be valid JavaScript code. |
||
| 213 | * @param object $initial |
||
| 214 | * The initial document. |
||
| 215 | * |
||
| 216 | * @return array|void |
||
| 217 | * Void in case of error, otherwise an array with the following keys: |
||
| 218 | * - waitedMS: time spent waiting |
||
| 219 | * - retval: an array of command results, containing at least the key |
||
| 258 |
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.