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 $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) |
||
154 | |||
155 | /** |
||
156 | * Sets a unique id for this particular form, so it can recognize |
||
157 | * it when a submit occurs |
||
158 | */ |
||
159 | private function setFormId() |
||
169 | |||
170 | /** |
||
171 | * Checks if this form has been submitted |
||
172 | * |
||
173 | * @param \CloudControl\Cms\cc\Request $request |
||
174 | * |
||
175 | * @return bool |
||
176 | */ |
||
177 | private function isFormSubmitted($request) |
||
181 | |||
182 | /** |
||
183 | * @param \cc\Request $request |
||
184 | * @return array |
||
185 | */ |
||
186 | private function getPostValues($request) |
||
195 | |||
196 | /** |
||
197 | * Temporarily stores the current user session in a backup variable |
||
198 | * and sets a fake user instead |
||
199 | */ |
||
200 | private function setUserSessionBackup() |
||
207 | |||
208 | /** |
||
209 | * Removes the fake user and restores the existing user |
||
210 | * session if it was there |
||
211 | */ |
||
212 | private function restoreUserSessionBackup() |
||
220 | |||
221 | private function setSubmitToSession() |
||
225 | |||
226 | private function isSubmitAllowed() |
||
234 | |||
235 | View Code Duplication | private function checkDocumentTypeParameter() |
|
242 | |||
243 | View Code Duplication | private function checkResponseFolderParameter() |
|
250 | |||
251 | View Code Duplication | private function checkSubTemplateParameter() |
|
258 | |||
259 | View Code Duplication | private function checkFormParameterNameParameter() |
|
266 | |||
267 | View Code Duplication | private function checkThankYouMessageParameter() |
|
274 | |||
275 | View Code Duplication | private function checkSubmitOncePerSessionParameter() |
|
282 | |||
283 | /** |
||
284 | * @throws \Exception |
||
285 | */ |
||
286 | private function checkRequiredParameters() |
||
292 | |||
293 | /** |
||
294 | * @return \cc\Request |
||
295 | */ |
||
296 | private function setPathBackup() |
||
306 | |||
307 | /** |
||
308 | * @param \cc\Request $request |
||
309 | */ |
||
310 | private function resetPathBackup($request) |
||
318 | |||
319 | /** |
||
320 | * @param $form |
||
321 | */ |
||
322 | private function setFormParameter($form) |
||
330 | } |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: