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 ASSET_TYPE_CSS = 'css'; |
||
| 30 | const ASSET_TYPE_JS = 'js'; |
||
| 31 | const ASSET_NAMESPACE = 'core'; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var EE_Template_Config $template_config |
||
| 35 | */ |
||
| 36 | protected $template_config; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var EE_Currency_Config $currency_config |
||
| 40 | */ |
||
| 41 | protected $currency_config; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * This holds the jsdata data object that will be exposed on pages that enqueue the `eejs-core` script. |
||
| 45 | * |
||
| 46 | * @var array |
||
| 47 | */ |
||
| 48 | protected $jsdata = array(); |
||
| 49 | |||
| 50 | |||
| 51 | /** |
||
| 52 | * This keeps track of all scripts with registered data. It is used to prevent duplicate data objects setup in the |
||
| 53 | * page source. |
||
| 54 | * @var array |
||
| 55 | */ |
||
| 56 | protected $script_handles_with_data = array(); |
||
| 57 | |||
| 58 | |||
| 59 | /** |
||
| 60 | * @var DomainInterface |
||
| 61 | */ |
||
| 62 | protected $domain; |
||
| 63 | |||
| 64 | |||
| 65 | /** |
||
| 66 | * @var I18nRegistry |
||
| 67 | */ |
||
| 68 | private $i18n_registry; |
||
| 69 | |||
| 70 | |||
| 71 | |||
| 72 | /** |
||
| 73 | * Holds the manifest data obtained from registered manifest files. |
||
| 74 | * Manifests are maps of asset chunk name to actual built asset file names. |
||
| 75 | * Shape of this array is: |
||
| 76 | * |
||
| 77 | * array( |
||
| 78 | * 'some_namespace_slug' => array( |
||
| 79 | * 'some_chunk_name' => array( |
||
| 80 | * 'js' => 'filename.js' |
||
| 81 | * 'css' => 'filename.js' |
||
| 82 | * ), |
||
| 83 | * 'url_base' => 'https://baseurl.com/to/assets |
||
| 84 | * ) |
||
| 85 | * ) |
||
| 86 | * |
||
| 87 | * @var array |
||
| 88 | */ |
||
| 89 | private $manifest_data = array(); |
||
| 90 | |||
| 91 | |||
| 92 | /** |
||
| 93 | * Registry constructor. |
||
| 94 | * Hooking into WP actions for script registry. |
||
| 95 | * |
||
| 96 | * @param EE_Template_Config $template_config |
||
| 97 | * @param EE_Currency_Config $currency_config |
||
| 98 | * @param I18nRegistry $i18n_registry |
||
| 99 | * @param DomainInterface $domain |
||
| 100 | * @throws InvalidArgumentException |
||
| 101 | * @throws InvalidFilePathException |
||
| 102 | */ |
||
| 103 | public function __construct( |
||
| 125 | |||
| 126 | |||
| 127 | /** |
||
| 128 | * For classes that have Registry as a dependency, this provides a handy way to register script handles for i18n |
||
| 129 | * translation handling. |
||
| 130 | * |
||
| 131 | * @return I18nRegistry |
||
| 132 | */ |
||
| 133 | public function getI18nRegistry() |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Callback for the WP script actions. |
||
| 140 | * Used to register globally accessible core scripts. |
||
| 141 | * Also used to add the eejs.data object to the source for any js having eejs-core as a dependency. |
||
| 142 | * |
||
| 143 | */ |
||
| 144 | public function scripts() |
||
| 191 | |||
| 192 | |||
| 193 | |||
| 194 | /** |
||
| 195 | * Call back for the script print in frontend and backend. |
||
| 196 | * Used to call wp_localize_scripts so that data can be added throughout the runtime until this later hook point. |
||
| 197 | * |
||
| 198 | * @since 4.9.31.rc.015 |
||
| 199 | */ |
||
| 200 | public function enqueueData() |
||
| 213 | |||
| 214 | |||
| 215 | |||
| 216 | /** |
||
| 217 | * Used to add data to eejs.data object. |
||
| 218 | * Note: Overriding existing data is not allowed. |
||
| 219 | * Data will be accessible as a javascript object when you list `eejs-core` as a dependency for your javascript. |
||
| 220 | * If the data you add is something like this: |
||
| 221 | * $this->addData( 'my_plugin_data', array( 'foo' => 'gar' ) ); |
||
| 222 | * It will be exposed in the page source as: |
||
| 223 | * eejs.data.my_plugin_data.foo == gar |
||
| 224 | * |
||
| 225 | * @param string $key Key used to access your data |
||
| 226 | * @param string|array $value Value to attach to key |
||
| 227 | * @throws InvalidArgumentException |
||
| 228 | */ |
||
| 229 | public function addData($key, $value) |
||
| 235 | |||
| 236 | |||
| 237 | |||
| 238 | /** |
||
| 239 | * Similar to addData except this allows for users to push values to an existing key where the values on key are |
||
| 240 | * elements in an array. |
||
| 241 | * When you use this method, the value you include will be appended to the end of an array on $key. |
||
| 242 | * So if the $key was 'test' and you added a value of 'my_data' then it would be represented in the javascript |
||
| 243 | * object like this, eejs.data.test = [ my_data, |
||
| 244 | * ] |
||
| 245 | * If there has already been a scalar value attached to the data object given key, then |
||
| 246 | * this will throw an exception. |
||
| 247 | * |
||
| 248 | * @param string $key Key to attach data to. |
||
| 249 | * @param string|array $value Value being registered. |
||
| 250 | * @throws InvalidArgumentException |
||
| 251 | */ |
||
| 252 | public function pushData($key, $value) |
||
| 271 | |||
| 272 | |||
| 273 | |||
| 274 | /** |
||
| 275 | * Used to set content used by javascript for a template. |
||
| 276 | * Note: Overrides of existing registered templates are not allowed. |
||
| 277 | * |
||
| 278 | * @param string $template_reference |
||
| 279 | * @param string $template_content |
||
| 280 | * @throws InvalidArgumentException |
||
| 281 | */ |
||
| 282 | public function addTemplate($template_reference, $template_content) |
||
| 301 | |||
| 302 | |||
| 303 | |||
| 304 | /** |
||
| 305 | * Retrieve the template content already registered for the given reference. |
||
| 306 | * |
||
| 307 | * @param string $template_reference |
||
| 308 | * @return string |
||
| 309 | */ |
||
| 310 | public function getTemplate($template_reference) |
||
| 316 | |||
| 317 | |||
| 318 | |||
| 319 | /** |
||
| 320 | * Retrieve registered data. |
||
| 321 | * |
||
| 322 | * @param string $key Name of key to attach data to. |
||
| 323 | * @return mixed If there is no for the given key, then false is returned. |
||
| 324 | */ |
||
| 325 | public function getData($key) |
||
| 331 | |||
| 332 | |||
| 333 | /** |
||
| 334 | * Get the actual asset path for asset manifests. |
||
| 335 | * If there is no asset path found for the given $chunk_name, then the $chunk_name is returned. |
||
| 336 | * @param string $namespace The namespace associated with the manifest file hosting the map of chunk_name to actual |
||
| 337 | * asset file location. |
||
| 338 | * @param string $chunk_name |
||
| 339 | * @param string $asset_type |
||
| 340 | * @return string |
||
| 341 | * @since 4.9.59.p |
||
| 342 | */ |
||
| 343 | public function getAssetUrl($namespace, $chunk_name, $asset_type) |
||
| 360 | |||
| 361 | |||
| 362 | /** |
||
| 363 | * Return the url to a js file for the given namespace and chunk name. |
||
| 364 | * |
||
| 365 | * @param string $namespace |
||
| 366 | * @param string $chunk_name |
||
| 367 | * @return string |
||
| 368 | */ |
||
| 369 | public function getJsUrl($namespace, $chunk_name) |
||
| 373 | |||
| 374 | |||
| 375 | /** |
||
| 376 | * Return the url to a css file for the given namespace and chunk name. |
||
| 377 | * |
||
| 378 | * @param string $namespace |
||
| 379 | * @param string $chunk_name |
||
| 380 | * @return string |
||
| 381 | */ |
||
| 382 | public function getCssUrl($namespace, $chunk_name) |
||
| 386 | |||
| 387 | |||
| 388 | /** |
||
| 389 | * Used to register a js/css manifest file with the registered_manifest_files property. |
||
| 390 | * |
||
| 391 | * @param string $namespace Provided to associate the manifest file with a specific namespace. |
||
| 392 | * @param string $url_base The url base for the manifest file location. |
||
| 393 | * @param string $manifest_file The absolute path to the manifest file. |
||
| 394 | * @throws InvalidArgumentException |
||
| 395 | * @throws InvalidFilePathException |
||
| 396 | * @since 4.9.59.p |
||
| 397 | */ |
||
| 398 | public function registerManifestFile($namespace, $url_base, $manifest_file) |
||
| 436 | |||
| 437 | |||
| 438 | |||
| 439 | /** |
||
| 440 | * Decodes json from the provided manifest file. |
||
| 441 | * |
||
| 442 | * @since 4.9.59.p |
||
| 443 | * @param string $manifest_file Path to manifest file. |
||
| 444 | * @return array |
||
| 445 | * @throws InvalidFilePathException |
||
| 446 | */ |
||
| 447 | private function decodeManifestFile($manifest_file) |
||
| 454 | |||
| 455 | |||
| 456 | |||
| 457 | /** |
||
| 458 | * Verifies whether the given data exists already on the jsdata array. |
||
| 459 | * Overriding data is not allowed. |
||
| 460 | * |
||
| 461 | * @param string $key Index for data. |
||
| 462 | * @return bool If valid then return true. |
||
| 463 | * @throws InvalidArgumentException if data already exists. |
||
| 464 | */ |
||
| 465 | protected function verifyDataNotExisting($key) |
||
| 495 | |||
| 496 | |||
| 497 | |||
| 498 | /** |
||
| 499 | * registers core default stylesheets |
||
| 500 | */ |
||
| 501 | private function loadCoreCss() |
||
| 524 | |||
| 525 | |||
| 526 | |||
| 527 | /** |
||
| 528 | * registers core default javascript |
||
| 529 | */ |
||
| 530 | private function loadCoreJs() |
||
| 541 | |||
| 542 | |||
| 543 | |||
| 544 | /** |
||
| 545 | * registers jQuery Validate for form validation |
||
| 546 | */ |
||
| 547 | private function loadJqueryValidate() |
||
| 565 | |||
| 566 | |||
| 567 | |||
| 568 | /** |
||
| 569 | * registers accounting.js for performing client-side calculations |
||
| 570 | */ |
||
| 571 | private function loadAccountingJs() |
||
| 590 | |||
| 591 | |||
| 592 | |||
| 593 | /** |
||
| 594 | * registers accounting.js for performing client-side calculations |
||
| 595 | */ |
||
| 596 | private function localizeAccountingJs() |
||
| 622 | |||
| 623 | |||
| 624 | |||
| 625 | /** |
||
| 626 | * registers assets for cleaning your ears |
||
| 627 | */ |
||
| 628 | private function loadQtipJs() |
||
| 636 | |||
| 637 | |||
| 638 | /** |
||
| 639 | * This is used to set registered script handles that have data. |
||
| 640 | * @param string $script_handle |
||
| 641 | */ |
||
| 642 | private function addRegisteredScriptHandlesWithData($script_handle) |
||
| 646 | |||
| 647 | |||
| 648 | /**i |
||
| 649 | * Checks WP_Scripts for all of each script handle registered internally as having data and unsets from the |
||
| 650 | * Dependency stored in WP_Scripts if its set. |
||
| 651 | */ |
||
| 652 | private function removeAlreadyRegisteredDataForScriptHandles() |
||
| 661 | |||
| 662 | |||
| 663 | /** |
||
| 664 | * Removes any data dependency registered in WP_Scripts if its set. |
||
| 665 | * @param string $script_handle |
||
| 666 | */ |
||
| 667 | private function removeAlreadyRegisteredDataForScriptHandle($script_handle) |
||
| 689 | |||
| 690 | |||
| 691 | /** |
||
| 692 | * Registers assets that are used in the WordPress admin. |
||
| 693 | */ |
||
| 694 | private function registerAdminAssets() |
||
| 714 | |||
| 715 | |||
| 716 | /** |
||
| 717 | * All handles that are registered via the registry that might have translations have their translations registered |
||
| 718 | * |
||
| 719 | * @param array $handles_to_register |
||
| 720 | */ |
||
| 721 | private function registerTranslationsForHandles(array $handles_to_register) |
||
| 727 | } |
||
| 728 |