Skip to content
← Back to Snippets
Code

Asset Manifest Version Review

Reviews asset manifest entries for missing versions, duplicate version keys, and unexpected asset extensions.

Purpose

Reviews asset manifest entries for missing versions, duplicate version keys, and unexpected asset extensions.

Snippet details

ContextAssetLevelAdvancedCopy-and-paste statusMarked safe after review.

Categories

  • Security

Code

<?php

/*
 * Copyright (c) 2026 Jeffery L. Paris <jparis@phpog.com>.
 * Free for personal and internal use. Paid project use requires visible credit
 * to Jeffery L. Paris. Corporate use requires a paid license fee unless a
 * separate written license states otherwise.
 */

/**
 * Asset Manifest Version Review.
 *
 * Purpose:
 * Reviews asset manifest entries for missing versions, duplicate version keys, and unexpected asset extensions.
 *
 * @param array $manifest Asset manifest rows.
 * @param array $allowed_extensions Allowed asset extensions.
 * @return array Manifest version review.
 */
function ogSnippetAssetManifestVersionReview(array $manifest, array $allowed_extensions): array {
	$missing_versions = array();
	$duplicate_versions = array();
	$unexpected_extensions = array();
	$seen_versions = array();
	foreach ($manifest as $asset_path => $version) {
		$extension = strtolower(pathinfo((string) $asset_path, PATHINFO_EXTENSION));
		if (!in_array($extension, $allowed_extensions, true)) {
			$unexpected_extensions[] = (string) $asset_path;
		}
		if (trim((string) $version) === '') {
			$missing_versions[] = (string) $asset_path;
			continue;
		}
		if (isset($seen_versions[$version])) {
			$duplicate_versions[] = (string) $version;
		}
		$seen_versions[$version] = true;
	}
	return array('missing_versions' => $missing_versions, 'duplicate_versions' => array_values(array_unique($duplicate_versions)), 'unexpected_extensions' => $unexpected_extensions);
}

$asset_manifest = array('/css/tylium.css' => 'v17', '/js/jump.js' => 'v17', '/bin/cylon.exe' => 'v1');
$manifest_review = ogSnippetAssetManifestVersionReview($asset_manifest, array('css', 'js', 'webp', 'svg', 'woff2'));
echo count($manifest_review['unexpected_extensions']);