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 WP_Scripts 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 WP_Scripts, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class WP_Scripts extends WP_Dependencies { |
||
| 19 | /** |
||
| 20 | * Base URL for scripts. |
||
| 21 | * |
||
| 22 | * Full URL with trailing slash. |
||
| 23 | * |
||
| 24 | * @since 2.6.0 |
||
| 25 | * @access public |
||
| 26 | * @var string |
||
| 27 | */ |
||
| 28 | public $base_url; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * URL of the content directory. |
||
| 32 | * |
||
| 33 | * @since 2.8.0 |
||
| 34 | * @access public |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | public $content_url; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Default version string for stylesheets. |
||
| 41 | * |
||
| 42 | * @since 2.6.0 |
||
| 43 | * @access public |
||
| 44 | * @var string |
||
| 45 | */ |
||
| 46 | public $default_version; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Holds handles of scripts which are enqueued in footer. |
||
| 50 | * |
||
| 51 | * @since 2.8.0 |
||
| 52 | * @access public |
||
| 53 | * @var array |
||
| 54 | */ |
||
| 55 | public $in_footer = array(); |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Holds a list of script handles which will be concatenated. |
||
| 59 | * |
||
| 60 | * @since 2.8.0 |
||
| 61 | * @access public |
||
| 62 | * @var string |
||
| 63 | */ |
||
| 64 | public $concat = ''; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Holds a string which contains script handles and their version. |
||
| 68 | * |
||
| 69 | * @since 2.8.0 |
||
| 70 | * @deprecated 3.4.0 |
||
| 71 | * @access public |
||
| 72 | * @var string |
||
| 73 | */ |
||
| 74 | public $concat_version = ''; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Whether to perform concatenation. |
||
| 78 | * |
||
| 79 | * @since 2.8.0 |
||
| 80 | * @access public |
||
| 81 | * @var bool |
||
| 82 | */ |
||
| 83 | public $do_concat = false; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Holds HTML markup of scripts and additional data if concatenation |
||
| 87 | * is enabled. |
||
| 88 | * |
||
| 89 | * @since 2.8.0 |
||
| 90 | * @access public |
||
| 91 | * @var string |
||
| 92 | */ |
||
| 93 | public $print_html = ''; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Holds inline code if concatenation is enabled. |
||
| 97 | * |
||
| 98 | * @since 2.8.0 |
||
| 99 | * @access public |
||
| 100 | * @var string |
||
| 101 | */ |
||
| 102 | public $print_code = ''; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Holds a list of script handles which are not in the default directory |
||
| 106 | * if concatenation is enabled. |
||
| 107 | * |
||
| 108 | * Unused in core. |
||
| 109 | * |
||
| 110 | * @since 2.8.0 |
||
| 111 | * @access public |
||
| 112 | * @var string |
||
| 113 | */ |
||
| 114 | public $ext_handles = ''; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Holds a string which contains handles and versions of scripts which |
||
| 118 | * are not in the default directory if concatenation is enabled. |
||
| 119 | * |
||
| 120 | * Unused in core. |
||
| 121 | * |
||
| 122 | * @since 2.8.0 |
||
| 123 | * @access public |
||
| 124 | * @var string |
||
| 125 | */ |
||
| 126 | public $ext_version = ''; |
||
| 127 | |||
| 128 | /** |
||
| 129 | * List of default directories. |
||
| 130 | * |
||
| 131 | * @since 2.8.0 |
||
| 132 | * @access public |
||
| 133 | * @var array |
||
| 134 | */ |
||
| 135 | public $default_dirs; |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Constructor. |
||
| 139 | * |
||
| 140 | * @since 2.6.0 |
||
| 141 | * @access public |
||
| 142 | */ |
||
| 143 | public function __construct() { |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Initialize the class. |
||
| 150 | * |
||
| 151 | * @since 3.4.0 |
||
| 152 | * @access public |
||
| 153 | */ |
||
| 154 | public function init() { |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Prints scripts. |
||
| 167 | * |
||
| 168 | * Prints the scripts passed to it or the print queue. Also prints all necessary dependencies. |
||
| 169 | * |
||
| 170 | * @since 2.1.0 |
||
| 171 | * @since 2.8.0 Added the `$group` parameter. |
||
| 172 | * @access public |
||
| 173 | * |
||
| 174 | * @param mixed $handles Optional. Scripts to be printed. (void) prints queue, (string) prints |
||
| 175 | * that script, (array of strings) prints those scripts. Default false. |
||
| 176 | * @param int $group Optional. If scripts were queued in groups prints this group number. |
||
| 177 | * Default false. |
||
| 178 | * @return array Scripts that have been printed. |
||
| 179 | */ |
||
| 180 | public function print_scripts( $handles = false, $group = false ) { |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Prints extra scripts of a registered script. |
||
| 186 | * |
||
| 187 | * @since 2.1.0 |
||
| 188 | * @since 2.8.0 Added the `$echo` parameter. |
||
| 189 | * @deprecated 3.3.0 |
||
| 190 | * @access public |
||
| 191 | * |
||
| 192 | * @see print_extra_script() |
||
| 193 | * |
||
| 194 | * @param string $handle The script's registered handle. |
||
| 195 | * @param bool $echo Optional. Whether to echo the extra script instead of just returning it. |
||
| 196 | * Default true. |
||
| 197 | * @return bool|string|void Void if no data exists, extra scripts if `$echo` is true, true otherwise. |
||
| 198 | */ |
||
| 199 | public function print_scripts_l10n( $handle, $echo = true ) { |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Prints extra scripts of a registered script. |
||
| 206 | * |
||
| 207 | * @since 3.3.0 |
||
| 208 | * @access public |
||
| 209 | * |
||
| 210 | * @param string $handle The script's registered handle. |
||
| 211 | * @param bool $echo Optional. Whether to echo the extra script instead of just returning it. |
||
| 212 | * Default true. |
||
| 213 | * @return bool|string|void Void if no data exists, extra scripts if `$echo` is true, true otherwise. |
||
| 214 | */ |
||
| 215 | public function print_extra_script( $handle, $echo = true ) { |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Processes a script dependency. |
||
| 233 | * |
||
| 234 | * @since 2.6.0 |
||
| 235 | * @since 2.8.0 Added the `$group` parameter. |
||
| 236 | * @access public |
||
| 237 | * |
||
| 238 | * @see WP_Dependencies::do_item() |
||
| 239 | * |
||
| 240 | * @param string $handle The script's registered handle. |
||
| 241 | * @param int|false $group Optional. Group level: (int) level, (false) no groups. Default false. |
||
| 242 | * @return bool True on success, false on failure. |
||
| 243 | */ |
||
| 244 | public function do_item( $handle, $group = false ) { |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Adds extra code to a registered script. |
||
| 369 | * |
||
| 370 | * @since 4.5.0 |
||
| 371 | * @access public |
||
| 372 | * |
||
| 373 | * @param string $handle Name of the script to add the inline script to. Must be lowercase. |
||
| 374 | * @param string $data String containing the javascript to be added. |
||
| 375 | * @param string $position Optional. Whether to add the inline script before the handle |
||
| 376 | * or after. Default 'after'. |
||
| 377 | * @return bool True on success, false on failure. |
||
| 378 | */ |
||
| 379 | View Code Duplication | public function add_inline_script( $handle, $data, $position = 'after' ) { |
|
| 380 | if ( ! $data ) { |
||
| 381 | return false; |
||
| 382 | } |
||
| 383 | |||
| 384 | if ( 'after' !== $position ) { |
||
| 385 | $position = 'before'; |
||
| 386 | } |
||
| 387 | |||
| 388 | $script = (array) $this->get_data( $handle, $position ); |
||
| 389 | $script[] = $data; |
||
| 390 | |||
| 391 | return $this->add_data( $handle, $position, $script ); |
||
| 392 | } |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Prints inline scripts registered for a specific handle. |
||
| 396 | * |
||
| 397 | * @since 4.5.0 |
||
| 398 | * @access public |
||
| 399 | * |
||
| 400 | * @param string $handle Name of the script to add the inline script to. Must be lowercase. |
||
| 401 | * @param string $position Optional. Whether to add the inline script before the handle |
||
| 402 | * or after. Default 'after'. |
||
| 403 | * @param bool $echo Optional. Whether to echo the script instead of just returning it. |
||
| 404 | * Default true. |
||
| 405 | * @return string|false Script on success, false otherwise. |
||
| 406 | */ |
||
| 407 | View Code Duplication | public function print_inline_script( $handle, $position = 'after', $echo = true ) { |
|
| 408 | $output = $this->get_data( $handle, $position ); |
||
| 409 | |||
| 410 | if ( empty( $output ) ) { |
||
| 411 | return false; |
||
| 412 | } |
||
| 413 | |||
| 414 | $output = trim( implode( "\n", $output ), "\n" ); |
||
| 415 | |||
| 416 | if ( $echo ) { |
||
| 417 | printf( "<script type='text/javascript'>\n%s\n</script>\n", $output ); |
||
| 418 | } |
||
| 419 | |||
| 420 | return $output; |
||
| 421 | } |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Localizes a script, only if the script has already been added. |
||
| 425 | * |
||
| 426 | * @since 2.1.0 |
||
| 427 | * @access public |
||
| 428 | * |
||
| 429 | * @param string $handle |
||
| 430 | * @param string $object_name |
||
| 431 | * @param array $l10n |
||
| 432 | * @return bool |
||
| 433 | */ |
||
| 434 | public function localize( $handle, $object_name, $l10n ) { |
||
| 462 | |||
| 463 | /** |
||
| 464 | * Sets handle group. |
||
| 465 | * |
||
| 466 | * @since 2.8.0 |
||
| 467 | * @access public |
||
| 468 | * |
||
| 469 | * @see WP_Dependencies::set_group() |
||
| 470 | * |
||
| 471 | * @param string $handle Name of the item. Should be unique. |
||
| 472 | * @param bool $recursion Internal flag that calling function was called recursively. |
||
| 473 | * @param int|false $group Optional. Group level: (int) level, (false) no groups. Default false. |
||
| 474 | * @return bool Not already in the group or a lower group |
||
| 475 | */ |
||
| 476 | public function set_group( $handle, $recursion, $group = false ) { |
||
| 487 | |||
| 488 | /** |
||
| 489 | * Determines script dependencies. |
||
| 490 | * |
||
| 491 | * @since 2.1.0 |
||
| 492 | * @access public |
||
| 493 | * |
||
| 494 | * @see WP_Dependencies::all_deps() |
||
| 495 | * |
||
| 496 | * @param mixed $handles Item handle and argument (string) or item handles and arguments (array of strings). |
||
| 497 | * @param bool $recursion Internal flag that function is calling itself. |
||
| 498 | * @param int|false $group Optional. Group level: (int) level, (false) no groups. Default false. |
||
| 499 | * @return bool True on success, false on failure. |
||
| 500 | */ |
||
| 501 | View Code Duplication | public function all_deps( $handles, $recursion = false, $group = false ) { |
|
| 515 | |||
| 516 | /** |
||
| 517 | * Processes items and dependencies for the head group. |
||
| 518 | * |
||
| 519 | * @since 2.8.0 |
||
| 520 | * @access public |
||
| 521 | * |
||
| 522 | * @see WP_Dependencies::do_items() |
||
| 523 | * |
||
| 524 | * @return array Handles of items that have been processed. |
||
| 525 | */ |
||
| 526 | public function do_head_items() { |
||
| 530 | |||
| 531 | /** |
||
| 532 | * Processes items and dependencies for the footer group. |
||
| 533 | * |
||
| 534 | * @since 2.8.0 |
||
| 535 | * @access public |
||
| 536 | * |
||
| 537 | * @see WP_Dependencies::do_items() |
||
| 538 | * |
||
| 539 | * @return array Handles of items that have been processed. |
||
| 540 | */ |
||
| 541 | public function do_footer_items() { |
||
| 545 | |||
| 546 | /** |
||
| 547 | * Whether a handle's source is in a default directory. |
||
| 548 | * |
||
| 549 | * @since 2.8.0 |
||
| 550 | * @access public |
||
| 551 | * |
||
| 552 | * @param string $src The source of the enqueued script. |
||
| 553 | * @return bool True if found, false if not. |
||
| 554 | */ |
||
| 555 | public function in_default_dir( $src ) { |
||
| 571 | |||
| 572 | /** |
||
| 573 | * Resets class properties. |
||
| 574 | * |
||
| 575 | * @since 2.8.0 |
||
| 576 | * @access public |
||
| 577 | */ |
||
| 578 | public function reset() { |
||
| 587 | } |
||
| 588 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: