| Conditions | 10 |
| Paths | 7 |
| Total Lines | 40 |
| Code Lines | 25 |
| 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 |
||
| 24 | public function formElement( |
||
| 25 | Request $request, |
||
| 26 | ComponentInterface $component, |
||
| 27 | $field, |
||
| 28 | $id = null |
||
| 29 | ) { |
||
| 30 | if (! (is_null($id) && $component->isCreate()) || ! ($id && $component->isEdit())) { |
||
| 31 | throw new AuthenticationException(); |
||
| 32 | } |
||
| 33 | |||
| 34 | if (! $request->hasFile($field)) { |
||
| 35 | throw new InvalidArgumentException('Not found upload file'); |
||
| 36 | } |
||
| 37 | |||
| 38 | $file = $request->file($field); |
||
| 39 | if (is_null($file) || ! ($file instanceof UploadedFile)) { |
||
| 40 | throw new InvalidArgumentException('Must be upload file'); |
||
| 41 | } |
||
| 42 | |||
| 43 | if (is_null($id)) { |
||
| 44 | $form = $component->fireCreate(); |
||
| 45 | } else { |
||
| 46 | $form = $component->fireEdit($id); |
||
| 47 | } |
||
| 48 | |||
| 49 | $element = $form->getElement($field); |
||
| 50 | if (! ($element instanceof File)) { |
||
| 51 | throw new InvalidArgumentException( |
||
| 52 | sprintf( |
||
| 53 | '[%s] element must be instanced of "%s".', |
||
| 54 | $field, |
||
| 55 | File::class |
||
| 56 | ) |
||
| 57 | ); |
||
| 58 | } |
||
| 59 | |||
| 60 | $data = $element->saveFile($file); |
||
| 61 | |||
| 62 | return response()->json($data); |
||
| 63 | } |
||
| 64 | } |
||
| 65 |