| Conditions | 7 |
| Paths | 22 |
| Total Lines | 71 |
| Code Lines | 57 |
| 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 |
||
| 50 | public function getFields($invoice) |
||
| 51 | { |
||
| 52 | try { |
||
| 53 | //dd($invoice); |
||
| 54 | $item = []; |
||
| 55 | $data = []; |
||
| 56 | $user = \Auth::user(); |
||
| 57 | if (!$user) { |
||
| 58 | throw new \Exception('No autherized user'); |
||
| 59 | } |
||
| 60 | $config = $this->paypal->where('id', 1)->first(); |
||
| 61 | if ($config) { |
||
| 62 | $business = $config->business; |
||
| 63 | $cmd = $config->cmd; |
||
| 64 | $return = $config->success_url; |
||
| 65 | $cancel_return = $config->cancel_url; |
||
| 66 | $notify_url = $config->notify_url; |
||
| 67 | $image_url = $config->image_url; |
||
| 68 | $rm = 1; |
||
| 69 | $currency_code = $invoice->currency; |
||
| 70 | $invoice_id = $invoice->id; |
||
| 71 | $first_name = $user->first_name; |
||
| 72 | $last_name = $user->last_name; |
||
| 73 | $address1 = $user->address; |
||
| 74 | $city = $user->town; |
||
| 75 | $zip = $user->zip; |
||
| 76 | $email = $user->email; |
||
| 77 | $product_name = ''; |
||
| 78 | if ($invoice->invoiceItem()->first()) { |
||
| 79 | $product_name = str_replace(' ', '-', $invoice->invoiceItem()->first()->product_name); |
||
| 80 | } |
||
| 81 | |||
| 82 | $data = [ |
||
| 83 | 'business' => $business, |
||
| 84 | 'cmd' => $cmd, |
||
| 85 | 'return' => $return, |
||
| 86 | 'cancel_return' => $cancel_return, |
||
| 87 | 'notify_url' => $notify_url, |
||
| 88 | 'image_url' => $image_url, |
||
| 89 | 'rm' => $rm, |
||
| 90 | 'currency_code' => 'USD', //$currency_code, |
||
| 91 | 'invoice' => $invoice_id, |
||
| 92 | 'first_name' => $first_name, |
||
| 93 | 'last_name' => $last_name, |
||
| 94 | 'address1' => $address1, |
||
| 95 | 'city' => $city, |
||
| 96 | 'zip' => $zip, |
||
| 97 | 'email' => $email, |
||
| 98 | 'item_name' => $product_name, |
||
| 99 | ]; |
||
| 100 | |||
| 101 | $items = $invoice->invoiceItem()->get()->toArray(); |
||
| 102 | //dd($items); |
||
| 103 | $c = count($items); |
||
| 104 | if (count($items) > 0) { |
||
| 105 | for ($i = 0; $i < $c; $i++) { |
||
| 106 | $n = $i + 1; |
||
| 107 | $item = [ |
||
| 108 | "item_name_$n" => $items[$i]['product_name'], |
||
| 109 | "quantity_$n" => $items[$i]['quantity'], |
||
| 110 | ]; |
||
| 111 | } |
||
| 112 | $data = array_merge($data, $item); |
||
| 113 | $total = ['amount' => $invoice->grand_total]; |
||
| 114 | $data = array_merge($data, $total); |
||
| 115 | } |
||
| 116 | } |
||
| 117 | |||
| 118 | return $data; |
||
| 119 | } catch (\Exception $ex) { |
||
| 120 | throw new \Exception($ex->getMessage(), $ex->getCode(), $ex->getPrevious()); |
||
| 121 | } |
||
| 243 |
This check looks for method names that are not written in camelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes
databaseConnectionSeeker.