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 |
||
| 17 | class ConfirmedPasswordField extends FormField { |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Minimum character length of the password. |
||
| 21 | * |
||
| 22 | * @var int |
||
| 23 | */ |
||
| 24 | public $minLength = null; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Maximum character length of the password. |
||
| 28 | * |
||
| 29 | * @var int |
||
| 30 | */ |
||
| 31 | public $maxLength = null; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Enforces at least one digit and one alphanumeric |
||
| 35 | * character (in addition to {$minLength} and {$maxLength} |
||
| 36 | * |
||
| 37 | * @var boolean |
||
| 38 | */ |
||
| 39 | public $requireStrongPassword = false; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Allow empty fields in serverside validation |
||
| 43 | * |
||
| 44 | * @var boolean |
||
| 45 | */ |
||
| 46 | public $canBeEmpty = false; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * If set to TRUE, the "password" and "confirm password" form fields will |
||
| 50 | * be hidden via CSS and JavaScript by default, and triggered by a link. |
||
| 51 | * |
||
| 52 | * An additional hidden field determines if showing the fields has been |
||
| 53 | * triggered and just validates/saves the input in this case. |
||
| 54 | * |
||
| 55 | * This behaviour works unobtrusively, without JavaScript enabled |
||
| 56 | * the fields show, validate and save by default. |
||
| 57 | * |
||
| 58 | * @param boolean $showOnClick |
||
| 59 | */ |
||
| 60 | protected $showOnClick = false; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Check if the existing password should be entered first |
||
| 64 | * |
||
| 65 | * @var bool |
||
| 66 | */ |
||
| 67 | protected $requireExistingPassword = false; |
||
| 68 | |||
| 69 | |||
| 70 | /** |
||
| 71 | * A place to temporarily store the confirm password value |
||
| 72 | * |
||
| 73 | * @var string |
||
| 74 | */ |
||
| 75 | protected $confirmValue; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Store value of "Current Password" field |
||
| 79 | * |
||
| 80 | * @var string |
||
| 81 | */ |
||
| 82 | protected $currentPasswordValue; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Title for the link that triggers the visibility of password fields. |
||
| 86 | * |
||
| 87 | * @var string |
||
| 88 | */ |
||
| 89 | public $showOnClickTitle; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Child fields (_Password, _ConfirmPassword) |
||
| 93 | * |
||
| 94 | * @var FieldList |
||
| 95 | */ |
||
| 96 | public $children; |
||
| 97 | |||
| 98 | protected $schemaDataType = FormField::SCHEMA_DATA_TYPE_STRUCTURAL; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @var PasswordField |
||
| 102 | */ |
||
| 103 | protected $passwordField = null; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @var PasswordField |
||
| 107 | */ |
||
| 108 | protected $confirmPasswordfield = null; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @var HiddenField |
||
| 112 | */ |
||
| 113 | protected $hiddenField = null; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @param string $name |
||
| 117 | * @param string $title |
||
| 118 | * @param mixed $value |
||
| 119 | * @param Form $form |
||
| 120 | * @param boolean $showOnClick |
||
| 121 | * @param string $titleConfirmField Alternate title (not localizeable) |
||
| 122 | */ |
||
| 123 | public function __construct($name, $title = null, $value = "", $form = null, $showOnClick = false, |
||
| 157 | |||
| 158 | public function Title() |
||
| 163 | |||
| 164 | public function setTitle($title) |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @param array $properties |
||
| 172 | * |
||
| 173 | * @return string |
||
| 174 | */ |
||
| 175 | public function Field($properties = array()) { |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Returns the children of this field for use in templating. |
||
| 223 | * @return FieldList |
||
| 224 | */ |
||
| 225 | public function getChildren() { |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Can be empty is a flag that turns on / off empty field checking. |
||
| 231 | * |
||
| 232 | * For example, set this to false (the default) when creating a user account, |
||
| 233 | * and true when displaying on an edit form. |
||
| 234 | * |
||
| 235 | * @param boolean $value |
||
| 236 | * |
||
| 237 | * @return ConfirmedPasswordField |
||
| 238 | */ |
||
| 239 | public function setCanBeEmpty($value) { |
||
| 244 | |||
| 245 | /** |
||
| 246 | * The title on the link which triggers display of the "password" and |
||
| 247 | * "confirm password" formfields. Only used if {@link setShowOnClick()} |
||
| 248 | * is set to TRUE. |
||
| 249 | * |
||
| 250 | * @param string $title |
||
| 251 | * |
||
| 252 | * @return ConfirmedPasswordField |
||
| 253 | */ |
||
| 254 | public function setShowOnClickTitle($title) { |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @return string $title |
||
| 262 | */ |
||
| 263 | public function getShowOnClickTitle() { |
||
| 266 | |||
| 267 | /** |
||
| 268 | * @param string $title |
||
| 269 | * |
||
| 270 | * @return ConfirmedPasswordField |
||
| 271 | */ |
||
| 272 | public function setRightTitle($title) { |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Set child field titles. Titles in order should be: |
||
| 283 | * - "Current Password" (if getRequireExistingPassword() is set) |
||
| 284 | * - "Password" |
||
| 285 | * - "Confirm Password" |
||
| 286 | * |
||
| 287 | * @param array $titles List of child titles |
||
| 288 | * @return $this |
||
| 289 | */ |
||
| 290 | public function setChildrenTitles($titles) { |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Value is sometimes an array, and sometimes a single value, so we need |
||
| 308 | * to handle both cases. |
||
| 309 | * |
||
| 310 | * @param mixed $value |
||
| 311 | * @param mixed $data |
||
| 312 | * @return $this |
||
| 313 | */ |
||
| 314 | public function setValue($value, $data = null) { |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Update the names of the child fields when updating name of field. |
||
| 353 | * |
||
| 354 | * @param string $name new name to give to the field. |
||
| 355 | * @return $this |
||
| 356 | */ |
||
| 357 | public function setName($name) { |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Determines if the field was actually shown on the client side - if not, |
||
| 369 | * we don't validate or save it. |
||
| 370 | * |
||
| 371 | * @return boolean |
||
| 372 | */ |
||
| 373 | public function isSaveable() { |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Validate this field |
||
| 380 | * |
||
| 381 | * @param Validator $validator |
||
| 382 | * @return bool |
||
| 383 | */ |
||
| 384 | public function validate($validator) { |
||
| 514 | |||
| 515 | /** |
||
| 516 | * Only save if field was shown on the client, and is not empty. |
||
| 517 | * |
||
| 518 | * @param DataObjectInterface $record |
||
| 519 | * |
||
| 520 | * @return boolean |
||
| 521 | */ |
||
| 522 | public function saveInto(DataObjectInterface $record) { |
||
| 531 | |||
| 532 | /** |
||
| 533 | * Makes a read only field with some stars in it to replace the password |
||
| 534 | * |
||
| 535 | * @return ReadonlyField |
||
| 536 | */ |
||
| 537 | public function performReadonlyTransformation() { |
||
| 544 | |||
| 545 | public function performDisabledTransformation() |
||
| 549 | |||
| 550 | /** |
||
| 551 | * Check if existing password is required |
||
| 552 | * |
||
| 553 | * @return bool |
||
| 554 | */ |
||
| 555 | public function getRequireExistingPassword() { |
||
| 558 | |||
| 559 | /** |
||
| 560 | * Set if the existing password should be required |
||
| 561 | * |
||
| 562 | * @param bool $show Flag to show or hide this field |
||
| 563 | * @return $this |
||
| 564 | */ |
||
| 565 | public function setRequireExistingPassword($show) { |
||
| 581 | } |
||
| 582 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.