| Conditions | 12 |
| Paths | 28 |
| Total Lines | 96 |
| Code Lines | 58 |
| Lines | 12 |
| Ratio | 12.5 % |
| Changes | 7 | ||
| Bugs | 1 | Features | 2 |
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 |
||
| 40 | public function formAction() |
||
| 41 | { |
||
| 42 | $serviceLocator = $this->serviceLocator; |
||
| 43 | $repositories = $serviceLocator->get('repositories'); |
||
| 44 | /* @var $cvRepository \Cv\Repository\Cv */ |
||
| 45 | $cvRepository = $repositories->get('Cv/Cv'); |
||
| 46 | $user = $this->auth()->getUser(); |
||
| 47 | /* @var $cv \Cv\Entity\Cv */ |
||
| 48 | $cv = $cvRepository->findDraft($user); |
||
| 49 | |||
| 50 | if (empty($cv)) { |
||
| 51 | // create draft CV |
||
| 52 | $cv = $cvRepository->create(); |
||
| 53 | $cv->setIsDraft(true); |
||
| 54 | $cv->setContact($user->getInfo()); |
||
| 55 | $cv->setUser($user); |
||
| 56 | $repositories->store($cv); |
||
| 57 | } |
||
| 58 | |||
| 59 | /* @var $container \Core\Form\Container */ |
||
| 60 | $container = $serviceLocator->get('FormElementManager') |
||
| 61 | ->get('CvContainer') |
||
| 62 | ->setEntity($cv); |
||
| 63 | |||
| 64 | // process post method |
||
| 65 | if ($this->getRequest()->isPost()) { |
||
| 66 | $params = $this->params(); |
||
| 67 | $form = $container->getForm($params->fromQuery('form')); |
||
| 68 | |||
| 69 | if ($form) { |
||
| 70 | $form->setData(array_merge( |
||
| 71 | $params->fromPost(), |
||
| 72 | $params->fromFiles() |
||
| 73 | )); |
||
| 74 | |||
| 75 | if (!$form->isValid()) { |
||
| 76 | return new JsonModel([ |
||
| 77 | 'valid' => false, |
||
| 78 | 'errors' => $form->getMessages() |
||
| 79 | ]); |
||
| 80 | } |
||
| 81 | /* |
||
| 82 | * @todo This is a workaround for GeoJSON data insertion |
||
| 83 | * until we figured out, what we really want it to be. |
||
| 84 | */ |
||
| 85 | $formId = $params->fromQuery('form'); |
||
| 86 | View Code Duplication | if ('preferredJob' == $formId) { |
|
| 87 | $locElem = $form->getBaseFieldset()->get('geo-location'); |
||
| 88 | if ($locElem instanceof GeoText) { |
||
| 89 | $loc = $locElem->getValue('entity'); |
||
| 90 | $locations = $cv->getPreferredJob()->getDesiredLocations(); |
||
| 91 | if (count($locations)) { |
||
| 92 | $locations->clear(); |
||
| 93 | } |
||
| 94 | $locations->add($loc); |
||
| 95 | $cv->getPreferredJob()->setDesiredLocation($locElem->getValue()); |
||
| 96 | } |
||
| 97 | } |
||
| 98 | |||
| 99 | $repositories->store($cv); |
||
| 100 | |||
| 101 | if ($form instanceof SummaryFormInterface) { |
||
| 102 | $form->setRenderMode(SummaryFormInterface::RENDER_SUMMARY); |
||
| 103 | $viewHelper = 'summaryform'; |
||
| 104 | } else { |
||
| 105 | $viewHelper = 'form'; |
||
| 106 | } |
||
| 107 | |||
| 108 | // render form |
||
| 109 | $content = $serviceLocator->get('ViewHelperManager') |
||
| 110 | ->get($viewHelper) |
||
| 111 | ->__invoke($form); |
||
| 112 | |||
| 113 | return new JsonModel([ |
||
| 114 | 'valid' => true, |
||
| 115 | 'content' => $content |
||
| 116 | ]); |
||
| 117 | } elseif (($action = $params->fromQuery('action')) !== null) { |
||
| 118 | return new JsonModel($container->executeAction($action, $params->fromPost())); |
||
| 119 | } |
||
| 120 | }// end of process post method |
||
| 121 | else { |
||
| 122 | $locElem = $container->getForm('preferredJob')->getBaseFieldset()->get('geo-location'); |
||
| 123 | if ($locElem instanceof GeoText) { |
||
| 124 | $loc = $cv->getPreferredJob()->getDesiredLocations(); |
||
| 125 | if (count($loc)) { |
||
| 126 | $locElem->setValue($loc->first()); |
||
| 127 | } |
||
| 128 | } |
||
| 129 | } |
||
| 130 | |||
| 131 | |||
| 132 | return [ |
||
| 133 | 'container' => $container |
||
| 134 | ]; |
||
| 135 | } |
||
| 136 | } |
||
| 137 |
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: