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 AssetManifest 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 AssetManifest, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class AssetManifest implements AssetManifestInterface |
||
| 15 | { |
||
| 16 | |||
| 17 | const ASSET_EXT_CSS = '.css'; |
||
| 18 | |||
| 19 | const ASSET_EXT_JS = '.js'; |
||
| 20 | |||
| 21 | const ASSET_EXT_PHP = '.php'; |
||
| 22 | |||
| 23 | const FILE_NAME = 'asset-manifest.json'; |
||
| 24 | |||
| 25 | const KEY_DEPENDENCIES = 'dependencies'; |
||
| 26 | |||
| 27 | const KEY_ENTRY_POINTS = 'entrypoints'; |
||
| 28 | |||
| 29 | const KEY_FILES = 'files'; |
||
| 30 | |||
| 31 | const KEY_VERSION = 'version'; |
||
| 32 | |||
| 33 | |||
| 34 | /** |
||
| 35 | * @var array |
||
| 36 | */ |
||
| 37 | private $asset_files; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | private $assets_namespace; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | private $assets_path; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var DomainInterface |
||
| 51 | */ |
||
| 52 | protected $domain; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var array |
||
| 56 | */ |
||
| 57 | private $entry_points; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var array |
||
| 61 | */ |
||
| 62 | private $manifest; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var string |
||
| 66 | */ |
||
| 67 | private $manifest_path; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * This is a list of known handles that are used for css. |
||
| 71 | * @var array |
||
| 72 | */ |
||
| 73 | private $wp_css_handle_dependencies = [ |
||
| 74 | 'wp-components', |
||
| 75 | 'wp-block-editor', |
||
| 76 | 'wp-block-library', |
||
| 77 | 'wp-edit-post', |
||
| 78 | 'wp-edit-widgets', |
||
| 79 | 'wp-editor', |
||
| 80 | 'wp-format-library', |
||
| 81 | 'wp-list-reusable-blocks', |
||
| 82 | 'wp-nux', |
||
| 83 | ]; |
||
| 84 | |||
| 85 | |||
| 86 | /** |
||
| 87 | * AssetManifest constructor. |
||
| 88 | * |
||
| 89 | * @param DomainInterface $domain |
||
| 90 | */ |
||
| 91 | public function __construct(DomainInterface $domain) |
||
| 96 | |||
| 97 | |||
| 98 | /** |
||
| 99 | * @return void |
||
| 100 | */ |
||
| 101 | public function initialize() |
||
| 113 | |||
| 114 | |||
| 115 | /** |
||
| 116 | * @param string $manifest_path |
||
| 117 | */ |
||
| 118 | public function setManifestFilepath($manifest_path = '') |
||
| 136 | |||
| 137 | |||
| 138 | /** |
||
| 139 | * @return void |
||
| 140 | */ |
||
| 141 | private function loadManifest() |
||
| 149 | |||
| 150 | |||
| 151 | /** |
||
| 152 | * @param string $file_name |
||
| 153 | * @return string |
||
| 154 | */ |
||
| 155 | private function trimAssetFilename($file_name) |
||
| 159 | |||
| 160 | |||
| 161 | /** |
||
| 162 | * @return array |
||
| 163 | */ |
||
| 164 | View Code Duplication | public function getAssetFiles() |
|
| 177 | |||
| 178 | |||
| 179 | /** |
||
| 180 | * @return array |
||
| 181 | */ |
||
| 182 | View Code Duplication | public function getEntryPoints() |
|
| 195 | |||
| 196 | |||
| 197 | /** |
||
| 198 | * @param string $entry_point |
||
| 199 | * @param string $type |
||
| 200 | * @return string |
||
| 201 | */ |
||
| 202 | private function getAsset($entry_point, $type = AssetManifest::ASSET_EXT_JS) |
||
| 208 | |||
| 209 | |||
| 210 | /** |
||
| 211 | * @param string $entry_point |
||
| 212 | * @param string $type |
||
| 213 | * @return array |
||
| 214 | */ |
||
| 215 | public function getAssetDependencies($entry_point, $type = AssetManifest::ASSET_EXT_JS) |
||
| 241 | |||
| 242 | |||
| 243 | /** |
||
| 244 | * @param string $entry_point |
||
| 245 | * @return array |
||
| 246 | */ |
||
| 247 | public function getAssetDetails($entry_point) |
||
| 262 | |||
| 263 | |||
| 264 | /** |
||
| 265 | * @param string $entry_point |
||
| 266 | * @return string|int|false |
||
| 267 | */ |
||
| 268 | public function getAssetHandle($entry_point) |
||
| 272 | |||
| 273 | |||
| 274 | /** |
||
| 275 | * @return string |
||
| 276 | */ |
||
| 277 | public function getAssetsPath() |
||
| 281 | |||
| 282 | |||
| 283 | /** |
||
| 284 | * @param string $handle |
||
| 285 | * @return string|int|false |
||
| 286 | */ |
||
| 287 | private function getEntryPointFromHandle($handle) |
||
| 291 | |||
| 292 | |||
| 293 | /** |
||
| 294 | * @param string $entry_point |
||
| 295 | * @param string $type |
||
| 296 | * @return string |
||
| 297 | */ |
||
| 298 | public function getAssetPath($entry_point, $type = AssetManifest::ASSET_EXT_JS) |
||
| 303 | |||
| 304 | |||
| 305 | /** |
||
| 306 | * @param string $entry_point |
||
| 307 | * @param string $type |
||
| 308 | * @return string |
||
| 309 | */ |
||
| 310 | public function getAssetUrl($entry_point, $type = AssetManifest::ASSET_EXT_JS) |
||
| 315 | |||
| 316 | |||
| 317 | /** |
||
| 318 | * @param string $entry_point |
||
| 319 | * @param string $type |
||
| 320 | * @return string|int|false |
||
| 321 | */ |
||
| 322 | public function getAssetVersion($entry_point, $type = AssetManifest::ASSET_EXT_JS) |
||
| 329 | |||
| 330 | |||
| 331 | /** |
||
| 332 | * @param string $entry_point |
||
| 333 | * @param string $type |
||
| 334 | * @return string |
||
| 335 | */ |
||
| 336 | public function hasAsset($entry_point, $type = AssetManifest::ASSET_EXT_JS) |
||
| 341 | } |
||
| 342 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.