| Conditions | 16 |
| Paths | 195 |
| Total Lines | 100 |
| 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 |
||
| 173 | private function ldapSearch($oAccount, $sQuery) |
||
| 174 | { |
||
| 175 | $sSearchEscaped = $this->escape($sQuery); |
||
| 176 | |||
| 177 | $aResult = array(); |
||
| 178 | $oCon = @\ldap_connect($this->sHostName, $this->iHostPort); |
||
| 179 | if ($oCon) |
||
| 180 | { |
||
| 181 | $this->oLogger->Write('ldap_connect: connected', \MailSo\Log\Enumerations\Type::INFO, 'LDAP'); |
||
| 182 | |||
| 183 | @\ldap_set_option($oCon, LDAP_OPT_PROTOCOL_VERSION, 3); |
||
| 184 | |||
| 185 | if (!@\ldap_bind($oCon, $this->sAccessDn, $this->sAccessPassword)) |
||
| 186 | { |
||
| 187 | if ( is_null($this->sAccessDn) ) { |
||
| 188 | $this->logLdapError($oCon, 'ldap_bind (anonymous)'); |
||
| 189 | } else { |
||
| 190 | $this->logLdapError($oCon, 'ldap_bind'); |
||
| 191 | } |
||
| 192 | return $aResult; |
||
| 193 | } |
||
| 194 | |||
| 195 | $sDomain = \MailSo\Base\Utils::GetDomainFromEmail($oAccount->Email()); |
||
| 196 | $sSearchDn = \strtr($this->sUsersDn, array( |
||
| 197 | '{domain}' => $sDomain, |
||
| 198 | '{domain:dc}' => 'dc='.\strtr($sDomain, array('.' => ',dc=')), |
||
| 199 | '{email}' => $oAccount->Email(), |
||
| 200 | '{email:user}' => \MailSo\Base\Utils::GetAccountNameFromEmail($oAccount->Email()), |
||
| 201 | '{email:domain}' => $sDomain, |
||
| 202 | '{login}' => $oAccount->Login(), |
||
| 203 | '{imap:login}' => $oAccount->Login(), |
||
| 204 | '{imap:host}' => $oAccount->DomainIncHost(), |
||
| 205 | '{imap:port}' => $oAccount->DomainIncPort() |
||
| 206 | )); |
||
| 207 | |||
| 208 | $aEmails = empty($this->sEmailField) ? array() : \explode(',', $this->sEmailField); |
||
| 209 | $aNames = empty($this->sNameField) ? array() : \explode(',', $this->sNameField); |
||
| 210 | |||
| 211 | $aEmails = \array_map('trim', $aEmails); |
||
| 212 | $aNames = \array_map('trim', $aNames); |
||
| 213 | |||
| 214 | $aFields = \array_merge($aEmails, $aNames); |
||
| 215 | |||
| 216 | $aItems = array(); |
||
| 217 | $sSubFilter = ''; |
||
| 218 | foreach ($aFields as $sItem) |
||
| 219 | { |
||
| 220 | if (!empty($sItem)) |
||
| 221 | { |
||
| 222 | $aItems[] = $sItem; |
||
| 223 | $sSubFilter .= '('.$sItem.'=*'.$sSearchEscaped.'*)'; |
||
| 224 | } |
||
| 225 | } |
||
| 226 | |||
| 227 | $sFilter = '(&(objectclass='.$this->sObjectClass.')'; |
||
| 228 | $sFilter .= (1 < count($aItems) ? '(|' : '').$sSubFilter.(1 < count($aItems) ? ')' : ''); |
||
| 229 | $sFilter .= ')'; |
||
| 230 | |||
| 231 | $this->oLogger->Write('ldap_search: start: '.$sSearchDn.' / '.$sFilter, \MailSo\Log\Enumerations\Type::INFO, 'LDAP'); |
||
| 232 | $oS = @\ldap_search($oCon, $sSearchDn, $sFilter, $aItems, 0, 30, 30); |
||
| 233 | if ($oS) |
||
| 234 | { |
||
| 235 | $aEntries = @\ldap_get_entries($oCon, $oS); |
||
| 236 | if (is_array($aEntries)) |
||
| 237 | { |
||
| 238 | if (isset($aEntries['count'])) |
||
| 239 | { |
||
| 240 | unset($aEntries['count']); |
||
| 241 | } |
||
| 242 | |||
| 243 | foreach ($aEntries as $aItem) |
||
| 244 | { |
||
| 245 | if ($aItem) |
||
| 246 | { |
||
| 247 | $sName = $sEmail = ''; |
||
| 248 | list ($sEmail, $sName) = $this->findNameAndEmail($aItem, $aEmails, $aNames); |
||
| 249 | if (!empty($sEmail)) |
||
| 250 | { |
||
| 251 | $aResult[] = array($sEmail, $sName); |
||
| 252 | } |
||
| 253 | } |
||
| 254 | } |
||
| 255 | } |
||
| 256 | else |
||
| 257 | { |
||
| 258 | $this->logLdapError($oCon, 'ldap_get_entries'); |
||
| 259 | } |
||
| 260 | } |
||
| 261 | else |
||
| 262 | { |
||
| 263 | $this->logLdapError($oCon, 'ldap_search'); |
||
| 264 | } |
||
| 265 | } |
||
| 266 | else |
||
| 267 | { |
||
| 268 | return $aResult; |
||
| 269 | } |
||
| 270 | |||
| 271 | return $aResult; |
||
| 272 | } |
||
| 273 | |||
| 325 |
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.