Skip to content

Commit 2e32a09

Browse files
committed
Add gen_stub --legacy flag
This flag generates an arginfo file that discards all type and default value information. As such, it is compatible with old (and very old) PHP versions. Intended for use by extensions to generate one arginfo file for PHP 8 and one for older versions.
1 parent c137f4b commit 2e32a09

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

build/gen_stub.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ function processStubFile(string $stubFile, Context $context) {
3131
throw new Exception("File $stubFile does not exist");
3232
}
3333

34-
$arginfoFile = str_replace('.stub.php', '', $stubFile) . '_arginfo.h';
34+
$arginfoFile = str_replace('.stub.php', '', $stubFile)
35+
. ($context->legacy ? '_legacy' : '') . '_arginfo.h';
3536
$stubCode = file_get_contents($stubFile);
3637
$stubHash = computeStubHash($stubCode);
3738
$oldStubHash = extractStubHash($arginfoFile);
@@ -42,6 +43,12 @@ function processStubFile(string $stubFile, Context $context) {
4243

4344
initPhpParser();
4445
$fileInfo = parseStubFile($stubCode);
46+
if ($context->legacy) {
47+
foreach ($fileInfo->getAllFuncInfos() as $funcInfo) {
48+
$funcInfo->discardInfoForOldPhpVersions();
49+
}
50+
}
51+
4552
$arginfoCode = generateArgInfoCode($fileInfo, $stubHash);
4653
file_put_contents($arginfoFile, $arginfoCode);
4754

@@ -534,6 +541,14 @@ private function getFlagsAsString(): string
534541

535542
return $flags;
536543
}
544+
545+
public function discardInfoForOldPhpVersions(): void {
546+
$this->return->type = null;
547+
foreach ($this->args as $arg) {
548+
$arg->type = null;
549+
$arg->defaultValue = null;
550+
}
551+
}
537552
}
538553

539554
class ClassInfo {
@@ -1148,10 +1163,11 @@ function initPhpParser() {
11481163
}
11491164

11501165
$optind = null;
1151-
$options = getopt("f", ["force-regeneration", "parameter-stats"], $optind);
1166+
$options = getopt("f", ["force-regeneration", "parameter-stats", "legacy"], $optind);
11521167

11531168
$context = new Context;
11541169
$printParameterStats = isset($options["parameter-stats"]);
1170+
$context->legacy = isset($options["legacy"]);
11551171
$context->forceRegeneration =
11561172
isset($options["f"]) || isset($options["force-regeneration"]) || $printParameterStats;
11571173

0 commit comments

Comments
 (0)