@@ -219,12 +219,14 @@ public static function exportSignature(\ReflectionFunctionAbstract $function, bo
219
219
$ args = '' ;
220
220
$ param = null ;
221
221
$ parameters = [];
222
+ $ namespace = $ function instanceof \ReflectionMethod ? $ function ->class : $ function ->getNamespaceName ().'\\' ;
223
+ $ namespace = substr ($ namespace , 0 , strrpos ($ namespace , '\\' ) ?: 0 );
222
224
foreach ($ function ->getParameters () as $ param ) {
223
225
$ parameters [] = ($ param ->getAttributes (\SensitiveParameter::class) ? '#[\SensitiveParameter] ' : '' )
224
226
.($ withParameterTypes && $ param ->hasType () ? self ::exportType ($ param ).' ' : '' )
225
227
.($ param ->isPassedByReference () ? '& ' : '' )
226
228
.($ param ->isVariadic () ? '... ' : '' ).'$ ' .$ param ->name
227
- .($ param ->isOptional () && !$ param ->isVariadic () ? ' = ' .self ::exportDefault ($ param ) : '' );
229
+ .($ param ->isOptional () && !$ param ->isVariadic () ? ' = ' .self ::exportDefault ($ param, $ namespace ) : '' );
228
230
if ($ param ->isPassedByReference ()) {
229
231
$ byRefIndex = 1 + $ param ->getPosition ();
230
232
}
@@ -333,7 +335,7 @@ private static function exportPropertyScopes(string $parent): string
333
335
return $ propertyScopes ;
334
336
}
335
337
336
- private static function exportDefault (\ReflectionParameter $ param ): string
338
+ private static function exportDefault (\ReflectionParameter $ param, $ namespace ): string
337
339
{
338
340
$ default = rtrim (substr (explode ('$ ' .$ param ->name .' = ' , (string ) $ param , 2 )[1 ] ?? '' , 0 , -2 ));
339
341
@@ -347,26 +349,40 @@ private static function exportDefault(\ReflectionParameter $param): string
347
349
$ regexp = "/( \"(?:[^ \"\\\\]*+(?: \\\\.)*+)*+ \"|'(?:[^' \\\\]*+(?: \\\\.)*+)*+')/ " ;
348
350
$ parts = preg_split ($ regexp , $ default , -1 , \PREG_SPLIT_DELIM_CAPTURE | \PREG_SPLIT_NO_EMPTY );
349
351
350
- $ regexp = '/([\[\( ]|^)([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+(?: \\\\[a-zA-Z0-9_\x7f-\xff]++)*+)(?!: )/ ' ;
352
+ $ regexp = '/([\[\( ]|^)([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+(?: \\\\[a-zA-Z0-9_\x7f-\xff]++)*+)(\(?)( ?!: )/ ' ;
351
353
$ callback = (false !== strpbrk ($ default , "\\:(' " ) && $ class = $ param ->getDeclaringClass ())
352
354
? fn ($ m ) => $ m [1 ].match ($ m [2 ]) {
353
355
'new ' , 'false ' , 'true ' , 'null ' => $ m [2 ],
354
356
'NULL ' => 'null ' ,
355
357
'self ' => '\\' .$ class ->name ,
356
358
'namespace \\parent ' ,
357
359
'parent ' => ($ parent = $ class ->getParentClass ()) ? '\\' .$ parent ->name : 'parent ' ,
358
- default => '\\' . $ m [2 ] ,
359
- }
360
+ default => self :: exportSymbol ( $ m [ 2 ], ' ( ' !== $ m [3 ], $ namespace ) ,
361
+ }. $ m [ 3 ]
360
362
: fn ($ m ) => $ m [1 ].match ($ m [2 ]) {
361
363
'new ' , 'false ' , 'true ' , 'null ' , 'self ' , 'parent ' => $ m [2 ],
362
364
'NULL ' => 'null ' ,
363
- default => '\\' . $ m [2 ] ,
364
- };
365
+ default => self :: exportSymbol ( $ m [ 2 ], ' ( ' !== $ m [3 ], $ namespace ) ,
366
+ }. $ m [ 3 ] ;
365
367
366
368
return implode ('' , array_map (fn ($ part ) => match ($ part [0 ]) {
367
369
'" ' => $ part , // for internal classes only
368
370
"' " => false !== strpbrk ($ part , "\\\0\r\n" ) ? '" ' .substr (str_replace (['$ ' , "\0" , "\r" , "\n" ], ['\$ ' , '\0 ' , '\r ' , '\n ' ], $ part ), 1 , -1 ).'" ' : $ part ,
369
371
default => preg_replace_callback ($ regexp , $ callback , $ part ),
370
372
}, $ parts ));
371
373
}
374
+
375
+ private static function exportSymbol (string $ symbol , bool $ mightBeRootConst , string $ namespace ): string
376
+ {
377
+ if (!$ mightBeRootConst
378
+ || false === ($ ns = strrpos ($ symbol , '\\' ))
379
+ || substr ($ symbol , 0 , $ ns ) !== $ namespace
380
+ || \defined ($ symbol )
381
+ || !\defined (substr ($ symbol , $ ns + 1 ))
382
+ ) {
383
+ return '\\' .$ symbol ;
384
+ }
385
+
386
+ return '\\' .substr ($ symbol , $ ns + 1 );
387
+ }
372
388
}
0 commit comments