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 FrmEntryFormatter 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 FrmEntryFormatter, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 6 | class FrmEntryFormatter { |
||
| 7 | |||
| 8 | /** |
||
| 9 | * @var stdClass |
||
| 10 | * @since 2.03.11 |
||
| 11 | */ |
||
| 12 | protected $entry = null; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * @var FrmEntryValues |
||
| 16 | * @since 2.03.11 |
||
| 17 | */ |
||
| 18 | protected $entry_values = null; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var bool |
||
| 22 | * @since 2.03.11 |
||
| 23 | */ |
||
| 24 | protected $is_plain_text = false; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var bool |
||
| 28 | * @since 2.03.11 |
||
| 29 | */ |
||
| 30 | protected $include_user_info = false; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var bool |
||
| 34 | * @since 2.03.11 |
||
| 35 | */ |
||
| 36 | protected $include_blank = false; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var string |
||
| 40 | * @since 2.03.11 |
||
| 41 | */ |
||
| 42 | protected $format = 'text'; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var string |
||
| 46 | * @since 2.03.11 |
||
| 47 | */ |
||
| 48 | protected $direction = 'ltr'; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var FrmTableHTMLGenerator |
||
| 52 | * @since 2.03.11 |
||
| 53 | */ |
||
| 54 | protected $table_generator = null; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var bool |
||
| 58 | * @since 2.03.11 |
||
| 59 | */ |
||
| 60 | protected $is_clickable = false; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var array |
||
| 64 | * @since 2.03.11 |
||
| 65 | */ |
||
| 66 | protected $include_extras = array(); |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var array |
||
| 70 | * @since 2.03.11 |
||
| 71 | */ |
||
| 72 | protected $skip_fields = array( 'captcha' ); |
||
| 73 | |||
| 74 | /** |
||
| 75 | * FrmEntryFormat constructor |
||
| 76 | * |
||
| 77 | * @since 2.03.11 |
||
| 78 | * |
||
| 79 | * @param $atts |
||
| 80 | */ |
||
| 81 | public function __construct( $atts ) { |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Set the entry property |
||
| 103 | * |
||
| 104 | * @since 2.03.11 |
||
| 105 | * |
||
| 106 | * @param array $atts |
||
| 107 | */ |
||
| 108 | protected function init_entry( $atts ) { |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Set the entry values property |
||
| 124 | * |
||
| 125 | * @since 2.03.11 |
||
| 126 | * |
||
| 127 | * @param array $atts |
||
| 128 | */ |
||
| 129 | protected function init_entry_values( $atts ) { |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Set the format property |
||
| 136 | * |
||
| 137 | * @since 2.03.11 |
||
| 138 | * |
||
| 139 | * @param array $atts |
||
| 140 | */ |
||
| 141 | protected function init_format( $atts ) { |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Set the is_plain_text property |
||
| 162 | * |
||
| 163 | * @since 2.03.11 |
||
| 164 | * |
||
| 165 | * @param array $atts |
||
| 166 | */ |
||
| 167 | protected function init_is_plain_text( $atts ) { |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Set the include_blank property |
||
| 177 | * |
||
| 178 | * @since 2.03.11 |
||
| 179 | * |
||
| 180 | * @param array $atts |
||
| 181 | */ |
||
| 182 | protected function init_include_blank( $atts ) { |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Set the direction property |
||
| 190 | * |
||
| 191 | * @since 2.03.11 |
||
| 192 | * |
||
| 193 | * @param array $atts |
||
| 194 | */ |
||
| 195 | protected function init_direction( $atts ) { |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Set the include_user_info property |
||
| 203 | * |
||
| 204 | * @since 2.03.11 |
||
| 205 | * |
||
| 206 | * @param array $atts |
||
| 207 | */ |
||
| 208 | protected function init_include_user_info( $atts ) { |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Set the table_generator property |
||
| 216 | * |
||
| 217 | * @since 2.03.11 |
||
| 218 | * |
||
| 219 | * @param array $atts |
||
| 220 | */ |
||
| 221 | protected function init_table_generator( $atts ) { |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Set the is_clickable property |
||
| 227 | * |
||
| 228 | * @since 2.03.11 |
||
| 229 | * |
||
| 230 | * @param array $atts |
||
| 231 | */ |
||
| 232 | protected function init_is_clickable( $atts ) { |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Package and return the formatted entry values |
||
| 240 | * |
||
| 241 | * @since 2.03.11 |
||
| 242 | * |
||
| 243 | * @return array|string |
||
| 244 | */ |
||
| 245 | public function get_formatted_entry_values() { |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Return the formatted HTML table with entry values |
||
| 271 | * |
||
| 272 | * @since 2.03.11 |
||
| 273 | * |
||
| 274 | * @return string |
||
| 275 | */ |
||
| 276 | protected function prepare_html_table() { |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Return the formatted plain text content |
||
| 296 | * |
||
| 297 | * @since 2.03.11 |
||
| 298 | * |
||
| 299 | * @return string |
||
| 300 | */ |
||
| 301 | protected function prepare_plain_text_block() { |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Prepare the array output |
||
| 315 | * |
||
| 316 | * @since 2.03.11 |
||
| 317 | * |
||
| 318 | * @return array |
||
| 319 | */ |
||
| 320 | protected function prepare_array() { |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Push field values to array content |
||
| 330 | * |
||
| 331 | * @since 2.03.11 |
||
| 332 | * |
||
| 333 | * @param array $field_values |
||
| 334 | * @param array $output |
||
| 335 | */ |
||
| 336 | protected function push_field_values_to_array( $field_values, &$output ) { |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Push a single field to the array content |
||
| 344 | * |
||
| 345 | * @since 2.03.11 |
||
| 346 | * |
||
| 347 | * @param FrmFieldValue $field_value |
||
| 348 | * @param array $output |
||
| 349 | */ |
||
| 350 | protected function push_single_field_to_array( $field_value, &$output ) { |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Add a row of values to the plain text content |
||
| 364 | * |
||
| 365 | * @since 2.03.11 |
||
| 366 | * |
||
| 367 | * @param string $label |
||
| 368 | * @param mixed $display_value |
||
| 369 | * @param string $content |
||
| 370 | */ |
||
| 371 | protected function add_plain_text_row( $label, $display_value, &$content ) { |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Add a field value to the HTML table or plain text content |
||
| 383 | * |
||
| 384 | * @since 2.03.11 |
||
| 385 | * |
||
| 386 | * @param FrmFieldValue $field_value |
||
| 387 | * @param string $content |
||
| 388 | */ |
||
| 389 | protected function add_field_value_to_content( $field_value, &$content ) { |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Package the value arguments for an HTML row |
||
| 404 | * |
||
| 405 | * @since 2.03.11 |
||
| 406 | * |
||
| 407 | * @param FrmFieldValue $field_value |
||
| 408 | * |
||
| 409 | * @return array |
||
| 410 | */ |
||
| 411 | protected function package_value_args( $field_value ) { |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Add user info to an HTML table |
||
| 421 | * |
||
| 422 | * @since 2.03.11 |
||
| 423 | * |
||
| 424 | * @param string $content |
||
| 425 | */ |
||
| 426 | protected function add_user_info_to_html_table( &$content ) { |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Add user info to plain text content |
||
| 444 | * |
||
| 445 | * @since 2.03.11 |
||
| 446 | * |
||
| 447 | * @param string $content |
||
| 448 | */ |
||
| 449 | protected function add_user_info_to_plain_text_content( &$content ) { |
||
| 457 | |||
| 458 | /** |
||
| 459 | * Check if a field should be included in the content |
||
| 460 | * |
||
| 461 | * @since 2.03.11 |
||
| 462 | * |
||
| 463 | * @param FrmFieldValue $field_value |
||
| 464 | * |
||
| 465 | * @return bool |
||
| 466 | */ |
||
| 467 | protected function include_field_in_content( $field_value ) { |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Check if a field is normally a skipped type |
||
| 486 | * |
||
| 487 | * @since 2.03.11 |
||
| 488 | * |
||
| 489 | * @param FrmFieldValue $field_value |
||
| 490 | * |
||
| 491 | * @return bool |
||
| 492 | */ |
||
| 493 | protected function is_extra_field( $field_value ) { |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Check if an extra field is included |
||
| 499 | * |
||
| 500 | * @since 2.03.11 |
||
| 501 | * |
||
| 502 | * @param FrmFieldValue $field_value |
||
| 503 | * |
||
| 504 | * @return bool |
||
| 505 | */ |
||
| 506 | protected function is_extra_field_included( $field_value ) { |
||
| 509 | |||
| 510 | /** |
||
| 511 | * Add a row in an HTML table |
||
| 512 | * |
||
| 513 | * @since 2.03.11 |
||
| 514 | * |
||
| 515 | * @param array $value_args |
||
| 516 | * $value_args = [ |
||
| 517 | * 'label' => (string) The label. Required |
||
| 518 | * 'value' => (mixed) The value to add. Required |
||
| 519 | * 'field_type' => (string) The field type. Blank string if not a field. |
||
| 520 | * ] |
||
| 521 | * @param string $content |
||
| 522 | */ |
||
| 523 | protected function add_html_row( $value_args, &$content ) { |
||
| 528 | |||
| 529 | /** |
||
| 530 | * Prepare the displayed value for an array |
||
| 531 | * |
||
| 532 | * @since 2.03.11 |
||
| 533 | * |
||
| 534 | * @param mixed $value |
||
| 535 | * |
||
| 536 | * @return mixed|string |
||
| 537 | */ |
||
| 538 | protected function prepare_display_value_for_array( $value ) { |
||
| 541 | |||
| 542 | |||
| 543 | /** |
||
| 544 | * Prepare a field's display value for an HTML table |
||
| 545 | * |
||
| 546 | * @since 2.03.11 |
||
| 547 | * |
||
| 548 | * @param mixed $display_value |
||
| 549 | * @param string $field_type |
||
| 550 | * |
||
| 551 | * @return mixed|string |
||
| 552 | */ |
||
| 553 | protected function prepare_display_value_for_html_table( $display_value, $field_type = '' ) { |
||
| 559 | |||
| 560 | /** |
||
| 561 | * Prepare a field's display value for plain text content |
||
| 562 | * |
||
| 563 | * @since 2.03.11 |
||
| 564 | * |
||
| 565 | * @param mixed $display_value |
||
| 566 | * |
||
| 567 | * @return string|int |
||
| 568 | */ |
||
| 569 | protected function prepare_display_value_for_plain_text_content( $display_value ) { |
||
| 575 | |||
| 576 | /** |
||
| 577 | * Flatten an array |
||
| 578 | * |
||
| 579 | * @since 2.03.11 |
||
| 580 | * |
||
| 581 | * @param array|string|int $value |
||
| 582 | * |
||
| 583 | * @return string|int |
||
| 584 | */ |
||
| 585 | protected function flatten_array( $value ) { |
||
| 592 | |||
| 593 | /** |
||
| 594 | * Strip HTML if from email value if plain text is selected |
||
| 595 | * |
||
| 596 | * @since 2.0.21 |
||
| 597 | * |
||
| 598 | * @param mixed $value |
||
| 599 | * |
||
| 600 | * @return mixed |
||
| 601 | */ |
||
| 602 | protected function strip_html( $value ) { |
||
| 621 | |||
| 622 | } |