| Conditions | 17 |
| Paths | 387 |
| Total Lines | 102 |
| 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 |
||
| 191 | private function ldapSearch($oAccount, $sQuery) |
||
| 192 | { |
||
| 193 | $sSearchEscaped = $this->escape($sQuery); |
||
| 194 | |||
| 195 | $aResult = array(); |
||
| 196 | $oCon = @\ldap_connect($this->sHostName, $this->iHostPort); |
||
| 197 | if ($oCon) |
||
| 198 | { |
||
| 199 | $this->oLogger->Write('ldap_connect: connected', \MailSo\Log\Enumerations\Type::INFO, 'LDAP'); |
||
| 200 | |||
| 201 | @\ldap_set_option($oCon, LDAP_OPT_PROTOCOL_VERSION, 3); |
||
| 202 | |||
| 203 | if (!@\ldap_bind($oCon, $this->sAccessDn, $this->sAccessPassword)) |
||
| 204 | { |
||
| 205 | if ( is_null($this->sAccessDn) ) { |
||
| 206 | $this->logLdapError($oCon, 'ldap_bind (anonymous)'); |
||
| 207 | } else { |
||
| 208 | $this->logLdapError($oCon, 'ldap_bind'); |
||
| 209 | } |
||
| 210 | return $aResult; |
||
| 211 | } |
||
| 212 | |||
| 213 | $sDomain = \MailSo\Base\Utils::GetDomainFromEmail($oAccount->Email()); |
||
| 214 | $sSearchDn = \strtr($this->sUsersDn, array( |
||
| 215 | '{domain}' => $sDomain, |
||
| 216 | '{domain:dc}' => 'dc='.\strtr($sDomain, array('.' => ',dc=')), |
||
| 217 | '{email}' => $oAccount->Email(), |
||
| 218 | '{email:user}' => \MailSo\Base\Utils::GetAccountNameFromEmail($oAccount->Email()), |
||
| 219 | '{email:domain}' => $sDomain, |
||
| 220 | '{login}' => $oAccount->Login(), |
||
| 221 | '{imap:login}' => $oAccount->Login(), |
||
| 222 | '{imap:host}' => $oAccount->DomainIncHost(), |
||
| 223 | '{imap:port}' => $oAccount->DomainIncPort() |
||
| 224 | )); |
||
| 225 | |||
| 226 | $aEmails = empty($this->sEmailField) ? array() : \explode(',', $this->sEmailField); |
||
| 227 | $aNames = empty($this->sNameField) ? array() : \explode(',', $this->sNameField); |
||
| 228 | $aUIDs = empty($this->sUidField) ? array() : \explode(',', $this->sUidField); |
||
| 229 | |||
| 230 | $aEmails = \array_map('trim', $aEmails); |
||
| 231 | $aNames = \array_map('trim', $aNames); |
||
| 232 | $aUIDs = \array_map('trim', $aUIDs); |
||
| 233 | |||
| 234 | $aFields = \array_merge($aEmails, $aNames, $aUIDs); |
||
| 235 | |||
| 236 | $aItems = array(); |
||
| 237 | $sSubFilter = ''; |
||
| 238 | foreach ($aFields as $sItem) |
||
| 239 | { |
||
| 240 | if (!empty($sItem)) |
||
| 241 | { |
||
| 242 | $aItems[] = $sItem; |
||
| 243 | $sSubFilter .= '('.$sItem.'=*'.$sSearchEscaped.'*)'; |
||
| 244 | } |
||
| 245 | } |
||
| 246 | |||
| 247 | $sFilter = '(&(objectclass='.$this->sObjectClass.')'; |
||
| 248 | $sFilter .= (1 < count($aItems) ? '(|' : '').$sSubFilter.(1 < count($aItems) ? ')' : ''); |
||
| 249 | $sFilter .= ')'; |
||
| 250 | |||
| 251 | $this->oLogger->Write('ldap_search: start: '.$sSearchDn.' / '.$sFilter, \MailSo\Log\Enumerations\Type::INFO, 'LDAP'); |
||
| 252 | $oS = @\ldap_search($oCon, $sSearchDn, $sFilter, $aItems, 0, 30, 30); |
||
| 253 | if ($oS) |
||
| 254 | { |
||
| 255 | $aEntries = @\ldap_get_entries($oCon, $oS); |
||
| 256 | if (is_array($aEntries)) |
||
| 257 | { |
||
| 258 | if (isset($aEntries['count'])) |
||
| 259 | { |
||
| 260 | unset($aEntries['count']); |
||
| 261 | } |
||
| 262 | |||
| 263 | foreach ($aEntries as $aItem) |
||
| 264 | { |
||
| 265 | if ($aItem) |
||
| 266 | { |
||
| 267 | $sName = $sEmail = ''; |
||
| 268 | list ($sEmail, $sName) = $this->findNameAndEmail($aItem, $aEmails, $aNames, $aUIDs); |
||
| 269 | if (!empty($sEmail)) |
||
| 270 | { |
||
| 271 | $aResult[] = array($sEmail, $sName); |
||
| 272 | } |
||
| 273 | } |
||
| 274 | } |
||
| 275 | } |
||
| 276 | else |
||
| 277 | { |
||
| 278 | $this->logLdapError($oCon, 'ldap_get_entries'); |
||
| 279 | } |
||
| 280 | } |
||
| 281 | else |
||
| 282 | { |
||
| 283 | $this->logLdapError($oCon, 'ldap_search'); |
||
| 284 | } |
||
| 285 | } |
||
| 286 | else |
||
| 287 | { |
||
| 288 | return $aResult; |
||
| 289 | } |
||
| 290 | |||
| 291 | return $aResult; |
||
| 292 | } |
||
| 293 | |||
| 345 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.