| Conditions | 6 |
| Paths | 12 |
| Total Lines | 68 |
| Code Lines | 54 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 1 | 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 |
||
| 45 | public function getFields($invoice) |
||
| 46 | { |
||
| 47 | try { |
||
| 48 | //dd($invoice); |
||
| 49 | $item = []; |
||
| 50 | $data = []; |
||
| 51 | $user = \Auth::user(); |
||
| 52 | if (!$user) { |
||
| 53 | throw new \Exception('No autherized user'); |
||
| 54 | } |
||
| 55 | $config = $this->paypal->where('id', 1)->first(); |
||
| 56 | if ($config) { |
||
| 57 | $business = $config->business; |
||
| 58 | $cmd = $config->cmd; |
||
| 59 | $return = $config->success_url; |
||
| 60 | $cancel_return = $config->cancel_url; |
||
| 61 | $notify_url = $config->notify_url; |
||
| 62 | $image_url = $config->image_url; |
||
| 63 | $rm = 1; |
||
| 64 | $currency_code = $invoice->currency; |
||
| 65 | $invoice_id = $invoice->id; |
||
| 66 | $first_name = $user->first_name; |
||
| 67 | $last_name = $user->last_name; |
||
| 68 | $address1 = $user->address; |
||
| 69 | $city = $user->town; |
||
| 70 | $zip = $user->zip; |
||
| 71 | $email = $user->email; |
||
| 72 | |||
| 73 | $data = [ |
||
| 74 | 'business' => $business, |
||
| 75 | 'cmd' => $cmd, |
||
| 76 | 'return' => $return, |
||
| 77 | 'cancel_return' => $cancel_return, |
||
| 78 | 'notify_url' => $notify_url, |
||
| 79 | 'image_url' => $image_url, |
||
| 80 | 'rm' => $rm, |
||
| 81 | 'currency_code' => $currency_code, |
||
| 82 | 'invoice' => $invoice_id, |
||
| 83 | 'first_name' => $first_name, |
||
| 84 | 'last_name' => $last_name, |
||
| 85 | 'address1' => $address1, |
||
| 86 | 'city' => $city, |
||
| 87 | 'zip' => $zip, |
||
| 88 | 'email' => $email, |
||
| 89 | ]; |
||
| 90 | |||
| 91 | $items = $invoice->invoiceItem()->get()->toArray(); |
||
| 92 | //dd($items); |
||
| 93 | if (count($items) > 0) { |
||
| 94 | for ($i = 0; $i < count($items); $i++) { |
||
|
|
|||
| 95 | $n = $i + 1; |
||
| 96 | $item = [ |
||
| 97 | "item_name_$n" => $items[$i]['product_name'], |
||
| 98 | "quantity_$n" => $items[$i]['quantity'], |
||
| 99 | ]; |
||
| 100 | } |
||
| 101 | $data = array_merge($data, $item); |
||
| 102 | $total = ['amount' => $invoice->grand_total]; |
||
| 103 | $data = array_merge($data, $total); |
||
| 104 | } |
||
| 105 | } |
||
| 106 | |||
| 107 | return $data; |
||
| 108 | } catch (\Exception $ex) { |
||
| 109 | dd($ex); |
||
| 110 | throw new \Exception($ex->getMessage(), $ex->getCode(), $ex->getPrevious()); |
||
| 111 | } |
||
| 112 | } |
||
| 113 | |||
| 161 |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: