Conditions | 11 |
Paths | 326 |
Total Lines | 96 |
Code Lines | 63 |
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 |
||
32 | public function checkAction(Request $request) |
||
33 | { |
||
34 | $responseCode = Codes::HTTP_BAD_REQUEST; |
||
35 | |||
36 | $data = null; |
||
37 | $id = $request->get('id', false); |
||
38 | if (false !== $id) { |
||
39 | $data = $this->getDoctrine()->getRepository('OroImapBundle:UserEmailOrigin')->find($id); |
||
40 | } |
||
41 | |||
42 | $form = $this->createForm('oro_imap_configuration', null, ['csrf_protection' => false,]); |
||
43 | $form->setData($data); |
||
44 | $form->submit($request); |
||
45 | /** @var UserEmailOrigin $origin */ |
||
46 | $origin = $form->getData(); |
||
47 | |||
48 | if ($form->isValid() && null !== $origin) { |
||
49 | $response = []; |
||
50 | $password = $this->get('oro_security.encoder.mcrypt')->decryptData($origin->getPassword()); |
||
51 | |||
52 | if ($origin->getImapHost() !== null) { |
||
53 | $response['imap'] = []; |
||
54 | |||
55 | $config = new ImapConfig( |
||
56 | $origin->getImapHost(), |
||
57 | $origin->getImapPort(), |
||
58 | $origin->getImapEncryption(), |
||
59 | $origin->getUser(), |
||
60 | $password |
||
61 | ); |
||
62 | |||
63 | try { |
||
64 | $connector = $this->get('oro_imap.connector.factory')->createImapConnector($config); |
||
65 | $this->manager = new ImapEmailFolderManager( |
||
66 | $connector, |
||
67 | $this->getDoctrine()->getManager(), |
||
|
|||
68 | $origin |
||
69 | ); |
||
70 | |||
71 | $emailFolders = $this->manager->getFolders(); |
||
72 | $origin->setFolders($emailFolders); |
||
73 | |||
74 | $entity = $request->get('for_entity', 'user'); |
||
75 | $organizationId = $request->get('organization'); |
||
76 | $organization = $this->getOrganization($organizationId); |
||
77 | if ($entity === 'user') { |
||
78 | $user = new User(); |
||
79 | $user->setImapConfiguration($origin); |
||
80 | $user->setOrganization($organization); |
||
81 | $userForm = $this->get('oro_user.form.user'); |
||
82 | $userForm->setData($user); |
||
83 | |||
84 | $response['imap']['folders'] = $this->renderView('OroImapBundle:Connection:check.html.twig', [ |
||
85 | 'form' => $userForm->createView(), |
||
86 | ]); |
||
87 | } elseif ($entity === 'mailbox') { |
||
88 | $mailbox = new Mailbox(); |
||
89 | $mailbox->setOrigin($origin); |
||
90 | if ($organization) { |
||
91 | $mailbox->setOrganization($organization); |
||
92 | } |
||
93 | $mailboxForm = $this->createForm('oro_email_mailbox'); |
||
94 | $mailboxForm->setData($mailbox); |
||
95 | |||
96 | $response['imap']['folders'] = $this->renderView( |
||
97 | 'OroImapBundle:Connection:checkMailbox.html.twig', |
||
98 | [ |
||
99 | 'form' => $mailboxForm->createView(), |
||
100 | ] |
||
101 | ); |
||
102 | } |
||
103 | } catch (\Exception $e) { |
||
104 | $response['imap']['error'] = $e->getMessage(); |
||
105 | } |
||
106 | } |
||
107 | |||
108 | if ($origin->getSmtpHost() !== null) { |
||
109 | $response['smtp'] = []; |
||
110 | |||
111 | try { |
||
112 | /** @var DirectMailer $mailer */ |
||
113 | $mailer = $this->get('oro_email.direct_mailer'); |
||
114 | // Prepare Smtp Transport |
||
115 | $mailer->prepareSmtpTransport($origin); |
||
116 | $transport = $mailer->getTransport(); |
||
117 | $transport->start(); |
||
118 | } catch (\Exception $e) { |
||
119 | $response['smtp']['error'] = $e->getMessage(); |
||
120 | } |
||
121 | } |
||
122 | |||
123 | return new JsonResponse($response); |
||
124 | } |
||
125 | |||
126 | return new Response('', $responseCode); |
||
127 | } |
||
128 | |||
171 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.