Skip to content

Commit 9680cc4

Browse files
SanderMullerclaude
authored andcommitted
Use early exit when collecting the registered class loaders
The Coding Standard job is red on 2.2.x after c10897b: 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) <noreply@anthropic.com>
1 parent c10897b commit 9680cc4

1 file changed

Lines changed: 30 additions & 11 deletions

File tree

‎src/autoloadFunctions.php‎

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,31 @@ function isComposerClassLoader($autoloadFunction): bool // phpcs:ignore Squiz.Fu
4848
&& get_class($autoloadFunction[0]) === ClassLoader::class;
4949
}
5050

51+
/**
52+
* Index of the given class loader in the queue, or null when it is not registered any more -
53+
* a bootstrap file has then taken Composer's place and there is no boundary to split on.
54+
*
55+
* @param list<mixed> $autoloadFunctions
56+
*/
57+
function findClassLoaderIndex(array $autoloadFunctions, ?int $classLoaderId): ?int // phpcs:ignore Squiz.Functions.GlobalFunction.Found
58+
{
59+
if ($classLoaderId === null) {
60+
return null;
61+
}
62+
63+
foreach ($autoloadFunctions as $index => $autoloadFunction) {
64+
if (!isComposerClassLoader($autoloadFunction)) {
65+
continue;
66+
}
67+
68+
if (spl_object_id($autoloadFunction[0]) === $classLoaderId) {
69+
return $index;
70+
}
71+
}
72+
73+
return null;
74+
}
75+
5176
/**
5277
* Splits the autoload functions registered while loading Composer's autoloader
5378
* and the bootstrap files into those registered before and after Composer's own
@@ -83,23 +108,17 @@ function collectNewAutoloadFunctions($autoloadFunctionsBefore, $autoloadFunction
83108
// it below the static source locators.
84109
$classLoaderIds = [];
85110
foreach ($autoloadFunctionsBefore as $before) {
86-
if (isComposerClassLoader($before)) {
87-
$classLoaderIds[] = spl_object_id($before[0]);
111+
if (!isComposerClassLoader($before)) {
112+
continue;
88113
}
114+
115+
$classLoaderIds[] = spl_object_id($before[0]);
89116
}
90117

91118
array_shift($classLoaderIds);
92119
$projectLoaderId = $classLoaderIds === [] ? null : $classLoaderIds[count($classLoaderIds) - 1];
93120

94-
$composerIndex = null;
95-
if ($projectLoaderId !== null) {
96-
foreach ($autoloadFunctionsAfter as $index => $after) {
97-
if (isComposerClassLoader($after) && spl_object_id($after[0]) === $projectLoaderId) {
98-
$composerIndex = $index;
99-
break;
100-
}
101-
}
102-
}
121+
$composerIndex = findClassLoaderIndex($autoloadFunctionsAfter, $projectLoaderId);
103122

104123
foreach ($autoloadFunctionsAfter as $index => $after) {
105124
if (is_array($after) && count($after) > 0) {

0 commit comments

Comments
 (0)