Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like LdapContactsSuggestions often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use LdapContactsSuggestions, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 3 | class LdapContactsSuggestions implements \RainLoop\Providers\Suggestions\ISuggestions |
||
| 4 | { |
||
| 5 | /** |
||
| 6 | * @var string |
||
| 7 | */ |
||
| 8 | private $sHostName = '127.0.0.1'; |
||
| 9 | |||
| 10 | /** |
||
| 11 | * @var int |
||
| 12 | */ |
||
| 13 | private $iHostPort = 389; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @var string |
||
| 17 | */ |
||
| 18 | private $sAccessDn = NULL; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var string |
||
| 22 | */ |
||
| 23 | private $sAccessPassword = NULL; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var string |
||
| 27 | */ |
||
| 28 | private $sUsersDn = ''; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var string |
||
| 32 | */ |
||
| 33 | private $sObjectClass = 'inetOrgPerson'; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var string |
||
| 37 | */ |
||
| 38 | private $sNameField = 'givenname'; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var string |
||
| 42 | */ |
||
| 43 | private $sEmailField = 'mail'; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var \MailSo\Log\Logger |
||
| 47 | */ |
||
| 48 | private $oLogger = null; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var string |
||
| 52 | */ |
||
| 53 | private $sAllowedEmails = ''; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @param string $sHostName |
||
| 57 | * @param int $iHostPort |
||
| 58 | * @param string $sAccessDn |
||
| 59 | * @param string $sAccessPassword |
||
| 60 | * @param string $sUsersDn |
||
| 61 | * @param string $sObjectClass |
||
| 62 | * @param string $sNameField |
||
| 63 | * @param string $sEmailField |
||
| 64 | * |
||
| 65 | * @return \LdapContactsSuggestions |
||
| 66 | */ |
||
| 67 | public function SetConfig($sHostName, $iHostPort, $sAccessDn, $sAccessPassword, $sUsersDn, $sObjectClass, $sNameField, $sEmailField) |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @param string $sAllowedEmails |
||
| 86 | * |
||
| 87 | * @return \LdapContactsSuggestions |
||
| 88 | */ |
||
| 89 | public function SetAllowedEmails($sAllowedEmails) |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @param \RainLoop\Model\Account $oAccount |
||
| 98 | * @param string $sQuery |
||
| 99 | * @param int $iLimit = 20 |
||
| 100 | * |
||
| 101 | * @return array |
||
| 102 | */ |
||
| 103 | public function Process($oAccount, $sQuery, $iLimit = 20) |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @param array $aLdapItem |
||
| 129 | * @param array $aEmailFields |
||
| 130 | * @param array $aNameFields |
||
| 131 | * |
||
| 132 | * @return array |
||
| 133 | */ |
||
| 134 | private function findNameAndEmail($aLdapItem, $aEmailFields, $aNameFields) |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @param \RainLoop\Model\Account $oAccount |
||
| 169 | * @param string $sQuery |
||
| 170 | * |
||
| 171 | * @return array |
||
| 172 | */ |
||
| 173 | private function ldapSearch($oAccount, $sQuery) |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @param string $sStr |
||
| 276 | * |
||
| 277 | * @return string |
||
| 278 | */ |
||
| 279 | public function escape($sStr) |
||
| 291 | |||
| 292 | /** |
||
| 293 | * @param mixed $oCon |
||
| 294 | * @param string $sCmd |
||
| 295 | * |
||
| 296 | * @return string |
||
| 297 | */ |
||
| 298 | public function logLdapError($oCon, $sCmd) |
||
| 309 | |||
| 310 | /** |
||
| 311 | * @param \MailSo\Log\Logger $oLogger |
||
| 312 | * |
||
| 313 | * @return \LdapContactsSuggestions |
||
| 314 | */ |
||
| 315 | public function SetLogger($oLogger) |
||
| 324 | } |
||
| 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.