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 ConfirmedPasswordField 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 ConfirmedPasswordField, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class ConfirmedPasswordField extends FormField { |
||
| 13 | |||
| 14 | /** |
||
| 15 | * Minimum character length of the password. |
||
| 16 | * |
||
| 17 | * @var int |
||
| 18 | */ |
||
| 19 | public $minLength = null; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Maximum character length of the password. |
||
| 23 | * |
||
| 24 | * @var int |
||
| 25 | */ |
||
| 26 | public $maxLength = null; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Enforces at least one digit and one alphanumeric |
||
| 30 | * character (in addition to {$minLength} and {$maxLength} |
||
| 31 | * |
||
| 32 | * @var boolean |
||
| 33 | */ |
||
| 34 | public $requireStrongPassword = false; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Allow empty fields in serverside validation |
||
| 38 | * |
||
| 39 | * @var boolean |
||
| 40 | */ |
||
| 41 | public $canBeEmpty = false; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * If set to TRUE, the "password" and "confirm password" form fields will |
||
| 45 | * be hidden via CSS and JavaScript by default, and triggered by a link. |
||
| 46 | * |
||
| 47 | * An additional hidden field determines if showing the fields has been |
||
| 48 | * triggered and just validates/saves the input in this case. |
||
| 49 | * |
||
| 50 | * This behaviour works unobtrusively, without JavaScript enabled |
||
| 51 | * the fields show, validate and save by default. |
||
| 52 | * |
||
| 53 | * @param boolean $showOnClick |
||
| 54 | */ |
||
| 55 | protected $showOnClick = false; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Check if the existing password should be entered first |
||
| 59 | * |
||
| 60 | * @var bool |
||
| 61 | */ |
||
| 62 | protected $requireExistingPassword = false; |
||
| 63 | |||
| 64 | |||
| 65 | /** |
||
| 66 | * A place to temporarily store the confirm password value |
||
| 67 | * |
||
| 68 | * @var string |
||
| 69 | */ |
||
| 70 | protected $confirmValue; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Store value of "Current Password" field |
||
| 74 | * |
||
| 75 | * @var string |
||
| 76 | */ |
||
| 77 | protected $currentPasswordValue; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Title for the link that triggers the visibility of password fields. |
||
| 81 | * |
||
| 82 | * @var string |
||
| 83 | */ |
||
| 84 | public $showOnClickTitle; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Child fields (_Password, _ConfirmPassword) |
||
| 88 | * |
||
| 89 | * @var FieldList |
||
| 90 | */ |
||
| 91 | public $children; |
||
| 92 | |||
| 93 | protected $schemaDataType = FormField::SCHEMA_DATA_TYPE_STRUCTURAL; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @param string $name |
||
| 97 | * @param string $title |
||
| 98 | * @param mixed $value |
||
| 99 | * @param Form $form |
||
| 100 | * @param boolean $showOnClick |
||
| 101 | * @param string $titleConfirmField Alternate title (not localizeable) |
||
| 102 | */ |
||
| 103 | public function __construct($name, $title = null, $value = "", $form = null, $showOnClick = false, |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @param array $properties |
||
| 140 | * |
||
| 141 | * @return HTMLText |
||
| 142 | */ |
||
| 143 | public function Field($properties = array()) { |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Returns the children of this field for use in templating. |
||
| 191 | * @return FieldList |
||
| 192 | */ |
||
| 193 | public function getChildren() { |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Can be empty is a flag that turns on / off empty field checking. |
||
| 199 | * |
||
| 200 | * For example, set this to false (the default) when creating a user account, |
||
| 201 | * and true when displaying on an edit form. |
||
| 202 | * |
||
| 203 | * @param boolean $value |
||
| 204 | * |
||
| 205 | * @return ConfirmedPasswordField |
||
| 206 | */ |
||
| 207 | public function setCanBeEmpty($value) { |
||
| 212 | |||
| 213 | /** |
||
| 214 | * The title on the link which triggers display of the "password" and |
||
| 215 | * "confirm password" formfields. Only used if {@link setShowOnClick()} |
||
| 216 | * is set to TRUE. |
||
| 217 | * |
||
| 218 | * @param string $title |
||
| 219 | * |
||
| 220 | * @return ConfirmedPasswordField |
||
| 221 | */ |
||
| 222 | public function setShowOnClickTitle($title) { |
||
| 227 | |||
| 228 | /** |
||
| 229 | * @return string $title |
||
| 230 | */ |
||
| 231 | public function getShowOnClickTitle() { |
||
| 234 | |||
| 235 | /** |
||
| 236 | * @param string $title |
||
| 237 | * |
||
| 238 | * @return ConfirmedPasswordField |
||
| 239 | */ |
||
| 240 | public function setRightTitle($title) { |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Set child field titles. Titles in order should be: |
||
| 251 | * - "Current Password" (if getRequireExistingPassword() is set) |
||
| 252 | * - "Password" |
||
| 253 | * - "Confirm Password" |
||
| 254 | * |
||
| 255 | * @param array $titles List of child titles |
||
| 256 | * @return $this |
||
| 257 | */ |
||
| 258 | public function setChildrenTitles($titles) { |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Value is sometimes an array, and sometimes a single value, so we need |
||
| 276 | * to handle both cases. |
||
| 277 | * |
||
| 278 | * @param mixed $value |
||
| 279 | * @param mixed $data |
||
| 280 | * @return $this |
||
| 281 | */ |
||
| 282 | public function setValue($value, $data = null) { |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Update the names of the child fields when updating name of field. |
||
| 321 | * |
||
| 322 | * @param string $name new name to give to the field. |
||
| 323 | * @return $this |
||
| 324 | */ |
||
| 325 | public function setName($name) { |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Determines if the field was actually shown on the client side - if not, |
||
| 336 | * we don't validate or save it. |
||
| 337 | * |
||
| 338 | * @return boolean |
||
| 339 | */ |
||
| 340 | public function isSaveable() { |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Validate this field |
||
| 348 | * |
||
| 349 | * @param Validator $validator |
||
| 350 | * @return bool |
||
| 351 | */ |
||
| 352 | public function validate($validator) { |
||
| 485 | |||
| 486 | /** |
||
| 487 | * Only save if field was shown on the client, and is not empty. |
||
| 488 | * |
||
| 489 | * @param DataObjectInterface $record |
||
| 490 | * |
||
| 491 | * @return boolean |
||
| 492 | */ |
||
| 493 | public function saveInto(DataObjectInterface $record) { |
||
| 502 | |||
| 503 | /** |
||
| 504 | * Makes a read only field with some stars in it to replace the password |
||
| 505 | * |
||
| 506 | * @return ReadonlyField |
||
| 507 | */ |
||
| 508 | public function performReadonlyTransformation() { |
||
| 515 | |||
| 516 | /** |
||
| 517 | * Check if existing password is required |
||
| 518 | * |
||
| 519 | * @return bool |
||
| 520 | */ |
||
| 521 | public function getRequireExistingPassword() { |
||
| 524 | |||
| 525 | /** |
||
| 526 | * Set if the existing password should be required |
||
| 527 | * |
||
| 528 | * @param bool $show Flag to show or hide this field |
||
| 529 | * @return $this |
||
| 530 | */ |
||
| 531 | public function setRequireExistingPassword($show) { |
||
| 547 | } |
||
| 548 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.