| Conditions | 7 |
| Paths | 11 |
| Total Lines | 65 |
| Code Lines | 36 |
| 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 |
||
| 30 | public function indexAction(Request $request) |
||
| 31 | { |
||
| 32 | $this->denyAccessUnlessGranted('ROLE_USER'); |
||
| 33 | $user = $this->getUser(); |
||
| 34 | |||
| 35 | $fieldNoteService = $this->get('app.service.field_note'); |
||
| 36 | $dataProvider = $this->get('app.dataprovider.upload_field_note'); |
||
| 37 | |||
| 38 | $repository = $this->getDoctrine()->getRepository('AppBundle:FieldNote'); |
||
| 39 | $fieldNotes = $repository->findBy([ |
||
| 40 | 'user' => $user->getId() |
||
| 41 | ], [ |
||
| 42 | 'date' => 'ASC', |
||
| 43 | 'id' => 'ASC' |
||
| 44 | ]); |
||
| 45 | |||
| 46 | $form = $this->createForm(UploadFieldNotesType::class, $dataProvider->getData($user->getId())); |
||
| 47 | $form->handleRequest($request); |
||
| 48 | |||
| 49 | if ($form->isSubmitted() && $form->isValid()) { |
||
| 50 | /** |
||
| 51 | * @var UploadedFile $file |
||
| 52 | */ |
||
| 53 | $file = $form->getData()[UploadFieldNotesType::FIELD_FILE]; |
||
| 54 | |||
| 55 | try { |
||
| 56 | $ignoreDate = null; |
||
| 57 | |||
| 58 | if (!empty($form->getData()[UploadFieldNotesType::FIELD_IGNORE])) { |
||
| 59 | $ignoreDate = DateUtil::dateTimeFromMySqlFormat( |
||
| 60 | $form->getData()[UploadFieldNotesType::FIELD_IGNORE_DATE] |
||
| 61 | ); |
||
| 62 | } |
||
| 63 | |||
| 64 | $fieldNoteService->importFromFile($file->getRealPath(), $user->getId(), $ignoreDate); |
||
| 65 | } catch (\Exception $e) { |
||
| 66 | $this->addErrorMessage($e->getMessage()); |
||
| 67 | |||
| 68 | return $this->redirectToRoute('field-notes'); |
||
| 69 | } |
||
| 70 | |||
| 71 | if ($fieldNoteService->hasErrors()) { |
||
| 72 | foreach ($fieldNoteService->getErrors() as $error) { |
||
| 73 | $this->addErrorMessage($error); |
||
| 74 | } |
||
| 75 | |||
| 76 | return $this->redirectToRoute('field-notes'); |
||
| 77 | } |
||
| 78 | |||
| 79 | $this->addSuccessMessage( |
||
| 80 | $this->get('translator')->trans('field_notes.upload.success') |
||
| 81 | ); |
||
| 82 | |||
| 83 | return $this->redirectToRoute('field-notes'); |
||
| 84 | } |
||
| 85 | |||
| 86 | $this->setMenu(MNU_MYPROFILE_FIELD_NOTES); |
||
| 87 | $this->setTitle($this->get('translator')->trans('field_notes.field_notes')); |
||
| 88 | |||
| 89 | return $this->render('field-notes/index.html.twig', [ |
||
| 90 | 'user' => $user, |
||
| 91 | 'form' => $form->createView(), |
||
| 92 | 'fieldNotes' => $fieldNotes, |
||
| 93 | ]); |
||
| 94 | } |
||
| 95 | |||
| 174 |