| Conditions | 8 |
| Paths | 16 |
| Total Lines | 54 |
| Code Lines | 32 |
| 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 |
||
| 60 | public function editAction($id = null, Request $request = null) |
||
| 61 | { |
||
| 62 | if (!$request) { |
||
| 63 | $request = $this->getRequest(); |
||
| 64 | } |
||
| 65 | if (!$request->isMethod('POST')) { |
||
| 66 | return $this->redirect($this->admin->generateUrl('list')); |
||
| 67 | } |
||
| 68 | |||
| 69 | /* @var $transUnit \Lexik\Bundle\TranslationBundle\Model\TransUnit */ |
||
| 70 | $transUnit = $this->get('lexik_translation.translation_storage')->getTransUnitById($id); |
||
| 71 | if (!$transUnit) { |
||
| 72 | throw new NotFoundHttpException(sprintf('unable to find the object with id : %s', $id)); |
||
| 73 | } |
||
| 74 | |||
| 75 | if (false === $this->admin->isGranted('EDIT', $transUnit)) { |
||
| 76 | return $this->renderJson([ |
||
| 77 | 'message' => 'access denied', |
||
| 78 | ], 403); |
||
| 79 | } |
||
| 80 | |||
| 81 | $this->admin->setSubject($transUnit); |
||
| 82 | |||
| 83 | /* @var $transUnitManager \Lexik\Bundle\TranslationBundle\Manager\TransUnitManager */ |
||
| 84 | $transUnitManager = $this->get('lexik_translation.trans_unit.manager'); |
||
| 85 | |||
| 86 | $parameters = $this->getRequest()->request; |
||
| 87 | |||
| 88 | $locale = $parameters->get('locale'); |
||
| 89 | $content = $parameters->get('value'); |
||
| 90 | |||
| 91 | if (!$locale) { |
||
| 92 | return $this->renderJson([ |
||
| 93 | 'message' => 'locale missing', |
||
| 94 | ], 422); |
||
| 95 | } |
||
| 96 | |||
| 97 | /* @var $translation \Lexik\Bundle\TranslationBundle\Entity\Translation */ |
||
| 98 | if ($parameters->get('pk')) { |
||
| 99 | $translation = $transUnitManager->updateTranslation($transUnit, $locale, $content, true); |
||
| 100 | } else { |
||
| 101 | $translation = $transUnitManager->addTranslation($transUnit, $locale, $content, null, true); |
||
| 102 | } |
||
| 103 | |||
| 104 | if ($request->query->get('clear_cache')) { |
||
| 105 | $this->get('translator')->removeLocalesCacheFiles([$locale]); |
||
| 106 | } |
||
| 107 | |||
| 108 | return $this->renderJson([ |
||
| 109 | 'key' => $transUnit->getKey(), |
||
| 110 | 'domain' => $transUnit->getDomain(), |
||
| 111 | 'pk' => $translation->getId(), |
||
| 112 | 'locale' => $translation->getLocale(), |
||
| 113 | 'value' => $translation->getContent(), |
||
| 114 | ]); |
||
| 135 |