Skip to content
← Back to Functions
Code

Route Coverage Auditor

Compares declared routes, generated links, sitemap URLs, and rewrite rules to find gaps or orphaned pages.

Function signature

ogAuditRouteCoverage(declared_routes = array(), generated_links = array(), sitemap_urls = array(), rewrite_rules = array())

Categories

  • Import and Export

Parameters

declared_routesDeclared route patterns from controller/router inventory.generated_linksLinks generated by controllers or content that should map to declared routes.sitemap_urlsSitemap URLs reviewed for coverage and orphan checks.rewrite_rulesRewrite rules reviewed for coverage of declared routes.

Return value

Public-safe status string returned by the function for explicit controller branching or logging.

  • success
  • message
  • data

Compatibility

Existing function name, slug, path, and call order preserved; advertised metadata corrected to the actual source behavior.

Minimum PHP version: 7.4

Security notes

Use caller-owned allowlists and context-specific escaping; validate admin actions, export fields, privacy plans, cache keys, templates, settings, routes, and ecommerce policies before production use.

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.
 */

/**
 * Audits declared routes against generated links, sitemap URLs, and rewrite rules.
 *
 * This function is report-only. It does not rewrite .htaccess or change route data.
 *
 * @param array $declared_routes Canonical route paths.
 * @param array $generated_links Links found in templates/content.
 * @param array $sitemap_urls Sitemap URL paths.
 * @param array $rewrite_rules Rewrite target paths or rule labels.
 * @return array Route coverage report.
 */
function ogAuditRouteCoverage($declared_routes = array(), $generated_links = array(), $sitemap_urls = array(), $rewrite_rules = array()) {
	$result = array(
		'success' => false,
		'message' => '',
		'data' => array()
	);

	if (!is_array($declared_routes)) {
		$declared_routes = array();
	}
	if (!is_array($generated_links)) {
		$generated_links = array();
	}
	if (!is_array($sitemap_urls)) {
		$sitemap_urls = array();
	}
	if (!is_array($rewrite_rules)) {
		$rewrite_rules = array();
	}

	$sets = array(
		'declared' => $declared_routes,
		'links' => $generated_links,
		'sitemap' => $sitemap_urls,
		'rewrites' => $rewrite_rules
	);
	$normalized_sets = array();
	foreach ($sets as $set_name => $route_list) {
		$normalized_sets[$set_name] = array();
		foreach ($route_list as $route) {
			if (is_array($route) && !empty($route['path'])) {
				$route = $route['path'];
			}
			$route = trim((string)$route);
			$route = preg_replace('/^https?:\/\/[^\/]+/i', '', $route);
			$route = preg_replace('/^\/\/[^\/]+/i', '', $route);
			$route = '/' . ltrim($route, '/');
			$route = rtrim($route, '/');
			if (empty($route)) {
				$route = '/';
			}
			$normalized_sets[$set_name][$route] = $route;
		}
		$normalized_sets[$set_name] = array_values($normalized_sets[$set_name]);
	}

	$missing_from_sitemap = array_values(array_diff($normalized_sets['declared'], $normalized_sets['sitemap']));
	$unknown_links = array_values(array_diff($normalized_sets['links'], $normalized_sets['declared']));
	$missing_rewrites = array_values(array_diff($normalized_sets['declared'], $normalized_sets['rewrites']));
	$orphan_sitemap = array_values(array_diff($normalized_sets['sitemap'], $normalized_sets['declared']));

	$result['success'] = true;
	$result['message'] = 'Route coverage audit completed.';
	$result['data'] = array(
		'missing_from_sitemap' => $missing_from_sitemap,
		'unknown_links' => $unknown_links,
		'missing_rewrite_coverage' => $missing_rewrites,
		'orphan_sitemap_urls' => $orphan_sitemap,
		'changes_made' => false
	);

	return $result;
}