| Total Complexity | 71 |
| Total Lines | 472 |
| Duplicated Lines | 4.45 % |
| Changes | 0 | ||
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 UserDefinedFormController 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.
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 UserDefinedFormController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 30 | class UserDefinedFormController extends PageController |
||
| 31 | { |
||
| 32 | private static $finished_anchor = '#uff'; |
||
| 33 | |||
| 34 | private static $allowed_actions = [ |
||
| 35 | 'index', |
||
| 36 | 'ping', |
||
| 37 | 'Form', |
||
| 38 | 'finished' |
||
| 39 | ]; |
||
| 40 | |||
| 41 | protected function init() |
||
| 42 | { |
||
| 43 | parent::init(); |
||
| 44 | |||
| 45 | $page = $this->data(); |
||
| 46 | |||
| 47 | // load the css |
||
| 48 | if (!$page->config()->get('block_default_userforms_css')) { |
||
| 49 | Requirements::css('silverstripe/userforms:client/dist/styles/userforms.css'); |
||
| 50 | } |
||
| 51 | |||
| 52 | // load the jquery |
||
| 53 | if (!$page->config()->get('block_default_userforms_js')) { |
||
| 54 | Requirements::javascript('//code.jquery.com/jquery-1.7.2.min.js'); |
||
| 55 | Requirements::javascript( |
||
| 56 | 'silverstripe/userforms:client/thirdparty/jquery-validate/jquery.validate.min.js' |
||
| 57 | ); |
||
| 58 | Requirements::javascript('silverstripe/admin:client/dist/js/i18n.js'); |
||
| 59 | Requirements::add_i18n_javascript('silverstripe/userforms:client/lang'); |
||
| 60 | Requirements::javascript('silverstripe/userforms:client/dist/js/userforms.js'); |
||
| 61 | |||
| 62 | $this->addUserFormsValidatei18n(); |
||
| 63 | |||
| 64 | // Bind a confirmation message when navigating away from a partially completed form. |
||
| 65 | if ($page::config()->get('enable_are_you_sure')) { |
||
| 66 | Requirements::javascript( |
||
| 67 | 'silverstripe/userforms:client/thirdparty/jquery.are-you-sure/jquery.are-you-sure.js' |
||
| 68 | ); |
||
| 69 | } |
||
| 70 | } |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Add the necessary jQuery validate i18n translation files, either by locale or by langauge, |
||
| 75 | * e.g. 'en_NZ' or 'en'. This adds "methods_abc.min.js" as well as "messages_abc.min.js" from the |
||
| 76 | * jQuery validate thirdparty library. |
||
| 77 | */ |
||
| 78 | protected function addUserFormsValidatei18n() |
||
| 79 | { |
||
| 80 | $module = ModuleLoader::getModule('silverstripe/userforms'); |
||
| 81 | |||
| 82 | $candidates = [ |
||
| 83 | i18n::getData()->langFromLocale(i18n::config()->get('default_locale')), |
||
| 84 | i18n::config()->get('default_locale'), |
||
| 85 | i18n::getData()->langFromLocale(i18n::get_locale()), |
||
| 86 | i18n::get_locale(), |
||
| 87 | ]; |
||
| 88 | |||
| 89 | foreach ($candidates as $candidate) { |
||
| 90 | foreach (['messages', 'methods'] as $candidateType) { |
||
| 91 | $localisationCandidate = "client/thirdparty/jquery-validate/localization/{$candidateType}_{$candidate}.min.js"; |
||
| 92 | |||
| 93 | $resource = $module->getResource($localisationCandidate); |
||
| 94 | if ($resource->exists()) { |
||
| 95 | Requirements::javascript($resource->getRelativePath()); |
||
| 96 | } |
||
| 97 | } |
||
| 98 | } |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Using $UserDefinedForm in the Content area of the page shows |
||
| 103 | * where the form should be rendered into. If it does not exist |
||
| 104 | * then default back to $Form. |
||
| 105 | * |
||
| 106 | * @return array |
||
| 107 | */ |
||
| 108 | public function index() |
||
| 109 | { |
||
| 110 | if ($this->Content && $form = $this->Form()) { |
||
| 111 | $hasLocation = stristr($this->Content, '$UserDefinedForm'); |
||
| 112 | if ($hasLocation) { |
||
| 113 | /** @see Requirements_Backend::escapeReplacement */ |
||
| 114 | $formEscapedForRegex = addcslashes($form->forTemplate(), '\\$'); |
||
| 115 | $content = preg_replace( |
||
| 116 | '/(<p[^>]*>)?\\$UserDefinedForm(<\\/p>)?/i', |
||
| 117 | $formEscapedForRegex, |
||
| 118 | $this->Content |
||
| 119 | ); |
||
| 120 | return [ |
||
| 121 | 'Content' => DBField::create_field('HTMLText', $content), |
||
| 122 | 'Form' => '' |
||
| 123 | ]; |
||
| 124 | } |
||
| 125 | } |
||
| 126 | |||
| 127 | return [ |
||
| 128 | 'Content' => DBField::create_field('HTMLText', $this->Content), |
||
| 129 | 'Form' => $this->Form() |
||
| 130 | ]; |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Keep the session alive for the user. |
||
| 135 | * |
||
| 136 | * @return int |
||
| 137 | */ |
||
| 138 | public function ping() |
||
| 139 | { |
||
| 140 | return 1; |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Get the form for the page. Form can be modified by calling {@link updateForm()} |
||
| 145 | * on a UserDefinedForm extension. |
||
| 146 | * |
||
| 147 | * @return Form |
||
| 148 | */ |
||
| 149 | public function Form() |
||
| 150 | { |
||
| 151 | $form = UserForm::create($this, 'Form_' . $this->ID); |
||
| 152 | /** @skipUpgrade */ |
||
| 153 | $form->setFormAction(Controller::join_links($this->Link(), 'Form')); |
||
| 154 | $this->generateConditionalJavascript(); |
||
| 155 | return $form; |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Generate the javascript for the conditional field show / hiding logic. |
||
| 160 | * |
||
| 161 | * @return void |
||
| 162 | */ |
||
| 163 | public function generateConditionalJavascript() |
||
| 164 | { |
||
| 165 | $default = ''; |
||
| 166 | $rules = ''; |
||
| 167 | |||
| 168 | $watch = []; |
||
| 169 | |||
| 170 | if ($this->data()->Fields()) { |
||
| 171 | /** @var EditableFormField $field */ |
||
| 172 | foreach ($this->data()->Fields() as $field) { |
||
| 173 | if ($result = $field->formatDisplayRules()) { |
||
| 174 | $watch[] = $result; |
||
| 175 | } |
||
| 176 | } |
||
| 177 | } |
||
| 178 | if ($watch) { |
||
| 179 | $rules .= $this->buildWatchJS($watch); |
||
| 180 | } |
||
| 181 | |||
| 182 | // Only add customScript if $default or $rules is defined |
||
| 183 | if ($rules) { |
||
| 184 | Requirements::customScript(<<<JS |
||
| 185 | (function($) { |
||
| 186 | $(document).ready(function() { |
||
| 187 | {$rules} |
||
| 188 | }); |
||
| 189 | })(jQuery); |
||
| 190 | JS |
||
| 191 | , 'UserFormsConditional'); |
||
| 192 | } |
||
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Process the form that is submitted through the site |
||
| 197 | * |
||
| 198 | * {@see UserForm::validate()} for validation step prior to processing |
||
| 199 | * |
||
| 200 | * @param array $data |
||
| 201 | * @param Form $form |
||
| 202 | * |
||
| 203 | * @return \SilverStripe\Control\HTTPResponse |
||
| 204 | */ |
||
| 205 | public function process($data, $form) |
||
| 401 | } |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Allows the use of field values in email body. |
||
| 405 | * |
||
| 406 | * @param ArrayList $fields |
||
| 407 | * @return ArrayData |
||
| 408 | */ |
||
| 409 | protected function getMergeFieldsMap($fields = []) |
||
| 410 | { |
||
| 411 | $data = ArrayData::create([]); |
||
| 412 | |||
| 413 | foreach ($fields as $field) { |
||
| 414 | $data->setField($field->Name, DBField::create_field('Text', $field->Value)); |
||
| 415 | } |
||
| 416 | |||
| 417 | return $data; |
||
| 418 | } |
||
| 419 | |||
| 420 | /** |
||
| 421 | * This action handles rendering the "finished" message, which is |
||
| 422 | * customizable by editing the ReceivedFormSubmission template. |
||
| 423 | * |
||
| 424 | * @return ViewableData |
||
| 425 | */ |
||
| 426 | public function finished() |
||
| 427 | { |
||
| 428 | $submission = $this->getRequest()->getSession()->get('userformssubmission'. $this->ID); |
||
| 429 | |||
| 430 | if ($submission) { |
||
| 431 | $submission = SubmittedForm::get()->byId($submission); |
||
| 432 | } |
||
| 433 | |||
| 434 | $referrer = isset($_GET['referrer']) ? urldecode($_GET['referrer']) : null; |
||
| 435 | |||
| 436 | if (!$this->DisableAuthenicatedFinishAction) { |
||
| 437 | $formProcessed = $this->getRequest()->getSession()->get('FormProcessed'); |
||
| 438 | |||
| 439 | if (!isset($formProcessed)) { |
||
| 440 | return $this->redirect($this->Link() . $referrer); |
||
| 441 | } else { |
||
| 442 | $securityID = $this->getRequest()->getSession()->get('SecurityID'); |
||
| 443 | // make sure the session matches the SecurityID and is not left over from another form |
||
| 444 | if ($formProcessed != $securityID) { |
||
| 445 | // they may have disabled tokens on the form |
||
| 446 | $securityID = md5($this->getRequest()->getSession()->get('FormProcessedNum')); |
||
| 447 | if ($formProcessed != $securityID) { |
||
| 448 | return $this->redirect($this->Link() . $referrer); |
||
| 449 | } |
||
| 450 | } |
||
| 451 | } |
||
| 452 | |||
| 453 | $this->getRequest()->getSession()->clear('FormProcessed'); |
||
| 454 | } |
||
| 455 | |||
| 456 | $data = [ |
||
| 457 | 'Submission' => $submission, |
||
| 458 | 'Link' => $referrer |
||
| 459 | ]; |
||
| 460 | |||
| 461 | $this->extend('updateReceivedFormSubmissionData', $data); |
||
| 462 | |||
| 463 | return $this->customise([ |
||
| 464 | 'Content' => $this->customise($data)->renderWith(__CLASS__ . '_ReceivedFormSubmission'), |
||
| 465 | 'Form' => '', |
||
| 466 | ]); |
||
| 467 | } |
||
| 468 | |||
| 469 | /** |
||
| 470 | * Outputs the required JS from the $watch input |
||
| 471 | * |
||
| 472 | * @param array $watch |
||
| 473 | * |
||
| 474 | * @return string |
||
| 475 | */ |
||
| 476 | protected function buildWatchJS($watch) |
||
| 502 | } |
||
| 503 | } |
||
| 504 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths