Skip to content

Commit 45d313b

Browse files
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
1 parent bfa2b92 commit 45d313b

File tree

1 file changed

+18
-33
lines changed

1 file changed

+18
-33
lines changed

build/gen_stub.php

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1396,7 +1396,7 @@ public function getFunctionEntry(): string {
13961396
$flagsByPhpVersions,
13971397
$this->minimumPhpVersionIdCompatibility
13981398
);
1399-
$functionEntryCode = rtrim(implode("", $flagsCode));
1399+
$functionEntryCode = rtrim($flagsCode);
14001400
}
14011401
}
14021402
}
@@ -1439,25 +1439,21 @@ public function getFunctionEntry(): string {
14391439
$docComment = $this->exposedDocComment ? '"' . $this->exposedDocComment->escape() . '"' : "NULL";
14401440
$framelessFuncInfosName = !empty($this->framelessFunctionInfos) ? $this->getFramelessFunctionInfosName() : "NULL";
14411441

1442-
$template = "\tZEND_RAW_FENTRY($zendName, $name, $argInfoName, %s, $framelessFuncInfosName, $docComment)\n";
1443-
$flagsCode = generateVersionDependentFlagCode(
1444-
$template,
1442+
$code .= generateVersionDependentFlagCode(
1443+
"\tZEND_RAW_FENTRY($zendName, $name, $argInfoName, %s, $framelessFuncInfosName, $docComment)\n",
14451444
$php84AndAboveFlags,
14461445
PHP_84_VERSION_ID
14471446
);
1448-
$code .= implode("", $flagsCode);
14491447

14501448
if (!$php84MinimumCompatibility) {
14511449
$code .= "#else\n";
14521450

14531451
$flags = array_slice($flagsByPhpVersions, 0, 4, true);
1454-
$template = "\tZEND_RAW_FENTRY($zendName, $name, $argInfoName, %s)\n";
1455-
$flagsCode = generateVersionDependentFlagCode(
1456-
$template,
1452+
$code .= generateVersionDependentFlagCode(
1453+
"\tZEND_RAW_FENTRY($zendName, $name, $argInfoName, %s)\n",
14571454
$flags,
14581455
$this->minimumPhpVersionIdCompatibility
14591456
);
1460-
$code .= implode("", $flagsCode);
14611457

14621458
$code .= "#endif\n";
14631459
}
@@ -2750,12 +2746,11 @@ private function getClassConstDeclaration(EvaluatedValue $value, array $allConst
27502746
}
27512747
$template .= "zend_declare_typed_class_constant(class_entry, $nameCode, &const_{$constName}_value, %s, $commentCode, $typeCode);\n";
27522748

2753-
$flagsCode = generateVersionDependentFlagCode(
2749+
$code .= generateVersionDependentFlagCode(
27542750
$template,
27552751
$this->getFlagsByPhpVersion(),
27562752
$this->phpVersionIdMinimumCompatibility
27572753
);
2758-
$code .= implode("", $flagsCode);
27592754
}
27602755

27612756
if ($this->type && !$php83MinimumCompatibility) {
@@ -2769,12 +2764,11 @@ private function getClassConstDeclaration(EvaluatedValue $value, array $allConst
27692764
$template = "\t";
27702765
}
27712766
$template .= "zend_declare_class_constant_ex(class_entry, $nameCode, &const_{$constName}_value, %s, $commentCode);\n";
2772-
$flagsCode = generateVersionDependentFlagCode(
2767+
$code .= generateVersionDependentFlagCode(
27732768
$template,
27742769
$this->getFlagsByPhpVersion(),
27752770
$this->phpVersionIdMinimumCompatibility
27762771
);
2777-
$code .= implode("", $flagsCode);
27782772
}
27792773

27802774
if ($this->type && !$php83MinimumCompatibility) {
@@ -3074,12 +3068,11 @@ public function getDeclaration(array $allConstInfos): string {
30743068
$template .= "zend_declare_property_ex(class_entry, $nameCode, &$zvalName, %s, $commentCode);\n";
30753069
}
30763070

3077-
$flagsCode = generateVersionDependentFlagCode(
3071+
$code .= generateVersionDependentFlagCode(
30783072
$template,
30793073
$this->getFlagsByPhpVersion(),
30803074
$this->phpVersionIdMinimumCompatibility
30813075
);
3082-
$code .= implode("", $flagsCode);
30833076

30843077
$code .= $stringRelease;
30853078

@@ -3396,8 +3389,7 @@ public function getRegistration(array $allConstInfos): string
33963389

33973390
$code .= "{\n";
33983391

3399-
$flagCodes = generateVersionDependentFlagCode("%s", $this->getFlagsByPhpVersion(), $this->phpVersionIdMinimumCompatibility);
3400-
$flags = implode("", $flagCodes);
3392+
$flags = generateVersionDependentFlagCode("%s", $this->getFlagsByPhpVersion(), $this->phpVersionIdMinimumCompatibility);
34013393

34023394
$classMethods = ($this->funcInfos === []) ? 'NULL' : "class_{$escapedName}_methods";
34033395
if ($this->type === "enum") {
@@ -5403,9 +5395,9 @@ function generateOptimizerInfo(array $funcMap): string {
54035395

54045396
/**
54055397
* @param array<int, string[]> $flagsByPhpVersions
5406-
* @return string[]
5398+
* @return string
54075399
*/
5408-
function generateVersionDependentFlagCode(string $codeTemplate, array $flagsByPhpVersions, ?int $phpVersionIdMinimumCompatibility): array
5400+
function generateVersionDependentFlagCode(string $codeTemplate, array $flagsByPhpVersions, ?int $phpVersionIdMinimumCompatibility): string
54095401
{
54105402
$phpVersions = ALL_PHP_VERSION_IDS;
54115403
sort($phpVersions);
@@ -5414,10 +5406,10 @@ function generateVersionDependentFlagCode(string $codeTemplate, array $flagsByPh
54145406
// No version compatibility is needed
54155407
if ($phpVersionIdMinimumCompatibility === null) {
54165408
if (empty($flagsByPhpVersions[$currentPhpVersion])) {
5417-
return [];
5409+
return '';
54185410
}
54195411

5420-
return [sprintf($codeTemplate, implode("|", $flagsByPhpVersions[$currentPhpVersion]))];
5412+
return sprintf($codeTemplate, implode("|", $flagsByPhpVersions[$currentPhpVersion]));
54215413
}
54225414

54235415
// Remove flags which depend on a PHP version below the minimally supported one
@@ -5429,15 +5421,11 @@ function generateVersionDependentFlagCode(string $codeTemplate, array $flagsByPh
54295421
$flagsByPhpVersions = array_slice($flagsByPhpVersions, $index, null, true);
54305422

54315423
// Remove empty version-specific flags
5432-
$flagsByPhpVersions = array_filter(
5433-
$flagsByPhpVersions,
5434-
static function (array $value): bool {
5435-
return !empty($value);
5436-
});
5424+
$flagsByPhpVersions = array_filter($flagsByPhpVersions);
54375425

54385426
// There are no version-specific flags
54395427
if (empty($flagsByPhpVersions)) {
5440-
return [];
5428+
return '';
54415429
}
54425430

54435431
// Remove version-specific flags which don't differ from the previous one
@@ -5457,16 +5445,14 @@ static function (array $value): bool {
54575445
reset($flagsByPhpVersions);
54585446
$firstVersion = key($flagsByPhpVersions);
54595447
if ($firstVersion === $phpVersionIdMinimumCompatibility) {
5460-
return [sprintf($codeTemplate, implode("|", reset($flagsByPhpVersions)))];
5448+
return sprintf($codeTemplate, implode("|", reset($flagsByPhpVersions)));
54615449
}
54625450
}
54635451

54645452
// Add the necessary conditions around the code using the version-specific flags
5465-
$result = [];
5453+
$code = '';
54665454
$i = 0;
54675455
foreach (array_reverse($flagsByPhpVersions, true) as $version => $versionFlags) {
5468-
$code = "";
5469-
54705456
$if = $i === 0 ? "#if" : "#elif";
54715457
$endif = $i === $flagCount - 1 ? "#endif\n" : "";
54725458

@@ -5475,11 +5461,10 @@ static function (array $value): bool {
54755461
$code .= sprintf($codeTemplate, implode("|", $versionFlags));
54765462
$code .= $endif;
54775463

5478-
$result[] = $code;
54795464
$i++;
54805465
}
54815466

5482-
return $result;
5467+
return $code;
54835468
}
54845469

54855470
/**

0 commit comments

Comments
 (0)