Conditions | 34 |
Paths | 6069 |
Total Lines | 164 |
Code Lines | 94 |
Lines | 0 |
Ratio | 0 % |
Changes | 19 | ||
Bugs | 3 | Features | 4 |
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 |
||
129 | public function handleForm( |
||
130 | Form $form, |
||
131 | Request $request, |
||
132 | $handlerCallback, |
||
133 | $formTargetRoute, |
||
134 | $formTargetParams = array(), |
||
135 | $extraViewVars = array() |
||
136 | ) { |
||
137 | $formId = $this->getFormId($form); |
||
138 | if ($request->hasPreviousSession()) { |
||
139 | // cannot store errors iterator in session because somewhere there is a closure that can't be serialized |
||
140 | // therefore convert the errors iterator to array, on get from session convert to iterator |
||
141 | // see [1] |
||
|
|||
142 | $formState = $request->getSession()->get($formId); |
||
143 | $formState['form_errors'] = is_array($formState['form_errors']) ? $formState['form_errors'] : array(); |
||
144 | $formState['form_errors'] = new FormErrorIterator($form, $formState['form_errors']); |
||
145 | } else { |
||
146 | $formState = null; |
||
147 | } |
||
148 | $formStatus = ''; |
||
149 | |||
150 | |||
151 | // This only binds the form, so the event listeners are triggered, but no actual submit-handling is done. |
||
152 | // This is useful for AJAX requests which need to modify the form based on submitted data. |
||
153 | if ($request->get('_submit_type') === 'bind') { |
||
154 | $form->submit($request); |
||
155 | } elseif ($request->getMethod() == 'POST') { |
||
156 | $form->submit($request); |
||
157 | |||
158 | $returnUrl = $this->container->get(UrlCheckerService::class)->getSafeUrl($request->get('return_url')); |
||
159 | $successUrl = $this->container->get(UrlCheckerService::class)->getSafeUrl($request->get('success_url')); |
||
160 | |||
161 | $handlerResult = false; |
||
162 | |||
163 | |||
164 | // if it is valid, we can use the callback to handle the actual handling |
||
165 | if ($form->isValid()) { |
||
166 | try { |
||
167 | $handlerResult = call_user_func($handlerCallback, $request, $form, $this->container); |
||
168 | } catch (\Exception $e) { |
||
169 | if (!$this->isMarkExceptionsAsFormErrors) { |
||
170 | throw $e; |
||
171 | } else { |
||
172 | $form->addError($this->convertExceptionToFormError($e)); |
||
173 | } |
||
174 | } |
||
175 | |||
176 | if ($handlerResult) { |
||
177 | // any lingering errors may be removed now. |
||
178 | unset($formState['has_errors']); |
||
179 | unset($formState['data']); |
||
180 | unset($formState['form_errors']); |
||
181 | $formStatus = 'ok'; |
||
182 | |||
183 | if ($handlerResult && $handlerResult instanceof Response) { |
||
184 | return $handlerResult; |
||
185 | } elseif (is_array($handlerResult)) { |
||
186 | $extraViewVars = $handlerResult + $extraViewVars; |
||
187 | } |
||
188 | if ($successUrl) { |
||
189 | $returnUrl = $successUrl; |
||
190 | |||
191 | if ($request->isXmlHttpRequest()) { |
||
192 | return new JsonResponse(array('success_url' => $successUrl)); |
||
193 | } |
||
194 | } else { |
||
195 | // we set a convenience flash message if there was no success url, because |
||
196 | // we will probably return to the return url re-displaying the form. |
||
197 | $this->setFlashMessage($form, 'confirmed', $request->getSession()); |
||
198 | } |
||
199 | } else { |
||
200 | $formStatus = 'errors'; |
||
201 | |||
202 | $formState['has_errors'] = true; |
||
203 | $formState['data'] = $request->request->get($form->getName()); |
||
204 | $formState['form_errors'] = $form->getErrors(); |
||
205 | } |
||
206 | } else { |
||
207 | $formStatus = 'errors'; |
||
208 | |||
209 | $formState['has_errors'] = true; |
||
210 | $formState['data'] = $request->request->get($form->getName()); |
||
211 | $formState['form_errors'] = $form->getErrors(); |
||
212 | } |
||
213 | // redirect to the return url, if available |
||
214 | if ($returnUrl && !$request->isXmlHttpRequest()) { |
||
215 | $response = new RedirectResponse($returnUrl); |
||
216 | } |
||
217 | } elseif (!empty($formState['has_errors'])) { |
||
218 | $formStatus = 'errors'; |
||
219 | |||
220 | // see if there were any errors left in the session from a previous post, so we show them |
||
221 | if (!empty($formState['data']) && is_array($formState['data'])) { |
||
222 | $form->submit($formState['data']); |
||
223 | unset($formState['data']); |
||
224 | } |
||
225 | if (!empty($formState['form_errors'])) { |
||
226 | foreach ($formState['form_errors'] as $error) { |
||
227 | $form->addError($error); |
||
228 | } |
||
229 | } |
||
230 | // and we only show them once. |
||
231 | unset($formState['has_errors']); |
||
232 | unset($formState['form_errors']); |
||
233 | } |
||
234 | |||
235 | if ($formState && !$request->isXmlHttpRequest()) { |
||
236 | if (!empty($formState['form_errors'])) { |
||
237 | |||
238 | // 1. You cannot serialize or un-serialize PDO instances |
||
239 | // 2. We do not want to store cause and origin in the session since these can become quite large |
||
240 | foreach ($formState['form_errors'] as $key => $error) { |
||
241 | $refObject = new \ReflectionObject($error); |
||
242 | $refCauseProperty = $refObject->getProperty('cause'); |
||
243 | $refCauseProperty->setAccessible(true); |
||
244 | $refCauseProperty->setValue($error, null); |
||
245 | $refOriginProperty = $refObject->getProperty('origin'); |
||
246 | $refOriginProperty->setAccessible(true); |
||
247 | $refOriginProperty->setValue($error, null); |
||
248 | } |
||
249 | } |
||
250 | |||
251 | // see [1] for explanation |
||
252 | if (!isset($formState['form_errors'])) { |
||
253 | $formState['form_errors'] = []; |
||
254 | } elseif ($formState['form_errors'] instanceof \Traversable) { |
||
255 | $formState['form_errors'] = iterator_to_array($formState['form_errors']); |
||
256 | } |
||
257 | |||
258 | $request->getSession()->set($formId, $formState); |
||
259 | } elseif ($request->hasPreviousSession()) { |
||
260 | $request->getSession()->remove($formId); |
||
261 | } |
||
262 | |||
263 | $viewVars = $extraViewVars; |
||
264 | |||
265 | if (empty($response)) { |
||
266 | if ($request->get('extension')) { |
||
267 | $formTargetParams += array( |
||
268 | 'extension' => $request->get('extension') |
||
269 | ); |
||
270 | } |
||
271 | $viewVars['form_status'] = $formStatus; |
||
272 | |||
273 | $viewVars['form_url'] = $this->url($formTargetRoute, $formTargetParams); |
||
274 | $viewVars['form'] = $form->createView(); |
||
275 | |||
276 | $prefix = ''; |
||
277 | if ($root = self::getFormRoot($viewVars['form'])) { |
||
278 | $prefix = sprintf('form_messages.%s.', strtolower($root->vars['name'])); |
||
279 | } |
||
280 | |||
281 | $viewVars['messages'] = []; |
||
282 | if ($request->hasPreviousSession() && ($messages = $this->container->get('session')->getFlashBag()->get($formId))) { |
||
283 | foreach ($messages as $value) { |
||
284 | $viewVars['messages'][] = $prefix . $value; |
||
285 | } |
||
286 | } |
||
287 | |||
288 | |||
289 | return $viewVars; |
||
290 | } |
||
291 | |||
292 | return $response; |
||
293 | } |
||
363 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.