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 FormComponent 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 FormComponent, and based on these observations, apply Extract Interface, too.
| 1 | <?php | ||
| 8 | class FormComponent Extends BaseComponent | ||
| 9 | { | ||
| 10 | const GET_PARAMETER_PATH = 'path'; | ||
| 11 | |||
| 12 | const PARAMETER_CMS_PREFIX = 'cmsPrefix'; | ||
| 13 | const PARAMETER_DOCUMENT_TYPE = 'documentType'; | ||
| 14 | const PARAMETER_DOCUMENT_TYPES = 'documentTypes'; | ||
| 15 | const PARAMETER_FORM_ID = 'formId'; | ||
| 16 | const PARAMETER_FORM_PARAMETER_NAME = 'formParameterName'; | ||
| 17 | const PARAMETER_HIDE_TITLE_AND_STATE = 'hideTitleAndState'; | ||
| 18 | const PARAMETER_RESPONSE_FOLDER = 'responseFolder'; | ||
| 19 | const PARAMETER_SMALLEST_IMAGE = 'smallestImage'; | ||
| 20 | const PARAMETER_SUBMIT_ONCE_PER_SESSION = 'submitOncePerSession'; | ||
| 21 | const PARAMETER_SUB_TEMPLATE = 'subTemplate'; | ||
| 22 | const PARAMETER_THANK_YOU_MESSAGE = 'thankYouMessage'; | ||
| 23 | |||
| 24 | const SESSION_PARAMETER_CLOUDCONTROL = 'cloudcontrol'; | ||
| 25 | const SESSION_PARAMETER_FORM_COMPONENT = 'FormComponent'; | ||
| 26 | /** | ||
| 27 | * @var null|string | ||
| 28 | */ | ||
| 29 | protected $documentType = null; | ||
| 30 | /** | ||
| 31 | * @var null|string | ||
| 32 | */ | ||
| 33 | protected $responseFolder = null; | ||
| 34 | /** | ||
| 35 | * @var string | ||
| 36 | */ | ||
| 37 | protected $subTemplate = 'cms/documents/document-form-form'; | ||
| 38 | /** | ||
| 39 | * @var string | ||
| 40 | */ | ||
| 41 | protected $formParameterName = 'form'; | ||
| 42 | /** | ||
| 43 | * @var string | ||
| 44 | */ | ||
| 45 | protected $thankYouMessage = 'Thank you for sending us your response.'; | ||
| 46 | |||
| 47 | /** | ||
| 48 | * @var bool | ||
| 49 | */ | ||
| 50 | protected $submitOncePerSession = false; | ||
| 51 | |||
| 52 | /** | ||
| 53 | * @var string | ||
| 54 | */ | ||
| 55 | private $formId; | ||
| 56 | /** | ||
| 57 | * @var null|string | ||
| 58 | */ | ||
| 59 | private $getPathBackup = null; | ||
| 60 | |||
| 61 | /** | ||
| 62 | * @var null|\stdClass | ||
| 63 | */ | ||
| 64 | private $userSessionBackup = null; | ||
| 65 | |||
| 66 | /** | ||
| 67 | * @param Storage $storage | ||
| 68 | * | ||
| 69 | * @return void | ||
| 70 | * @throws \Exception | ||
| 71 | */ | ||
| 72 | public function run(Storage $storage) | ||
| 81 | |||
| 82 | /** | ||
| 83 | * @param null|Application $application | ||
| 84 | * | ||
| 85 | * @throws \Exception | ||
| 86 | */ | ||
| 87 | public function render($application = null) | ||
| 96 | |||
| 97 | /** | ||
| 98 | * Checks if parameters were given in the CMS configuration and | ||
| 99 | * sets them to their respective fields | ||
| 100 | */ | ||
| 101 | private function checkParameters() | ||
| 110 | |||
| 111 | /** | ||
| 112 | * Sets variables needed for rendering the form template | ||
| 113 | * | ||
| 114 | * @param $storage | ||
| 115 | */ | ||
| 116 | private function initialize($storage) | ||
| 126 | |||
| 127 | /** | ||
| 128 | * If the form has been submitted, save the document | ||
| 129 | * Calls $this->postSubmit() afterwards | ||
| 130 | * | ||
| 131 | * @param Storage $storage | ||
| 132 | */ | ||
| 133 | private function checkSubmit($storage) | ||
| 144 | |||
| 145 | /** | ||
| 146 | * Hook for derived classes to take actions after | ||
| 147 | * submitting the form | ||
| 148 | * | ||
| 149 | * @param $postValues | ||
| 150 | * @param $storage | ||
| 151 | */ | ||
| 152 | protected function postSubmit($postValues, $storage) | ||
| 155 | |||
| 156 | /** | ||
| 157 | * Sets a unique id for this particular form, so it can recognize | ||
| 158 | * it when a submit occurs | ||
| 159 | */ | ||
| 160 | private function setFormId() | ||
| 170 | |||
| 171 | /** | ||
| 172 | * Checks if this form has been submitted | ||
| 173 | * | ||
| 174 | * @param $request | ||
| 175 | * | ||
| 176 | * @return bool | ||
| 177 | */ | ||
| 178 | private function isFormSubmitted($request) | ||
| 182 | |||
| 183 | /** | ||
| 184 | * | ||
| 185 | * | ||
| 186 | * @param $request | ||
| 187 | */ | ||
| 188 | private function getPostValues($request) | ||
| 197 | |||
| 198 | /** | ||
| 199 | * Temporarily stores the current user session in a backup variable | ||
| 200 | * and sets a fake user instead | ||
| 201 | */ | ||
| 202 | private function setUserSessionBackup() | ||
| 209 | |||
| 210 | /** | ||
| 211 | * Removes the fake user and restores the existing user | ||
| 212 | * session if it was there | ||
| 213 | */ | ||
| 214 | private function restoreUserSessionBackup() | ||
| 222 | |||
| 223 | private function setSubmitToSession() | ||
| 227 | |||
| 228 | private function isSubmitAllowed() | ||
| 236 | |||
| 237 | View Code Duplication | private function checkDocumentTypeParameter() | |
| 244 | |||
| 245 | View Code Duplication | private function checkResponseFolderParameter() | |
| 252 | |||
| 253 | View Code Duplication | private function checkSubTemplateParameter() | |
| 260 | |||
| 261 | View Code Duplication | private function checkFormParameterNameParameter() | |
| 268 | |||
| 269 | View Code Duplication | private function checkThankYouMessageParameter() | |
| 276 | |||
| 277 | View Code Duplication | private function checkSubmitOncePerSessionParameter() | |
| 284 | |||
| 285 | /** | ||
| 286 | * @throws \Exception | ||
| 287 | */ | ||
| 288 | private function checkRequiredParameters() | ||
| 294 | |||
| 295 | /** | ||
| 296 | * @return \library\cc\Request | ||
| 297 | */ | ||
| 298 | private function setPathBackup() | ||
| 308 | |||
| 309 | /** | ||
| 310 | * @param $get | ||
| 311 | * @param $request | ||
| 312 | */ | ||
| 313 | private function resetPathBackup($get, $request) | ||
| 321 | |||
| 322 | /** | ||
| 323 | * @param $form | ||
| 324 | */ | ||
| 325 | private function setFormParameter($form) | ||
| 333 | } | 
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.