| Conditions | 7 |
| Paths | 7 |
| Total Lines | 60 |
| Code Lines | 37 |
| Lines | 0 |
| Ratio | 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 |
||
| 99 | protected function editTemplateAction() |
||
| 100 | { |
||
| 101 | $id = $this->params('id'); |
||
| 102 | $formIdentifier=$this->params()->fromQuery('form'); |
||
| 103 | $job = $this->jobRepository->find($id); |
||
| 104 | |||
| 105 | /** @var \Zend\Http\Request $request */ |
||
| 106 | $request = $this->getRequest(); |
||
| 107 | $isAjax = $request->isXmlHttpRequest(); |
||
| 108 | $services = $this->getServiceLocator(); |
||
| 109 | $viewHelperManager = $services->get('ViewHelperManager'); |
||
| 110 | $mvcEvent = $this->getEvent(); |
||
| 111 | $applicationViewModel = $mvcEvent->getViewModel(); |
||
| 112 | $forms = $services->get('FormElementManager'); |
||
| 113 | |||
| 114 | /** @var \Jobs\Form\JobDescriptionTemplate $formTemplate */ |
||
| 115 | $formTemplate = $forms->get( |
||
| 116 | 'Jobs/Description/Template', |
||
| 117 | array( |
||
| 118 | 'mode' => $job->id ? 'edit' : 'new' |
||
| 119 | ) |
||
| 120 | ); |
||
| 121 | |||
| 122 | $formTemplate->setParam('id', $job->id); |
||
| 123 | $formTemplate->setParam('applyId', $job->applyId); |
||
| 124 | |||
| 125 | $formTemplate->setEntity($job); |
||
| 126 | |||
| 127 | if (isset($formIdentifier) && $request->isPost()) { |
||
| 128 | // at this point the form get instantiated and immediately accumulated |
||
| 129 | |||
| 130 | $instanceForm = $formTemplate->get($formIdentifier); |
||
| 131 | if (!isset($instanceForm)) { |
||
| 132 | throw new \RuntimeException('No form found for "' . $formIdentifier . '"'); |
||
| 133 | } |
||
| 134 | |||
| 135 | // the id is part of the postData, but it never should be altered |
||
| 136 | $postData = $request->getPost(); |
||
| 137 | |||
| 138 | unset($postData['id']); |
||
| 139 | unset($postData['applyId']); |
||
| 140 | |||
| 141 | $instanceForm->setData($postData); |
||
| 142 | if ($instanceForm->isValid()) { |
||
| 143 | $this->getServiceLocator()->get('repositories')->persist($job); |
||
| 144 | } |
||
| 145 | } |
||
| 146 | |||
| 147 | $model = $services->get('Jobs/ViewModelTemplateFilter')->__invoke($formTemplate); |
||
| 148 | |||
| 149 | if (!$isAjax) { |
||
| 150 | $basePath = $viewHelperManager->get('basepath'); |
||
| 151 | $headScript = $viewHelperManager->get('headscript'); |
||
| 152 | $headScript->appendFile($basePath->__invoke('/Core/js/core.forms.js')); |
||
| 153 | } else { |
||
| 154 | return new JsonModel(array('valid' => true)); |
||
| 155 | } |
||
| 156 | $applicationViewModel->setTemplate('iframe/iFrameInjection'); |
||
| 157 | return $model; |
||
| 158 | } |
||
| 159 | } |
||
| 160 |
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: