| Conditions | 17 |
| Paths | 28 |
| Total Lines | 62 |
| 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 |
||
| 34 | public function indexDataTableResults(Request $request) |
||
| 35 | { |
||
| 36 | list($colSortBy, $colOrderBy) = $this->getSortInformation($this->columns, $request); |
||
| 37 | list($totalRows, $totalRowsFiltered) = $this->getRowsTotals($request->get('search')['value'] ?? null); |
||
| 38 | |||
| 39 | // Init |
||
| 40 | $entity = $this->entity; |
||
| 41 | |||
| 42 | // Relation Fields |
||
| 43 | if ($belongToFields = $this->getBelongToFields()) { |
||
| 44 | $entity = $entity->with($belongToFields); |
||
| 45 | } |
||
| 46 | |||
| 47 | // Filters |
||
| 48 | $entity = $this->filters($entity, $request); |
||
| 49 | |||
| 50 | // Search |
||
| 51 | $entity = $this->applySearchScope($entity, $request->get('search')['value'] ?? null); |
||
| 52 | |||
| 53 | // Order By |
||
| 54 | $entity = $entity->orderBy($colSortBy, $colOrderBy); |
||
| 55 | |||
| 56 | // Pagination |
||
| 57 | $entity = $entity->offset($request->get('start') ?? 0) |
||
| 58 | ->limit($request->get('length') ?? $totalRows); |
||
| 59 | |||
| 60 | // Get rows |
||
| 61 | $rows = $entity->get(); |
||
| 62 | $rowAsArray = $rows->toArray(); |
||
| 63 | |||
| 64 | $fields = $this->fields; |
||
| 65 | |||
| 66 | foreach ($rows as $key => $row) { |
||
| 67 | foreach ($this->columns as $name) { |
||
| 68 | $rowAsArray[$key][$name] = $row->$name ?? null; |
||
| 69 | $rowAsArray[$key]['values'][$name] = $row->$name ?? 'N/A'; |
||
| 70 | |||
| 71 | if ($fields[$name]['type'] == 'select') { |
||
| 72 | if (isset($fields[$name]['config']['options']) && count($fields[$name]['config']['options'])) { |
||
| 73 | $rowAsArray[$key]['values'][$name] = $fields[$name]['config']['options'][$row->$name] ?? 'N/A'; |
||
| 74 | } else { |
||
| 75 | $rowAsArray[$key]['values'][$name] = $row->{$fields[$name]['config']['rel']}->{$fields[$name]['config']['field_value']} ?? 'N/A'; |
||
| 76 | } |
||
| 77 | } elseif ($fields[$name]['type'] == 'date' && is_object($row->$name)) { |
||
| 78 | $rowAsArray[$key]['value'] = !empty($row->$name) && $row->$name->diff(Carbon::now())->format('%y') != date('Y') ? $row->$name->format('m/d/Y') : 'N/A'; |
||
| 79 | } elseif ($fields[$name]['type'] == 'datetime' && is_object($row->$name)) { |
||
| 80 | $rowAsArray[$key]['value'] = !empty($row->$name) && $row->$name->diff(Carbon::now())->format('%y') != date('Y') ? $row->$name->format('m/d/Y h:ia') : 'N/A'; |
||
| 81 | } elseif (substr($fields[$name]['type'], 0, 4) == 'file' && $row->getMedia($name)->last()) { |
||
| 82 | $rowAsArray[$key]['value'] = '<a href="#" target="_blank"></a>'; |
||
| 83 | } |
||
| 84 | } |
||
| 85 | } |
||
| 86 | |||
| 87 | return response()->json([ |
||
| 88 | 'data' => $rowAsArray, |
||
| 89 | 'draw' => (int) ($request->get('draw') ?? 0), |
||
| 90 | 'recordsFiltered' => $totalRowsFiltered, |
||
| 91 | 'recordsTotal' => $totalRows, |
||
| 92 | 'colSortBy' => $colSortBy, |
||
| 93 | 'colOrderBy' => $colOrderBy, |
||
| 94 | ]); |
||
| 95 | } |
||
| 96 | |||
| 144 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: