Total Complexity | 42 |
Total Lines | 330 |
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 | 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 | /** |
||
239 | * Extract an integrity hash for the given $path from the entries in the manifest |
||
240 | * |
||
241 | * @param string $path |
||
242 | * @return string |
||
243 | */ |
||
244 | public static function extractIntegrity(string $path): string |
||
245 | { |
||
246 | foreach (self::$manifest as $entryKey => $entry) { |
||
247 | if (strpos($entryKey, $path) !== false) { |
||
248 | return $entry['integrity'] ?? ''; |
||
249 | } |
||
250 | } |
||
251 | |||
252 | return ''; |
||
253 | } |
||
254 | |||
255 | /** |
||
256 | * Extract any asset files from all of the entries in the manifest |
||
257 | * |
||
258 | * @return array |
||
259 | */ |
||
260 | public static function extractAssetFiles(): array |
||
261 | { |
||
262 | // Used the memoized version if available |
||
263 | if (self::$assetFiles !== null) { |
||
264 | return self::$assetFiles; |
||
265 | } |
||
266 | $assetFiles = []; |
||
267 | foreach (self::$manifest as $entry) { |
||
268 | $assets = $entry['assets'] ?? []; |
||
269 | foreach ($assets as $asset) { |
||
270 | $assetKey = self::filenameWithoutHash($asset); |
||
271 | $assetFiles[$assetKey] = $asset; |
||
272 | } |
||
273 | } |
||
274 | self::$assetFiles = $assetFiles; |
||
275 | |||
276 | return $assetFiles; |
||
277 | } |
||
278 | |||
279 | // Protected Static Methods |
||
280 | // ========================================================================= |
||
281 | |||
282 | /** |
||
283 | * Extract any import files from entries recursively |
||
284 | * |
||
285 | * @param array $manifest |
||
286 | * @param string $manifestKey |
||
287 | * @param array $importFiles |
||
288 | * |
||
289 | * @return array |
||
290 | */ |
||
291 | protected static function extractImportFiles(array $manifest, string $manifestKey, array &$importFiles): array |
||
305 | } |
||
306 | |||
307 | /** |
||
308 | * Extract any CSS files from entries recursively |
||
309 | * |
||
310 | * @param array $manifest |
||
311 | * @param string $manifestKey |
||
312 | * @param array $cssFiles |
||
313 | * |
||
314 | * @return array |
||
315 | */ |
||
316 | protected static function extractCssFiles(array $manifest, string $manifestKey, array &$cssFiles): array |
||
317 | { |
||
318 | $entry = $manifest[$manifestKey] ?? null; |
||
319 | if (!$entry) { |
||
320 | return []; |
||
321 | } |
||
322 | $cssFiles = array_merge($cssFiles, $entry['css'] ?? []); |
||
323 | $imports = $entry['imports'] ?? []; |
||
324 | foreach ($imports as $import) { |
||
325 | self::extractCssFiles($manifest, $import, $cssFiles); |
||
326 | } |
||
327 | |||
328 | return $cssFiles; |
||
329 | } |
||
330 | |||
331 | /** |
||
332 | * Return a file name from the passed in $path, with any version hash removed from it |
||
333 | * |
||
334 | * @param string $path |
||
335 | * @return string |
||
336 | */ |
||
337 | protected static function filenameWithoutHash(string $path): string |
||
351 | } |
||
352 | } |
||
353 |