File tree 2 files changed +27
-0
lines changed
src/SourceLocator/SourceStubber/PhpStormStubs
test/unit/SourceLocator/SourceStubber
2 files changed +27
-0
lines changed Original file line number Diff line number Diff line change 13
13
14
14
use function array_key_exists ;
15
15
use function assert ;
16
+ use function class_exists ;
16
17
use function constant ;
18
+ use function count ;
17
19
use function defined ;
20
+ use function explode ;
18
21
use function in_array ;
19
22
use function is_resource ;
20
23
use function sprintf ;
@@ -195,6 +198,16 @@ public function clearNodes(): void
195
198
*/
196
199
private function updateConstantValue (Node \Expr \FuncCall |Node \Const_ $ node , string $ constantName ): void
197
200
{
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
+
198
211
if (! defined ($ constantName )) {
199
212
return ;
200
213
}
Original file line number Diff line number Diff line change 62
62
use function get_defined_functions ;
63
63
use function in_array ;
64
64
use function sort ;
65
+ use function spl_autoload_register ;
65
66
use function sprintf ;
66
67
67
68
use const PHP_VERSION_ID ;
@@ -626,6 +627,19 @@ public function testCaseSensitiveConstantSearchOptimization(): void
626
627
self ::assertNull ($ this ->sourceStubber ->generateConstantStub ('date_atom ' ));
627
628
}
628
629
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
+
629
643
#[RunInSeparateProcess]
630
644
public function testUpdateConstantValue (): void
631
645
{
You can’t perform that action at this time.
0 commit comments