Total Complexity | 64 |
Total Lines | 372 |
Duplicated Lines | 0 % |
Changes | 15 | ||
Bugs | 3 | Features | 2 |
Complex classes like DataTables often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use DataTables, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class DataTables |
||
20 | { |
||
21 | /** @var array */ |
||
22 | private $reqData; |
||
23 | |||
24 | /** @var string */ |
||
25 | private $table; |
||
26 | |||
27 | /** @var array */ |
||
28 | private $tableColumns; |
||
29 | |||
30 | /** @var Model */ |
||
31 | private $model; |
||
32 | |||
33 | /** @var Builder */ |
||
34 | private $query; |
||
35 | |||
36 | /** @var Builder */ |
||
37 | private $originalQuery; |
||
38 | |||
39 | /** @var array|null */ |
||
40 | private $aliases; |
||
41 | |||
42 | /** @var DatabaseManager */ |
||
43 | private $DB; |
||
44 | |||
45 | public function __construct(Request $request, DatabaseManager $DB) |
||
46 | { |
||
47 | $this->reqData = $request->all(); |
||
48 | $this->DB = $DB; |
||
49 | } |
||
50 | |||
51 | public function withInput(array $requestData) |
||
52 | { |
||
53 | $this->reqData = $requestData; |
||
54 | |||
55 | return $this; |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * @param \Illuminate\Database\Eloquent\Model $model |
||
60 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
61 | * @param array $aliases |
||
62 | * |
||
63 | * @return \Illuminate\Http\JsonResponse |
||
64 | */ |
||
65 | public function provide(Model $model, Builder $query = null, array $aliases = null): JsonResponse |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * @param \Illuminate\Database\Eloquent\Model $model |
||
89 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
90 | * @param array $aliases |
||
91 | * |
||
92 | * @return \Illuminate\Database\Eloquent\Builder |
||
93 | */ |
||
94 | public function provideQuery(Model $model, Builder $query = null, array $aliases = null): ?Builder |
||
97 | } |
||
98 | |||
99 | private function provider(Model $model, Builder $query = null, array $aliases = null): ?Builder |
||
100 | { |
||
101 | if ( |
||
102 | array_key_exists('draw', $this->reqData) && |
||
103 | array_key_exists('start', $this->reqData) && |
||
104 | array_key_exists('length', $this->reqData) |
||
105 | ) { |
||
106 | $this->model = $model; |
||
107 | $this->query = $query ?? $this->model->newQuery(); |
||
108 | $this->aliases = $aliases; |
||
109 | |||
110 | $this->table = $model->getTable(); |
||
111 | $this->tableColumns = $this->DB |
||
112 | ->connection($model->getConnectionName()) |
||
113 | ->getDoctrineSchemaManager() |
||
114 | ->listTableColumns($this->table); |
||
115 | $this->tableColumns = $this->removeKeyQuotes($this->tableColumns); |
||
116 | |||
117 | $this->prepareSelects(); |
||
118 | $this->wrapWheres(); |
||
119 | |||
120 | $this->originalQuery = clone $this->query; |
||
121 | |||
122 | if (array_key_exists('columns', $this->reqData) && is_array($this->reqData['columns'])) { |
||
123 | $columns = $this->reqData['columns']; |
||
124 | |||
125 | if (is_array($this->reqData['columns'])) { |
||
126 | $this->applySearch($columns); |
||
127 | $this->applyOrder($columns); |
||
128 | } |
||
129 | } |
||
130 | |||
131 | return $this->query; |
||
132 | } |
||
133 | |||
134 | return null; |
||
135 | } |
||
136 | |||
137 | private function wrapWheres() |
||
138 | { |
||
139 | $query = $this->query->getQuery(); |
||
140 | |||
141 | $nq = $query->forNestedWhere(); |
||
142 | $nq->mergeWheres($query->wheres, $query->bindings); |
||
143 | |||
144 | $query->wheres = []; |
||
145 | $query->bindings['where'] = []; |
||
146 | |||
147 | $query->addNestedWhereQuery($nq); |
||
148 | } |
||
149 | |||
150 | private function removeKeyQuotes($array) |
||
151 | { |
||
152 | foreach ($array as $key => $value) { |
||
153 | $newKey = str_replace('`', '', $key); |
||
154 | |||
155 | if ($key !== $newKey) { |
||
156 | $array[$newKey] = $value; |
||
157 | unset($array[$key]); |
||
158 | } |
||
159 | } |
||
160 | |||
161 | return $array; |
||
162 | } |
||
163 | |||
164 | private function prepareSelects() |
||
189 | } |
||
190 | } |
||
191 | |||
192 | private function applySearch(array $columns) |
||
193 | { |
||
194 | foreach ($columns as $column) { |
||
195 | $searchValue = Arr::get($column, 'search.value'); |
||
196 | $searchColumn = Arr::get($column, 'data'); |
||
197 | |||
198 | if (!is_null($searchValue) && !is_null($searchColumn)) { |
||
199 | $searchField = $this->getField($searchColumn); |
||
200 | if (!$searchField) { |
||
201 | continue; |
||
202 | } |
||
203 | |||
204 | $searchMethod = $this->getSearchMethod($searchField); |
||
205 | [$searchQuery, $searchBindings] = $this->getSearchQuery($searchField, $searchValue, $searchColumn); |
||
206 | |||
207 | $this->query->{$searchMethod}($searchQuery, $searchBindings); |
||
208 | } |
||
209 | } |
||
210 | } |
||
211 | |||
212 | private function getSearchQuery($searchField, $searchValue, $column) |
||
213 | { |
||
214 | if ($this->isDateRange($searchValue)) { |
||
215 | [$from, $to] = explode(' - ', $searchValue); |
||
216 | |||
217 | $from = $this->toMySQLDate($from); |
||
218 | $to = $this->toMySQLDate($to, 1); |
||
219 | |||
220 | return [ |
||
221 | $searchField.' between ? and ?', |
||
222 | [$from, $to], |
||
223 | ]; |
||
224 | } else { |
||
225 | if ($this->shouldUseLike($this->tableColumns, $column)) { |
||
226 | return [ |
||
227 | $searchField.' like ?', |
||
228 | ['%'.$searchValue.'%'], |
||
229 | ]; |
||
230 | } else { |
||
231 | return [ |
||
232 | $searchField.' = ?', |
||
233 | [$searchValue], |
||
234 | ]; |
||
235 | } |
||
236 | } |
||
237 | } |
||
238 | |||
239 | private function isDateRange($value): bool |
||
243 | } |
||
244 | |||
245 | private function toMySQLDate($value, $plusDay = 0) |
||
246 | { |
||
247 | return Carbon::createFromFormat('d/m/Y', $value) |
||
248 | ->addDays($plusDay) |
||
249 | ->toDateString(); |
||
250 | } |
||
251 | |||
252 | private function applyOrder(array $columns) |
||
253 | { |
||
254 | if (array_key_exists('order', $this->reqData)) { |
||
255 | $orderColumnId = Arr::get($this->reqData, 'order.0.column'); |
||
256 | $orderByColumn = Arr::get($columns, $orderColumnId.'.data'); |
||
257 | $direction = Arr::get($this->reqData, 'order.0.dir'); |
||
258 | |||
259 | $this->applyQueryOrder($orderByColumn, $direction); |
||
260 | } |
||
261 | } |
||
262 | |||
263 | private function applyQueryOrder($orderByColumn, $direction) |
||
264 | { |
||
265 | if ($direction !== 'asc' && $direction !== 'desc') { |
||
266 | return; |
||
267 | } |
||
268 | |||
269 | $orderField = $this->getField($orderByColumn); |
||
270 | if (!$orderField) { |
||
271 | return; |
||
272 | } |
||
273 | |||
274 | $this->query->orderByRaw($orderField.' '.$direction); |
||
275 | } |
||
276 | |||
277 | private function getField($column) |
||
278 | { |
||
279 | if (empty($this->aliases) || !array_key_exists($column, $this->aliases)) { |
||
280 | if (array_key_exists($column, $this->tableColumns)) { |
||
281 | return $this->table.'.'.$column; |
||
282 | } else { |
||
283 | return null; |
||
284 | } |
||
285 | } else { |
||
286 | return $this->aliases[$column]; |
||
287 | } |
||
288 | } |
||
289 | |||
290 | private function setResultCounters(array $response): array |
||
291 | { |
||
292 | $response['recordsTotal'] = $this->getCount($this->originalQuery); |
||
293 | |||
294 | if ($this->withWheres() || $this->withHavings()) { |
||
295 | $response['recordsFiltered'] = $this->getCount($this->query); |
||
296 | } else { |
||
297 | $response['recordsFiltered'] = $response['recordsTotal']; |
||
298 | } |
||
299 | |||
300 | return $response; |
||
301 | } |
||
302 | |||
303 | private function withWheres() |
||
304 | { |
||
305 | return !empty($this->query->getQuery()->wheres) && |
||
306 | $this->originalQuery->getQuery()->wheres !== $this->query->getQuery()->wheres; |
||
307 | } |
||
308 | |||
309 | private function withHavings() |
||
310 | { |
||
311 | return !empty($this->query->getQuery()->havings) && |
||
312 | $this->originalQuery->getQuery()->havings !== $this->query->getQuery()->havings; |
||
313 | } |
||
314 | |||
315 | private function getCount(Builder $query): int |
||
316 | { |
||
317 | $countQuery = (clone $query)->getQuery(); |
||
318 | $countQuery->columns = [new Expression('0')]; |
||
319 | |||
320 | if (!empty($countQuery->groups) || !empty($countQuery->havings)) { |
||
321 | return $this->DB |
||
322 | ->table($this->DB->raw('('.$countQuery->toSql().') as s')) |
||
323 | ->setBindings($countQuery->getBindings()) |
||
324 | ->selectRaw('count(*) as count') |
||
325 | ->first() |
||
326 | ->count; |
||
327 | } else { |
||
328 | return $countQuery->count(); |
||
329 | } |
||
330 | } |
||
331 | |||
332 | private function applyPagination() |
||
333 | { |
||
334 | if (array_key_exists('start', $this->reqData)) { |
||
335 | $this->query->offset(+$this->reqData['start']); |
||
336 | } |
||
337 | |||
338 | if (array_key_exists('length', $this->reqData)) { |
||
339 | $this->query->limit(+$this->reqData['length']); |
||
340 | } |
||
341 | } |
||
342 | |||
343 | private function getSearchMethod($alias) |
||
344 | { |
||
345 | $aggregate = [ |
||
346 | 'AVG', |
||
347 | 'BIT_AND', |
||
348 | 'BIT_OR', |
||
349 | 'BIT_XOR', |
||
350 | 'COUNT', |
||
351 | 'GROUP_CONCAT', |
||
352 | 'JSON_ARRAYAGG', |
||
353 | 'JSON_OBJECTAGG', |
||
354 | 'MAX', |
||
355 | 'MIN', |
||
356 | 'STD', |
||
357 | 'STDDEV', |
||
358 | 'STDDEV_POP', |
||
359 | 'STDDEV_SAMP', |
||
360 | 'SUM', |
||
361 | 'VAR_POP', |
||
362 | 'VAR_SAMP', |
||
363 | 'VARIANCE', |
||
364 | ]; |
||
365 | |||
366 | foreach ($aggregate as $m) { |
||
367 | if (strpos($alias, $m) !== false) { |
||
368 | return 'havingRaw'; |
||
369 | } |
||
370 | } |
||
371 | |||
372 | return 'whereRaw'; |
||
373 | } |
||
374 | |||
375 | /** |
||
376 | * @param Column[] $tableColumns |
||
377 | * @param string $column |
||
378 | * |
||
379 | * @return mixed |
||
380 | */ |
||
381 | private function shouldUseLike($tableColumns, $column) |
||
391 | } |
||
392 | } |
||
393 |
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()
can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.