Author: Daniel Scherzer (DanielEScherzer)
Committer: DanielEScherzer (DanielEScherzer)
Date: 2025-05-08T13:06:51-07:00
Commit: https://github.com/php/php-src/commit/45d313bbd761a80ae745f483e3f2b2c2c4b4678f
Raw diff: https://github.com/php/php-src/commit/45d313bbd761a80ae745f483e3f2b2c2c4b4678f.diff
gen_stub: simplify generateVersionDependentFlagCode()
* Return a string rather than an array, all callers just immediately used
implode()
to join the elements in the array with nothing between them
* In the callers, inline some single-use variables with the template for the
version-dependent code
* Remove the callback to array_filter
specifying that only items that are not
empty()
be removed - this is the default behavior
Changed paths:
M build/gen_stub.php
Diff:
diff --git a/build/gen_stub.php b/build/gen_stub.php
index dd29d6ff539f..9d21269af5e3 100755
--- a/build/gen_stub.php
+++ b/build/gen_stub.php
@@ -1396,7 +1396,7 @@ public function getFunctionEntry(): string {
$flagsByPhpVersions,
$this->minimumPhpVersionIdCompatibility
);
- $functionEntryCode = rtrim(implode("", $flagsCode));
+ $functionEntryCode = rtrim($flagsCode);
}
}
}
@@ -1439,25 +1439,21 @@ public function getFunctionEntry(): string {
$docComment = $this->exposedDocComment ? '"' .
$this->exposedDocComment->escape() . '"' : "NULL";
$framelessFuncInfosName = !empty($this->framelessFunctionInfos) ?
$this->getFramelessFunctionInfosName() : "NULL";
- $template = "\tZEND_RAW_FENTRY($zendName, $name, $argInfoName, %s,
$framelessFuncInfosName, $docComment)\n";
- $flagsCode = generateVersionDependentFlagCode(
- $template,
+ $code .= generateVersionDependentFlagCode(
+ "\tZEND_RAW_FENTRY($zendName, $name, $argInfoName, %s,
$framelessFuncInfosName, $docComment)\n",
$php84AndAboveFlags,
PHP_84_VERSION_ID
);
- $code .= implode("", $flagsCode);
if (!$php84MinimumCompatibility) {
$code .= "#else\n";
$flags = array_slice($flagsByPhpVersions, 0, 4, true);
- $template = "\tZEND_RAW_FENTRY($zendName, $name, $argInfoName, %s)\n";
- $flagsCode = generateVersionDependentFlagCode(
- $template,
+ $code .= generateVersionDependentFlagCode(
+ "\tZEND_RAW_FENTRY($zendName, $name, $argInfoName, %s)\n",
$flags,
$this->minimumPhpVersionIdCompatibility
);
- $code .= implode("", $flagsCode);
$code .= "#endif\n";
}
@@ -2750,12 +2746,11 @@ private function getClassConstDeclaration(EvaluatedValue $value, array
$allConst
}
$template .= "zend_declare_typed_class_constant(class_entry, $nameCode,
&const_{$constName}_value, %s, $commentCode, $typeCode);\n";
- $flagsCode = generateVersionDependentFlagCode(
+ $code .= generateVersionDependentFlagCode(
$template,
$this->getFlagsByPhpVersion(),
$this->phpVersionIdMinimumCompatibility
);
- $code .= implode("", $flagsCode);
}
if ($this->type && !$php83MinimumCompatibility) {
@@ -2769,12 +2764,11 @@ private function getClassConstDeclaration(EvaluatedValue $value, array
$allConst
$template = "\t";
}
$template .= "zend_declare_class_constant_ex(class_entry, $nameCode,
&const_{$constName}_value, %s, $commentCode);\n";
- $flagsCode = generateVersionDependentFlagCode(
+ $code .= generateVersionDependentFlagCode(
$template,
$this->getFlagsByPhpVersion(),
$this->phpVersionIdMinimumCompatibility
);
- $code .= implode("", $flagsCode);
}
if ($this->type && !$php83MinimumCompatibility) {
@@ -3074,12 +3068,11 @@ public function getDeclaration(array $allConstInfos): string {
$template .= "zend_declare_property_ex(class_entry, $nameCode, &$zvalName, %s,
$commentCode);\n";
}
- $flagsCode = generateVersionDependentFlagCode(
+ $code .= generateVersionDependentFlagCode(
$template,
$this->getFlagsByPhpVersion(),
$this->phpVersionIdMinimumCompatibility
);
- $code .= implode("", $flagsCode);
$code .= $stringRelease;
@@ -3396,8 +3389,7 @@ public function getRegistration(array $allConstInfos): string
$code .= "{\n";
- $flagCodes = generateVersionDependentFlagCode("%s",
$this->getFlagsByPhpVersion(), $this->phpVersionIdMinimumCompatibility);
- $flags = implode("", $flagCodes);
+ $flags = generateVersionDependentFlagCode("%s", $this->getFlagsByPhpVersion(),
$this->phpVersionIdMinimumCompatibility);
$classMethods = ($this->funcInfos === []) ? 'NULL' :
"class_{$escapedName}_methods";
if ($this->type === "enum") {
@@ -5403,9 +5395,9 @@ function generateOptimizerInfo(array $funcMap): string {
/**
* @param array<int, string[]> $flagsByPhpVersions
- * @return string[]
+ * @return string
*/
-function generateVersionDependentFlagCode(string $codeTemplate, array $flagsByPhpVersions, ?int
$phpVersionIdMinimumCompatibility): array
+function generateVersionDependentFlagCode(string $codeTemplate, array $flagsByPhpVersions, ?int
$phpVersionIdMinimumCompatibility): string
{
$phpVersions = ALL_PHP_VERSION_IDS;
sort($phpVersions);
@@ -5414,10 +5406,10 @@ function generateVersionDependentFlagCode(string $codeTemplate, array
$flagsByPh
// No version compatibility is needed
if ($phpVersionIdMinimumCompatibility === null) {
if (empty($flagsByPhpVersions[$currentPhpVersion])) {
- return [];
+ return '';
}
- return [sprintf($codeTemplate, implode("|",
$flagsByPhpVersions[$currentPhpVersion]))];
+ return sprintf($codeTemplate, implode("|",
$flagsByPhpVersions[$currentPhpVersion]));
}
// Remove flags which depend on a PHP version below the minimally supported one
@@ -5429,15 +5421,11 @@ function generateVersionDependentFlagCode(string $codeTemplate, array
$flagsByPh
$flagsByPhpVersions = array_slice($flagsByPhpVersions, $index, null, true);
// Remove empty version-specific flags
- $flagsByPhpVersions = array_filter(
- $flagsByPhpVersions,
- static function (array $value): bool {
- return !empty($value);
- });
+ $flagsByPhpVersions = array_filter($flagsByPhpVersions);
// There are no version-specific flags
if (empty($flagsByPhpVersions)) {
- return [];
+ return '';
}
// Remove version-specific flags which don't differ from the previous one
@@ -5457,16 +5445,14 @@ static function (array $value): bool {
reset($flagsByPhpVersions);
$firstVersion = key($flagsByPhpVersions);
if ($firstVersion === $phpVersionIdMinimumCompatibility) {
- return [sprintf($codeTemplate, implode("|", reset($flagsByPhpVersions)))];
+ return sprintf($codeTemplate, implode("|", reset($flagsByPhpVersions)));
}
}
// Add the necessary conditions around the code using the version-specific flags
- $result = [];
+ $code = '';
$i = 0;
foreach (array_reverse($flagsByPhpVersions, true) as $version => $versionFlags) {
- $code = "";
-
$if = $i === 0 ? "#if" : "#elif";
$endif = $i === $flagCount - 1 ? "#endif\n" : "";
@@ -5475,11 +5461,10 @@ static function (array $value): bool {
$code .= sprintf($codeTemplate, implode("|", $versionFlags));
$code .= $endif;
- $result[] = $code;
$i++;
}
- return $result;
+ return $code;
}
/**