| Conditions | 10 |
| Paths | 47 |
| Total Lines | 46 |
| Code Lines | 36 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 25 | public function process(App\Request $request) |
||
| 26 | { |
||
| 27 | try { |
||
| 28 | $orderId = $message = ''; |
||
| 29 | $responseType = 'success'; |
||
| 30 | $order = new \App\YetiForce\Order(); |
||
| 31 | $order->setPackageId($request->getByType('packageId', \App\Purifier::ALNUM2)); |
||
| 32 | foreach ($order->getFieldInstances() as $field) { |
||
| 33 | $fieldName = $field->getName(); |
||
| 34 | if ($request->has($fieldName) && !$request->isEmpty($fieldName)) { |
||
| 35 | $value = $request->getRaw($fieldName); |
||
| 36 | if ($value && preg_match('/[^A-Za-zÀ-ž\W\d\s]+/u', (string) $value, $mas)) { |
||
| 37 | throw new \App\Exceptions\AppException('ERR_PLEASE_USE_LATIN_CHARACTERS'); |
||
| 38 | } |
||
| 39 | $value = $request->getByType($fieldName, $field->get('purifyType')); |
||
| 40 | $field->getUITypeModel()->validate($value, true); |
||
| 41 | $order->set($fieldName, $field->getDBValue($value)); |
||
| 42 | } else { |
||
| 43 | throw new \App\Exceptions\AppException('LBL_NOT_FILLED_MANDATORY_FIELDS'); |
||
| 44 | } |
||
| 45 | } |
||
| 46 | $result = $order->send(); |
||
| 47 | if ($error = $order->getError()) { |
||
| 48 | throw new \App\Exceptions\AppException($error); |
||
| 49 | } |
||
| 50 | if ($result) { |
||
| 51 | $orderId = $order->getId(); |
||
| 52 | } |
||
| 53 | } catch (\App\Exceptions\AppException $e) { |
||
| 54 | $result = false; |
||
| 55 | $responseType = 'error'; |
||
| 56 | $message = $e->getDisplayMessage(); |
||
| 57 | } catch (\Throwable $e) { |
||
| 58 | $result = false; |
||
| 59 | $responseType = 'error'; |
||
| 60 | $message = $e->getMessage(); |
||
| 61 | } |
||
| 62 | |||
| 63 | $response = new Vtiger_Response(); |
||
| 64 | $response->setResult([ |
||
| 65 | 'success' => $result ?? false, |
||
| 66 | 'message' => $message ?? '', |
||
| 67 | 'type' => $responseType, |
||
| 68 | 'orderId' => $orderId |
||
| 69 | ]); |
||
| 70 | $response->emit(); |
||
| 71 | } |
||
| 73 |