Conditions | 28 |
Paths | 4064 |
Total Lines | 112 |
Code Lines | 67 |
Lines | 82 |
Ratio | 73.21 % |
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 |
||
84 | public function smtpConnect($options = array()) |
||
85 | { |
||
86 | if (is_null($this->smtp)) { |
||
87 | $this->smtp = $this->getSMTPInstance(); |
||
88 | } |
||
89 | |||
90 | if (is_null($this->oauth)) { |
||
91 | $this->oauth = $this->getOAUTHInstance(); |
||
92 | } |
||
93 | |||
94 | // Already connected? |
||
95 | if ($this->smtp->connected()) { |
||
96 | return true; |
||
97 | } |
||
98 | |||
99 | $this->smtp->setTimeout($this->Timeout); |
||
100 | $this->smtp->setDebugLevel($this->SMTPDebug); |
||
101 | $this->smtp->setDebugOutput($this->Debugoutput); |
||
102 | $this->smtp->setVerp($this->do_verp); |
||
103 | $hosts = explode(';', $this->Host); |
||
104 | $lastexception = null; |
||
105 | |||
106 | View Code Duplication | foreach ($hosts as $hostentry) { |
|
107 | $hostinfo = array(); |
||
108 | if (!preg_match('/^((ssl|tls):\/\/)*([a-zA-Z0-9\.-]*):?([0-9]*)$/', trim($hostentry), $hostinfo)) { |
||
109 | // Not a valid host entry |
||
110 | continue; |
||
111 | } |
||
112 | // $hostinfo[2]: optional ssl or tls prefix |
||
113 | // $hostinfo[3]: the hostname |
||
114 | // $hostinfo[4]: optional port number |
||
115 | // The host string prefix can temporarily override the current setting for SMTPSecure |
||
116 | // If it's not specified, the default value is used |
||
117 | $prefix = ''; |
||
118 | $secure = $this->SMTPSecure; |
||
119 | $tls = ($this->SMTPSecure == 'tls'); |
||
120 | if ('ssl' == $hostinfo[2] or ('' == $hostinfo[2] and 'ssl' == $this->SMTPSecure)) { |
||
121 | $prefix = 'ssl://'; |
||
122 | $tls = false; // Can't have SSL and TLS at the same time |
||
123 | $secure = 'ssl'; |
||
124 | } elseif ($hostinfo[2] == 'tls') { |
||
125 | $tls = true; |
||
126 | // tls doesn't use a prefix |
||
127 | $secure = 'tls'; |
||
128 | } |
||
129 | //Do we need the OpenSSL extension? |
||
130 | $sslext = defined('OPENSSL_ALGO_SHA1'); |
||
131 | if ('tls' === $secure or 'ssl' === $secure) { |
||
132 | //Check for an OpenSSL constant rather than using extension_loaded, which is sometimes disabled |
||
133 | if (!$sslext) { |
||
134 | throw new phpmailerException($this->lang('extension_missing').'openssl', self::STOP_CRITICAL); |
||
135 | } |
||
136 | } |
||
137 | $host = $hostinfo[3]; |
||
138 | $port = $this->Port; |
||
139 | $tport = (integer)$hostinfo[4]; |
||
140 | if ($tport > 0 and $tport < 65536) { |
||
141 | $port = $tport; |
||
142 | } |
||
143 | if ($this->smtp->connect($prefix . $host, $port, $this->Timeout, $options)) { |
||
144 | try { |
||
145 | if ($this->Helo) { |
||
146 | $hello = $this->Helo; |
||
147 | } else { |
||
148 | $hello = $this->serverHostname(); |
||
149 | } |
||
150 | $this->smtp->hello($hello); |
||
151 | //Automatically enable TLS encryption if: |
||
152 | // * it's not disabled |
||
153 | // * we have openssl extension |
||
154 | // * we are not already using SSL |
||
155 | // * the server offers STARTTLS |
||
156 | if ($this->SMTPAutoTLS and $sslext and $secure != 'ssl' and $this->smtp->getServerExt('STARTTLS')) { |
||
157 | $tls = true; |
||
158 | } |
||
159 | if ($tls) { |
||
160 | if (!$this->smtp->startTLS()) { |
||
161 | throw new phpmailerException($this->lang('connect_host')); |
||
162 | } |
||
163 | // We must resend HELO after tls negotiation |
||
164 | $this->smtp->hello($hello); |
||
165 | } |
||
166 | if ($this->SMTPAuth) { |
||
167 | if (!$this->smtp->authenticate( |
||
168 | $this->Username, |
||
169 | $this->Password, |
||
170 | $this->AuthType, |
||
171 | $this->Realm, |
||
172 | $this->Workstation, |
||
173 | $this->oauth |
||
174 | ) |
||
175 | ) { |
||
176 | throw new phpmailerException($this->lang('authenticate')); |
||
177 | } |
||
178 | } |
||
179 | return true; |
||
180 | } catch (phpmailerException $exc) { |
||
181 | $lastexception = $exc; |
||
182 | $this->edebug($exc->getMessage()); |
||
183 | // We must have connected, but then failed TLS or Auth, so close connection nicely |
||
184 | $this->smtp->quit(); |
||
185 | } |
||
186 | } |
||
187 | } |
||
188 | // If we get here, all connection attempts have failed, so close connection hard |
||
189 | $this->smtp->close(); |
||
190 | // As we've caught all exceptions, just report whatever the last one was |
||
191 | if ($this->exceptions and !is_null($lastexception)) { |
||
192 | throw $lastexception; |
||
193 | } |
||
194 | return false; |
||
195 | } |
||
196 | } |
||
197 |
PHP has two types of connecting operators (logical operators, and boolean operators):
and
&&
or
||
The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&
, or||
.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
die
introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrow
at this point:These limitations lead to logical operators rarely being of use in current PHP code.