File tree Expand file tree Collapse file tree 2 files changed +27
-0
lines changed
src/SourceLocator/SourceStubber/PhpStormStubs
test/unit/SourceLocator/SourceStubber Expand file tree Collapse file tree 2 files changed +27
-0
lines changed Original file line number Diff line number Diff line change 1313
1414use function array_key_exists ;
1515use function assert ;
16+ use function class_exists ;
1617use function constant ;
18+ use function count ;
1719use function defined ;
20+ use function explode ;
1821use function in_array ;
1922use function is_resource ;
2023use function sprintf ;
@@ -195,6 +198,16 @@ public function clearNodes(): void
195198 */
196199 private function updateConstantValue (Node \Expr \FuncCall |Node \Const_ $ node , string $ constantName ): void
197200 {
201+ // prevent autoloading while discovering class constants
202+ $ parts = explode (':: ' , $ constantName , 2 );
203+ if (count ($ parts ) === 2 ) {
204+ [$ className , $ classConstName ] = $ parts ;
205+
206+ if (! class_exists ($ className , false )) {
207+ return ;
208+ }
209+ }
210+
198211 if (! defined ($ constantName )) {
199212 return ;
200213 }
Original file line number Diff line number Diff line change 6262use function get_defined_functions ;
6363use function in_array ;
6464use function sort ;
65+ use function spl_autoload_register ;
6566use function sprintf ;
6667
6768use const PHP_VERSION_ID ;
@@ -626,6 +627,19 @@ public function testCaseSensitiveConstantSearchOptimization(): void
626627 self ::assertNull ($ this ->sourceStubber ->generateConstantStub ('date_atom ' ));
627628 }
628629
630+ #[RunInSeparateProcess]
631+ public function testUpdateConstantValueDoesNotTriggerAutoload (): void
632+ {
633+ spl_autoload_register (static function (string $ className ): void {
634+ self ::fail ('Parsing php-src constant should not trigger userland autoloading ' );
635+ });
636+
637+ $ sourceStubber = new PhpStormStubsSourceStubber (BetterReflectionSingleton::instance ()->phpParser ());
638+ $ constConstantStub = $ sourceStubber ->generateConstantStub ('JSON_PRETTY_PRINT ' );
639+ self ::assertNotNull ($ constConstantStub );
640+ self ::assertStringContainsString ("define('JSON_PRETTY_PRINT', " , $ constConstantStub ->getStub ());
641+ }
642+
629643 #[RunInSeparateProcess]
630644 public function testUpdateConstantValue (): void
631645 {
You can’t perform that action at this time.
0 commit comments