Skip to content

Commit ecdff3b

Browse files
gen_stub: add StringBuilder class for managing known strings
Split out from the PropertyInfo class so that known strings can also be used for attributes in a follow-up commit.
1 parent a4d39f9 commit ecdff3b

File tree

1 file changed

+75
-63
lines changed

1 file changed

+75
-63
lines changed

build/gen_stub.php

Lines changed: 75 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -2955,14 +2955,7 @@ protected function addModifiersToFieldSynopsis(DOMDocument $doc, DOMElement $fie
29552955
}
29562956
}
29572957

2958-
class PropertyInfo extends VariableLike
2959-
{
2960-
private /* readonly */ int $classFlags;
2961-
public /* readonly */ PropertyName $name;
2962-
private /* readonly */ ?Expr $defaultValue;
2963-
private /* readonly */ ?string $defaultValueString;
2964-
private /* readonly */ bool $isDocReadonly;
2965-
private /* readonly */ bool $isVirtual;
2958+
class StringBuilder {
29662959

29672960
// Map possible variable names to the known string constant, see
29682961
// ZEND_KNOWN_STRINGS
@@ -3067,6 +3060,75 @@ class PropertyInfo extends VariableLike
30673060
"clone" => "ZEND_STR_CLONE",
30683061
];
30693062

3063+
/**
3064+
* Get an array of three strings:
3065+
* - declaration of zend_string, if needed, or empty otherwise
3066+
* - usage of that zend_string, or usage with ZSTR_KNOWN()
3067+
* - freeing the zend_string, if needed
3068+
*
3069+
* @param string $varName
3070+
* @param string $strContent
3071+
* @param ?int $minPHPCompatibility
3072+
* @return string[]
3073+
*/
3074+
public static function getString(
3075+
string $varName,
3076+
string $content,
3077+
?int $minPHPCompatibility
3078+
): array {
3079+
// Generally strings will not be known
3080+
$result = [
3081+
"\tzend_string *$varName = zend_string_init(\"$content\", sizeof(\"$content\") - 1, 1);\n",
3082+
$varName,
3083+
"\tzend_string_release($varName);\n"
3084+
];
3085+
// If not set, use the current latest version
3086+
$allVersions = ALL_PHP_VERSION_IDS;
3087+
$minPhp = $minPHPCompatibility ?? end($allVersions);
3088+
if ($minPhp < PHP_80_VERSION_ID) {
3089+
// No known strings in 7.0
3090+
return $result;
3091+
}
3092+
$include = self::PHP_80_KNOWN;
3093+
switch ($minPhp) {
3094+
case PHP_85_VERSION_ID:
3095+
$include = array_merge($include, self::PHP_85_KNOWN);
3096+
// Intentional fall through
3097+
3098+
case PHP_84_VERSION_ID:
3099+
$include = array_merge($include, self::PHP_84_KNOWN);
3100+
// Intentional fall through
3101+
3102+
case PHP_83_VERSION_ID:
3103+
case PHP_82_VERSION_ID:
3104+
$include = array_merge($include, self::PHP_82_KNOWN);
3105+
// Intentional fall through
3106+
3107+
case PHP_81_VERSION_ID:
3108+
$include = array_merge($include, self::PHP_81_KNOWN);
3109+
break;
3110+
}
3111+
if (array_key_exists($content, $include)) {
3112+
$knownStr = $include[$content];
3113+
return [
3114+
'',
3115+
"ZSTR_KNOWN($knownStr)",
3116+
'',
3117+
];
3118+
}
3119+
return $result;
3120+
}
3121+
}
3122+
3123+
class PropertyInfo extends VariableLike
3124+
{
3125+
private /* readonly */ int $classFlags;
3126+
public /* readonly */ PropertyName $name;
3127+
private /* readonly */ ?Expr $defaultValue;
3128+
private /* readonly */ ?string $defaultValueString;
3129+
private /* readonly */ bool $isDocReadonly;
3130+
private /* readonly */ bool $isVirtual;
3131+
30703132
/**
30713133
* @param AttributeInfo[] $attributes
30723134
*/
@@ -3147,7 +3209,11 @@ public function getDeclaration(array $allConstInfos): string {
31473209
$code .= $defaultValue->initializeZval($zvalName);
31483210
}
31493211

3150-
[$stringInit, $nameCode, $stringRelease] = $this->getString($propertyName);
3212+
[$stringInit, $nameCode, $stringRelease] = StringBuilder::getString(
3213+
"property_{$propertyName}_name",
3214+
$propertyName,
3215+
$this->phpVersionIdMinimumCompatibility
3216+
);
31513217
$code .= $stringInit;
31523218

31533219
if ($this->exposedDocComment) {
@@ -3182,60 +3248,6 @@ public function getDeclaration(array $allConstInfos): string {
31823248
return $code;
31833249
}
31843250

3185-
/**
3186-
* Get an array of three strings:
3187-
* - declaration of zend_string, if needed, or empty otherwise
3188-
* - usage of that zend_string, or usage with ZSTR_KNOWN()
3189-
* - freeing the zend_string, if needed
3190-
*
3191-
* @param string $propName
3192-
* @return string[]
3193-
*/
3194-
private function getString(string $propName): array {
3195-
// Generally strings will not be known
3196-
$nameCode = "property_{$propName}_name";
3197-
$result = [
3198-
"\tzend_string *$nameCode = zend_string_init(\"$propName\", sizeof(\"$propName\") - 1, 1);\n",
3199-
$nameCode,
3200-
"\tzend_string_release($nameCode);\n"
3201-
];
3202-
// If not set, use the current latest version
3203-
$allVersions = ALL_PHP_VERSION_IDS;
3204-
$minPhp = $this->phpVersionIdMinimumCompatibility ?? end($allVersions);
3205-
if ($minPhp < PHP_80_VERSION_ID) {
3206-
// No known strings in 7.0
3207-
return $result;
3208-
}
3209-
$include = self::PHP_80_KNOWN;
3210-
switch ($minPhp) {
3211-
case PHP_85_VERSION_ID:
3212-
$include = array_merge($include, self::PHP_85_KNOWN);
3213-
// Intentional fall through
3214-
3215-
case PHP_84_VERSION_ID:
3216-
$include = array_merge($include, self::PHP_84_KNOWN);
3217-
// Intentional fall through
3218-
3219-
case PHP_83_VERSION_ID:
3220-
case PHP_82_VERSION_ID:
3221-
$include = array_merge($include, self::PHP_82_KNOWN);
3222-
// Intentional fall through
3223-
3224-
case PHP_81_VERSION_ID:
3225-
$include = array_merge($include, self::PHP_81_KNOWN);
3226-
break;
3227-
}
3228-
if (array_key_exists($propName, $include)) {
3229-
$knownStr = $include[$propName];
3230-
return [
3231-
'',
3232-
"ZSTR_KNOWN($knownStr)",
3233-
'',
3234-
];
3235-
}
3236-
return $result;
3237-
}
3238-
32393251
/**
32403252
* @return array<int, string[]>
32413253
*/

0 commit comments

Comments
 (0)