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 Form 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 Form, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class Form implements \ArrayAccess |
||
11 | { |
||
12 | /** |
||
13 | * @var array $form settings for the form |
||
14 | * @var array $elements all form elements |
||
15 | * @var array $output messages to display together with the form |
||
16 | * @var array $sessionKey key values for the session |
||
17 | */ |
||
18 | protected $form; |
||
19 | protected $elements; |
||
20 | protected $output; |
||
21 | protected $sessionKey; |
||
22 | |||
23 | /** |
||
24 | * @var boolean $rememberValues remember values in the session. |
||
25 | */ |
||
26 | protected $rememberValues; |
||
27 | |||
28 | /** |
||
29 | * @var Anax\DI\DIInterface $di the DI service container. |
||
30 | */ |
||
31 | protected $di; |
||
32 | |||
33 | |||
34 | |||
35 | /** |
||
36 | * Constructor injects with DI container. |
||
37 | * |
||
38 | * @param Anax\DI\DIInterface $di a service container |
||
39 | */ |
||
40 | 6 | public function __construct(DIInterface $di) |
|
44 | |||
45 | |||
46 | |||
47 | /** |
||
48 | * Implementing ArrayAccess for this->elements |
||
49 | */ |
||
50 | public function offsetSet($offset, $value) |
||
51 | { |
||
52 | View Code Duplication | if (is_null($offset)) { |
|
53 | $this->elements[] = $value; |
||
54 | } else { |
||
55 | $this->elements[$offset] = $value; |
||
56 | } |
||
57 | } |
||
58 | |||
59 | public function offsetExists($offset) |
||
63 | |||
64 | public function offsetUnset($offset) |
||
68 | |||
69 | public function offsetGet($offset) |
||
75 | |||
76 | |||
77 | |||
78 | /** |
||
79 | * Add a form element |
||
80 | * |
||
81 | * @param array $form details for the form |
||
82 | * @param array $elements all the elements |
||
83 | * |
||
84 | * @return $this |
||
85 | */ |
||
86 | 6 | public function create($form = [], $elements = []) |
|
87 | { |
||
88 | $defaults = [ |
||
89 | // Always have an id |
||
90 | 6 | "id" => "anax/htmlform", |
|
91 | |||
92 | // Use a default class on <form> to ease styling |
||
93 | "class" => "htmlform", |
||
94 | |||
95 | // Wrap fields within <fieldset> |
||
96 | "use_fieldset" => true, |
||
97 | |||
98 | // Use legend for fieldset, set it to string value |
||
99 | "legend" => null, |
||
100 | |||
101 | // Default wrapper element around form elements |
||
102 | "wrapper-element" => "p", |
||
103 | |||
104 | // Use a <br> after the label, where suitable |
||
105 | "br-after-label" => true, |
||
106 | |||
107 | // Default is to always encode values |
||
108 | "escape-values" => true, |
||
109 | ]; |
||
110 | 6 | $this->form = array_merge($defaults, $form); |
|
111 | |||
112 | 6 | $this->elements = []; |
|
113 | 6 | if (!empty($elements)) { |
|
114 | 4 | foreach ($elements as $key => $element) { |
|
115 | 4 | $this->elements[$key] = FormElementFactory::create($key, $element); |
|
116 | 4 | $this->elements[$key]->setDefault([ |
|
117 | 4 | "wrapper-element" => $this->form["wrapper-element"], |
|
118 | 4 | "br-after-label" => $this->form["br-after-label"], |
|
119 | 4 | "escape-values" => $this->form["escape-values"], |
|
120 | ]); |
||
121 | } |
||
122 | } |
||
123 | |||
124 | // Default values for <output> |
||
125 | 6 | $this->output = []; |
|
126 | |||
127 | // Setting keys used in the session |
||
128 | 6 | $generalKey = "anax/htmlform-" . $this->form["id"] . "#"; |
|
129 | 6 | $this->sessionKey = [ |
|
130 | 6 | "save" => $generalKey . "save", |
|
131 | 6 | "output" => $generalKey . "output", |
|
132 | 6 | "failed" => $generalKey . "failed", |
|
133 | 6 | "remember" => $generalKey . "remember", |
|
134 | ]; |
||
135 | |||
136 | 6 | return $this; |
|
137 | } |
||
138 | |||
139 | |||
140 | |||
141 | /** |
||
142 | * Add a form element |
||
143 | * |
||
144 | * @param FormElement $element the formelement to add. |
||
145 | * |
||
146 | * @return $this |
||
147 | */ |
||
148 | public function addElement($element) |
||
157 | |||
158 | |||
159 | |||
160 | /** |
||
161 | * Get a form element |
||
162 | * |
||
163 | * @param string $name the name of the element. |
||
164 | * |
||
165 | * @return \Anax\HTMLForm\FormElement |
||
166 | */ |
||
167 | View Code Duplication | public function getElement($name) |
|
174 | |||
175 | |||
176 | |||
177 | /** |
||
178 | * Remove an form element |
||
179 | * |
||
180 | * @param string $name the name of the element. |
||
181 | * |
||
182 | * @return $this |
||
183 | */ |
||
184 | View Code Duplication | public function removeElement($name) |
|
192 | |||
193 | |||
194 | |||
195 | /** |
||
196 | * Set validation to a form element |
||
197 | * |
||
198 | * @param string $element the name of the formelement to add validation rules to. |
||
199 | * @param array $rules array of validation rules. |
||
200 | * |
||
201 | * @return $this |
||
202 | */ |
||
203 | public function setValidation($element, $rules) |
||
208 | |||
209 | |||
210 | |||
211 | /** |
||
212 | * Add output to display to the user for what happened whith the form and |
||
213 | * optionally add a CSS class attribute. |
||
214 | * |
||
215 | * @param string $str the string to add as output. |
||
216 | * @param string $class a class attribute to set. |
||
217 | * |
||
218 | * @return $this. |
||
219 | */ |
||
220 | public function addOutput($str, $class = null) |
||
221 | { |
||
222 | $key = $this->sessionKey["output"]; |
||
223 | $session = $this->di->get("session"); |
||
224 | $output = $session->get($key); |
||
225 | |||
226 | $output["message"] = isset($output["message"]) |
||
227 | ? $output["message"] . " $str" |
||
228 | : $str; |
||
229 | |||
230 | if ($class) { |
||
231 | $output["class"] = $class; |
||
232 | } |
||
233 | $session->set($key, $output); |
||
234 | |||
235 | return $this; |
||
236 | } |
||
237 | |||
238 | |||
239 | |||
240 | /** |
||
241 | * Set a CSS class attribute for the <output> element. |
||
242 | * |
||
243 | * @param string $class a class attribute to set. |
||
244 | * |
||
245 | * @return $this. |
||
246 | */ |
||
247 | public function setOutputClass($class) |
||
256 | |||
257 | |||
258 | |||
259 | /** |
||
260 | * Remember current values in session, useful for storing values of |
||
261 | * current form when submitting it. |
||
262 | * |
||
263 | * @return $this. |
||
264 | */ |
||
265 | public function rememberValues() |
||
270 | |||
271 | |||
272 | |||
273 | |||
274 | /** |
||
275 | * Get value of a form element and respect settings of escaped or |
||
276 | * raw value. |
||
277 | * |
||
278 | * @param string $name the name of the formelement. |
||
279 | * |
||
280 | * @return mixed the value of the element. |
||
281 | */ |
||
282 | public function value($name) |
||
288 | |||
289 | |||
290 | |||
291 | /** |
||
292 | * Get escaped value of a form element. |
||
293 | * |
||
294 | * @param string $name the name of the formelement. |
||
295 | * |
||
296 | * @return mixed the value of the element. |
||
297 | */ |
||
298 | 4 | public function escValue($name) |
|
304 | |||
305 | |||
306 | |||
307 | /** |
||
308 | * Get raw value of a form element. |
||
309 | * |
||
310 | * @param string $name the name of the formelement. |
||
311 | * |
||
312 | * @return mixed the value of the element. |
||
313 | */ |
||
314 | 4 | public function rawValue($name) |
|
320 | |||
321 | |||
322 | |||
323 | /** |
||
324 | * Check if a element is checked |
||
325 | * |
||
326 | * @param string $name the name of the formelement. |
||
327 | * |
||
328 | * @return mixed the value of the element. |
||
329 | */ |
||
330 | public function checked($name) |
||
336 | |||
337 | |||
338 | |||
339 | /** |
||
340 | * Return HTML for the form. |
||
341 | * |
||
342 | * @param array $options with options affecting the form output. |
||
343 | * |
||
344 | * @return string with HTML for the form. |
||
345 | */ |
||
346 | 2 | public function getHTML($options = []) |
|
401 | |||
402 | |||
403 | |||
404 | /** |
||
405 | * Return HTML for the elements |
||
406 | * |
||
407 | * @param array $options with options affecting the form output. |
||
408 | * |
||
409 | * @return array with HTML for the formelements. |
||
410 | */ |
||
411 | 2 | public function getHTMLForElements($options = []) |
|
452 | |||
453 | |||
454 | |||
455 | |||
456 | /** |
||
457 | * Place the elements according to a layout and return the HTML |
||
458 | * |
||
459 | * @param array $elements as returned from GetHTMLForElements(). |
||
460 | * @param array $options with options affecting the layout. |
||
461 | * |
||
462 | * @return array with HTML for the formelements. |
||
463 | */ |
||
464 | 2 | public function getHTMLLayoutForElements($elements, $options = []) |
|
512 | |||
513 | |||
514 | |||
515 | /** |
||
516 | * Get an array with all elements that failed validation together with their id and validation message. |
||
517 | * |
||
518 | * @return array with elements that failed validation. |
||
519 | */ |
||
520 | public function getValidationErrors() |
||
534 | |||
535 | |||
536 | |||
537 | /** |
||
538 | * Get output messages as <output>. |
||
539 | * |
||
540 | * @return string|null with the complete <output> element or null if no output. |
||
541 | */ |
||
542 | 2 | public function getOutput() |
|
557 | |||
558 | |||
559 | |||
560 | /** |
||
561 | * Init all element with values from session, clear all and fill in with values from the session. |
||
562 | * |
||
563 | * @param array $values retrieved from session |
||
564 | * |
||
565 | * @return void |
||
566 | */ |
||
567 | protected function initElements($values) |
||
608 | |||
609 | |||
610 | |||
611 | /** |
||
612 | * Check if a form was submitted and perform validation and call callbacks. |
||
613 | * The form is stored in the session if validation or callback fails. The |
||
614 | * page should then be redirected to the original form page, the form |
||
615 | * will populate from the session and should be rendered again. |
||
616 | * Form elements may remember their value if 'remember' is set and true. |
||
617 | * |
||
618 | * @param callable $callIfSuccess handler to call if function returns true. |
||
619 | * @param callable $callIfFail handler to call if function returns true. |
||
620 | * |
||
621 | * @throws \Anax\HTMLForm\Exception |
||
622 | * |
||
623 | * @return boolean|null $callbackStatus if submitted&validates, false if |
||
624 | * not validate, null if not submitted. |
||
625 | * If submitted the callback function |
||
626 | * will return the actual value which |
||
627 | * should be true or false. |
||
628 | */ |
||
629 | public function check($callIfSuccess = null, $callIfFail = null) |
||
817 | } |
||
818 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..