| Conditions | 7 |
| Paths | 9 |
| Total Lines | 53 |
| Code Lines | 34 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 61 | public function changepasswordAction(Request $request) { |
||
| 62 | |||
| 63 | $user = $this->getUser(); |
||
| 64 | |||
| 65 | $defaultData = array('message' => 'Change Password'); |
||
| 66 | $form = $this->createFormBuilder($defaultData) |
||
| 67 | ->add('oldpassword', 'password', array('label' => 'Old Password')) |
||
| 68 | ->add('newpassword1', 'password', array('label' => 'New Password')) |
||
| 69 | ->add('newpassword2', 'password', array('label' => 'Repeat New Password')) |
||
| 70 | ->getForm(); |
||
| 71 | |||
| 72 | if ($request->isMethod('POST')) { |
||
| 73 | $form->bind($request); |
||
| 74 | $data = $form->getData(); |
||
| 75 | |||
| 76 | $factory = $this->get('security.encoder_factory'); |
||
| 77 | $encoder = $factory->getEncoder($this); |
||
| 78 | if ($encoder->encodePassword($data['oldpassword'], $user->getSalt()) == $user->getPassword()) { |
||
| 79 | #old password verified |
||
| 80 | if ((!$data['newpassword1']) || (strlen($data['newpassword1']) < 8)) { |
||
| 81 | $form->get('newpassword1')->addError(new FormError('Your new password must be at least 8 letters!')); |
||
| 82 | } elseif ($data['newpassword1'] == $data['newpassword2']) { |
||
| 83 | #both new passwords match |
||
| 84 | $user->newSalt(); |
||
| 85 | $user->setPassword($encoder->encodePassword($data['newpassword1'], $user->getSalt())); |
||
| 86 | |||
| 87 | #persist |
||
| 88 | $em = $this->getDoctrine()->getManager(); |
||
| 89 | $em->persist($user); |
||
| 90 | $em->flush(); |
||
| 91 | $this->get('session')->getFlashBag()->add('success', 'Your password has been changed successfully!'); |
||
| 92 | |||
| 93 | return $this->render('VivaitBootstrapBundle:Default:redirect.html.twig', array('redirect' => $request->query->get('parent', $request->request->get('parent', $request->headers->get('referer'))))); |
||
| 94 | } else { |
||
| 95 | // send error about mismatch new passwords |
||
| 96 | $form->get('newpassword1')->addError(new FormError('The two new passwords do not match!')); |
||
| 97 | $form->get('newpassword2')->addError(new FormError('The two new passwords do not match!')); |
||
| 98 | } |
||
| 99 | } else { |
||
| 100 | // send error about invalid old password |
||
| 101 | $form->get('oldpassword')->addError(new FormError('The old password is incorrect!')); |
||
| 102 | } |
||
| 103 | } |
||
| 104 | |||
| 105 | |||
| 106 | if (isset($form)) { |
||
| 107 | $formtpl['form'] = $form->createView(); |
||
| 108 | } |
||
| 109 | |||
| 110 | $formtpl['action'] = $this->generateUrl($this->container->get('request')->get('_route'), $request->query->all()); |
||
| 111 | $formtpl['title'] = 'Change Password'; |
||
| 112 | return $this->render('VivaitAuthBundle:Form:changepassword.html.twig', array('form' => array_merge($formtpl, array('parent' => $request->query->get('parent', $request->request->get('parent', $request->headers->get('referer'))))))); |
||
| 113 | } |
||
| 114 | |||
| 133 |
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.