Skip to content

Commit 36a9987

Browse files
committed
improved class reflection performance
1 parent 92d36bc commit 36a9987

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

packages/core/src/Kernel/LoadDiscoveryClasses.php

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,16 @@ private function scan(DiscoveryLocation $location, array $discoveries, string $p
158158
}
159159
} elseif (class_exists($className)) {
160160
$input = new ClassReflector($className);
161+
162+
$skipDiscovery = $input->getAttribute(SkipDiscovery::class);
163+
164+
if ($skipDiscovery !== null && $skipDiscovery->except === []) {
165+
$this->shouldSkipForClass[$className] = true;
166+
} elseif ($skipDiscovery !== null) {
167+
foreach ($skipDiscovery->except as $except) {
168+
$this->shouldSkipForClass[$className][$except] = true;
169+
}
170+
}
161171
}
162172

163173
// Check skipping once again, because at this point we might have converted our path to a class
@@ -229,18 +239,23 @@ private function shouldSkipBasedOnConfig(ClassReflector|string $input): bool
229239
*/
230240
private function shouldSkipDiscoveryForClass(Discovery $discovery, ClassReflector $input): bool
231241
{
242+
// There's no `#[SkipDiscovery]` attribute, so the class shouldn't be skipped
232243
if (! isset($this->shouldSkipForClass[$input->getName()])) {
233-
$attribute = $input->getAttribute(SkipDiscovery::class);
234-
235-
if ($attribute === null) {
236-
return false;
237-
}
244+
return false;
245+
}
238246

239-
$this->shouldSkipForClass[$input->getName()] = $attribute->except;
247+
// The class has a general `#[SkipDiscovery]` attribute without exceptions
248+
if ($this->shouldSkipForClass[$input->getName()] === true) {
249+
return true;
250+
}
240251

252+
// Current discovery is not added as "except", so it should be skipped
253+
if (! isset($this->shouldSkipForClass[$input->getName()][$discovery::class])) {
254+
return true;
241255
}
242256

243-
return ! in_array($discovery::class, $this->shouldSkipForClass[$input->getName()], strict: true);
257+
// Current discovery was present in the except array, so it shouldn't be skipped
258+
return false;
244259
}
245260

246261
/**

0 commit comments

Comments
 (0)