| Conditions | 6 |
| Paths | 8 |
| Total Lines | 54 |
| Code Lines | 35 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 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 |
||
| 24 | public function getStatementByPeriod(Request $request) |
||
| 25 | { |
||
| 26 | $rules = [ |
||
| 27 | 'dateStart' => 'required|date', |
||
| 28 | 'dateEnd' => 'required|date', |
||
| 29 | ]; |
||
| 30 | |||
| 31 | $validator = \Validator::make($request->all(), $rules); |
||
| 32 | |||
| 33 | if ($validator->fails()) { |
||
| 34 | return response()->json(['status' => 'error', 'errors' => $validator->errors()]); |
||
| 35 | } |
||
| 36 | |||
| 37 | $user = $this->model->where('id', $request->user()->id)->first(); |
||
| 38 | |||
| 39 | if (!$user) { |
||
| 40 | return response()->json(['status' => 'error', 'message' => 'Opss. Usuário não foi encontrado, favor verifique se esta logado.']); |
||
| 41 | } |
||
| 42 | |||
| 43 | $dateStart = $request->dateStart; |
||
| 44 | $dateEnd = $request->dateEnd; |
||
| 45 | $status = ($request->status !== null && $request->status !== false && $request->status !== '') |
||
| 46 | ? |
||
| 47 | ['status' => $request->status] |
||
| 48 | : |
||
| 49 | []; |
||
| 50 | |||
| 51 | $billPays = $user->bill_pays()->selectRaw('bill_pays.*, categories.name as category_name') |
||
| 52 | ->join('categories', 'categories.id', '=', 'bill_pays.category_id') |
||
| 53 | ->whereBetween('date_launch', [$dateStart, $dateEnd]) |
||
| 54 | ->orderBy('date_launch', 'DESC') |
||
| 55 | ->where($status) |
||
| 56 | ->get(); |
||
| 57 | |||
| 58 | $billReceives = $user->bill_receives()->selectRaw('bill_receives.*, categories.name as category_name') |
||
| 59 | ->join('categories', 'categories.id', '=', 'bill_receives.category_id') |
||
| 60 | ->whereBetween('date_launch', [$dateStart, $dateEnd]) |
||
| 61 | ->orderBy('date_launch', 'DESC') |
||
| 62 | ->where($status) |
||
| 63 | ->get(); |
||
| 64 | |||
| 65 | $collection = new Collection(array_merge_recursive($billPays->toArray(), $billReceives->toArray())); |
||
| 66 | $statements = $collection->sortByDesc('date_launch'); |
||
| 67 | |||
| 68 | $total_pays = $billPays->sum('value'); |
||
| 69 | $total_receives = $billReceives->sum('value'); |
||
| 70 | |||
| 71 | $data['billPays'] = $billPays; |
||
|
|
|||
| 72 | $data['billReceives'] = $billReceives; |
||
| 73 | $data['total_pays'] = $total_pays; |
||
| 74 | $data['total_receives'] = $total_receives; |
||
| 75 | $data['statements'] = $statements; |
||
| 76 | |||
| 77 | return response()->json(['status' => 'success', 'data' => $data]); |
||
| 78 | } |
||
| 159 | } |