Complex classes like FrmFieldFormHtml 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 FrmFieldFormHtml, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 6 | class FrmFieldFormHtml { |
||
| 7 | |||
| 8 | private $html; |
||
| 9 | |||
| 10 | private $html_id; |
||
| 11 | |||
| 12 | /** |
||
| 13 | * @var FrmFieldType |
||
| 14 | */ |
||
| 15 | private $field_obj; |
||
| 16 | |||
| 17 | private $field_id; |
||
| 18 | |||
| 19 | private $form = array(); |
||
| 20 | |||
| 21 | private $pass_args = array(); |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @since 3.0 |
||
| 25 | * |
||
| 26 | * @param array $atts |
||
| 27 | */ |
||
| 28 | public function __construct( $atts ) { |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @since 3.0 |
||
| 39 | * |
||
| 40 | * @param string $param |
||
| 41 | * @param array $atts |
||
| 42 | */ |
||
| 43 | private function _set( $param, $atts ) { |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @since 3.0 |
||
| 51 | * |
||
| 52 | * @param array $atts |
||
| 53 | */ |
||
| 54 | private function set_html( $atts ) { |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @since 3.0 |
||
| 63 | * |
||
| 64 | * @param array $atts |
||
| 65 | */ |
||
| 66 | private function set_field_id( $atts ) { |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @since 3.0 |
||
| 75 | * |
||
| 76 | * @param array $atts |
||
| 77 | */ |
||
| 78 | private function set_pass_args( $atts ) { |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @since 3.0 |
||
| 91 | * |
||
| 92 | * @param array $atts |
||
| 93 | * @param array $set |
||
| 94 | */ |
||
| 95 | private function set_from_field( $atts, $set ) { |
||
| 102 | |||
| 103 | public function get_html() { |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @since 3.0 |
||
| 113 | */ |
||
| 114 | private function replace_shortcodes_before_input() { |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @since 3.0 |
||
| 134 | */ |
||
| 135 | private function replace_field_values() { |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @since 3.0 |
||
| 151 | */ |
||
| 152 | private function replace_required_label_shortcode() { |
||
| 156 | |||
| 157 | /** |
||
| 158 | * If this is an HTML field, the values are included in the description. |
||
| 159 | * In this case, we don't want to run the wp shortcodes with the description included. |
||
| 160 | * |
||
| 161 | * @since 3.0 |
||
| 162 | */ |
||
| 163 | private function maybe_replace_description_shortcode( $wp_processed = false ) { |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @since 3.0 |
||
| 173 | */ |
||
| 174 | private function replace_description_shortcode() { |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Add an ID to the description for aria-describedby. |
||
| 182 | * This ID was added to the HTML in v3.0. |
||
| 183 | * @since 3.0 |
||
| 184 | */ |
||
| 185 | private function maybe_add_description_id() { |
||
| 199 | |||
| 200 | /** |
||
| 201 | * @since 3.0 |
||
| 202 | */ |
||
| 203 | private function replace_error_shortcode() { |
||
| 207 | |||
| 208 | /** |
||
| 209 | * replace [required_class] |
||
| 210 | * |
||
| 211 | * @since 3.0 |
||
| 212 | */ |
||
| 213 | private function replace_required_class() { |
||
| 217 | |||
| 218 | /** |
||
| 219 | * @since 3.0 |
||
| 220 | */ |
||
| 221 | private function replace_form_shortcodes() { |
||
| 232 | |||
| 233 | /** |
||
| 234 | * @since 3.0 |
||
| 235 | */ |
||
| 236 | public function replace_shortcodes_after_input() { |
||
| 244 | |||
| 245 | /** |
||
| 246 | * @since 3.0 |
||
| 247 | */ |
||
| 248 | private function filter_for_more_shortcodes() { |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Remove [collapse_this] if it's still included after all processing |
||
| 263 | * @since 3.0 |
||
| 264 | * |
||
| 265 | * @param string $html |
||
| 266 | */ |
||
| 267 | public function remove_collapse_shortcode( &$html ) { |
||
| 272 | |||
| 273 | /** |
||
| 274 | * @since 3.0 |
||
| 275 | */ |
||
| 276 | private function replace_shortcodes_with_atts() { |
||
| 294 | |||
| 295 | /** |
||
| 296 | * @param array $shortcode_atts |
||
| 297 | * |
||
| 298 | * @return string |
||
| 299 | */ |
||
| 300 | private function replace_input_shortcode( $shortcode_atts ) { |
||
| 304 | |||
| 305 | /** |
||
| 306 | * @param array $shortcode_atts |
||
| 307 | * |
||
| 308 | * @return array |
||
| 309 | */ |
||
| 310 | private function prepare_input_shortcode_atts( $shortcode_atts ) { |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Add the label position class into the HTML |
||
| 329 | * If the label position is inside, add a class to show the label if the field has a value. |
||
| 330 | * |
||
| 331 | * @since 3.0 |
||
| 332 | */ |
||
| 333 | private function add_class_to_label() { |
||
| 340 | |||
| 341 | /** |
||
| 342 | * replace [entry_key] |
||
| 343 | * |
||
| 344 | * @since 3.0 |
||
| 345 | */ |
||
| 346 | private function replace_entry_key() { |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Add classes to a field div |
||
| 353 | * |
||
| 354 | * @since 3.0 |
||
| 355 | */ |
||
| 356 | private function add_field_div_classes() { |
||
| 365 | |||
| 366 | |||
| 367 | /** |
||
| 368 | * Get the classes for a field div |
||
| 369 | * |
||
| 370 | * @since 3.0 |
||
| 371 | * |
||
| 372 | * @return string $classes |
||
| 373 | */ |
||
| 374 | private function get_field_div_classes() { |
||
| 398 | |||
| 399 | /** |
||
| 400 | * This filters shortcodes in the field HTML |
||
| 401 | * |
||
| 402 | * @since 3.0 |
||
| 403 | */ |
||
| 404 | private function process_wp_shortcodes() { |
||
| 409 | } |
||
| 410 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.