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 |
||
| 23 | class Registry |
||
| 24 | { |
||
| 25 | |||
| 26 | const FILE_NAME_BUILD_MANIFEST = 'build-manifest.json'; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var AssetCollection $assets |
||
| 30 | */ |
||
| 31 | protected $assets; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var I18nRegistry |
||
| 35 | */ |
||
| 36 | private $i18n_registry; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * This holds the jsdata data object that will be exposed on pages that enqueue the `eejs-core` script. |
||
| 40 | * |
||
| 41 | * @var array |
||
| 42 | */ |
||
| 43 | protected $jsdata = array(); |
||
| 44 | |||
| 45 | /** |
||
| 46 | * This keeps track of all scripts with registered data. It is used to prevent duplicate data objects setup in the |
||
| 47 | * page source. |
||
| 48 | * |
||
| 49 | * @var array |
||
| 50 | */ |
||
| 51 | private $script_handles_with_data = array(); |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Holds the manifest data obtained from registered manifest files. |
||
| 55 | * Manifests are maps of asset chunk name to actual built asset file names. |
||
| 56 | * Shape of this array is: |
||
| 57 | * array( |
||
| 58 | * 'some_namespace_slug' => array( |
||
| 59 | * 'some_chunk_name' => array( |
||
| 60 | * 'js' => 'filename.js' |
||
| 61 | * 'css' => 'filename.js' |
||
| 62 | * ), |
||
| 63 | * 'url_base' => 'https://baseurl.com/to/assets |
||
| 64 | * ) |
||
| 65 | * ) |
||
| 66 | * |
||
| 67 | * @var array |
||
| 68 | */ |
||
| 69 | private $manifest_data = array(); |
||
| 70 | |||
| 71 | |||
| 72 | /** |
||
| 73 | * Registry constructor. |
||
| 74 | * Hooking into WP actions for script registry. |
||
| 75 | * |
||
| 76 | * @param AssetCollection $assets |
||
| 77 | * @param I18nRegistry $i18n_registry |
||
| 78 | */ |
||
| 79 | public function __construct(AssetCollection $assets, I18nRegistry $i18n_registry) |
||
| 92 | |||
| 93 | |||
| 94 | /** |
||
| 95 | * For classes that have Registry as a dependency, this provides a handy way to register script handles for i18n |
||
| 96 | * translation handling. |
||
| 97 | * |
||
| 98 | * @return I18nRegistry |
||
| 99 | */ |
||
| 100 | public function getI18nRegistry() |
||
| 104 | |||
| 105 | |||
| 106 | /** |
||
| 107 | * Callback for the wp_enqueue_scripts actions used to register assets. |
||
| 108 | * |
||
| 109 | * @since $VID:$ |
||
| 110 | * @throws Exception |
||
| 111 | */ |
||
| 112 | public function registerScriptsAndStyles() |
||
| 121 | |||
| 122 | |||
| 123 | /** |
||
| 124 | * Registers JS assets with WP core |
||
| 125 | * |
||
| 126 | * @since $VID:$ |
||
| 127 | * @param JavascriptAsset[] $scripts |
||
| 128 | * @throws AssetRegistrationException |
||
| 129 | * @throws InvalidDataTypeException |
||
| 130 | */ |
||
| 131 | public function registerScripts(array $scripts) |
||
| 162 | |||
| 163 | |||
| 164 | /** |
||
| 165 | * Registers CSS assets with WP core |
||
| 166 | * |
||
| 167 | * @since $VID:$ |
||
| 168 | * @param StylesheetAsset[] $styles |
||
| 169 | * @throws InvalidDataTypeException |
||
| 170 | */ |
||
| 171 | public function registerStyles(array $styles) |
||
| 196 | |||
| 197 | |||
| 198 | /** |
||
| 199 | * Call back for the script print in frontend and backend. |
||
| 200 | * Used to call wp_localize_scripts so that data can be added throughout the runtime until this later hook point. |
||
| 201 | * |
||
| 202 | * @since 4.9.31.rc.015 |
||
| 203 | */ |
||
| 204 | public function enqueueData() |
||
| 221 | |||
| 222 | |||
| 223 | /** |
||
| 224 | * Used to add data to eejs.data object. |
||
| 225 | * Note: Overriding existing data is not allowed. |
||
| 226 | * Data will be accessible as a javascript object when you list `eejs-core` as a dependency for your javascript. |
||
| 227 | * If the data you add is something like this: |
||
| 228 | * $this->addData( 'my_plugin_data', array( 'foo' => 'gar' ) ); |
||
| 229 | * It will be exposed in the page source as: |
||
| 230 | * eejs.data.my_plugin_data.foo == gar |
||
| 231 | * |
||
| 232 | * @param string $key Key used to access your data |
||
| 233 | * @param string|array $value Value to attach to key |
||
| 234 | * @throws InvalidArgumentException |
||
| 235 | */ |
||
| 236 | public function addData($key, $value) |
||
| 242 | |||
| 243 | |||
| 244 | /** |
||
| 245 | * Similar to addData except this allows for users to push values to an existing key where the values on key are |
||
| 246 | * elements in an array. |
||
| 247 | * When you use this method, the value you include will be appended to the end of an array on $key. |
||
| 248 | * So if the $key was 'test' and you added a value of 'my_data' then it would be represented in the javascript |
||
| 249 | * object like this, eejs.data.test = [ my_data, |
||
| 250 | * ] |
||
| 251 | * If there has already been a scalar value attached to the data object given key, then |
||
| 252 | * this will throw an exception. |
||
| 253 | * |
||
| 254 | * @param string $key Key to attach data to. |
||
| 255 | * @param string|array $value Value being registered. |
||
| 256 | * @throws InvalidArgumentException |
||
| 257 | */ |
||
| 258 | public function pushData($key, $value) |
||
| 277 | |||
| 278 | |||
| 279 | /** |
||
| 280 | * Used to set content used by javascript for a template. |
||
| 281 | * Note: Overrides of existing registered templates are not allowed. |
||
| 282 | * |
||
| 283 | * @param string $template_reference |
||
| 284 | * @param string $template_content |
||
| 285 | * @throws InvalidArgumentException |
||
| 286 | */ |
||
| 287 | public function addTemplate($template_reference, $template_content) |
||
| 306 | |||
| 307 | |||
| 308 | /** |
||
| 309 | * Retrieve the template content already registered for the given reference. |
||
| 310 | * |
||
| 311 | * @param string $template_reference |
||
| 312 | * @return string |
||
| 313 | */ |
||
| 314 | public function getTemplate($template_reference) |
||
| 320 | |||
| 321 | |||
| 322 | /** |
||
| 323 | * Retrieve registered data. |
||
| 324 | * |
||
| 325 | * @param string $key Name of key to attach data to. |
||
| 326 | * @return mixed If there is no for the given key, then false is returned. |
||
| 327 | */ |
||
| 328 | public function getData($key) |
||
| 334 | |||
| 335 | |||
| 336 | /** |
||
| 337 | * Verifies whether the given data exists already on the jsdata array. |
||
| 338 | * Overriding data is not allowed. |
||
| 339 | * |
||
| 340 | * @param string $key Index for data. |
||
| 341 | * @return bool If valid then return true. |
||
| 342 | * @throws InvalidArgumentException if data already exists. |
||
| 343 | */ |
||
| 344 | protected function verifyDataNotExisting($key) |
||
| 374 | |||
| 375 | |||
| 376 | /** |
||
| 377 | * Get the actual asset path for asset manifests. |
||
| 378 | * If there is no asset path found for the given $chunk_name, then the $chunk_name is returned. |
||
| 379 | * |
||
| 380 | * @param string $namespace The namespace associated with the manifest file hosting the map of chunk_name to actual |
||
| 381 | * asset file location. |
||
| 382 | * @param string $chunk_name |
||
| 383 | * @param string $asset_type |
||
| 384 | * @return string |
||
| 385 | * @since 4.9.59.p |
||
| 386 | */ |
||
| 387 | public function getAssetUrl($namespace, $chunk_name, $asset_type) |
||
| 404 | |||
| 405 | |||
| 406 | /** |
||
| 407 | * Return the url to a js file for the given namespace and chunk name. |
||
| 408 | * |
||
| 409 | * @param string $namespace |
||
| 410 | * @param string $chunk_name |
||
| 411 | * @return string |
||
| 412 | */ |
||
| 413 | public function getJsUrl($namespace, $chunk_name) |
||
| 417 | |||
| 418 | |||
| 419 | /** |
||
| 420 | * Return the url to a css file for the given namespace and chunk name. |
||
| 421 | * |
||
| 422 | * @param string $namespace |
||
| 423 | * @param string $chunk_name |
||
| 424 | * @return string |
||
| 425 | */ |
||
| 426 | public function getCssUrl($namespace, $chunk_name) |
||
| 430 | |||
| 431 | |||
| 432 | /** |
||
| 433 | * @since $VID:$ |
||
| 434 | * @throws InvalidArgumentException |
||
| 435 | * @throws InvalidFilePathException |
||
| 436 | */ |
||
| 437 | public function registerManifestFiles() |
||
| 448 | |||
| 449 | |||
| 450 | /** |
||
| 451 | * Used to register a js/css manifest file with the registered_manifest_files property. |
||
| 452 | * |
||
| 453 | * @param string $namespace Provided to associate the manifest file with a specific namespace. |
||
| 454 | * @param string $url_base The url base for the manifest file location. |
||
| 455 | * @param string $manifest_file The absolute path to the manifest file. |
||
| 456 | * @throws InvalidArgumentException |
||
| 457 | * @throws InvalidFilePathException |
||
| 458 | * @since 4.9.59.p |
||
| 459 | */ |
||
| 460 | public function registerManifestFile($namespace, $url_base, $manifest_file) |
||
| 498 | |||
| 499 | |||
| 500 | /** |
||
| 501 | * Decodes json from the provided manifest file. |
||
| 502 | * |
||
| 503 | * @since 4.9.59.p |
||
| 504 | * @param string $manifest_file Path to manifest file. |
||
| 505 | * @return array |
||
| 506 | * @throws InvalidFilePathException |
||
| 507 | */ |
||
| 508 | private function decodeManifestFile($manifest_file) |
||
| 515 | |||
| 516 | |||
| 517 | /** |
||
| 518 | * This is used to set registered script handles that have data. |
||
| 519 | * |
||
| 520 | * @param string $script_handle |
||
| 521 | */ |
||
| 522 | private function addRegisteredScriptHandlesWithData($script_handle) |
||
| 526 | |||
| 527 | |||
| 528 | /**i |
||
| 529 | * Checks WP_Scripts for all of each script handle registered internally as having data and unsets from the |
||
| 530 | * Dependency stored in WP_Scripts if its set. |
||
| 531 | */ |
||
| 532 | private function removeAlreadyRegisteredDataForScriptHandles() |
||
| 541 | |||
| 542 | |||
| 543 | /** |
||
| 544 | * Removes any data dependency registered in WP_Scripts if its set. |
||
| 545 | * |
||
| 546 | * @param string $script_handle |
||
| 547 | */ |
||
| 548 | private function removeAlreadyRegisteredDataForScriptHandle($script_handle) |
||
| 570 | |||
| 571 | |||
| 572 | /** |
||
| 573 | * register translations for a registered script |
||
| 574 | * |
||
| 575 | * @param string $handle |
||
| 576 | */ |
||
| 577 | public function registerTranslation($handle) |
||
| 581 | } |
||
| 582 |