Skip to content

Commit e0ef34c

Browse files
gen_stub: add maximum option to generateVersionDependentFlagCode()
In preparation for moving this logic to a dedicated class, add support for a maximum version of PHP in the generation of version-dependent flags. This replaces the manual logic in `FuncInfo::getFunctionEntry()` to split up the flags that are used when PHP 8.4 is not supported.
1 parent 0a2f367 commit e0ef34c

File tree

1 file changed

+25
-12
lines changed

1 file changed

+25
-12
lines changed

build/gen_stub.php

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1524,24 +1524,23 @@ public function getFunctionEntry(): string {
15241524
$code .= "#if (PHP_VERSION_ID >= " . PHP_84_VERSION_ID . ")\n";
15251525
}
15261526

1527-
$php84AndAboveFlags = array_slice($flagsByPhpVersions, 5, null, true);
15281527
$docComment = $this->exposedDocComment ? '"' . $this->exposedDocComment->escape() . '"' : "NULL";
15291528
$framelessFuncInfosName = !empty($this->framelessFunctionInfos) ? $this->getFramelessFunctionInfosName() : "NULL";
15301529

15311530
$code .= generateVersionDependentFlagCode(
15321531
"\tZEND_RAW_FENTRY($zendName, $name, $argInfoName, %s, $framelessFuncInfosName, $docComment)\n",
1533-
$php84AndAboveFlags,
1532+
$flagsByPhpVersions,
15341533
PHP_84_VERSION_ID
15351534
);
15361535

15371536
if (!$php84MinimumCompatibility) {
15381537
$code .= "#else\n";
15391538

1540-
$flags = array_slice($flagsByPhpVersions, 0, 4, true);
15411539
$code .= generateVersionDependentFlagCode(
15421540
"\tZEND_RAW_FENTRY($zendName, $name, $argInfoName, %s)\n",
1543-
$flags,
1544-
$this->minimumPhpVersionIdCompatibility
1541+
$flagsByPhpVersions,
1542+
$this->minimumPhpVersionIdCompatibility,
1543+
PHP_83_VERSION_ID
15451544
);
15461545

15471546
$code .= "#endif\n";
@@ -5363,8 +5362,12 @@ function generateOptimizerInfo(array $funcMap): string {
53635362
* @param array<int, string[]> $flagsByPhpVersions
53645363
* @return string
53655364
*/
5366-
function generateVersionDependentFlagCode(string $codeTemplate, array $flagsByPhpVersions, ?int $phpVersionIdMinimumCompatibility): string
5367-
{
5365+
function generateVersionDependentFlagCode(
5366+
string $codeTemplate,
5367+
array $flagsByPhpVersions,
5368+
?int $phpVersionIdMinimumCompatibility,
5369+
?int $phpVersionIdMaxCompatibility = null
5370+
): string {
53685371
$phpVersions = ALL_PHP_VERSION_IDS;
53695372
sort($phpVersions);
53705373
$currentPhpVersion = end($phpVersions);
@@ -5378,13 +5381,23 @@ function generateVersionDependentFlagCode(string $codeTemplate, array $flagsByPh
53785381
return sprintf($codeTemplate, implode("|", $flagsByPhpVersions[$currentPhpVersion]));
53795382
}
53805383

5381-
// Remove flags which depend on a PHP version below the minimally supported one
53825384
ksort($flagsByPhpVersions);
5383-
$index = array_search($phpVersionIdMinimumCompatibility, array_keys($flagsByPhpVersions));
5384-
if ($index === false) {
5385-
throw new Exception("Missing version dependent flags for PHP version ID \"$phpVersionIdMinimumCompatibility\"");
5385+
if ($phpVersionIdMinimumCompatibility !== null) {
5386+
// Remove flags which depend on a PHP version below the minimally supported one
5387+
$index = array_search($phpVersionIdMinimumCompatibility, array_keys($flagsByPhpVersions));
5388+
if ($index === false) {
5389+
throw new Exception("Missing version dependent flags for PHP version ID \"$phpVersionIdMinimumCompatibility\"");
5390+
}
5391+
$flagsByPhpVersions = array_slice($flagsByPhpVersions, $index, null, true);
5392+
}
5393+
if ($phpVersionIdMaxCompatibility !== null) {
5394+
// Remove flags which depend on a PHP version above the maximally supported one
5395+
$index = array_search($phpVersionIdMaxCompatibility, array_keys($flagsByPhpVersions));
5396+
if ($index === false) {
5397+
throw new Exception("Missing version dependent flags for PHP version ID \"$phpVersionIdMaxCompatibility\"");
5398+
}
5399+
$flagsByPhpVersions = array_slice($flagsByPhpVersions, 0, $index, true);
53865400
}
5387-
$flagsByPhpVersions = array_slice($flagsByPhpVersions, $index, null, true);
53885401

53895402
// Remove empty version-specific flags
53905403
$flagsByPhpVersions = array_filter($flagsByPhpVersions);

0 commit comments

Comments
 (0)