Complex classes like Bootstrap3Form 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 Bootstrap3Form, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
7 | class Bootstrap3Form extends Form |
||
8 | { |
||
9 | use Base; |
||
10 | |||
11 | private $bp; |
||
12 | private $prompt = array( |
||
13 | 'info' => 'glyphicon glyphicon-question-sign', |
||
14 | ); |
||
15 | private $input = ''; |
||
16 | private $align = 'form-horizontal'; |
||
17 | private $collapse = 'sm'; |
||
18 | private $indent = 2; |
||
19 | |||
20 | /** |
||
21 | * {@inheritdoc} |
||
22 | */ |
||
23 | 1 | public function __construct($name = 'form', $method = 'post', Bootstrap3 $bp) |
|
28 | |||
29 | /** |
||
30 | * A private property getter. |
||
31 | * |
||
32 | * @param string $name |
||
33 | * |
||
34 | * @return null|string |
||
35 | */ |
||
36 | 1 | public function __get($name) |
|
48 | /** |
||
49 | * This is to add html tags, or semicolons, or asterisks, or whatever you would like to all of the form's prompts. |
||
50 | * |
||
51 | * @param string $place Either '**info**', '**append**', or '**prepend**' to the prompt. You only have one shot at each. |
||
52 | * @param string $html Whatever you would like to add. For 'info', this will be the icon class you want to use. |
||
53 | * @param false|mixed $required If this is anything but (bool) false and $place == 'prepend', the the $html will only be prepended if the field is required per the ``$form->validator``. |
||
54 | * |
||
55 | * ```php |
||
56 | $form->prompt('prepend', '<font color="red">*</font> ', 'required'); // If the field is required it will add a red asterisk to the front. |
||
57 | |||
58 | $form->prompt('append', ':'); // Adds a semicolon to all of the prompts. |
||
59 | * ``` |
||
60 | */ |
||
61 | 1 | public function prompt($place, $html, $required = false) |
|
73 | |||
74 | /** |
||
75 | * Supersize or undersize your input fields. |
||
76 | * |
||
77 | * @param string $input Either '**lg**' (large), '**md**' (medium - the default), or '**sm**' (small). |
||
78 | * |
||
79 | * ```php |
||
80 | * $form->size('lg'); |
||
81 | * ``` |
||
82 | */ |
||
83 | 1 | public function size($input) |
|
87 | |||
88 | /** |
||
89 | * Utilize any Bootstrap form style |
||
90 | * |
||
91 | * @param string $direction The options are: |
||
92 | * |
||
93 | * - '**collapse**' - This will display the form prompt immediately above the field. |
||
94 | * - '**inline**' - All of the fields will be inline with each other, and the form prompts will be removed. |
||
95 | * - '**horizontal**' - Vertically aligns all of the fields with the prompt immediately preceding, and right aligned. |
||
96 | * |
||
97 | * @param string $collapse Either '**xs**', '**sm**', '**md**', or '**lg**'. This is the breaking point so to speak for a '**horizontal**' form. It is the device size on which the form will '**collapse**'. |
||
98 | * @param integer $indent The number of columns (up to 12) that you would like to indent the field in a '**horizontal**' form. |
||
99 | * |
||
100 | * ```php |
||
101 | * $form->align('collapse'); |
||
102 | * ``` |
||
103 | */ |
||
104 | 1 | public function align($direction = 'horizontal', $collapse = 'sm', $indent = 2) |
|
116 | |||
117 | /** |
||
118 | * Use to display a message on the retrun trip after you have ``$page->eject()``ed them. The Bootstrap alert status message will be displayed at the top of the form when you return ``$form->header()``. |
||
119 | * |
||
120 | * @param string $status Either '**success**', '**info**', '**warning**', or '**danger**'. If this is '**html**', then the $message will be delivered as is. |
||
121 | * @param string $message The message you would like to get across to your user. ``<h1-6>`` headers and ``<a>`` links may be used. |
||
122 | * |
||
123 | * ```php |
||
124 | * if ($vars = $form->validator->certified()) { |
||
125 | * $form->message('success', 'Good job, you are doing great!'); |
||
126 | * $page->eject($form->eject); |
||
127 | * } |
||
128 | * ``` |
||
129 | */ |
||
130 | 1 | public function message($status, $message) |
|
137 | |||
138 | /** |
||
139 | * {@inheritdoc} |
||
140 | */ |
||
141 | 1 | public function header(array $validate = array()) |
|
168 | |||
169 | /** |
||
170 | * {@inheritdoc} |
||
171 | * |
||
172 | * @param false|mixed $inline This tells us if you want the checkboxes to be inline (any value but false), or not (false). |
||
173 | */ |
||
174 | 1 | public function checkbox($field, array $attributes = array(), $inline = false) |
|
175 | { |
||
176 | 1 | $disabled = in_array('disabled', $attributes) ? 'disabled' : ''; |
|
177 | 1 | if ($inline !== false) { |
|
178 | 1 | $wrap = $this->page->tag('label', array('class' => array('checkbox-inline', $this->input, $disabled)), '%s'); |
|
179 | 1 | } else { |
|
180 | 1 | $wrap = $this->page->tag('div', array('class' => array('checkbox', $this->input, $disabled)), '<label>%s</label>'); |
|
181 | } |
||
182 | |||
183 | 1 | return parent::checkbox($field, $attributes, $wrap); |
|
184 | } |
||
185 | |||
186 | /** |
||
187 | * {@inheritdoc} |
||
188 | * |
||
189 | * @param false|mixed $inline This tells us if you want the radio buttons to be inline (any value but false), or not (false). |
||
190 | */ |
||
191 | 1 | public function radio($field, array $attributes = array(), $inline = false) |
|
192 | { |
||
193 | 1 | $disabled = in_array('disabled', $attributes) ? 'disabled' : ''; |
|
194 | 1 | if ($inline !== false) { |
|
195 | 1 | $wrap = $this->page->tag('label', array('class' => array('radio-inline', $this->input, $disabled)), '%s'); |
|
196 | 1 | } else { |
|
197 | 1 | $wrap = $this->page->tag('div', array('class' => array('radio', $this->input, $disabled)), '<label>%s</label>'); |
|
198 | } |
||
199 | |||
200 | 1 | return parent::radio($field, $attributes, $wrap); |
|
201 | } |
||
202 | |||
203 | /** |
||
204 | * Group an input field with addons. You can prepend and/or append a ``$bp->button(...)``, ``$bp->icon('...')``, or just a string of text. To prepend or append multiple elements, then make it an ``array($html, ...)`` of addons. |
||
205 | * |
||
206 | * @param string|array $prepend An element to place before the $input. |
||
207 | * @param string|array $append An element to place after the $input. |
||
208 | * @param string $input The form field to wrap. |
||
209 | * |
||
210 | * @return string A ``<div class="input-group">...</div>`` html string. |
||
211 | * |
||
212 | * ```php |
||
213 | * echo $form->group('$', '.00', $form->text('amount')); |
||
214 | * ``` |
||
215 | */ |
||
216 | 1 | public function group($prepend, $append, $input) |
|
217 | { |
||
218 | 1 | if (!empty($prepend)) { |
|
219 | 1 | foreach ((array) $prepend as $html) { |
|
220 | 1 | $class = (strpos($html, 'btn') !== false) ? 'input-group-btn' : 'input-group-addon'; |
|
221 | 1 | $input = $this->page->tag('div', array('class' => $class), $html).$input; |
|
222 | 1 | } |
|
223 | 1 | } |
|
224 | 1 | if (!empty($append)) { |
|
225 | 1 | foreach ((array) $append as $html) { |
|
226 | 1 | $class = (strpos($html, 'btn') !== false) ? 'input-group-btn' : 'input-group-addon'; |
|
227 | 1 | $input = $input.$this->page->tag('div', array('class' => $class), $html); |
|
228 | 1 | } |
|
229 | 1 | } |
|
230 | 1 | $group = array('input-group'); |
|
231 | 1 | if (!empty($this->input)) { |
|
232 | 1 | $group[] = str_replace('-', '-group-', $this->input); |
|
233 | 1 | } |
|
234 | |||
235 | 1 | return $this->page->tag('div', array('class' => $group), $input); |
|
236 | } |
||
237 | |||
238 | /** |
||
239 | * This is to add html tags, or semicolons, or asterisks, or whatever you would like include with all of the form's prompts. |
||
240 | * |
||
241 | * @param string|array $prompt The form label reference. If you want to include additional information relative to the field, then you can make this an ``array($prompt => $info)``, or an ``array($prompt, $info)`` that will appear when cliked or hovered over. To customize the icon set ``$form->prompt('info', 'fa fa-info-circle')``. |
||
242 | * @param string $name The name of the associated input field. |
||
243 | * @param string $id The id of the associated input field. |
||
244 | * |
||
245 | * @return string The generated HTML ``<label>``. |
||
246 | */ |
||
247 | 1 | public function label($prompt, $name, $id) |
|
298 | |||
299 | /** |
||
300 | * Adds a (properly formatted) $prompt to your $input field, and manages any error messages. |
||
301 | * |
||
302 | * @param string $prompt The $input's name. |
||
303 | * @param string $input A form field. |
||
304 | * @param string $error An optional error to override, or include with the field. |
||
305 | * |
||
306 | * @return string A ``<div class="form-group">...</div>`` html string. |
||
307 | * |
||
308 | * ```php |
||
309 | * echo $form->field('Amount', $form->group('$', '.00', $form->text('amount'))); |
||
310 | * ``` |
||
311 | */ |
||
312 | 1 | public function field($prompt, $input, $error = null) |
|
360 | |||
361 | /** |
||
362 | * Quickly adds a submit button to your form. |
||
363 | * |
||
364 | * @param string $submit What you would like the submit button to say. If it starts with a '**<**', then we assume you have spelled it all out for us. |
||
365 | * @param string $reset This will add a reset button if you give it a value, and if it starts with a '**<**' then it can be whatever you want it to be. You can keep adding args until you run out of ideas for buttons to include. |
||
366 | * |
||
367 | * @return string A ``<div class="form-group">...</div>`` html string with buttons. |
||
368 | * |
||
369 | * ```php |
||
370 | * echo $form->submit(); |
||
371 | * ``` |
||
372 | */ |
||
373 | 1 | public function submit($submit = 'Submit', $reset = '') |
|
374 | { |
||
375 | // never use name="submit" per: http://jqueryvalidation.org/reference/#developing-and-debugging-a-form |
||
376 | 1 | $buttons = func_get_args(); |
|
377 | 1 | if (substr($submit, 0, 1) != '<') { |
|
378 | 1 | $buttons[0] = $this->page->tag('button', array( |
|
379 | 1 | 'type' => 'submit', |
|
380 | 1 | 'class' => array('btn', 'btn-primary', str_replace('input', 'btn', $this->input)), |
|
381 | 1 | 'data-loading-text' => 'Submitting...', |
|
382 | 1 | ), $submit); |
|
383 | 1 | } |
|
384 | 1 | if (isset($buttons[1]) && substr($reset, 0, 1) != '<') { |
|
385 | 1 | $buttons[1] = $this->page->tag('button', array( |
|
386 | 1 | 'type' => 'reset', |
|
387 | 1 | 'class' => array('btn', 'btn-default', str_replace('input', 'btn', $this->input)), |
|
388 | 1 | ), $reset); |
|
389 | 1 | } |
|
390 | |||
391 | 1 | return $this->field('', implode(' ', $buttons)); |
|
392 | } |
||
393 | |||
394 | /** |
||
395 | * {@inheritdoc} |
||
396 | */ |
||
397 | 1 | public function close() |
|
405 | } |
||
406 |
The break statement is not necessary if it is preceded for example by a return statement:
If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.