Conditions | 10 |
Paths | 30 |
Total Lines | 63 |
Code Lines | 40 |
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 |
||
40 | public function edit(Request $request, string $guid = null): Response |
||
41 | { |
||
42 | $em = $this->getDoctrine()->getManager(); |
||
43 | |||
44 | if ($guid === null) { |
||
45 | $account = new Account(); |
||
46 | $old_password = null; |
||
47 | $user = null; |
||
48 | } else { |
||
49 | $user = $em->getRepository(User::class)->findOneBy(["guid" => $guid]); |
||
50 | $account = $em->getRepository(Account::class)->findOneBy(["user" => $user->getId()]); |
||
51 | $old_password = $account->getPassword(); |
||
52 | } |
||
53 | |||
54 | $form = $this->createForm("PiouPiou\RibsAdminBundle\Form\Account", $account); |
||
55 | |||
56 | $form->handleRequest($request); |
||
57 | |||
58 | if ($form->isSubmitted() && $form->isValid()) { |
||
59 | /** |
||
60 | * @var Account |
||
61 | */ |
||
62 | $data = $form->getData(); |
||
63 | |||
64 | $account_exist = $em->getRepository(Account::class)->findOneBy(["username" => $data->getUsername()]); |
||
65 | |||
66 | if ($account_exist && $account_exist === $account) { |
||
67 | $account_exist = null; |
||
68 | } |
||
69 | |||
70 | if (!$account_exist) { |
||
71 | if ($guid === null) { |
||
72 | $temp_password = $this->get("security.password_encoder")->encodePassword($data, $form->get("password")->getData()); |
||
|
|||
73 | $data->setPassword($temp_password); |
||
74 | } else if ($form->get("password")->getData()) { |
||
75 | $temp_password = $this->get("security.password_encoder")->encodePassword($data, $form->get("password")->getData()); |
||
76 | $data->setPassword($temp_password); |
||
77 | } else { |
||
78 | $data->setPassword($old_password); |
||
79 | } |
||
80 | |||
81 | $em->persist($data); |
||
82 | $em->flush(); |
||
83 | |||
84 | $username = $data->getUser()->getFirstName() . " " . $data->getUser()->getLastName(); |
||
85 | |||
86 | if ($guid === null) { |
||
87 | $this->addFlash("success-flash", "the account of " . $username . " was created"); |
||
88 | } else { |
||
89 | $this->addFlash("success-flash", "the account of " . $username . " was edited"); |
||
90 | } |
||
91 | |||
92 | return $this->redirectToRoute("ribsadmin_accounts"); |
||
93 | } else { |
||
94 | $this->addFlash("error-flash", "An account with username " . $data->getUsername() . " already exist"); |
||
95 | return $this->redirectToRoute($request->get("_route"), ["guid" => $guid]); |
||
96 | } |
||
97 | } |
||
98 | |||
99 | return $this->render("@RibsAdmin/accounts/edit.html.twig", [ |
||
100 | "form" => $form->createView(), |
||
101 | "form_errors" => $form->getErrors(), |
||
102 | "user" => $user |
||
103 | ]); |
||
138 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.