| Conditions | 6 |
| Paths | 8 |
| Total Lines | 56 |
| Code Lines | 31 |
| 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 |
||
| 127 | public function handleQuery(ServerRequestInterface $request, Builder $query, array $search_columns, array $sort_columns, Closure $callback): ResponseInterface |
||
| 128 | { |
||
| 129 | $search = Validator::queryParams($request)->array('search')['value'] ?? ''; |
||
| 130 | $start = Validator::queryParams($request)->integer('start', 0); |
||
| 131 | $length = Validator::queryParams($request)->integer('length', 0); |
||
| 132 | $order = Validator::queryParams($request)->array('order'); |
||
| 133 | $draw = Validator::queryParams($request)->integer('draw', 0); |
||
| 134 | |||
| 135 | // Count unfiltered records |
||
| 136 | $recordsTotal = (clone $query)->count(); |
||
| 137 | |||
| 138 | // Filtering |
||
| 139 | if ($search !== '') { |
||
| 140 | $query->where(static function (Builder $query) use ($search, $search_columns): void { |
||
| 141 | $like = '%' . addcslashes($search, '\\%_') . '%'; |
||
| 142 | $like = strtr($like, [' ' => '%']); |
||
| 143 | |||
| 144 | foreach ($search_columns as $search_column) { |
||
| 145 | $query->orWhere($search_column, 'LIKE', $like); |
||
| 146 | } |
||
| 147 | }); |
||
| 148 | } |
||
| 149 | |||
| 150 | // Sorting |
||
| 151 | if ($order !== []) { |
||
| 152 | foreach ($order as $value) { |
||
| 153 | // Columns in datatables are numbered from zero. |
||
| 154 | // Columns in MySQL are numbered starting with one. |
||
| 155 | // If not specified, the Nth table column maps onto the Nth query column. |
||
| 156 | $sort_column = $sort_columns[$value['column']] ?? new Expression(1 + $value['column']); |
||
| 157 | |||
| 158 | $query->orderBy($sort_column, $value['dir']); |
||
| 159 | } |
||
| 160 | } else { |
||
| 161 | $query->orderBy(new Expression(1)); |
||
| 162 | } |
||
| 163 | |||
| 164 | // Paginating |
||
| 165 | if ($length > 0) { |
||
| 166 | $recordsFiltered = (clone $query)->count(); |
||
| 167 | |||
| 168 | $query->skip($start)->limit($length); |
||
| 169 | $data = $query->get(); |
||
| 170 | } else { |
||
| 171 | $data = $query->get(); |
||
| 172 | |||
| 173 | $recordsFiltered = $data->count(); |
||
| 174 | } |
||
| 175 | |||
| 176 | $data = $data->map($callback)->all(); |
||
| 177 | |||
| 178 | return response([ |
||
| 179 | 'draw' => $draw, |
||
| 180 | 'recordsTotal' => $recordsTotal, |
||
| 181 | 'recordsFiltered' => $recordsFiltered, |
||
| 182 | 'data' => $data, |
||
| 183 | ]); |
||
| 186 |