| Conditions | 15 |
| Paths | 72 |
| Total Lines | 117 |
| Code 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 |
||
| 36 | public function handle(Request $request) |
||
| 37 | { |
||
| 38 | $debugData = ['url' => $request->fullUrl(), 'data' => $request->all(), 'method' => $request->method()]; |
||
| 39 | |||
| 40 | Log::debug('Service Request', $debugData); |
||
| 41 | |||
| 42 | $data = $request->all(); |
||
| 43 | |||
| 44 | $postData = null; |
||
| 45 | |||
| 46 | if (isset($data['MMEX_Post'])) { |
||
| 47 | $postData = json_decode($data['MMEX_Post']); |
||
| 48 | } elseif (isset($data['postData'])) { |
||
| 49 | $postData = json_decode($data['postData']); |
||
| 50 | } |
||
| 51 | |||
| 52 | if ($postData) { |
||
| 53 | Log::debug('MmexController, $postData', [$postData]); |
||
| 54 | } |
||
| 55 | |||
| 56 | $function = $this->getFunction($data); |
||
| 57 | |||
| 58 | if ($function == MmexFunctions::CheckGuid) { |
||
| 59 | // TODO |
||
| 60 | return $this->returnSuccess(); |
||
| 61 | } |
||
| 62 | |||
| 63 | if ($function == MmexFunctions::CheckApiVersion) { |
||
| 64 | return $this->returnText(Constants::$api_version); |
||
| 65 | } |
||
| 66 | |||
| 67 | if ($function == MmexFunctions::DownloadTransactions) { |
||
| 68 | $transactions = $this->mmexService->getTransactions(); |
||
| 69 | |||
| 70 | $result = fractal() |
||
| 71 | ->collection($transactions) |
||
| 72 | ->serializeWith(new MmexArraySerializer()) |
||
| 73 | ->transformWith(new TransactionTransformer()) |
||
|
|
|||
| 74 | ->toJson(); |
||
| 75 | |||
| 76 | return $this->returnText($result); |
||
| 77 | } |
||
| 78 | |||
| 79 | if ($function == MmexFunctions::DonwloadAttachment) { |
||
| 80 | |||
| 81 | // is something like: Transaction_3_test-receipt-3.png |
||
| 82 | $fileName = $data["download_attachment"]; |
||
| 83 | |||
| 84 | // extract transaction |
||
| 85 | $fileNameParts = explode('_', $fileName); |
||
| 86 | $transactionId = $fileNameParts[1]; |
||
| 87 | $transaction = Transaction::findOrFail($transactionId); |
||
| 88 | |||
| 89 | // get attachment of transaction |
||
| 90 | $media = $transaction->getMedia('attachments')->first(function ($item) use ($fileName) { |
||
| 91 | return $item->file_name == $fileName; |
||
| 92 | }); |
||
| 93 | |||
| 94 | // return file as download |
||
| 95 | $filePath = $media->getPath(); |
||
| 96 | |||
| 97 | $headers = [ |
||
| 98 | "Content-Type" => "", |
||
| 99 | "Cache-Control" => "public", |
||
| 100 | "Content-Description" => "File Transfer", |
||
| 101 | "Content-Disposition" => "attachment; filename= ".$fileName, |
||
| 102 | "Content-Transfer-Encoding" => "binary", |
||
| 103 | ]; |
||
| 104 | |||
| 105 | return response()->file($filePath, $headers); |
||
| 106 | } |
||
| 107 | |||
| 108 | if ($function == MmexFunctions::DeleteBankAccounts) { |
||
| 109 | $this->mmexService->deleteAccounts(); |
||
| 110 | |||
| 111 | return $this->returnSuccess(); |
||
| 112 | } |
||
| 113 | |||
| 114 | if ($function == MmexFunctions::ImportBankAccounts) { |
||
| 115 | $this->mmexService->importBankAccounts($postData); |
||
| 116 | |||
| 117 | return $this->returnSuccess(); |
||
| 118 | } |
||
| 119 | |||
| 120 | if ($function == MmexFunctions::DeletePayees) { |
||
| 121 | $this->mmexService->deletePayees(); |
||
| 122 | |||
| 123 | return $this->returnSuccess(); |
||
| 124 | } |
||
| 125 | |||
| 126 | if ($function == MmexFunctions::ImportPayees) { |
||
| 127 | $this->mmexService->importPayees($postData); |
||
| 128 | |||
| 129 | return $this->returnSuccess(); |
||
| 130 | } |
||
| 131 | |||
| 132 | if ($function == MmexFunctions::DeleteCategories) { |
||
| 133 | $this->mmexService->deleteCategories(); |
||
| 134 | |||
| 135 | return $this->returnSuccess(); |
||
| 136 | } |
||
| 137 | |||
| 138 | if ($function == MmexFunctions::ImportCategories) { |
||
| 139 | $this->mmexService->importCategories($postData); |
||
| 140 | |||
| 141 | return $this->returnSuccess(); |
||
| 142 | } |
||
| 143 | |||
| 144 | if ($function == MmexFunctions::DeleteTransactions) { |
||
| 145 | $transactionId = $data['delete_group']; |
||
| 146 | $this->mmexService->deleteTransactions($transactionId); |
||
| 147 | |||
| 148 | return $this->returnSuccess(); |
||
| 149 | } |
||
| 150 | |||
| 151 | return $data; |
||
| 152 | } |
||
| 153 | |||
| 215 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: