mirror of
https://github.com/barryvdh/laravel-ide-helper.git
synced 2026-08-17 17:47:13 +00:00
fix: skip autoload exception when class existence is being checked (#1764)
The MetaCommand's custom autoloader throws a ReflectionException for any class not found during meta generation. However, this breaks libraries that use class_exists() to check for optional dependencies before loading them (e.g. Doctrine ORM checking for StaticReflectionService removed in doctrine/persistence v4). This fix checks the call stack (up to 3 frames) for class_exists(), interface_exists(), trait_exists(), or enum_exists() calls and returns gracefully instead of throwing, allowing the existence check to return false as expected. Fixes #1750
This commit is contained in:
@@ -195,6 +195,18 @@ class MetaCommand extends Command
|
||||
return;
|
||||
}
|
||||
|
||||
// Don't throw when class existence is being checked via class_exists(),
|
||||
// interface_exists(), trait_exists(), or enum_exists(). These functions
|
||||
// expect the autoloader to return gracefully when the class doesn't exist.
|
||||
// Throwing here would break libraries that use class_exists() to check for
|
||||
// optional dependencies (e.g. Doctrine ORM checking for removed classes).
|
||||
$existsFunctions = ['class_exists', 'interface_exists', 'trait_exists', 'enum_exists'];
|
||||
foreach (debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 3) as $frame) {
|
||||
if (isset($frame['function']) && in_array($frame['function'], $existsFunctions, true)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
throw new \ReflectionException("Class '$class' not found.");
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user