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 $sUidField = 'uid'; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var string |
||
| 42 | */ |
||
| 43 | private $sNameField = 'givenname'; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var string |
||
| 47 | */ |
||
| 48 | private $sEmailField = 'mail'; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var \MailSo\Log\Logger |
||
| 52 | */ |
||
| 53 | private $oLogger = null; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var string |
||
| 57 | */ |
||
| 58 | private $sAllowedEmails = ''; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @param string $sHostName |
||
| 62 | * @param int $iHostPort |
||
| 63 | * @param string $sAccessDn |
||
| 64 | * @param string $sAccessPassword |
||
| 65 | * @param string $sUsersDn |
||
| 66 | * @param string $sObjectClass |
||
| 67 | * @param string $sNameField |
||
| 68 | * @param string $sEmailField |
||
| 69 | * |
||
| 70 | * @return \LdapContactsSuggestions |
||
| 71 | */ |
||
| 72 | public function SetConfig($sHostName, $iHostPort, $sAccessDn, $sAccessPassword, $sUsersDn, $sObjectClass, $sUidField, $sNameField, $sEmailField) |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @param string $sAllowedEmails |
||
| 92 | * |
||
| 93 | * @return \LdapContactsSuggestions |
||
| 94 | */ |
||
| 95 | public function SetAllowedEmails($sAllowedEmails) |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @param \RainLoop\Model\Account $oAccount |
||
| 104 | * @param string $sQuery |
||
| 105 | * @param int $iLimit = 20 |
||
| 106 | * |
||
| 107 | * @return array |
||
| 108 | */ |
||
| 109 | public function Process($oAccount, $sQuery, $iLimit = 20) |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @param array $aLdapItem |
||
| 135 | * @param array $aEmailFields |
||
| 136 | * @param array $aNameFields |
||
| 137 | * |
||
| 138 | * @return array |
||
| 139 | */ |
||
| 140 | private function findNameAndEmail($aLdapItem, $aEmailFields, $aNameFields, $aUidFields) |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @param \RainLoop\Model\Account $oAccount |
||
| 187 | * @param string $sQuery |
||
| 188 | * |
||
| 189 | * @return array |
||
| 190 | */ |
||
| 191 | private function ldapSearch($oAccount, $sQuery) |
||
| 293 | |||
| 294 | /** |
||
| 295 | * @param string $sStr |
||
| 296 | * |
||
| 297 | * @return string |
||
| 298 | */ |
||
| 299 | public function escape($sStr) |
||
| 311 | |||
| 312 | /** |
||
| 313 | * @param mixed $oCon |
||
| 314 | * @param string $sCmd |
||
| 315 | * |
||
| 316 | * @return string |
||
| 317 | */ |
||
| 318 | public function logLdapError($oCon, $sCmd) |
||
| 329 | |||
| 330 | /** |
||
| 331 | * @param \MailSo\Log\Logger $oLogger |
||
| 332 | * |
||
| 333 | * @return \LdapContactsSuggestions |
||
| 334 | */ |
||
| 335 | public function SetLogger($oLogger) |
||
| 344 | } |
||
| 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.