From 654ed28f2f13e91a533a6752027870484985a95f Mon Sep 17 00:00:00 2001 From: Sander Muller Date: Wed, 26 Aug 2026 23:02:37 +0200 Subject: [PATCH] Use early exit when collecting the registered class loaders The Coding Standard job is red on 2.2.x after c10897b1f: the loop collecting ClassLoader ids nests its body in an if, and the boundary lookup nests a foreach in an if. Both read better inverted, and the lookup becomes a named function. Co-Authored-By: Claude Opus 5 (1M context) --- src/autoloadFunctions.php | 41 ++++++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/src/autoloadFunctions.php b/src/autoloadFunctions.php index 17bcce1dcc..4700363867 100644 --- a/src/autoloadFunctions.php +++ b/src/autoloadFunctions.php @@ -48,6 +48,31 @@ function isComposerClassLoader($autoloadFunction): bool // phpcs:ignore Squiz.Fu && get_class($autoloadFunction[0]) === ClassLoader::class; } +/** + * Index of the given class loader in the queue, or null when it is not registered any more - + * a bootstrap file has then taken Composer's place and there is no boundary to split on. + * + * @param list $autoloadFunctions + */ +function findClassLoaderIndex(array $autoloadFunctions, ?int $classLoaderId): ?int // phpcs:ignore Squiz.Functions.GlobalFunction.Found +{ + if ($classLoaderId === null) { + return null; + } + + foreach ($autoloadFunctions as $index => $autoloadFunction) { + if (!isComposerClassLoader($autoloadFunction)) { + continue; + } + + if (spl_object_id($autoloadFunction[0]) === $classLoaderId) { + return $index; + } + } + + return null; +} + /** * Splits the autoload functions registered while loading Composer's autoloader * and the bootstrap files into those registered before and after Composer's own @@ -83,23 +108,17 @@ function collectNewAutoloadFunctions($autoloadFunctionsBefore, $autoloadFunction // it below the static source locators. $classLoaderIds = []; foreach ($autoloadFunctionsBefore as $before) { - if (isComposerClassLoader($before)) { - $classLoaderIds[] = spl_object_id($before[0]); + if (!isComposerClassLoader($before)) { + continue; } + + $classLoaderIds[] = spl_object_id($before[0]); } array_shift($classLoaderIds); $projectLoaderId = $classLoaderIds === [] ? null : $classLoaderIds[count($classLoaderIds) - 1]; - $composerIndex = null; - if ($projectLoaderId !== null) { - foreach ($autoloadFunctionsAfter as $index => $after) { - if (isComposerClassLoader($after) && spl_object_id($after[0]) === $projectLoaderId) { - $composerIndex = $index; - break; - } - } - } + $composerIndex = findClassLoaderIndex($autoloadFunctionsAfter, $projectLoaderId); foreach ($autoloadFunctionsAfter as $index => $after) { if (is_array($after) && count($after) > 0) {