Complex classes like Assets 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 Assets, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class Assets { |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Load minified assets. |
||
| 26 | * |
||
| 27 | * @access private |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | private $min = '.min'; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Scripts. |
||
| 34 | * |
||
| 35 | * @access private |
||
| 36 | * @var array |
||
| 37 | */ |
||
| 38 | private $scripts = array(); |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Styles. |
||
| 42 | * |
||
| 43 | * @access private |
||
| 44 | * @var array |
||
| 45 | */ |
||
| 46 | private $styles = array(); |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Disable styles. |
||
| 50 | * |
||
| 51 | * @access public |
||
| 52 | * @var bool |
||
| 53 | */ |
||
| 54 | public $disable_styles = false; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Hook in tabs. |
||
| 58 | * |
||
| 59 | * @since 3.0.0 |
||
| 60 | */ |
||
| 61 | public function __construct() { |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Register scripts and styles. |
||
| 77 | * |
||
| 78 | * @since 3.0.0 |
||
| 79 | */ |
||
| 80 | public function register() { |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Enqueue scripts and styles. |
||
| 86 | * |
||
| 87 | * @since 3.0.0 |
||
| 88 | */ |
||
| 89 | public function enqueue() { |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Load scripts and styles. |
||
| 114 | * |
||
| 115 | * @since 3.0.0 |
||
| 116 | */ |
||
| 117 | public function load() { |
||
| 118 | |||
| 119 | $types = simcal_get_calendar_types(); |
||
| 120 | |||
| 121 | foreach ( $types as $calendar => $views ) { |
||
| 122 | foreach( $views as $key => $view ) { |
||
| 123 | |||
| 124 | $view = simcal_get_calendar_view( 0, $calendar . '-' . $view ); |
||
| 125 | |||
| 126 | $scripts[] = $view->scripts( $this->min ); |
||
|
|
|||
| 127 | $styles[] = $view->styles( $this->min ); |
||
| 128 | } |
||
| 129 | } |
||
| 130 | |||
| 131 | $this->get_widgets_assets(); |
||
| 132 | $this->scripts = apply_filters( 'simcal_front_end_scripts', $scripts, $this->min ); |
||
| 133 | // First check if there is a multi-dimensional array of scripts |
||
| 134 | if ( isset( $this->scripts[0] ) ) { |
||
| 135 | foreach ( $this->scripts as $script ) { |
||
| 136 | $this->load_scripts ( $script ); |
||
| 137 | } |
||
| 138 | } else { |
||
| 139 | $this->load_scripts( $this->scripts ); |
||
| 140 | } |
||
| 141 | $this->styles = apply_filters( 'simcal_front_end_styles', $styles, $this->min ); |
||
| 142 | // First check if there is a multi-dimensional array of styles |
||
| 143 | if ( isset( $this->styles[0] ) ) { |
||
| 144 | foreach( $this->styles as $style ) { |
||
| 145 | $this->load_styles( $style ); |
||
| 146 | } |
||
| 147 | } else { |
||
| 148 | $this->load_styles( $this->styles ); |
||
| 149 | } |
||
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Get widgets assets. |
||
| 154 | * |
||
| 155 | * @since 3.0.0 |
||
| 156 | */ |
||
| 157 | public function get_widgets_assets() { |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Scripts. |
||
| 190 | * |
||
| 191 | * @since 3.0.0 |
||
| 192 | * |
||
| 193 | * @param array $scripts |
||
| 194 | */ |
||
| 195 | public function load_scripts( $scripts ) { |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Styles. |
||
| 236 | * |
||
| 237 | * @since 3.0.0 |
||
| 238 | * |
||
| 239 | * @param array $styles |
||
| 240 | */ |
||
| 241 | public function load_styles( $styles ) { |
||
| 267 | } |
||
| 268 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.