Total Complexity | 42 |
Total Lines | 329 |
Duplicated Lines | 0 % |
Changes | 6 | ||
Bugs | 3 | Features | 0 |
Complex classes like ManifestHelper 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.
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 ManifestHelper, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | class ManifestHelper |
||
22 | { |
||
23 | // Constants |
||
24 | // ========================================================================= |
||
25 | |||
26 | public const LEGACY_EXTENSION = '-legacy.'; |
||
27 | |||
28 | // Protected Static Properties |
||
29 | // ========================================================================= |
||
30 | |||
31 | /** |
||
32 | * @var array|null |
||
33 | */ |
||
34 | protected static $manifest; |
||
35 | |||
36 | /** |
||
37 | * @var array|null |
||
38 | */ |
||
39 | protected static $assetFiles; |
||
40 | |||
41 | // Public Static Methods |
||
42 | // ========================================================================= |
||
43 | |||
44 | /** |
||
45 | * Fetch and memoize the manifest file |
||
46 | * |
||
47 | * @param string $manifestPath |
||
48 | */ |
||
49 | public static function fetchManifest(string $manifestPath) |
||
50 | { |
||
51 | // Grab the manifest |
||
52 | $pathOrUrl = (string)Craft::parseEnv($manifestPath); |
||
53 | $manifest = FileHelper::fetch($pathOrUrl, [JsonHelper::class, 'decodeIfJson']); |
||
54 | // If no manifest file is found, log it |
||
55 | if ($manifest === null) { |
||
56 | Craft::error('Manifest not found at ' . $manifestPath, __METHOD__); |
||
57 | } |
||
58 | // Ensure we're dealing with an array |
||
59 | self::$manifest = (array)$manifest; |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * Return an array of tags from the manifest, for both modern and legacy builds |
||
64 | * |
||
65 | * @param string $path |
||
66 | * @param bool $asyncCss |
||
67 | * @param array $scriptTagAttrs |
||
68 | * @param array $cssTagAttrs |
||
69 | * |
||
70 | * @return array |
||
71 | */ |
||
72 | public static function manifestTags(string $path, bool $asyncCss = true, array $scriptTagAttrs = [], array $cssTagAttrs = []): array |
||
73 | { |
||
74 | // Get the modern tags for this $path |
||
75 | return self::extractManifestTags($path, $asyncCss, $scriptTagAttrs, $cssTagAttrs); |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * Return an array of data describing the script, module link, and CSS link tags for the |
||
80 | * script from the manifest.json file |
||
81 | * |
||
82 | * @param string $path |
||
83 | * @param bool $asyncCss |
||
84 | * @param array $scriptTagAttrs |
||
85 | * @param array $cssTagAttrs |
||
86 | * @param bool $legacy |
||
87 | * |
||
88 | * @return array |
||
89 | */ |
||
90 | public static function extractManifestTags(string $path, bool $asyncCss = true, array $scriptTagAttrs = [], array $cssTagAttrs = [], bool $legacy = false): array |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * Return an array of tags from the manifest, for both modern and legacy builds |
||
183 | * |
||
184 | * @param string $path |
||
185 | * @param bool $asyncCss |
||
186 | * @param array $scriptTagAttrs |
||
187 | * @param array $cssTagAttrs |
||
188 | * |
||
189 | * @return array |
||
190 | */ |
||
191 | public static function legacyManifestTags(string $path, bool $asyncCss = true, array $scriptTagAttrs = [], array $cssTagAttrs = []): array |
||
202 | } |
||
203 | |||
204 | /** |
||
205 | * Extract an entry file URL from all of the entries in the manifest |
||
206 | * |
||
207 | * @param string $path |
||
208 | * @return string |
||
209 | */ |
||
210 | public static function extractEntry(string $path): string |
||
235 | } |
||
236 | |||
237 | /** |
||
238 | * Extract an integrity hash for the given $path from the entries in the manifest |
||
239 | * |
||
240 | * @param string $path |
||
241 | * @return string |
||
242 | */ |
||
243 | public static function extractIntegrity(string $path): string |
||
244 | { |
||
245 | foreach (self::$manifest as $entryKey => $entry) { |
||
246 | if (strpos($entryKey, $path) !== false) { |
||
247 | return $entry['integrity'] ?? ''; |
||
248 | } |
||
249 | } |
||
250 | |||
251 | return ''; |
||
252 | } |
||
253 | |||
254 | /** |
||
255 | * Extract any asset files from all of the entries in the manifest |
||
256 | * |
||
257 | * @return array |
||
258 | */ |
||
259 | public static function extractAssetFiles(): array |
||
260 | { |
||
261 | // Used the memoized version if available |
||
262 | if (self::$assetFiles !== null) { |
||
263 | return self::$assetFiles; |
||
264 | } |
||
265 | $assetFiles = []; |
||
266 | foreach (self::$manifest as $entry) { |
||
267 | $assets = $entry['assets'] ?? []; |
||
268 | foreach ($assets as $asset) { |
||
269 | $assetKey = self::filenameWithoutHash($asset); |
||
270 | $assetFiles[$assetKey] = $asset; |
||
271 | } |
||
272 | } |
||
273 | self::$assetFiles = $assetFiles; |
||
274 | |||
275 | return $assetFiles; |
||
276 | } |
||
277 | |||
278 | // Protected Static Methods |
||
279 | // ========================================================================= |
||
280 | |||
281 | /** |
||
282 | * Extract any import files from entries recursively |
||
283 | * |
||
284 | * @param array $manifest |
||
285 | * @param string $manifestKey |
||
286 | * @param array $importFiles |
||
287 | * |
||
288 | * @return array |
||
289 | */ |
||
290 | protected static function extractImportFiles(array $manifest, string $manifestKey, array &$importFiles): array |
||
304 | } |
||
305 | |||
306 | /** |
||
307 | * Extract any CSS files from entries recursively |
||
308 | * |
||
309 | * @param array $manifest |
||
310 | * @param string $manifestKey |
||
311 | * @param array $cssFiles |
||
312 | * |
||
313 | * @return array |
||
314 | */ |
||
315 | protected static function extractCssFiles(array $manifest, string $manifestKey, array &$cssFiles): array |
||
328 | } |
||
329 | |||
330 | /** |
||
331 | * Return a file name from the passed in $path, with any version hash removed from it |
||
332 | * |
||
333 | * @param string $path |
||
334 | * @return string |
||
335 | */ |
||
336 | protected static function filenameWithoutHash(string $path): string |
||
352 |