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 | * HTML to print before the script handle. |
||
97 | * |
||
98 | * @since 4.5.0 |
||
99 | * @access public |
||
100 | * @var string |
||
101 | */ |
||
102 | public $print_html_before = ''; |
||
103 | |||
104 | /** |
||
105 | * Holds inline code if concatenation is enabled. |
||
106 | * |
||
107 | * @since 2.8.0 |
||
108 | * @access public |
||
109 | * @var string |
||
110 | */ |
||
111 | public $print_code = ''; |
||
112 | |||
113 | /** |
||
114 | * Holds a list of script handles which are not in the default directory |
||
115 | * if concatenation is enabled. |
||
116 | * |
||
117 | * Unused in core. |
||
118 | * |
||
119 | * @since 2.8.0 |
||
120 | * @access public |
||
121 | * @var string |
||
122 | */ |
||
123 | public $ext_handles = ''; |
||
124 | |||
125 | /** |
||
126 | * Holds a string which contains handles and versions of scripts which |
||
127 | * are not in the default directory if concatenation is enabled. |
||
128 | * |
||
129 | * Unused in core. |
||
130 | * |
||
131 | * @since 2.8.0 |
||
132 | * @access public |
||
133 | * @var string |
||
134 | */ |
||
135 | public $ext_version = ''; |
||
136 | |||
137 | /** |
||
138 | * List of default directories. |
||
139 | * |
||
140 | * @since 2.8.0 |
||
141 | * @access public |
||
142 | * @var array |
||
143 | */ |
||
144 | public $default_dirs; |
||
145 | |||
146 | /** |
||
147 | * Constructor. |
||
148 | * |
||
149 | * @since 2.6.0 |
||
150 | * @access public |
||
151 | */ |
||
152 | public function __construct() { |
||
156 | |||
157 | /** |
||
158 | * Initialize the class. |
||
159 | * |
||
160 | * @since 3.4.0 |
||
161 | * @access public |
||
162 | */ |
||
163 | public function init() { |
||
173 | |||
174 | /** |
||
175 | * Prints scripts. |
||
176 | * |
||
177 | * Prints the scripts passed to it or the print queue. Also prints all necessary dependencies. |
||
178 | * |
||
179 | * @since 2.1.0 |
||
180 | * @since 2.8.0 Added the `$group` parameter. |
||
181 | * @access public |
||
182 | * |
||
183 | * @param mixed $handles Optional. Scripts to be printed. (void) prints queue, (string) prints |
||
184 | * that script, (array of strings) prints those scripts. Default false. |
||
185 | * @param int $group Optional. If scripts were queued in groups prints this group number. |
||
186 | * Default false. |
||
187 | * @return array Scripts that have been printed. |
||
188 | */ |
||
189 | public function print_scripts( $handles = false, $group = false ) { |
||
192 | |||
193 | /** |
||
194 | * Prints extra scripts of a registered script. |
||
195 | * |
||
196 | * @since 2.1.0 |
||
197 | * @since 2.8.0 Added the `$echo` parameter. |
||
198 | * @deprecated 3.3.0 |
||
199 | * @access public |
||
200 | * |
||
201 | * @see print_extra_script() |
||
202 | * |
||
203 | * @param string $handle The script's registered handle. |
||
204 | * @param bool $echo Optional. Whether to echo the extra script instead of just returning it. |
||
205 | * Default true. |
||
206 | * @return bool|string|void Void if no data exists, extra scripts if `$echo` is true, true otherwise. |
||
207 | */ |
||
208 | public function print_scripts_l10n( $handle, $echo = true ) { |
||
212 | |||
213 | /** |
||
214 | * Prints extra scripts of a registered script. |
||
215 | * |
||
216 | * @since 3.3.0 |
||
217 | * @access public |
||
218 | * |
||
219 | * @param string $handle The script's registered handle. |
||
220 | * @param bool $echo Optional. Whether to echo the extra script instead of just returning it. |
||
221 | * Default true. |
||
222 | * @return bool|string|void Void if no data exists, extra scripts if `$echo` is true, true otherwise. |
||
223 | */ |
||
224 | public function print_extra_script( $handle, $echo = true ) { |
||
239 | |||
240 | /** |
||
241 | * Processes a script dependency. |
||
242 | * |
||
243 | * @since 2.6.0 |
||
244 | * @since 2.8.0 Added the `$group` parameter. |
||
245 | * @access public |
||
246 | * |
||
247 | * @see WP_Dependencies::do_item() |
||
248 | * |
||
249 | * @param string $handle The script's registered handle. |
||
250 | * @param int|false $group Optional. Group level: (int) level, (false) no groups. Default false. |
||
251 | * @return bool True on success, false on failure. |
||
252 | */ |
||
253 | public function do_item( $handle, $group = false ) { |
||
377 | |||
378 | /** |
||
379 | * Adds extra code to a registered script. |
||
380 | * |
||
381 | * @since 4.5.0 |
||
382 | * @access public |
||
383 | * |
||
384 | * @param string $handle Name of the script to add the inline script to. Must be lowercase. |
||
385 | * @param string $data String containing the javascript to be added. |
||
386 | * @param string $position Optional. Whether to add the inline script before the handle |
||
387 | * or after. Default 'after'. |
||
388 | * @return bool True on success, false on failure. |
||
389 | */ |
||
390 | View Code Duplication | public function add_inline_script( $handle, $data, $position = 'after' ) { |
|
391 | if ( ! $data ) { |
||
392 | return false; |
||
393 | } |
||
394 | |||
395 | if ( 'after' !== $position ) { |
||
396 | $position = 'before'; |
||
397 | } |
||
398 | |||
399 | $script = (array) $this->get_data( $handle, $position ); |
||
400 | $script[] = $data; |
||
401 | |||
402 | return $this->add_data( $handle, $position, $script ); |
||
403 | } |
||
404 | |||
405 | /** |
||
406 | * Prints inline scripts registered for a specific handle. |
||
407 | * |
||
408 | * @since 4.5.0 |
||
409 | * @access public |
||
410 | * |
||
411 | * @param string $handle Name of the script to add the inline script to. Must be lowercase. |
||
412 | * @param string $position Optional. Whether to add the inline script before the handle |
||
413 | * or after. Default 'after'. |
||
414 | * @param bool $echo Optional. Whether to echo the script instead of just returning it. |
||
415 | * Default true. |
||
416 | * @return string|false Script on success, false otherwise. |
||
417 | */ |
||
418 | View Code Duplication | public function print_inline_script( $handle, $position = 'after', $echo = true ) { |
|
419 | $output = $this->get_data( $handle, $position ); |
||
420 | |||
421 | if ( empty( $output ) ) { |
||
422 | return false; |
||
423 | } |
||
424 | |||
425 | $output = trim( implode( "\n", $output ), "\n" ); |
||
426 | |||
427 | if ( $echo ) { |
||
428 | printf( "<script type='text/javascript'>\n%s\n</script>\n", $output ); |
||
429 | } |
||
430 | |||
431 | return $output; |
||
432 | } |
||
433 | |||
434 | /** |
||
435 | * Localizes a script, only if the script has already been added. |
||
436 | * |
||
437 | * @since 2.1.0 |
||
438 | * @access public |
||
439 | * |
||
440 | * @param string $handle |
||
441 | * @param string $object_name |
||
442 | * @param array $l10n |
||
443 | * @return bool |
||
444 | */ |
||
445 | public function localize( $handle, $object_name, $l10n ) { |
||
473 | |||
474 | /** |
||
475 | * Sets handle group. |
||
476 | * |
||
477 | * @since 2.8.0 |
||
478 | * @access public |
||
479 | * |
||
480 | * @see WP_Dependencies::set_group() |
||
481 | * |
||
482 | * @param string $handle Name of the item. Should be unique. |
||
483 | * @param bool $recursion Internal flag that calling function was called recursively. |
||
484 | * @param int|false $group Optional. Group level: (int) level, (false) no groups. Default false. |
||
485 | * @return bool Not already in the group or a lower group |
||
486 | */ |
||
487 | public function set_group( $handle, $recursion, $group = false ) { |
||
498 | |||
499 | /** |
||
500 | * Determines script dependencies. |
||
501 | * |
||
502 | * @since 2.1.0 |
||
503 | * @access public |
||
504 | * |
||
505 | * @see WP_Dependencies::all_deps() |
||
506 | * |
||
507 | * @param mixed $handles Item handle and argument (string) or item handles and arguments (array of strings). |
||
508 | * @param bool $recursion Internal flag that function is calling itself. |
||
509 | * @param int|false $group Optional. Group level: (int) level, (false) no groups. Default false. |
||
510 | * @return bool True on success, false on failure. |
||
511 | */ |
||
512 | View Code Duplication | public function all_deps( $handles, $recursion = false, $group = false ) { |
|
526 | |||
527 | /** |
||
528 | * Processes items and dependencies for the head group. |
||
529 | * |
||
530 | * @since 2.8.0 |
||
531 | * @access public |
||
532 | * |
||
533 | * @see WP_Dependencies::do_items() |
||
534 | * |
||
535 | * @return array Handles of items that have been processed. |
||
536 | */ |
||
537 | public function do_head_items() { |
||
541 | |||
542 | /** |
||
543 | * Processes items and dependencies for the footer group. |
||
544 | * |
||
545 | * @since 2.8.0 |
||
546 | * @access public |
||
547 | * |
||
548 | * @see WP_Dependencies::do_items() |
||
549 | * |
||
550 | * @return array Handles of items that have been processed. |
||
551 | */ |
||
552 | public function do_footer_items() { |
||
556 | |||
557 | /** |
||
558 | * Whether a handle's source is in a default directory. |
||
559 | * |
||
560 | * @since 2.8.0 |
||
561 | * @access public |
||
562 | * |
||
563 | * @param string $src The source of the enqueued script. |
||
564 | * @return bool True if found, false if not. |
||
565 | */ |
||
566 | public function in_default_dir( $src ) { |
||
582 | |||
583 | /** |
||
584 | * Resets class properties. |
||
585 | * |
||
586 | * @since 2.8.0 |
||
587 | * @access public |
||
588 | */ |
||
589 | public function reset() { |
||
599 | } |
||
600 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: