Complex classes like DependencyManager 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 DependencyManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 29 | class DependencyManager implements DependencyManagerInterface { |
||
| 30 | |||
| 31 | use ConfigTrait; |
||
| 32 | |||
| 33 | /* |
||
| 34 | * Default dependency handler implementations. |
||
| 35 | */ |
||
| 36 | const DEFAULT_SCRIPT_HANDLER = '\BrightNucleus\Dependency\ScriptHandler'; |
||
| 37 | const DEFAULT_STYLE_HANDLER = '\BrightNucleus\Dependency\StyleHandler'; |
||
| 38 | |||
| 39 | /* |
||
| 40 | * Names of the configuration keys. |
||
| 41 | */ |
||
| 42 | const KEY_HANDLERS = 'handlers'; |
||
| 43 | const KEY_SCRIPTS = 'scripts'; |
||
| 44 | const KEY_STYLES = 'styles'; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Hold the dependencies, grouped by type. |
||
| 48 | * |
||
| 49 | * @since 0.1.0 |
||
| 50 | * |
||
| 51 | * @var array; |
||
| 52 | */ |
||
| 53 | protected $dependencies = [ ]; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Hold the handlers. |
||
| 57 | * |
||
| 58 | * @since 0.1.0 |
||
| 59 | * |
||
| 60 | * @var DependencyHandlerInterface[] |
||
| 61 | */ |
||
| 62 | protected $handlers = [ ]; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Whether to enqueue immediately upon registration. |
||
| 66 | * |
||
| 67 | * @since 0.2.2 |
||
| 68 | * |
||
| 69 | * @var bool |
||
| 70 | */ |
||
| 71 | protected $enqueue_immediately; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Instantiate DependencyManager object. |
||
| 75 | * |
||
| 76 | * @since 0.1.0 |
||
| 77 | * |
||
| 78 | * @param ConfigInterface $config ConfigInterface object that contains |
||
| 79 | * dependency settings. |
||
| 80 | * @param bool $enqueue Optional. Whether to enqueue |
||
| 81 | * immediately. Defaults to true. |
||
| 82 | * @throws RuntimeException If the config could not be processed. |
||
| 83 | * @throws InvalidArgumentException If no dependency handlers were |
||
| 84 | * specified. |
||
| 85 | */ |
||
| 86 | public function __construct( ConfigInterface $config, $enqueue = true ) { |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Initialize the dependency handlers. |
||
| 95 | * |
||
| 96 | * @since 0.1.0 |
||
| 97 | */ |
||
| 98 | protected function init_handlers() { |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Add a single dependency handler. |
||
| 109 | * |
||
| 110 | * @since 0.1.0 |
||
| 111 | * |
||
| 112 | * @param string $dependency The dependency type for which to add a handler. |
||
| 113 | */ |
||
| 114 | protected function add_handler( $dependency ) { |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Get the default handler class for a given type of dependency. |
||
| 127 | * |
||
| 128 | * @since 0.1.0 |
||
| 129 | * |
||
| 130 | * @param string $dependency The dependency that needs a handler. |
||
| 131 | * @return string|null Class name of the handler. Null if none. |
||
| 132 | */ |
||
| 133 | protected function get_default_handler( $dependency ) { |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Initialize the actual dependencies. |
||
| 146 | * |
||
| 147 | * @since 0.1.0 |
||
| 148 | */ |
||
| 149 | protected function init_dependencies() { |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Initialize the dependencies of a given type. |
||
| 160 | * |
||
| 161 | * @since 0.2.2 |
||
| 162 | * |
||
| 163 | * @param string $type The type of dependency to initialize. |
||
| 164 | * @return array Array of dependency configurations. |
||
| 165 | */ |
||
| 166 | protected function init_dependency_type( $type ) { |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Register all dependencies. |
||
| 179 | * |
||
| 180 | * @since 0.1.0 |
||
| 181 | * |
||
| 182 | * @param mixed $context Optional. The context to pass to the dependencies. |
||
| 183 | */ |
||
| 184 | public function register( $context = null ) { |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Validate the context to make sure it is an array. |
||
| 192 | * |
||
| 193 | * @since 0.2.1 |
||
| 194 | * |
||
| 195 | * @param mixed $context The context as passed in by WordPress. |
||
| 196 | * @return array Validated context. |
||
| 197 | */ |
||
| 198 | protected function validate_context( $context ) { |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Enqueue all dependencies. |
||
| 207 | * |
||
| 208 | * @since 0.1.0 |
||
| 209 | * |
||
| 210 | * @param mixed $context Optional. The context to pass to the |
||
| 211 | * dependencies. |
||
| 212 | */ |
||
| 213 | public function enqueue( $context = null ) { |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Enqueue a single dependency retrieved by its handle. |
||
| 222 | * |
||
| 223 | * @since 0.2.2 |
||
| 224 | * |
||
| 225 | * @param string $handle The dependency handle to enqueue. |
||
| 226 | * @param mixed $context Optional. The context to pass to the |
||
| 227 | * dependencies. |
||
| 228 | * @param bool $fallback Whether to fall back to dependencies registered |
||
| 229 | * outside of DependencyManager. Defaults to false. |
||
| 230 | * @return bool Returns whether the handle was found or not. |
||
| 231 | */ |
||
| 232 | public function enqueue_handle( $handle, $context = null, $fallback = false ) { |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Enqueue a single dependency from the internal dependencies, retrieved by |
||
| 246 | * its handle. |
||
| 247 | * |
||
| 248 | * @since 0.2.4 |
||
| 249 | * |
||
| 250 | * @param string $handle The dependency handle to enqueue. |
||
| 251 | * @param mixed $context Optional. The context to pass to the |
||
| 252 | * dependencies. |
||
| 253 | * @return bool Returns whether the handle was found or not. |
||
| 254 | */ |
||
| 255 | protected function enqueue_internal_handle( $handle, $context = null ) { |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Get the matching dependency for a given handle. |
||
| 277 | * |
||
| 278 | * @since 0.2.2 |
||
| 279 | * |
||
| 280 | * @param string $handle The dependency handle to search for. |
||
| 281 | * @return array Array containing the dependency key as well as the |
||
| 282 | * dependency array itself. |
||
| 283 | */ |
||
| 284 | protected function get_dependency_array( $handle ) { |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Enqueue a single dependency. |
||
| 296 | * |
||
| 297 | * @since 0.1.0 |
||
| 298 | * |
||
| 299 | * @param array $dependency Configuration data of the dependency. |
||
| 300 | * @param string $dependency_key Config key of the dependency. |
||
| 301 | * @param mixed $context Optional. Context to pass to the |
||
| 302 | * dependencies. Contains the type of the |
||
| 303 | * dependency at key |
||
| 304 | * 'dependency_type'. |
||
| 305 | */ |
||
| 306 | protected function enqueue_dependency( $dependency, $dependency_key, $context = null ) { |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Check whether a specific dependency is needed. |
||
| 323 | * |
||
| 324 | * @since 0.1.0 |
||
| 325 | * |
||
| 326 | * @param array $dependency Configuration of the dependency to check. |
||
| 327 | * @param mixed $context Context to pass to the dependencies. |
||
| 328 | * Contains the type of the dependency at key |
||
| 329 | * 'dependency_type'. |
||
| 330 | * @return bool Whether it is needed or not. |
||
| 331 | */ |
||
| 332 | protected function is_needed( $dependency, $context ) { |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Localize the script of a given dependency. |
||
| 346 | * |
||
| 347 | * @since 0.1.0 |
||
| 348 | * |
||
| 349 | * @param array $dependency The dependency to localize the script of. |
||
| 350 | * @param mixed $context Contextual data to pass to the callback. |
||
| 351 | * Contains the type of the dependency at key |
||
| 352 | * 'dependency_type'. |
||
| 353 | */ |
||
| 354 | protected function maybe_localize( $dependency, $context ) { |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Add an inline script snippet to a given dependency. |
||
| 373 | * |
||
| 374 | * @since 0.1.0 |
||
| 375 | * |
||
| 376 | * @param array $dependency The dependency to add the inline script to. |
||
| 377 | * @param mixed $context Contextual data to pass to the callback. |
||
| 378 | * Contains the type of the dependency at key |
||
| 379 | * 'dependency_type'. |
||
| 380 | */ |
||
| 381 | protected function maybe_add_inline_script( $dependency, $context ) { |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Enqueue a single dependency from the WP-registered dependencies, |
||
| 397 | * retrieved by its handle. |
||
| 398 | * |
||
| 399 | * @since 0.2.4 |
||
| 400 | * |
||
| 401 | * @param string $handle The dependency handle to enqueue. |
||
| 402 | * @return bool Returns whether the handle was found or not. |
||
| 403 | */ |
||
| 404 | protected function enqueue_fallback_handle( $handle ) { |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Enqueue all dependencies of a specific type. |
||
| 414 | * |
||
| 415 | * @since 0.1.0 |
||
| 416 | * |
||
| 417 | * @param array $dependencies The dependencies to enqueue. |
||
| 418 | * @param string $dependency_type The type of the dependencies. |
||
| 419 | * @param mixed $context Optional. The context to pass to the |
||
| 420 | * dependencies. |
||
| 421 | */ |
||
| 422 | protected function enqueue_dependency_type( $dependencies, $dependency_type, $context = null ) { |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Register all dependencies of a specific type. |
||
| 429 | * |
||
| 430 | * @since 0.1.0 |
||
| 431 | * |
||
| 432 | * @param array $dependencies The dependencies to register. |
||
| 433 | * @param string $dependency_type The type of the dependencies. |
||
| 434 | * @param mixed $context Optional. The context to pass to the |
||
| 435 | * dependencies. |
||
| 436 | */ |
||
| 437 | protected function register_dependency_type( $dependencies, $dependency_type, $context = null ) { |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Register a single dependency. |
||
| 444 | * |
||
| 445 | * @since 0.1.0 |
||
| 446 | * |
||
| 447 | * @param array $dependency Configuration data of the dependency. |
||
| 448 | * @param string $dependency_key Config key of the dependency. |
||
| 449 | * @param mixed $context Optional. Context to pass to the |
||
| 450 | * dependencies. Contains the type of the |
||
| 451 | * dependency at key |
||
| 452 | * 'dependency_type'. |
||
| 453 | */ |
||
| 454 | protected function register_dependency( $dependency, $dependency_key, $context = null ) { |
||
| 462 | |||
| 463 | /** |
||
| 464 | * Register the enqueueing to WordPress hooks. |
||
| 465 | * |
||
| 466 | * @since 0.2.2 |
||
| 467 | * |
||
| 468 | * @param array $dependency Configuration data of the dependency. |
||
| 469 | * @param mixed $context Optional. Context to pass to the dependencies. |
||
| 470 | * Contains the type of the dependency at key |
||
| 471 | * 'dependency_type'. |
||
| 472 | */ |
||
| 473 | protected function register_enqueue_hooks( $dependency, $context = null ) { |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Get the priority of a dependency. |
||
| 486 | * |
||
| 487 | * @since 0.2.2 |
||
| 488 | * |
||
| 489 | * @param array $dependency Configuration data of the dependency. |
||
| 490 | * @return int Priority to use. |
||
| 491 | */ |
||
| 492 | protected function get_priority( $dependency ) { |
||
| 498 | } |
||
| 499 |