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) |
||
129 | |||
130 | |||
131 | /** |
||
132 | * @param AssetCollection $asset_collection |
||
133 | */ |
||
134 | public function addAssetCollection(AssetCollection $asset_collection) |
||
141 | |||
142 | |||
143 | |||
144 | |||
145 | /** |
||
146 | * For classes that have Registry as a dependency, this provides a handy way to register script handles for i18n |
||
147 | * translation handling. |
||
148 | * |
||
149 | * @return I18nRegistry |
||
150 | */ |
||
151 | public function getI18nRegistry() |
||
155 | |||
156 | |||
157 | /** |
||
158 | * Callback for the wp_enqueue_scripts actions used to register assets. |
||
159 | * |
||
160 | * @since 4.9.62.p |
||
161 | * @throws Exception |
||
162 | */ |
||
163 | public function registerScriptsAndStyles() |
||
174 | |||
175 | |||
176 | /** |
||
177 | * Registers JS assets with WP core |
||
178 | * |
||
179 | * @param JavascriptAsset[] $scripts |
||
180 | * @throws AssetRegistrationException |
||
181 | * @throws InvalidDataTypeException |
||
182 | * @throws DomainException |
||
183 | * @since 4.9.62.p |
||
184 | */ |
||
185 | public function registerScripts(array $scripts) |
||
219 | |||
220 | |||
221 | /** |
||
222 | * Registers CSS assets with WP core |
||
223 | * |
||
224 | * @param StylesheetAsset[] $styles |
||
225 | * @throws InvalidDataTypeException |
||
226 | * @throws DomainException |
||
227 | * @since 4.9.62.p |
||
228 | */ |
||
229 | public function registerStyles(array $styles) |
||
257 | |||
258 | |||
259 | /** |
||
260 | * Call back for the script print in frontend and backend. |
||
261 | * Used to call wp_localize_scripts so that data can be added throughout the runtime until this later hook point. |
||
262 | * |
||
263 | * @throws Exception |
||
264 | * @since 4.9.31.rc.015 |
||
265 | */ |
||
266 | public function enqueueData() |
||
290 | |||
291 | |||
292 | /** |
||
293 | * Used to add data to eejs.data object. |
||
294 | * Note: Overriding existing data is not allowed. |
||
295 | * Data will be accessible as a javascript object when you list `eejs-core` as a dependency for your javascript. |
||
296 | * If the data you add is something like this: |
||
297 | * $this->addData( 'my_plugin_data', array( 'foo' => 'gar' ) ); |
||
298 | * It will be exposed in the page source as: |
||
299 | * eejs.data.my_plugin_data.foo == gar |
||
300 | * |
||
301 | * @param string $key Key used to access your data |
||
302 | * @param string|array $value Value to attach to key |
||
303 | * @throws InvalidArgumentException |
||
304 | */ |
||
305 | public function addData($key, $value) |
||
311 | |||
312 | |||
313 | /** |
||
314 | * Similar to addData except this allows for users to push values to an existing key where the values on key are |
||
315 | * elements in an array. |
||
316 | * |
||
317 | * When you use this method, the value you include will be merged with the array on $key. |
||
318 | * So if the $key was 'test' and you added a value of ['my_data'] then it would be represented in the javascript |
||
319 | * object like this, eejs.data.test = [ my_data, |
||
320 | * ] |
||
321 | * If there has already been a scalar value attached to the data object given key (via addData for instance), then |
||
322 | * this will throw an exception. |
||
323 | * |
||
324 | * Caution: Only add data using this method if you are okay with the potential for additional data added on the same |
||
325 | * key potentially overriding the existing data on merge (specifically with associative arrays). |
||
326 | * |
||
327 | * @param string $key Key to attach data to. |
||
328 | * @param string|array $value Value being registered. |
||
329 | * @throws InvalidArgumentException |
||
330 | */ |
||
331 | public function pushData($key, $value) |
||
357 | |||
358 | |||
359 | /** |
||
360 | * Used to set content used by javascript for a template. |
||
361 | * Note: Overrides of existing registered templates are not allowed. |
||
362 | * |
||
363 | * @param string $template_reference |
||
364 | * @param string $template_content |
||
365 | * @throws InvalidArgumentException |
||
366 | */ |
||
367 | public function addTemplate($template_reference, $template_content) |
||
389 | |||
390 | |||
391 | /** |
||
392 | * Retrieve the template content already registered for the given reference. |
||
393 | * |
||
394 | * @param string $template_reference |
||
395 | * @return string |
||
396 | */ |
||
397 | public function getTemplate($template_reference) |
||
403 | |||
404 | |||
405 | /** |
||
406 | * Retrieve registered data. |
||
407 | * |
||
408 | * @param string $key Name of key to attach data to. |
||
409 | * @return mixed If there is no for the given key, then false is returned. |
||
410 | */ |
||
411 | public function getData($key) |
||
417 | |||
418 | |||
419 | /** |
||
420 | * Verifies whether the given data exists already on the jsdata array. |
||
421 | * Overriding data is not allowed. |
||
422 | * |
||
423 | * @param string $key Index for data. |
||
424 | * @return bool If valid then return true. |
||
425 | * @throws InvalidArgumentException if data already exists. |
||
426 | */ |
||
427 | protected function verifyDataNotExisting($key) |
||
460 | |||
461 | |||
462 | /** |
||
463 | * Get the actual asset path for asset manifests. |
||
464 | * If there is no asset path found for the given $chunk_name, then the $chunk_name is returned. |
||
465 | * |
||
466 | * @param string $namespace The namespace associated with the manifest file hosting the map of chunk_name to actual |
||
467 | * asset file location. |
||
468 | * @param string $chunk_name |
||
469 | * @param string $asset_type |
||
470 | * @return string |
||
471 | * @since 4.9.59.p |
||
472 | */ |
||
473 | public function getAssetUrl($namespace, $chunk_name, $asset_type) |
||
491 | |||
492 | |||
493 | |||
494 | /** |
||
495 | * Return the url to a js file for the given namespace and chunk name. |
||
496 | * |
||
497 | * @param string $namespace |
||
498 | * @param string $chunk_name |
||
499 | * @return string |
||
500 | */ |
||
501 | public function getJsUrl($namespace, $chunk_name) |
||
505 | |||
506 | |||
507 | /** |
||
508 | * Return the url to a css file for the given namespace and chunk name. |
||
509 | * |
||
510 | * @param string $namespace |
||
511 | * @param string $chunk_name |
||
512 | * @return string |
||
513 | */ |
||
514 | public function getCssUrl($namespace, $chunk_name) |
||
518 | |||
519 | |||
520 | /** |
||
521 | * Return the dependencies array and version string for a given asset $chunk_name |
||
522 | * |
||
523 | * @param string $namespace |
||
524 | * @param string $chunk_name |
||
525 | * @param string $asset_type |
||
526 | * @return array |
||
527 | * @since 4.9.82.p |
||
528 | */ |
||
529 | private function getDetailsForAsset($namespace, $chunk_name, $asset_type) |
||
547 | |||
548 | |||
549 | /** |
||
550 | * Return dependencies array and version string according to asset type. |
||
551 | * For css assets, this filters the auto generated dependencies by css type. |
||
552 | * |
||
553 | * @param string $namespace |
||
554 | * @param string $asset_type |
||
555 | * @param string $file_path |
||
556 | * @param string $chunk_name |
||
557 | * @return array |
||
558 | * @since 4.9.82.p |
||
559 | */ |
||
560 | private function getDetailsForAssetType($namespace, $asset_type, $file_path, $chunk_name) |
||
606 | |||
607 | |||
608 | /** |
||
609 | * Get the dependencies array and version string for the given js asset chunk name |
||
610 | * |
||
611 | * @param string $namespace |
||
612 | * @param string $chunk_name |
||
613 | * @return array |
||
614 | * @since 4.10.2.p |
||
615 | */ |
||
616 | public function getJsAssetDetails($namespace, $chunk_name) |
||
620 | |||
621 | |||
622 | /** |
||
623 | * Get the dependencies array and version string for the given css asset chunk name |
||
624 | * |
||
625 | * @param string $namespace |
||
626 | * @param string $chunk_name |
||
627 | * @return array |
||
628 | * @since 4.10.2.p |
||
629 | */ |
||
630 | public function getCssAssetDetails($namespace, $chunk_name) |
||
634 | |||
635 | |||
636 | /** |
||
637 | * @throws Exception |
||
638 | * @throws InvalidArgumentException |
||
639 | * @throws InvalidFilePathException |
||
640 | * @since 4.9.62.p |
||
641 | */ |
||
642 | public function registerManifestFiles() |
||
661 | |||
662 | |||
663 | /** |
||
664 | * Used to register a js/css manifest file with the registered_manifest_files property. |
||
665 | * |
||
666 | * @param string $namespace Provided to associate the manifest file with a specific namespace. |
||
667 | * @param string $url_base The url base for the manifest file location. |
||
668 | * @param string $manifest_file The absolute path to the manifest file. |
||
669 | * @param string $manifest_file_path The path to the folder containing the manifest file. If not provided will be |
||
670 | * default to `plugin_root/assets/dist`. |
||
671 | * @throws InvalidArgumentException |
||
672 | * @throws InvalidFilePathException |
||
673 | * @since 4.9.59.p |
||
674 | */ |
||
675 | public function registerManifestFile($namespace, $url_base, $manifest_file, $manifest_file_path = '') |
||
719 | |||
720 | |||
721 | /** |
||
722 | * Decodes json from the provided manifest file. |
||
723 | * |
||
724 | * @since 4.9.59.p |
||
725 | * @param string $manifest_file Path to manifest file. |
||
726 | * @return array |
||
727 | * @throws InvalidFilePathException |
||
728 | */ |
||
729 | private function decodeManifestFile($manifest_file) |
||
736 | |||
737 | |||
738 | /** |
||
739 | * This is used to set registered script handles that have data. |
||
740 | * |
||
741 | * @param string $script_handle |
||
742 | */ |
||
743 | private function addRegisteredScriptHandlesWithData($script_handle) |
||
747 | |||
748 | |||
749 | /**i |
||
750 | * Checks WP_Scripts for all of each script handle registered internally as having data and unsets from the |
||
751 | * Dependency stored in WP_Scripts if its set. |
||
752 | */ |
||
753 | private function removeAlreadyRegisteredDataForScriptHandles() |
||
762 | |||
763 | |||
764 | /** |
||
765 | * Removes any data dependency registered in WP_Scripts if its set. |
||
766 | * |
||
767 | * @param string $script_handle |
||
768 | */ |
||
769 | private function removeAlreadyRegisteredDataForScriptHandle($script_handle) |
||
791 | |||
792 | |||
793 | /** |
||
794 | * register translations for a registered script |
||
795 | * |
||
796 | * @param string $handle |
||
797 | */ |
||
798 | public function registerTranslation($handle) |
||
802 | |||
803 | |||
804 | /** |
||
805 | * @since 4.9.63.p |
||
806 | * @return bool |
||
807 | */ |
||
808 | private function debug() |
||
815 | |||
816 | |||
817 | /** |
||
818 | * Get the dependencies array for the given js asset chunk name |
||
819 | * |
||
820 | * @param string $namespace |
||
821 | * @param string $chunk_name |
||
822 | * @return array |
||
823 | * @deprecated 4.10.2.p |
||
824 | * @since 4.9.82.p |
||
825 | */ |
||
826 | public function getJsDependencies($namespace, $chunk_name) |
||
831 | |||
832 | |||
833 | /** |
||
834 | * Get the dependencies array for the given css asset chunk name |
||
835 | * |
||
836 | * @param string $namespace |
||
837 | * @param string $chunk_name |
||
838 | * @return array |
||
839 | * @deprecated 4.10.2.p |
||
840 | * @since 4.9.82.p |
||
841 | */ |
||
842 | public function getCssDependencies($namespace, $chunk_name) |
||
847 | } |
||
848 |