Conditions | 10 |
Paths | 16 |
Total Lines | 49 |
Code Lines | 34 |
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 |
||
60 | public function test(MailAccount $account, $host, $users, $password, |
||
61 | $withHostPrefix = false) { |
||
62 | if (!is_array($users)) { |
||
63 | $users = [$users]; |
||
64 | } |
||
65 | |||
66 | // port 25 should be the last one to test |
||
67 | $ports = [587, 465, 25]; |
||
68 | $protocols = ['ssl', 'tls', null]; |
||
69 | $hostPrefixes = ['']; |
||
70 | if ($withHostPrefix) { |
||
71 | $hostPrefixes = ['', 'imap.']; |
||
72 | } |
||
73 | foreach ($hostPrefixes as $hostPrefix) { |
||
74 | $url = $hostPrefix . $host; |
||
75 | if (gethostbyname($url) === $url) { |
||
76 | continue; |
||
77 | } |
||
78 | foreach ($ports as $port) { |
||
79 | if (!$this->canConnect($url, $port)) { |
||
80 | continue; |
||
81 | } |
||
82 | foreach ($protocols as $protocol) { |
||
83 | foreach ($users as $user) { |
||
84 | try { |
||
85 | $account->setOutboundHost($url); |
||
86 | $account->setOutboundPort($port); |
||
87 | $account->setOutboundUser($user); |
||
88 | $password = $this->crypto->encrypt($password); |
||
89 | $account->setOutboundPassword($password); |
||
90 | $account->setOutboundSslMode($protocol); |
||
91 | |||
92 | $a = new Account($account); |
||
93 | $smtp = $a->createTransport(); |
||
94 | $smtp->getSMTPObject(); |
||
95 | |||
96 | $this->logger->info("Test-Account-Successful: $this->userId, $url, $port, $user, $protocol"); |
||
97 | |||
98 | return true; |
||
99 | } catch (\Exception $e) { |
||
100 | $error = $e->getMessage(); |
||
101 | $this->logger->info("Test-Account-Failed: $this->userId, $url, $port, $user, $protocol -> $error"); |
||
102 | } |
||
103 | } |
||
104 | } |
||
105 | } |
||
106 | } |
||
107 | return false; |
||
108 | } |
||
109 | |||
111 |