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 Registry 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 Registry, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | class Registry |
||
| 27 | { |
||
| 28 | |||
| 29 | const FILE_NAME_BUILD_MANIFEST = 'build-manifest.json'; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var AssetCollection $assets |
||
| 33 | */ |
||
| 34 | protected $assets; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var I18nRegistry |
||
| 38 | */ |
||
| 39 | private $i18n_registry; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * This holds the jsdata data object that will be exposed on pages that enqueue the `eejs-core` script. |
||
| 43 | * |
||
| 44 | * @var array |
||
| 45 | */ |
||
| 46 | protected $jsdata = array(); |
||
| 47 | |||
| 48 | /** |
||
| 49 | * This keeps track of all scripts with registered data. It is used to prevent duplicate data objects setup in the |
||
| 50 | * page source. |
||
| 51 | * |
||
| 52 | * @var array |
||
| 53 | */ |
||
| 54 | private $script_handles_with_data = array(); |
||
| 55 | |||
| 56 | |||
| 57 | /** |
||
| 58 | * Holds the manifest data obtained from registered manifest files. |
||
| 59 | * Manifests are maps of asset chunk name to actual built asset file names. |
||
| 60 | * Shape of this array is: |
||
| 61 | * array( |
||
| 62 | * 'some_namespace_slug' => array( |
||
| 63 | * 'some_chunk_name' => array( |
||
| 64 | * 'js' => 'filename.js' |
||
| 65 | * 'css' => 'filename.js' |
||
| 66 | * ), |
||
| 67 | * 'url_base' => 'https://baseurl.com/to/assets |
||
| 68 | * ) |
||
| 69 | * ) |
||
| 70 | * |
||
| 71 | * @var array |
||
| 72 | */ |
||
| 73 | private $manifest_data = array(); |
||
| 74 | |||
| 75 | |||
| 76 | /** |
||
| 77 | * Holds any dependency data obtained from registered dependency map json. |
||
| 78 | * Dependency map json is generated via the @wordpress/dependency-extraction-webpack-plugin via the webpack config. |
||
| 79 | * @see https://github.com/WordPress/gutenberg/tree/master/packages/dependency-extraction-webpack-plugin |
||
| 80 | * |
||
| 81 | * @var array |
||
| 82 | */ |
||
| 83 | private $dependencies_data = []; |
||
| 84 | |||
| 85 | |||
| 86 | /** |
||
| 87 | * This is a known array of possible wp css handles that correspond to what may be exposed as dependencies in our |
||
| 88 | * build process. Currently the dependency export process in webpack does not consider css imports, so we derive |
||
| 89 | * them via the js dependencies (WP uses the same handle for both js and css). This is a list of known handles that |
||
| 90 | * are used for both js and css. |
||
| 91 | * @var array |
||
| 92 | */ |
||
| 93 | private $wp_css_handle_dependencies = [ |
||
| 94 | 'wp-components', |
||
| 95 | 'wp-block-editor', |
||
| 96 | 'wp-block-library', |
||
| 97 | 'wp-edit-post', |
||
| 98 | 'wp-edit-widgets', |
||
| 99 | 'wp-editor', |
||
| 100 | 'wp-format-library', |
||
| 101 | 'wp-list-reusable-blocks', |
||
| 102 | 'wp-nux', |
||
| 103 | ]; |
||
| 104 | |||
| 105 | |||
| 106 | /** |
||
| 107 | * Registry constructor. |
||
| 108 | * Hooking into WP actions for script registry. |
||
| 109 | * |
||
| 110 | * @param AssetCollection $assets |
||
| 111 | * @param I18nRegistry $i18n_registry |
||
| 112 | * @throws InvalidArgumentException |
||
| 113 | * @throws InvalidDataTypeException |
||
| 114 | * @throws InvalidInterfaceException |
||
| 115 | */ |
||
| 116 | public function __construct(AssetCollection $assets, I18nRegistry $i18n_registry) |
||
| 117 | { |
||
| 118 | $this->assets = $assets; |
||
| 119 | $this->i18n_registry = $i18n_registry; |
||
| 120 | add_action('wp_enqueue_scripts', array($this, 'registerManifestFiles'), 1); |
||
| 121 | add_action('admin_enqueue_scripts', array($this, 'registerManifestFiles'), 1); |
||
| 122 | add_action('wp_enqueue_scripts', array($this, 'registerScriptsAndStyles'), 3); |
||
| 123 | add_action('admin_enqueue_scripts', array($this, 'registerScriptsAndStyles'), 3); |
||
| 124 | add_action('wp_enqueue_scripts', array($this, 'enqueueData'), 4); |
||
| 125 | add_action('admin_enqueue_scripts', array($this, 'enqueueData'), 4); |
||
| 126 | add_action('wp_print_footer_scripts', array($this, 'enqueueData'), 1); |
||
| 127 | add_action('admin_print_footer_scripts', array($this, 'enqueueData'), 1); |
||
| 128 | } |
||
| 129 | |||
| 130 | |||
| 131 | /** |
||
| 132 | * For classes that have Registry as a dependency, this provides a handy way to register script handles for i18n |
||
| 133 | * translation handling. |
||
| 134 | * |
||
| 135 | * @return I18nRegistry |
||
| 136 | */ |
||
| 137 | public function getI18nRegistry() |
||
| 138 | { |
||
| 139 | return $this->i18n_registry; |
||
| 140 | } |
||
| 141 | |||
| 142 | |||
| 143 | /** |
||
| 144 | * Callback for the wp_enqueue_scripts actions used to register assets. |
||
| 145 | * |
||
| 146 | * @since 4.9.62.p |
||
| 147 | * @throws Exception |
||
| 148 | */ |
||
| 149 | public function registerScriptsAndStyles() |
||
| 150 | { |
||
| 151 | try { |
||
| 152 | $this->registerScripts($this->assets->getJavascriptAssets()); |
||
| 153 | $this->registerStyles($this->assets->getStylesheetAssets()); |
||
| 154 | } catch (Exception $exception) { |
||
| 155 | new ExceptionStackTraceDisplay($exception); |
||
| 156 | } |
||
| 157 | } |
||
| 158 | |||
| 159 | |||
| 160 | /** |
||
| 161 | * Registers JS assets with WP core |
||
| 162 | * |
||
| 163 | * @param JavascriptAsset[] $scripts |
||
| 164 | * @throws AssetRegistrationException |
||
| 165 | * @throws InvalidDataTypeException |
||
| 166 | * @throws DomainException |
||
| 167 | * @since 4.9.62.p |
||
| 168 | */ |
||
| 169 | public function registerScripts(array $scripts) |
||
| 200 | |||
| 201 | |||
| 202 | /** |
||
| 203 | * Registers CSS assets with WP core |
||
| 204 | * |
||
| 205 | * @param StylesheetAsset[] $styles |
||
| 206 | * @throws InvalidDataTypeException |
||
| 207 | * @throws DomainException |
||
| 208 | * @since 4.9.62.p |
||
| 209 | */ |
||
| 210 | public function registerStyles(array $styles) |
||
| 235 | |||
| 236 | |||
| 237 | /** |
||
| 238 | * Call back for the script print in frontend and backend. |
||
| 239 | * Used to call wp_localize_scripts so that data can be added throughout the runtime until this later hook point. |
||
| 240 | * |
||
| 241 | * @since 4.9.31.rc.015 |
||
| 242 | */ |
||
| 243 | public function enqueueData() |
||
| 260 | |||
| 261 | |||
| 262 | /** |
||
| 263 | * Used to add data to eejs.data object. |
||
| 264 | * Note: Overriding existing data is not allowed. |
||
| 265 | * Data will be accessible as a javascript object when you list `eejs-core` as a dependency for your javascript. |
||
| 266 | * If the data you add is something like this: |
||
| 267 | * $this->addData( 'my_plugin_data', array( 'foo' => 'gar' ) ); |
||
| 268 | * It will be exposed in the page source as: |
||
| 269 | * eejs.data.my_plugin_data.foo == gar |
||
| 270 | * |
||
| 271 | * @param string $key Key used to access your data |
||
| 272 | * @param string|array $value Value to attach to key |
||
| 273 | * @throws InvalidArgumentException |
||
| 274 | */ |
||
| 275 | public function addData($key, $value) |
||
| 281 | |||
| 282 | |||
| 283 | /** |
||
| 284 | * Similar to addData except this allows for users to push values to an existing key where the values on key are |
||
| 285 | * elements in an array. |
||
| 286 | * |
||
| 287 | * When you use this method, the value you include will be merged with the array on $key. |
||
| 288 | * So if the $key was 'test' and you added a value of ['my_data'] then it would be represented in the javascript |
||
| 289 | * object like this, eejs.data.test = [ my_data, |
||
| 290 | * ] |
||
| 291 | * If there has already been a scalar value attached to the data object given key (via addData for instance), then |
||
| 292 | * this will throw an exception. |
||
| 293 | * |
||
| 294 | * Caution: Only add data using this method if you are okay with the potential for additional data added on the same |
||
| 295 | * key potentially overriding the existing data on merge (specifically with associative arrays). |
||
| 296 | * |
||
| 297 | * @param string $key Key to attach data to. |
||
| 298 | * @param string|array $value Value being registered. |
||
| 299 | * @throws InvalidArgumentException |
||
| 300 | */ |
||
| 301 | public function pushData($key, $value) |
||
| 327 | |||
| 328 | |||
| 329 | /** |
||
| 330 | * Used to set content used by javascript for a template. |
||
| 331 | * Note: Overrides of existing registered templates are not allowed. |
||
| 332 | * |
||
| 333 | * @param string $template_reference |
||
| 334 | * @param string $template_content |
||
| 335 | * @throws InvalidArgumentException |
||
| 336 | */ |
||
| 337 | public function addTemplate($template_reference, $template_content) |
||
| 359 | |||
| 360 | |||
| 361 | /** |
||
| 362 | * Retrieve the template content already registered for the given reference. |
||
| 363 | * |
||
| 364 | * @param string $template_reference |
||
| 365 | * @return string |
||
| 366 | */ |
||
| 367 | public function getTemplate($template_reference) |
||
| 373 | |||
| 374 | |||
| 375 | /** |
||
| 376 | * Retrieve registered data. |
||
| 377 | * |
||
| 378 | * @param string $key Name of key to attach data to. |
||
| 379 | * @return mixed If there is no for the given key, then false is returned. |
||
| 380 | */ |
||
| 381 | public function getData($key) |
||
| 387 | |||
| 388 | |||
| 389 | /** |
||
| 390 | * Verifies whether the given data exists already on the jsdata array. |
||
| 391 | * Overriding data is not allowed. |
||
| 392 | * |
||
| 393 | * @param string $key Index for data. |
||
| 394 | * @return bool If valid then return true. |
||
| 395 | * @throws InvalidArgumentException if data already exists. |
||
| 396 | */ |
||
| 397 | protected function verifyDataNotExisting($key) |
||
| 430 | |||
| 431 | |||
| 432 | /** |
||
| 433 | * Get the actual asset path for asset manifests. |
||
| 434 | * If there is no asset path found for the given $chunk_name, then the $chunk_name is returned. |
||
| 435 | * |
||
| 436 | * @param string $namespace The namespace associated with the manifest file hosting the map of chunk_name to actual |
||
| 437 | * asset file location. |
||
| 438 | * @param string $chunk_name |
||
| 439 | * @param string $asset_type |
||
| 440 | * @return string |
||
| 441 | * @since 4.9.59.p |
||
| 442 | */ |
||
| 443 | public function getAssetUrl($namespace, $chunk_name, $asset_type) |
||
| 461 | |||
| 462 | |||
| 463 | |||
| 464 | /** |
||
| 465 | * Return the url to a js file for the given namespace and chunk name. |
||
| 466 | * |
||
| 467 | * @param string $namespace |
||
| 468 | * @param string $chunk_name |
||
| 469 | * @return string |
||
| 470 | */ |
||
| 471 | public function getJsUrl($namespace, $chunk_name) |
||
| 475 | |||
| 476 | |||
| 477 | /** |
||
| 478 | * Return the url to a css file for the given namespace and chunk name. |
||
| 479 | * |
||
| 480 | * @param string $namespace |
||
| 481 | * @param string $chunk_name |
||
| 482 | * @return string |
||
| 483 | */ |
||
| 484 | public function getCssUrl($namespace, $chunk_name) |
||
| 488 | |||
| 489 | |||
| 490 | /** |
||
| 491 | * Return the dependencies array and version string for a given asset $chunk_name |
||
| 492 | * |
||
| 493 | * @param string $namespace |
||
| 494 | * @param string $chunk_name |
||
| 495 | * @param string $asset_type |
||
| 496 | * @return array |
||
| 497 | * @since 4.9.82.p |
||
| 498 | */ |
||
| 499 | private function getDetailsForAsset($namespace, $chunk_name, $asset_type) |
||
| 518 | |||
| 519 | |||
| 520 | /** |
||
| 521 | * Return dependencies array and version string according to asset type. |
||
| 522 | * For css assets, this filters the auto generated dependencies by css type. |
||
| 523 | * |
||
| 524 | * @param string $namespace |
||
| 525 | * @param string $asset_type |
||
| 526 | * @param string $file_path |
||
| 527 | * @param string $chunk_name |
||
| 528 | * @return array |
||
| 529 | * @since 4.9.82.p |
||
| 530 | */ |
||
| 531 | private function getDetailsForAssetType($namespace, $asset_type, $file_path, $chunk_name) |
||
| 576 | |||
| 577 | |||
| 578 | /** |
||
| 579 | * Get the dependencies array and version string for the given js asset chunk name |
||
| 580 | * |
||
| 581 | * @param string $namespace |
||
| 582 | * @param string $chunk_name |
||
| 583 | * @return array |
||
| 584 | * @since $VID:$ |
||
| 585 | */ |
||
| 586 | public function getJsAssetDetails($namespace, $chunk_name) |
||
| 590 | |||
| 591 | |||
| 592 | /** |
||
| 593 | * Get the dependencies array and version string for the given css asset chunk name |
||
| 594 | * |
||
| 595 | * @param string $namespace |
||
| 596 | * @param string $chunk_name |
||
| 597 | * @return array |
||
| 598 | * @since $VID:$ |
||
| 599 | */ |
||
| 600 | public function getCssAssetDetails($namespace, $chunk_name) |
||
| 604 | |||
| 605 | |||
| 606 | /** |
||
| 607 | * @since 4.9.62.p |
||
| 608 | * @throws InvalidArgumentException |
||
| 609 | * @throws InvalidFilePathException |
||
| 610 | */ |
||
| 611 | public function registerManifestFiles() |
||
| 623 | |||
| 624 | |||
| 625 | /** |
||
| 626 | * Used to register a js/css manifest file with the registered_manifest_files property. |
||
| 627 | * |
||
| 628 | * @param string $namespace Provided to associate the manifest file with a specific namespace. |
||
| 629 | * @param string $url_base The url base for the manifest file location. |
||
| 630 | * @param string $manifest_file The absolute path to the manifest file. |
||
| 631 | * @param string $manifest_file_path The path to the folder containing the manifest file. If not provided will be |
||
| 632 | * default to `plugin_root/assets/dist`. |
||
| 633 | * @throws InvalidArgumentException |
||
| 634 | * @throws InvalidFilePathException |
||
| 635 | * @since 4.9.59.p |
||
| 636 | */ |
||
| 637 | public function registerManifestFile($namespace, $url_base, $manifest_file, $manifest_file_path = '') |
||
| 681 | |||
| 682 | |||
| 683 | /** |
||
| 684 | * Decodes json from the provided manifest file. |
||
| 685 | * |
||
| 686 | * @since 4.9.59.p |
||
| 687 | * @param string $manifest_file Path to manifest file. |
||
| 688 | * @return array |
||
| 689 | * @throws InvalidFilePathException |
||
| 690 | */ |
||
| 691 | private function decodeManifestFile($manifest_file) |
||
| 698 | |||
| 699 | |||
| 700 | /** |
||
| 701 | * This is used to set registered script handles that have data. |
||
| 702 | * |
||
| 703 | * @param string $script_handle |
||
| 704 | */ |
||
| 705 | private function addRegisteredScriptHandlesWithData($script_handle) |
||
| 709 | |||
| 710 | |||
| 711 | /**i |
||
| 712 | * Checks WP_Scripts for all of each script handle registered internally as having data and unsets from the |
||
| 713 | * Dependency stored in WP_Scripts if its set. |
||
| 714 | */ |
||
| 715 | private function removeAlreadyRegisteredDataForScriptHandles() |
||
| 724 | |||
| 725 | |||
| 726 | /** |
||
| 727 | * Removes any data dependency registered in WP_Scripts if its set. |
||
| 728 | * |
||
| 729 | * @param string $script_handle |
||
| 730 | */ |
||
| 731 | private function removeAlreadyRegisteredDataForScriptHandle($script_handle) |
||
| 753 | |||
| 754 | |||
| 755 | /** |
||
| 756 | * register translations for a registered script |
||
| 757 | * |
||
| 758 | * @param string $handle |
||
| 759 | */ |
||
| 760 | public function registerTranslation($handle) |
||
| 764 | |||
| 765 | |||
| 766 | /** |
||
| 767 | * @since 4.9.63.p |
||
| 768 | * @return bool |
||
| 769 | */ |
||
| 770 | private function debug() |
||
| 777 | |||
| 778 | |||
| 779 | /** |
||
| 780 | * Get the dependencies array for the given js asset chunk name |
||
| 781 | * |
||
| 782 | * @param string $namespace |
||
| 783 | * @param string $chunk_name |
||
| 784 | * @return array |
||
| 785 | * @deprecated $VID:$ |
||
| 786 | * @since 4.9.82.p |
||
| 787 | */ |
||
| 788 | public function getJsDependencies($namespace, $chunk_name) |
||
| 793 | |||
| 794 | |||
| 795 | /** |
||
| 796 | * Get the dependencies array for the given css asset chunk name |
||
| 797 | * |
||
| 798 | * @param string $namespace |
||
| 799 | * @param string $chunk_name |
||
| 800 | * @return array |
||
| 801 | * @deprecated $VID:$ |
||
| 802 | * @since 4.9.82.p |
||
| 803 | */ |
||
| 804 | public function getCssDependencies($namespace, $chunk_name) |
||
| 809 | } |
||
| 810 |