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 FroxlorChangePasswordDriver 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 FroxlorChangePasswordDriver, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 3 | class FroxlorChangePasswordDriver implements \RainLoop\Providers\ChangePassword\ChangePasswordInterface |
||
|
|
|||
| 4 | { |
||
| 5 | /** |
||
| 6 | * @var string |
||
| 7 | */ |
||
| 8 | private $sDsn = ''; |
||
| 9 | |||
| 10 | /** |
||
| 11 | * @var string |
||
| 12 | */ |
||
| 13 | private $sUser = ''; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @var string |
||
| 17 | */ |
||
| 18 | private $sPassword = ''; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var string |
||
| 22 | */ |
||
| 23 | private $sAllowedEmails = ''; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var \MailSo\Log\Logger |
||
| 27 | */ |
||
| 28 | private $oLogger = null; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @param string $sDsn |
||
| 32 | * @param string $sUser |
||
| 33 | * @param string $sPassword |
||
| 34 | * |
||
| 35 | * @return \FroxlorChangePasswordDriver |
||
| 36 | */ |
||
| 37 | public function SetConfig($sDsn, $sUser, $sPassword) |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @param string $sAllowedEmails |
||
| 48 | * |
||
| 49 | * @return \FroxlorChangePasswordDriver |
||
| 50 | */ |
||
| 51 | public function SetAllowedEmails($sAllowedEmails) |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @param \MailSo\Log\Logger $oLogger |
||
| 59 | * |
||
| 60 | * @return \FroxlorChangePasswordDriver |
||
| 61 | */ |
||
| 62 | public function SetLogger($oLogger) |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @param \RainLoop\Account $oAccount |
||
| 74 | * |
||
| 75 | * @return bool |
||
| 76 | */ |
||
| 77 | public function PasswordChangePossibility($oAccount) |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @param \RainLoop\Account $oAccount |
||
| 85 | * @param string $sPrevPassword |
||
| 86 | * @param string $sNewPassword |
||
| 87 | * |
||
| 88 | * @return bool |
||
| 89 | */ |
||
| 90 | public function ChangePassword(\RainLoop\Account $oAccount, $sPrevPassword, $sNewPassword) |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @param string $sPassword |
||
| 135 | * @return string |
||
| 136 | */ |
||
| 137 | private function cryptPassword($sPassword, $type = 3) |
||
| 141 | |||
| 142 | /** |
||
| 143 | * This file is part of the Froxlor project. |
||
| 144 | * Copyright (c) 2010 the Froxlor Team (see authors). |
||
| 145 | * |
||
| 146 | * For the full copyright and license information, please view the COPYING |
||
| 147 | * file that was distributed with this source code. You can also view the |
||
| 148 | * COPYING file online at http://files.froxlor.org/misc/COPYING.txt |
||
| 149 | * |
||
| 150 | * @copyright (c) the authors |
||
| 151 | * @author Michal Wojcik <[email protected]> |
||
| 152 | * @author Michael Kaufmann <[email protected]> |
||
| 153 | * @author Froxlor team <[email protected]> (2010-) |
||
| 154 | * @license GPLv2 http://files.froxlor.org/misc/COPYING.txt |
||
| 155 | * @package Functions |
||
| 156 | * |
||
| 157 | */ |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Make crypted password from clear text password |
||
| 161 | * |
||
| 162 | * @author Michal Wojcik <[email protected]> |
||
| 163 | * @author Michael Kaufmann <[email protected]> |
||
| 164 | * @author Froxlor team <[email protected]> (2010-) |
||
| 165 | * |
||
| 166 | * 0 - default crypt (depenend on system configuration) |
||
| 167 | * 1 - MD5 $1$ |
||
| 168 | * 2 - BLOWFISH $2a$ | $2y$07$ (on php 5.3.7+) |
||
| 169 | * 3 - SHA-256 $5$ (default) |
||
| 170 | * 4 - SHA-512 $6$ |
||
| 171 | * |
||
| 172 | * @param string $password Password to be crypted |
||
| 173 | * |
||
| 174 | * @return string encrypted password |
||
| 175 | */ |
||
| 176 | private function makeCryptPassword ($password,$type = 3) { |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Generates a random password |
||
| 212 | * |
||
| 213 | * @param boolean $isSalt |
||
| 214 | * optional, create a hash for a salt used in makeCryptPassword because crypt() does not like some special characters in its salts, default is false |
||
| 215 | */ |
||
| 216 | private function generatePassword($isSalt = false) |
||
| 231 | |||
| 232 | /** |
||
| 233 | * multibyte-character safe shuffle function |
||
| 234 | * |
||
| 235 | * @param string $str |
||
| 236 | * |
||
| 237 | * @return string |
||
| 238 | */ |
||
| 239 | private function special_shuffle($str = null) |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Function validatePasswordLogin |
||
| 252 | * |
||
| 253 | * compare user password-hash with given user-password |
||
| 254 | * and check if they are the same |
||
| 255 | * additionally it updates the hash if the system settings changed |
||
| 256 | * or if the very old md5() sum is used |
||
| 257 | * |
||
| 258 | * @param array $userinfo user-data from table |
||
| 259 | * @param string $password the password to validate |
||
| 260 | * @param string $table either panel_customers or panel_admins |
||
| 261 | * @param string $uid user-id-field in $table |
||
| 262 | * |
||
| 263 | * @return boolean |
||
| 264 | */ |
||
| 265 | private function validatePasswordLogin($pwd_hash, $password = null) { |
||
| 291 | |||
| 292 | |||
| 293 | } |
||
| 294 |
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.