ProxyHelper.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\VarExporter;
  11. use Symfony\Component\VarExporter\Exception\LogicException;
  12. use Symfony\Component\VarExporter\Internal\Hydrator;
  13. use Symfony\Component\VarExporter\Internal\LazyObjectRegistry;
  14. /**
  15. * @author Nicolas Grekas <p@tchwork.com>
  16. */
  17. final class ProxyHelper
  18. {
  19. /**
  20. * Helps generate lazy-loading ghost objects.
  21. *
  22. * @throws LogicException When the class is incompatible with ghost objects
  23. */
  24. public static function generateLazyGhost(\ReflectionClass $class): string
  25. {
  26. if (\PHP_VERSION_ID < 80300 && $class->isReadOnly()) {
  27. throw new LogicException(\sprintf('Cannot generate lazy ghost with PHP < 8.3: class "%s" is readonly.', $class->name));
  28. }
  29. if ($class->isFinal()) {
  30. throw new LogicException(\sprintf('Cannot generate lazy ghost: class "%s" is final.', $class->name));
  31. }
  32. if ($class->isInterface() || $class->isAbstract() || $class->isTrait()) {
  33. throw new LogicException(\sprintf('Cannot generate lazy ghost: "%s" is not a concrete class.', $class->name));
  34. }
  35. if (\stdClass::class !== $class->name && $class->isInternal()) {
  36. throw new LogicException(\sprintf('Cannot generate lazy ghost: class "%s" is internal.', $class->name));
  37. }
  38. if ($class->hasMethod('__get') && 'mixed' !== (self::exportType($class->getMethod('__get')) ?? 'mixed')) {
  39. throw new LogicException(\sprintf('Cannot generate lazy ghost: return type of method "%s::__get()" should be "mixed".', $class->name));
  40. }
  41. static $traitMethods;
  42. $traitMethods ??= (new \ReflectionClass(LazyGhostTrait::class))->getMethods();
  43. foreach ($traitMethods as $method) {
  44. if ($class->hasMethod($method->name) && $class->getMethod($method->name)->isFinal()) {
  45. throw new LogicException(\sprintf('Cannot generate lazy ghost: method "%s::%s()" is final.', $class->name, $method->name));
  46. }
  47. }
  48. $parent = $class;
  49. while ($parent = $parent->getParentClass()) {
  50. if (\stdClass::class !== $parent->name && $parent->isInternal()) {
  51. throw new LogicException(\sprintf('Cannot generate lazy ghost: class "%s" extends "%s" which is internal.', $class->name, $parent->name));
  52. }
  53. }
  54. $hooks = '';
  55. $propertyScopes = Hydrator::$propertyScopes[$class->name] ??= Hydrator::getPropertyScopes($class->name);
  56. foreach ($propertyScopes as $key => [$scope, $name, , $access]) {
  57. $propertyScopes[$k = "\0$scope\0$name"] ?? $propertyScopes[$k = "\0*\0$name"] ?? $k = $name;
  58. $flags = $access >> 2;
  59. if ($k !== $key || !($access & Hydrator::PROPERTY_HAS_HOOKS) || $flags & \ReflectionProperty::IS_VIRTUAL) {
  60. continue;
  61. }
  62. if ($flags & (\ReflectionProperty::IS_FINAL | \ReflectionProperty::IS_PRIVATE)) {
  63. throw new LogicException(sprintf('Cannot generate lazy ghost: property "%s::$%s" is final or private(set).', $class->name, $name));
  64. }
  65. $p = $propertyScopes[$k][4] ?? Hydrator::$propertyScopes[$class->name][$k][4] = new \ReflectionProperty($scope, $name);
  66. $type = self::exportType($p);
  67. $hooks .= "\n "
  68. .($p->isProtected() ? 'protected' : 'public')
  69. .($p->isProtectedSet() ? ' protected(set)' : '')
  70. ." {$type} \${$name}"
  71. .($p->hasDefaultValue() ? ' = '.$p->getDefaultValue() : '')
  72. ." {\n";
  73. foreach ($p->getHooks() as $hook => $method) {
  74. if ('get' === $hook) {
  75. $ref = ($method->returnsReference() ? '&' : '');
  76. $hooks .= " {$ref}get { \$this->initializeLazyObject(); return parent::\${$name}::get(); }\n";
  77. } elseif ('set' === $hook) {
  78. $parameters = self::exportParameters($method, true);
  79. $arg = '$'.$method->getParameters()[0]->name;
  80. $hooks .= " set({$parameters}) { \$this->initializeLazyObject(); parent::\${$name}::set({$arg}); }\n";
  81. } else {
  82. throw new LogicException(sprintf('Cannot generate lazy ghost: hook "%s::%s()" is not supported.', $class->name, $method->name));
  83. }
  84. }
  85. $hooks .= " }\n";
  86. }
  87. $propertyScopes = self::exportPropertyScopes($class->name, $propertyScopes);
  88. return <<<EOPHP
  89. extends \\{$class->name} implements \Symfony\Component\VarExporter\LazyObjectInterface
  90. {
  91. use \Symfony\Component\VarExporter\LazyGhostTrait;
  92. private const LAZY_OBJECT_PROPERTY_SCOPES = {$propertyScopes};
  93. {$hooks}}
  94. // Help opcache.preload discover always-needed symbols
  95. class_exists(\Symfony\Component\VarExporter\Internal\Hydrator::class);
  96. class_exists(\Symfony\Component\VarExporter\Internal\LazyObjectRegistry::class);
  97. class_exists(\Symfony\Component\VarExporter\Internal\LazyObjectState::class);
  98. EOPHP;
  99. }
  100. /**
  101. * Helps generate lazy-loading virtual proxies.
  102. *
  103. * @param \ReflectionClass[] $interfaces
  104. *
  105. * @throws LogicException When the class is incompatible with virtual proxies
  106. */
  107. public static function generateLazyProxy(?\ReflectionClass $class, array $interfaces = []): string
  108. {
  109. if (!class_exists($class?->name ?? \stdClass::class, false)) {
  110. throw new LogicException(\sprintf('Cannot generate lazy proxy: "%s" is not a class.', $class->name));
  111. }
  112. if ($class?->isFinal()) {
  113. throw new LogicException(\sprintf('Cannot generate lazy proxy: class "%s" is final.', $class->name));
  114. }
  115. if (\PHP_VERSION_ID < 80300 && $class?->isReadOnly()) {
  116. throw new LogicException(\sprintf('Cannot generate lazy proxy with PHP < 8.3: class "%s" is readonly.', $class->name));
  117. }
  118. $propertyScopes = $class ? Hydrator::$propertyScopes[$class->name] ??= Hydrator::getPropertyScopes($class->name) : [];
  119. $abstractProperties = [];
  120. $hookedProperties = [];
  121. if (\PHP_VERSION_ID >= 80400 && $class) {
  122. foreach ($propertyScopes as $key => [$scope, $name, , $access]) {
  123. $propertyScopes[$k = "\0$scope\0$name"] ?? $propertyScopes[$k = "\0*\0$name"] ?? $k = $name;
  124. $flags = $access >> 2;
  125. if ($k !== $key) {
  126. continue;
  127. }
  128. if ($flags & \ReflectionProperty::IS_ABSTRACT) {
  129. $abstractProperties[$name] = $propertyScopes[$k][4] ?? Hydrator::$propertyScopes[$class->name][$k][4] = new \ReflectionProperty($scope, $name);
  130. continue;
  131. }
  132. $abstractProperties[$name] = false;
  133. if (!($access & Hydrator::PROPERTY_HAS_HOOKS) || $flags & \ReflectionProperty::IS_VIRTUAL) {
  134. continue;
  135. }
  136. if ($flags & (\ReflectionProperty::IS_FINAL | \ReflectionProperty::IS_PRIVATE)) {
  137. throw new LogicException(sprintf('Cannot generate lazy proxy: property "%s::$%s" is final or private(set).', $class->name, $name));
  138. }
  139. $p = $propertyScopes[$k][4] ?? Hydrator::$propertyScopes[$class->name][$k][4] = new \ReflectionProperty($scope, $name);
  140. $hookedProperties[$name] = [$p, $p->getHooks()];
  141. }
  142. }
  143. $methodReflectors = [$class?->getMethods(\ReflectionMethod::IS_PUBLIC | \ReflectionMethod::IS_PROTECTED) ?? []];
  144. foreach ($interfaces as $interface) {
  145. if (!$interface->isInterface()) {
  146. throw new LogicException(\sprintf('Cannot generate lazy proxy: "%s" is not an interface.', $interface->name));
  147. }
  148. $methodReflectors[] = $interface->getMethods();
  149. if (\PHP_VERSION_ID >= 80400) {
  150. foreach ($interface->getProperties() as $p) {
  151. $abstractProperties[$p->name] ??= $p;
  152. $hookedProperties[$p->name] ??= [$p, []];
  153. $hookedProperties[$p->name][1] += $p->getHooks();
  154. }
  155. }
  156. }
  157. $hooks = '';
  158. foreach (array_filter($abstractProperties) as $name => $p) {
  159. $type = self::exportType($p);
  160. $hooks .= "\n "
  161. .($p->isProtected() ? 'protected' : 'public')
  162. .($p->isProtectedSet() ? ' protected(set)' : '')
  163. ." {$type} \${$name};\n";
  164. }
  165. foreach ($hookedProperties as $name => [$p, $methods]) {
  166. $type = self::exportType($p);
  167. $hooks .= "\n "
  168. .($p->isProtected() ? 'protected' : 'public')
  169. .($p->isProtectedSet() ? ' protected(set)' : '')
  170. ." {$type} \${$name} {\n";
  171. foreach ($methods as $hook => $method) {
  172. if ('get' === $hook) {
  173. $ref = ($method->returnsReference() ? '&' : '');
  174. $hooks .= <<<EOPHP
  175. {$ref}get {
  176. if (isset(\$this->lazyObjectState)) {
  177. return (\$this->lazyObjectState->realInstance ??= (\$this->lazyObjectState->initializer)())->{$p->name};
  178. }
  179. return parent::\${$p->name}::get();
  180. }
  181. EOPHP;
  182. } elseif ('set' === $hook) {
  183. $parameters = self::exportParameters($method, true);
  184. $arg = '$'.$method->getParameters()[0]->name;
  185. $hooks .= <<<EOPHP
  186. set({$parameters}) {
  187. if (isset(\$this->lazyObjectState)) {
  188. \$this->lazyObjectState->realInstance ??= (\$this->lazyObjectState->initializer)();
  189. \$this->lazyObjectState->realInstance->{$p->name} = {$arg};
  190. }
  191. parent::\${$p->name}::set({$arg});
  192. }
  193. EOPHP;
  194. } else {
  195. throw new LogicException(sprintf('Cannot generate lazy proxy: hook "%s::%s()" is not supported.', $class->name, $method->name));
  196. }
  197. }
  198. $hooks .= " }\n";
  199. }
  200. $extendsInternalClass = false;
  201. if ($parent = $class) {
  202. do {
  203. $extendsInternalClass = \stdClass::class !== $parent->name && $parent->isInternal();
  204. } while (!$extendsInternalClass && $parent = $parent->getParentClass());
  205. }
  206. $methodsHaveToBeProxied = $extendsInternalClass;
  207. $methods = [];
  208. $methodReflectors = array_merge(...$methodReflectors);
  209. foreach ($methodReflectors as $method) {
  210. if ('__get' !== strtolower($method->name) || 'mixed' === ($type = self::exportType($method) ?? 'mixed')) {
  211. continue;
  212. }
  213. $methodsHaveToBeProxied = true;
  214. $trait = new \ReflectionMethod(LazyProxyTrait::class, '__get');
  215. $body = \array_slice(file($trait->getFileName()), $trait->getStartLine() - 1, $trait->getEndLine() - $trait->getStartLine());
  216. $body[0] = str_replace('): mixed', '): '.$type, $body[0]);
  217. $methods['__get'] = strtr(implode('', $body).' }', [
  218. 'Hydrator' => '\\'.Hydrator::class,
  219. 'Registry' => '\\'.LazyObjectRegistry::class,
  220. ]);
  221. break;
  222. }
  223. foreach ($methodReflectors as $method) {
  224. if (($method->isStatic() && !$method->isAbstract()) || isset($methods[$lcName = strtolower($method->name)])) {
  225. continue;
  226. }
  227. if ($method->isFinal()) {
  228. if ($extendsInternalClass || $methodsHaveToBeProxied || method_exists(LazyProxyTrait::class, $method->name)) {
  229. throw new LogicException(\sprintf('Cannot generate lazy proxy: method "%s::%s()" is final.', $class->name, $method->name));
  230. }
  231. continue;
  232. }
  233. if (method_exists(LazyProxyTrait::class, $method->name) || ($method->isProtected() && !$method->isAbstract())) {
  234. continue;
  235. }
  236. $signature = self::exportSignature($method, true, $args);
  237. $parentCall = $method->isAbstract() ? "throw new \BadMethodCallException('Cannot forward abstract method \"{$method->class}::{$method->name}()\".')" : "parent::{$method->name}({$args})";
  238. if ($method->isStatic()) {
  239. $body = " $parentCall;";
  240. } elseif (str_ends_with($signature, '): never') || str_ends_with($signature, '): void')) {
  241. $body = <<<EOPHP
  242. if (isset(\$this->lazyObjectState)) {
  243. (\$this->lazyObjectState->realInstance ??= (\$this->lazyObjectState->initializer)())->{$method->name}({$args});
  244. } else {
  245. {$parentCall};
  246. }
  247. EOPHP;
  248. } else {
  249. if (!$methodsHaveToBeProxied && !$method->isAbstract()) {
  250. // Skip proxying methods that might return $this
  251. foreach (preg_split('/[()|&]++/', self::exportType($method) ?? 'static') as $type) {
  252. if (\in_array($type = ltrim($type, '?'), ['static', 'object'], true)) {
  253. continue 2;
  254. }
  255. foreach ([$class, ...$interfaces] as $r) {
  256. if ($r && is_a($r->name, $type, true)) {
  257. continue 3;
  258. }
  259. }
  260. }
  261. }
  262. $body = <<<EOPHP
  263. if (isset(\$this->lazyObjectState)) {
  264. return (\$this->lazyObjectState->realInstance ??= (\$this->lazyObjectState->initializer)())->{$method->name}({$args});
  265. }
  266. return {$parentCall};
  267. EOPHP;
  268. }
  269. $methods[$lcName] = " {$signature}\n {\n{$body}\n }";
  270. }
  271. $types = $interfaces = array_unique(array_column($interfaces, 'name'));
  272. $interfaces[] = LazyObjectInterface::class;
  273. $interfaces = implode(', \\', $interfaces);
  274. $parent = $class ? ' extends \\'.$class->name : '';
  275. array_unshift($types, $class ? 'parent' : '');
  276. $type = ltrim(implode('&\\', $types), '&');
  277. if (!$class) {
  278. $trait = new \ReflectionMethod(LazyProxyTrait::class, 'initializeLazyObject');
  279. $body = \array_slice(file($trait->getFileName()), $trait->getStartLine() - 1, $trait->getEndLine() - $trait->getStartLine());
  280. $body[0] = str_replace('): parent', '): '.$type, $body[0]);
  281. $methods = ['initializeLazyObject' => implode('', $body).' }'] + $methods;
  282. }
  283. $body = $methods ? "\n".implode("\n\n", $methods)."\n" : '';
  284. $propertyScopes = $class ? self::exportPropertyScopes($class->name, $propertyScopes) : '[]';
  285. if (
  286. $class?->hasMethod('__unserialize')
  287. && !$class->getMethod('__unserialize')->getParameters()[0]->getType()
  288. ) {
  289. // fix contravariance type problem when $class declares a `__unserialize()` method without typehint.
  290. $lazyProxyTraitStatement = <<<EOPHP
  291. use \Symfony\Component\VarExporter\LazyProxyTrait {
  292. __unserialize as private __doUnserialize;
  293. }
  294. EOPHP;
  295. $body .= <<<EOPHP
  296. public function __unserialize(\$data): void
  297. {
  298. \$this->__doUnserialize(\$data);
  299. }
  300. EOPHP;
  301. } else {
  302. $lazyProxyTraitStatement = <<<EOPHP
  303. use \Symfony\Component\VarExporter\LazyProxyTrait;
  304. EOPHP;
  305. }
  306. return <<<EOPHP
  307. {$parent} implements \\{$interfaces}
  308. {
  309. {$lazyProxyTraitStatement}
  310. private const LAZY_OBJECT_PROPERTY_SCOPES = {$propertyScopes};
  311. {$hooks}{$body}}
  312. // Help opcache.preload discover always-needed symbols
  313. class_exists(\Symfony\Component\VarExporter\Internal\Hydrator::class);
  314. class_exists(\Symfony\Component\VarExporter\Internal\LazyObjectRegistry::class);
  315. class_exists(\Symfony\Component\VarExporter\Internal\LazyObjectState::class);
  316. EOPHP;
  317. }
  318. public static function exportParameters(\ReflectionFunctionAbstract $function, bool $withParameterTypes = true, ?string &$args = null): string
  319. {
  320. $byRefIndex = 0;
  321. $args = '';
  322. $param = null;
  323. $parameters = [];
  324. $namespace = $function instanceof \ReflectionMethod ? $function->class : $function->getNamespaceName().'\\';
  325. $namespace = substr($namespace, 0, strrpos($namespace, '\\') ?: 0);
  326. foreach ($function->getParameters() as $param) {
  327. $parameters[] = ($param->getAttributes(\SensitiveParameter::class) ? '#[\SensitiveParameter] ' : '')
  328. .($withParameterTypes && $param->hasType() ? self::exportType($param).' ' : '')
  329. .($param->isPassedByReference() ? '&' : '')
  330. .($param->isVariadic() ? '...' : '').'$'.$param->name
  331. .($param->isOptional() && !$param->isVariadic() ? ' = '.self::exportDefault($param, $namespace) : '');
  332. if ($param->isPassedByReference()) {
  333. $byRefIndex = 1 + $param->getPosition();
  334. }
  335. $args .= ($param->isVariadic() ? '...$' : '$').$param->name.', ';
  336. }
  337. if (!$param || !$byRefIndex) {
  338. $args = '...\func_get_args()';
  339. } elseif ($param->isVariadic()) {
  340. $args = substr($args, 0, -2);
  341. } else {
  342. $args = explode(', ', $args, 1 + $byRefIndex);
  343. $args[$byRefIndex] = \sprintf('...\array_slice(\func_get_args(), %d)', $byRefIndex);
  344. $args = implode(', ', $args);
  345. }
  346. return implode(', ', $parameters);
  347. }
  348. public static function exportSignature(\ReflectionFunctionAbstract $function, bool $withParameterTypes = true, ?string &$args = null): string
  349. {
  350. $parameters = self::exportParameters($function, $withParameterTypes, $args);
  351. $signature = 'function '.($function->returnsReference() ? '&' : '')
  352. .($function->isClosure() ? '' : $function->name).'('.$parameters.')';
  353. if ($function instanceof \ReflectionMethod) {
  354. $signature = ($function->isPublic() ? 'public ' : ($function->isProtected() ? 'protected ' : 'private '))
  355. .($function->isStatic() ? 'static ' : '').$signature;
  356. }
  357. if ($function->hasReturnType()) {
  358. $signature .= ': '.self::exportType($function);
  359. }
  360. static $getPrototype;
  361. $getPrototype ??= (new \ReflectionMethod(\ReflectionMethod::class, 'getPrototype'))->invoke(...);
  362. while ($function) {
  363. if ($function->hasTentativeReturnType()) {
  364. return '#[\ReturnTypeWillChange] '.$signature;
  365. }
  366. try {
  367. $function = $function instanceof \ReflectionMethod && $function->isAbstract() ? false : $getPrototype($function);
  368. } catch (\ReflectionException) {
  369. break;
  370. }
  371. }
  372. return $signature;
  373. }
  374. public static function exportType(\ReflectionFunctionAbstract|\ReflectionProperty|\ReflectionParameter $owner, bool $noBuiltin = false, ?\ReflectionType $type = null): ?string
  375. {
  376. if (!$type ??= $owner instanceof \ReflectionFunctionAbstract ? $owner->getReturnType() : $owner->getType()) {
  377. return null;
  378. }
  379. $class = null;
  380. $types = [];
  381. if ($type instanceof \ReflectionUnionType) {
  382. $reflectionTypes = $type->getTypes();
  383. $glue = '|';
  384. } elseif ($type instanceof \ReflectionIntersectionType) {
  385. $reflectionTypes = $type->getTypes();
  386. $glue = '&';
  387. } else {
  388. $reflectionTypes = [$type];
  389. $glue = null;
  390. }
  391. foreach ($reflectionTypes as $type) {
  392. if ($type instanceof \ReflectionIntersectionType) {
  393. if ('' !== $name = '('.self::exportType($owner, $noBuiltin, $type).')') {
  394. $types[] = $name;
  395. }
  396. continue;
  397. }
  398. $name = $type->getName();
  399. if ($noBuiltin && $type->isBuiltin()) {
  400. continue;
  401. }
  402. if (\in_array($name, ['parent', 'self'], true) && $class ??= $owner->getDeclaringClass()) {
  403. $name = 'parent' === $name ? ($class->getParentClass() ?: null)?->name ?? 'parent' : $class->name;
  404. }
  405. $types[] = ($noBuiltin || $type->isBuiltin() || 'static' === $name ? '' : '\\').$name;
  406. }
  407. if (!$types) {
  408. return '';
  409. }
  410. if (null === $glue) {
  411. return (!$noBuiltin && $type->allowsNull() && !\in_array($name, ['mixed', 'null'], true) ? '?' : '').$types[0];
  412. }
  413. sort($types);
  414. return implode($glue, $types);
  415. }
  416. private static function exportPropertyScopes(string $parent, array $propertyScopes): string
  417. {
  418. uksort($propertyScopes, 'strnatcmp');
  419. foreach ($propertyScopes as $k => $v) {
  420. unset($propertyScopes[$k][4]);
  421. }
  422. $propertyScopes = VarExporter::export($propertyScopes);
  423. $propertyScopes = str_replace(VarExporter::export($parent), 'parent::class', $propertyScopes);
  424. $propertyScopes = preg_replace("/(?|(,)\n( ) |\n |,\n (\]))/", '$1$2', $propertyScopes);
  425. return str_replace("\n", "\n ", $propertyScopes);
  426. }
  427. private static function exportDefault(\ReflectionParameter $param, $namespace): string
  428. {
  429. $default = rtrim(substr(explode('$'.$param->name.' = ', (string) $param, 2)[1] ?? '', 0, -2));
  430. if (\in_array($default, ['<default>', 'NULL'], true)) {
  431. return 'null';
  432. }
  433. if (str_ends_with($default, "...'") && preg_match("/^'(?:[^'\\\\]*+(?:\\\\.)*+)*+'$/", $default)) {
  434. return VarExporter::export($param->getDefaultValue());
  435. }
  436. $regexp = "/(\"(?:[^\"\\\\]*+(?:\\\\.)*+)*+\"|'(?:[^'\\\\]*+(?:\\\\.)*+)*+')/";
  437. $parts = preg_split($regexp, $default, -1, \PREG_SPLIT_DELIM_CAPTURE | \PREG_SPLIT_NO_EMPTY);
  438. $regexp = '/([\[\( ]|^)([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+(?:\\\\[a-zA-Z0-9_\x7f-\xff]++)*+)(\(?)(?!: )/';
  439. $callback = (false !== strpbrk($default, "\\:('") && $class = $param->getDeclaringClass())
  440. ? fn ($m) => $m[1].match ($m[2]) {
  441. 'new', 'false', 'true', 'null' => $m[2],
  442. 'NULL' => 'null',
  443. 'self' => '\\'.$class->name,
  444. 'namespace\\parent',
  445. 'parent' => ($parent = $class->getParentClass()) ? '\\'.$parent->name : 'parent',
  446. default => self::exportSymbol($m[2], '(' !== $m[3], $namespace),
  447. }.$m[3]
  448. : fn ($m) => $m[1].match ($m[2]) {
  449. 'new', 'false', 'true', 'null', 'self', 'parent' => $m[2],
  450. 'NULL' => 'null',
  451. default => self::exportSymbol($m[2], '(' !== $m[3], $namespace),
  452. }.$m[3];
  453. return implode('', array_map(fn ($part) => match ($part[0]) {
  454. '"' => $part, // for internal classes only
  455. "'" => false !== strpbrk($part, "\\\0\r\n") ? '"'.substr(str_replace(['$', "\0", "\r", "\n"], ['\$', '\0', '\r', '\n'], $part), 1, -1).'"' : $part,
  456. default => preg_replace_callback($regexp, $callback, $part),
  457. }, $parts));
  458. }
  459. private static function exportSymbol(string $symbol, bool $mightBeRootConst, string $namespace): string
  460. {
  461. if (!$mightBeRootConst
  462. || false === ($ns = strrpos($symbol, '\\'))
  463. || substr($symbol, 0, $ns) !== $namespace
  464. || \defined($symbol)
  465. || !\defined(substr($symbol, $ns + 1))
  466. ) {
  467. return '\\'.$symbol;
  468. }
  469. return '\\'.substr($symbol, $ns + 1);
  470. }
  471. }