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 MultiForm 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 MultiForm, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | abstract class MultiForm extends Form { |
||
|
1 ignored issue
–
show
|
|||
| 18 | |||
| 19 | /** |
||
| 20 | * A session object stored in the database, to identify and store |
||
| 21 | * data for this MultiForm instance. |
||
| 22 | * |
||
| 23 | * @var MultiFormSession |
||
| 24 | */ |
||
| 25 | protected $session; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * The current encrypted MultiFormSession identification. |
||
| 29 | * |
||
| 30 | * @var string |
||
| 31 | */ |
||
| 32 | protected $currentSessionHash; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Defines which subclass of {@link MultiFormStep} should be the first |
||
| 36 | * step in the multi-step process. |
||
| 37 | * |
||
| 38 | * @var string Classname of a {@link MultiFormStep} subclass |
||
| 39 | */ |
||
| 40 | public static $start_step; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Set the casting for these fields. |
||
| 44 | * |
||
| 45 | * @var array |
||
| 46 | */ |
||
| 47 | private static $casting = array( |
||
| 48 | 'CompletedStepCount' => 'Int', |
||
| 49 | 'TotalStepCount' => 'Int', |
||
| 50 | 'CompletedPercent' => 'Float' |
||
| 51 | ); |
||
| 52 | |||
| 53 | /** |
||
| 54 | * These fields are ignored when saving the raw form data into session. |
||
| 55 | * This ensures only field data is saved, and nothing else that's useless |
||
| 56 | * or potentially dangerous. |
||
| 57 | * |
||
| 58 | * @var array |
||
| 59 | */ |
||
| 60 | public static $ignored_fields = array( |
||
| 61 | 'url', |
||
| 62 | 'executeForm', |
||
| 63 | 'MultiFormSessionID', |
||
| 64 | 'SecurityID' |
||
| 65 | ); |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Any of the actions defined in this variable are exempt from |
||
| 69 | * being validated. |
||
| 70 | * |
||
| 71 | * This is most useful for the "Back" (action_prev) action, as |
||
| 72 | * you typically don't validate the form when the user is going |
||
| 73 | * back a step. |
||
| 74 | * |
||
| 75 | * @var array |
||
| 76 | */ |
||
| 77 | public static $actions_exempt_from_validation = array( |
||
| 78 | 'action_prev' |
||
| 79 | ); |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var string |
||
| 83 | */ |
||
| 84 | protected $displayLink; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Flag which is being used in getAllStepsRecursive() to allow adding the completed flag on the steps |
||
| 88 | * |
||
| 89 | * @var boolean |
||
| 90 | */ |
||
| 91 | protected $currentStepHasBeenFound = false; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Start the MultiForm instance. |
||
| 95 | * |
||
| 96 | * @param Controller instance $controller Controller this form is created on |
||
| 97 | * @param string $name The form name, typically the same as the method name |
||
| 98 | */ |
||
| 99 | public function __construct($controller, $name) { |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Accessor method to $this->controller. |
||
| 164 | * |
||
| 165 | * @return Controller this MultiForm was instanciated on. |
||
| 166 | */ |
||
| 167 | public function getController() { |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Get the current step. |
||
| 173 | * |
||
| 174 | * If StepID has been set in the URL, we attempt to get that record |
||
| 175 | * by the ID. Otherwise, we check if there's a current step ID in |
||
| 176 | * our session record. Failing those cases, we assume that the form has |
||
| 177 | * just been started, and so we create the first step and return it. |
||
| 178 | * |
||
| 179 | * @return MultiFormStep subclass |
||
| 180 | */ |
||
| 181 | public function getCurrentStep() { |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Set the step passed in as the current step. |
||
| 213 | * |
||
| 214 | * @param MultiFormStep $step A subclass of MultiFormStep |
||
| 215 | * @return boolean The return value of write() |
||
| 216 | */ |
||
| 217 | protected function setCurrentStep($step) { |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Accessor method to $this->session. |
||
| 226 | * |
||
| 227 | * @return MultiFormSession |
||
| 228 | */ |
||
| 229 | public function getSession() { |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Set up the session. |
||
| 235 | * |
||
| 236 | * If MultiFormSessionID isn't set, we assume that this is a new |
||
| 237 | * multiform that requires a new session record to be created. |
||
| 238 | * |
||
| 239 | * @TODO Fix the fact you can continually refresh and create new records |
||
| 240 | * if MultiFormSessionID isn't set. |
||
| 241 | * |
||
| 242 | * @TODO Not sure if we should bake the session stuff directly into MultiForm. |
||
| 243 | * Perhaps it would be best dealt with on a separate class? |
||
| 244 | */ |
||
| 245 | protected function setSession() { |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Set the currently used encrypted hash to identify |
||
| 262 | * the MultiFormSession. |
||
| 263 | * |
||
| 264 | * @param string $hash Encrypted identification to session |
||
| 265 | */ |
||
| 266 | public function setCurrentSessionHash($hash) { |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Return the currently used {@link MultiFormSession} |
||
| 273 | * @return MultiFormSession|boolean FALSE |
||
| 274 | */ |
||
| 275 | public function getCurrentSession() { |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Get all steps saved in the database for the currently active session, |
||
| 294 | * in the order they were saved, oldest to newest (automatically ordered by ID). |
||
| 295 | * If you want a full chain of steps regardless if they've already been saved |
||
| 296 | * to the database, use {@link getAllStepsLinear()}. |
||
| 297 | * |
||
| 298 | * @param String $filter SQL WHERE statement |
||
| 299 | * @return DataObjectSet|boolean A set of MultiFormStep subclasses |
||
| 300 | */ |
||
| 301 | function getSavedSteps($filter = null) { |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Get a step which was previously saved to the database in the current session. |
||
| 309 | * Caution: This might cause unexpected behaviour if you have multiple steps |
||
| 310 | * in your chain with the same classname. |
||
| 311 | * |
||
| 312 | * @param string $className Classname of a {@link MultiFormStep} subclass |
||
| 313 | * @return MultiFormStep |
||
| 314 | */ |
||
| 315 | function getSavedStepByClass($className) { |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Build a FieldList of the FormAction fields for the given step. |
||
| 327 | * |
||
| 328 | * If the current step is the final step, we push in a submit button, which |
||
| 329 | * calls the action {@link finish()} to finalise the submission. Otherwise, |
||
| 330 | * we push in a next button which calls the action {@link next()} to determine |
||
| 331 | * where to go next in our step process, and save any form data collected. |
||
| 332 | * |
||
| 333 | * If there's a previous step (a step that has the current step as it's next |
||
| 334 | * step class), then we allow a previous button, which calls the previous action |
||
| 335 | * to determine which step to go back to. |
||
| 336 | * |
||
| 337 | * If there are any extra actions defined in MultiFormStep->getExtraActions() |
||
| 338 | * then that set of actions is appended to the end of the actions FieldSet we |
||
| 339 | * have created in this method. |
||
| 340 | * |
||
| 341 | * @param $currentStep Subclass of MultiFormStep |
||
| 342 | * @return FieldList of FormAction objects |
||
| 343 | */ |
||
| 344 | function actionsFor($step) { |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Return a rendered version of this form, with a specific template. |
||
| 375 | * Looks through the step ancestory templates (MultiFormStep, current step |
||
| 376 | * subclass template) to see if one is available to render the form with. If |
||
| 377 | * any of those don't exist, look for a default Form template to render |
||
| 378 | * with instead. |
||
| 379 | * |
||
| 380 | * @return SSViewer object to render the template with |
||
| 381 | */ |
||
| 382 | function forTemplate() { |
||
| 395 | |||
| 396 | /** |
||
| 397 | * This method saves the data on the final step, after submitting. |
||
| 398 | * It should always be overloaded with parent::finish($data, $form) |
||
| 399 | * so you can create your own functionality which handles saving |
||
| 400 | * of all the data collected through each step of the form. |
||
| 401 | * |
||
| 402 | * @param array $data The request data returned from the form |
||
| 403 | * @param object $form The form that the action was called on |
||
| 404 | */ |
||
| 405 | public function finish($data, $form) { |
||
| 421 | |||
| 422 | /** |
||
| 423 | * Determine what to do when the next action is called. |
||
| 424 | * |
||
| 425 | * Saves the current step session data to the database, creates the |
||
| 426 | * new step based on getNextStep() of the current step (or fetches |
||
| 427 | * an existing one), resets the current step to the next step, |
||
| 428 | * then redirects to the newly set step. |
||
| 429 | * |
||
| 430 | * @param array $data The request data returned from the form |
||
| 431 | * @param object $form The form that the action was called on |
||
| 432 | */ |
||
| 433 | public function next($data, $form) { |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Determine what to do when the previous action is called. |
||
| 473 | * |
||
| 474 | * Retrieves the previous step class, finds the record for that |
||
| 475 | * class in the DB, and sets the current step to that step found. |
||
| 476 | * Finally, it redirects to that step. |
||
| 477 | * |
||
| 478 | * @param array $data The request data returned from the form |
||
| 479 | * @param object $form The form that the action was called on |
||
| 480 | */ |
||
| 481 | public function prev($data, $form) { |
||
| 502 | |||
| 503 | /** |
||
| 504 | * Save the raw data given back from the form into session. |
||
| 505 | * |
||
| 506 | * Take the submitted form data for the current step, removing |
||
| 507 | * any key => value pairs that shouldn't be saved, then saves |
||
| 508 | * the data into the session. |
||
| 509 | * |
||
| 510 | * @param array $data An array of data to save |
||
| 511 | */ |
||
| 512 | protected function save($data) { |
||
| 524 | |||
| 525 | // ############ Misc ############ |
||
| 526 | |||
| 527 | /** |
||
| 528 | * Add the MultiFormSessionID variable to the URL on form submission. |
||
| 529 | * This is a means to persist the session, by adding it's identification |
||
| 530 | * to the URL, which ties it back to this MultiForm instance. |
||
| 531 | * |
||
| 532 | * @return string |
||
| 533 | */ |
||
| 534 | function FormAction() { |
||
| 541 | |||
| 542 | /** |
||
| 543 | * Returns the link to the page where the form is displayed. The user is |
||
| 544 | * redirected to this link with a session param after each step is |
||
| 545 | * submitted. |
||
| 546 | * |
||
| 547 | * @return string |
||
| 548 | */ |
||
| 549 | public function getDisplayLink() { |
||
| 552 | |||
| 553 | /** |
||
| 554 | * Set the link to the page on which the form is displayed. |
||
| 555 | * |
||
| 556 | * The link defaults to the controllers current link. However if the form |
||
| 557 | * is displayed inside an action the display link must be explicitly set. |
||
| 558 | * |
||
| 559 | * @param string $link |
||
| 560 | */ |
||
| 561 | public function setDisplayLink($link) { |
||
| 564 | |||
| 565 | /** |
||
| 566 | * Determine the steps to show in a linear fashion, starting from the |
||
| 567 | * first step. We run {@link getAllStepsRecursive} passing the steps found |
||
| 568 | * by reference to get a listing of the steps. |
||
| 569 | * |
||
| 570 | * @return DataObjectSet of MultiFormStep instances |
||
| 571 | */ |
||
| 572 | public function getAllStepsLinear() { |
||
| 588 | |||
| 589 | /** |
||
| 590 | * Recursively run through steps using the getNextStep() method on each step |
||
| 591 | * to determine what the next step is, gathering each step along the way. |
||
| 592 | * We stop on the last step, and return the results. |
||
| 593 | * If a step in the chain was already saved to the database in the current |
||
| 594 | * session, its used - otherwise a singleton of this step is used. |
||
| 595 | * Caution: Doesn't consider branching for steps which aren't in the database yet. |
||
| 596 | * |
||
| 597 | * @param $step Subclass of MultiFormStep to find the next step of |
||
| 598 | * @param $stepsFound $stepsFound DataObjectSet reference, the steps found to call back on |
||
| 599 | * @return DataObjectSet of MultiFormStep instances |
||
| 600 | */ |
||
| 601 | protected function getAllStepsRecursive($step, &$stepsFound) { |
||
| 632 | |||
| 633 | /** |
||
| 634 | * Number of steps already completed (excluding currently started step). |
||
| 635 | * The way we determine a step is complete is to check if it has the Data |
||
| 636 | * field filled out with a serialized value, then we know that the user has |
||
| 637 | * clicked next on the given step, to proceed. |
||
| 638 | * |
||
| 639 | * @TODO Not sure if it's entirely appropriate to check if Data is set as a |
||
| 640 | * way to determine a step is "completed". |
||
| 641 | * |
||
| 642 | * @return int |
||
| 643 | */ |
||
| 644 | public function getCompletedStepCount() { |
||
| 648 | |||
| 649 | /** |
||
| 650 | * Total number of steps in the shortest path (only counting straight path without any branching) |
||
| 651 | * The way we determine this is to check if each step has a next_step string variable set. If it's |
||
| 652 | * anything else (like an array, for defining multiple branches) then it gets counted as a single step. |
||
| 653 | * |
||
| 654 | * @return int |
||
| 655 | */ |
||
| 656 | public function getTotalStepCount() { |
||
| 659 | |||
| 660 | /** |
||
| 661 | * Percentage of steps completed (excluding currently started step) |
||
| 662 | * |
||
| 663 | * @return float |
||
| 664 | */ |
||
| 665 | public function getCompletedPercent() { |
||
| 668 | |||
| 669 | } |
||
| 670 |
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.