diff --git a/src/SourceLocator/SourceStubber/PhpStormStubs/CachingVisitor.php b/src/SourceLocator/SourceStubber/PhpStormStubs/CachingVisitor.php index 4cbe3b094..ef9a2d8a2 100644 --- a/src/SourceLocator/SourceStubber/PhpStormStubs/CachingVisitor.php +++ b/src/SourceLocator/SourceStubber/PhpStormStubs/CachingVisitor.php @@ -13,8 +13,11 @@ use function array_key_exists; use function assert; +use function class_exists; use function constant; +use function count; use function defined; +use function explode; use function in_array; use function is_resource; use function sprintf; @@ -195,6 +198,16 @@ public function clearNodes(): void */ private function updateConstantValue(Node\Expr\FuncCall|Node\Const_ $node, string $constantName): void { + // prevent autoloading while discovering class constants + $parts = explode('::', $constantName, 2); + if (count($parts) === 2) { + [$className, $classConstName] = $parts; + + if (! class_exists($className, false)) { + return; + } + } + if (! defined($constantName)) { return; } diff --git a/test/unit/SourceLocator/SourceStubber/PhpStormStubsSourceStubberTest.php b/test/unit/SourceLocator/SourceStubber/PhpStormStubsSourceStubberTest.php index 68498b083..37ae5fb39 100644 --- a/test/unit/SourceLocator/SourceStubber/PhpStormStubsSourceStubberTest.php +++ b/test/unit/SourceLocator/SourceStubber/PhpStormStubsSourceStubberTest.php @@ -62,6 +62,7 @@ use function get_defined_functions; use function in_array; use function sort; +use function spl_autoload_register; use function sprintf; use const PHP_VERSION_ID; @@ -626,6 +627,19 @@ public function testCaseSensitiveConstantSearchOptimization(): void self::assertNull($this->sourceStubber->generateConstantStub('date_atom')); } + #[RunInSeparateProcess] + public function testUpdateConstantValueDoesNotTriggerAutoload(): void + { + spl_autoload_register(static function (string $className): void { + self::fail('Parsing php-src constant should not trigger userland autoloading'); + }); + + $sourceStubber = new PhpStormStubsSourceStubber(BetterReflectionSingleton::instance()->phpParser()); + $constConstantStub = $sourceStubber->generateConstantStub('JSON_PRETTY_PRINT'); + self::assertNotNull($constConstantStub); + self::assertStringContainsString("define('JSON_PRETTY_PRINT',", $constConstantStub->getStub()); + } + #[RunInSeparateProcess] public function testUpdateConstantValue(): void {