Skip to content

Commit

Permalink
Merge pull request #1416 from staabm/no-auto
Browse files Browse the repository at this point in the history
Prevent autoloading when determinig constant values
  • Loading branch information
Ocramius authored Mar 27, 2024
2 parents 0b5ec6e + b9e11a2 commit 21f41bb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/SourceLocator/SourceStubber/PhpStormStubs/CachingVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
{
Expand Down

0 comments on commit 21f41bb

Please sign in to comment.