diff --git a/src/utils.php b/src/utils.php index c13f1be..2ed1c6a 100644 --- a/src/utils.php +++ b/src/utils.php @@ -138,11 +138,22 @@ function stripStringOverlap(string $a, string $b): string * in ascending order. * * @param array - * @return array + * @return void */ function sortUrisLevelOrder(&$uriList) { - usort($uriList, function ($a, $b) { - return substr_count(Uri\parse($a)['path'], '/') - substr_count(Uri\parse($b)['path'], '/'); + // Parse URIs so we are not continually parsing them while sorting + $parsedUriList = array_map(function ($uri) { + return Uri\parse($uri); + }, $uriList); + + // Sort by number of slashes in parsed URI path, ascending + usort($parsedUriList, function ($a, $b) { + return substr_count($a['path'], '/') - substr_count($b['path'], '/'); }); + + // Reassemble the URIs + $uriList = array_map(function ($parsedUri) { + return Uri\build($parsedUri); + }, $parsedUriList); }