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