| Conditions | 9 |
| Paths | 8 |
| Total Lines | 64 |
| Code Lines | 36 |
| 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 |
||
| 47 | public function handleCollection(ServerRequestInterface $request, Collection $collection, array $search_columns, array $sort_columns, Closure $callback): ResponseInterface |
||
| 48 | { |
||
| 49 | $search = $request->getQueryParams()['search']['value'] ?? ''; |
||
| 50 | $start = (int) ($request->getQueryParams()['start'] ?? 0); |
||
| 51 | $length = (int) ($request->getQueryParams()['length'] ?? 0); |
||
| 52 | $order = $request->getQueryParams()['order'] ?? []; |
||
| 53 | $draw = (int) ($request->getQueryParams()['draw'] ?? 0); |
||
| 54 | |||
| 55 | // Count unfiltered records |
||
| 56 | $recordsTotal = $collection->count(); |
||
| 57 | |||
| 58 | // Filtering |
||
| 59 | if ($search !== '') { |
||
| 60 | $collection = $collection->filter(static function (array $row) use ($search, $search_columns): bool { |
||
| 61 | foreach ($search_columns as $search_column) { |
||
| 62 | if (stripos($row[$search_column], $search) !== false) { |
||
| 63 | return true; |
||
| 64 | } |
||
| 65 | } |
||
| 66 | |||
| 67 | return false; |
||
| 68 | }); |
||
| 69 | } |
||
| 70 | |||
| 71 | // Sorting |
||
| 72 | if ($order !== []) { |
||
| 73 | $collection = $collection->sort(static function (array $row1, array $row2) use ($order, $sort_columns): int { |
||
| 74 | foreach ($order as $column) { |
||
| 75 | $key = $sort_columns[$column['column']]; |
||
| 76 | $dir = $column['dir']; |
||
| 77 | |||
| 78 | if ($dir === 'asc') { |
||
| 79 | $comparison = $row1[$key] <=> $row2[$key]; |
||
| 80 | } else { |
||
| 81 | $comparison = $row2[$key] <=> $row1[$key]; |
||
| 82 | } |
||
| 83 | |||
| 84 | if ($comparison !== 0) { |
||
| 85 | return $comparison; |
||
| 86 | } |
||
| 87 | } |
||
| 88 | |||
| 89 | return 0; |
||
| 90 | }); |
||
| 91 | } |
||
| 92 | |||
| 93 | // Paginating |
||
| 94 | if ($length > 0) { |
||
| 95 | $recordsFiltered = $collection->count(); |
||
| 96 | |||
| 97 | $data = $collection->slice($start, $length); |
||
| 98 | } else { |
||
| 99 | $recordsFiltered = $collection->count(); |
||
| 100 | |||
| 101 | $data = $collection; |
||
| 102 | } |
||
| 103 | |||
| 104 | $data = $data->map($callback)->values()->all(); |
||
| 105 | |||
| 106 | return response([ |
||
| 107 | 'draw' => $draw, |
||
| 108 | 'recordsTotal' => $recordsTotal, |
||
| 109 | 'recordsFiltered' => $recordsFiltered, |
||
| 110 | 'data' => $data, |
||
| 111 | ]); |
||
| 181 |