@@ -2257,6 +2257,38 @@ public function getDeclaration(iterable $allConstInfos): string {
2257
2257
}
2258
2258
}
2259
2259
2260
+ class AttributeInfo {
2261
+ /** @var string */
2262
+ public $ class ;
2263
+ /** @var \PhpParser\Node\Arg[] */
2264
+ public $ args ;
2265
+
2266
+ /** @param \PhpParser\Node\Arg[] $args */
2267
+ public function __construct (string $ class , array $ args ) {
2268
+ $ this ->class = $ class ;
2269
+ $ this ->args = $ args ;
2270
+ }
2271
+
2272
+ /** @param iterable<ConstInfo> $allConstInfos */
2273
+ public function generateCode (string $ invocation , string $ nameSuffix , iterable $ allConstInfos ): string {
2274
+ $ code = "\n" ;
2275
+ $ escapedAttributeName = strtr ($ this ->class , '\\' , '_ ' );
2276
+ $ code .= "\tzend_string *attribute_name_ {$ escapedAttributeName }_ $ nameSuffix = zend_string_init( \"" . addcslashes ($ this ->class , "\\" ) . "\", sizeof( \"" . addcslashes ($ this ->class , "\\" ) . "\") - 1, 1); \n" ;
2277
+ $ code .= "\t" . ($ this ->args ? "zend_attribute *attribute_ {$ escapedAttributeName }_ $ nameSuffix = " : "" ) . "$ invocation, attribute_name_ {$ escapedAttributeName }_ $ nameSuffix, " . count ($ this ->args ) . "); \n" ;
2278
+ $ code .= "\tzend_string_release(attribute_name_ {$ escapedAttributeName }_ $ nameSuffix); \n" ;
2279
+ foreach ($ this ->args as $ i => $ arg ) {
2280
+ $ value = EvaluatedValue::createFromExpression ($ arg ->value , null , null , $ allConstInfos );
2281
+ $ zvalName = "attribute_ {$ escapedAttributeName }_ {$ nameSuffix }_arg $ i " ;
2282
+ $ code .= $ value ->initializeZval ($ zvalName , $ allConstInfos );
2283
+ $ code .= "\tZVAL_COPY_VALUE(&attribute_ {$ escapedAttributeName }_ {$ nameSuffix }->args[ $ i].value, & $ zvalName); \n" ;
2284
+ if ($ arg ->name ) {
2285
+ $ code .= "\tattribute_ {$ escapedAttributeName }_ {$ nameSuffix }->args[ $ i].name = zend_string_init( \"{$ arg ->name ->name }\", sizeof( \"{$ arg ->name ->name }\") - 1, 1); \n" ;
2286
+ }
2287
+ }
2288
+ return $ code ;
2289
+ }
2290
+ }
2291
+
2260
2292
class ClassInfo {
2261
2293
/** @var Name */
2262
2294
public $ name ;
@@ -2272,7 +2304,7 @@ class ClassInfo {
2272
2304
public $ isDeprecated ;
2273
2305
/** @var bool */
2274
2306
public $ isStrictProperties ;
2275
- /** @var array{0: string, 1: \PhpParser\Node\Arg[]} [] */
2307
+ /** @var AttributeInfo [] */
2276
2308
public $ attributes ;
2277
2309
/** @var bool */
2278
2310
public $ isNotSerializable ;
@@ -2292,7 +2324,7 @@ class ClassInfo {
2292
2324
public $ cond ;
2293
2325
2294
2326
/**
2295
- * @param array{0: string, 1: \PhpParser\Node\Arg[]} [] $attributes
2327
+ * @param AttributeInfo [] $attributes
2296
2328
* @param Name[] $extends
2297
2329
* @param Name[] $implements
2298
2330
* @param ConstInfo[] $constInfos
@@ -2414,7 +2446,9 @@ function (Name $item) {
2414
2446
$ code .= $ property ->getDeclaration ($ allConstInfos );
2415
2447
}
2416
2448
2417
- $ code .= generateAttributesCode ($ this ->attributes , "zend_add_class_attribute(class_entry " , "class_ $ escapedName " , $ allConstInfos );
2449
+ foreach ($ this ->attributes as $ attribute ) {
2450
+ $ code .= $ attribute ->generateCode ("zend_add_class_attribute(class_entry " , "class_ $ escapedName " , $ allConstInfos );
2451
+ }
2418
2452
2419
2453
if ($ attributeInitializationCode = generateAttributeInitialization ($ this ->funcInfos , $ this ->cond )) {
2420
2454
$ code .= "\n" . $ attributeInitializationCode ;
@@ -2459,8 +2493,8 @@ private function getFlagsAsString(): string
2459
2493
$ flags [] = "ZEND_ACC_NO_DYNAMIC_PROPERTIES " ;
2460
2494
}
2461
2495
2462
- foreach ($ this ->attributes as list ( $ name ) ) {
2463
- if ($ name === "AllowDynamicProperties " ) {
2496
+ foreach ($ this ->attributes as $ attr ) {
2497
+ if ($ attr -> class === "AllowDynamicProperties " ) {
2464
2498
$ flags [] = "ZEND_ACC_ALLOW_DYNAMIC_PROPERTIES " ;
2465
2499
}
2466
2500
}
@@ -2980,30 +3014,6 @@ public function getVariableName(): string {
2980
3014
}
2981
3015
}
2982
3016
2983
- /**
2984
- * @param array{0: string, 1: \PhpParser\Node\Arg[]}[] $attributes
2985
- * @param iterable<ConstInfo> $allConstInfos
2986
- */
2987
- function generateAttributesCode (array $ attributes , string $ invocation , string $ nameSuffix , iterable $ allConstInfos ): string {
2988
- $ code = "" ;
2989
- foreach ($ attributes as list ($ name , $ args )) {
2990
- $ escapedAttributeName = strtr ($ name , '\\' , '_ ' );
2991
- $ code .= "\tzend_string *attribute_name_ {$ escapedAttributeName }_ $ nameSuffix = zend_string_init( \"" . addcslashes ($ name , "\\" ) . "\", sizeof( \"" . addcslashes ($ name , "\\" ) . "\") - 1, 1); \n" ;
2992
- $ code .= "\t" . ($ args ? "zend_attribute *attribute_ {$ escapedAttributeName }_ $ nameSuffix = " : "" ) . "$ invocation, attribute_name_ {$ escapedAttributeName }_ $ nameSuffix, " . count ($ args ) . "); \n" ;
2993
- $ code .= "\tzend_string_release(attribute_name_ {$ escapedAttributeName }_ $ nameSuffix); \n" ;
2994
- foreach ($ args as $ i => $ arg ) {
2995
- $ value = EvaluatedValue::createFromExpression ($ arg ->value , null , null , $ allConstInfos );
2996
- $ zvalName = "attribute_ {$ escapedAttributeName }_ {$ nameSuffix }_arg $ i " ;
2997
- $ code .= $ value ->initializeZval ($ zvalName , $ allConstInfos );
2998
- $ code .= "\tZVAL_COPY_VALUE(&attribute_ {$ escapedAttributeName }_ {$ nameSuffix }->args[ $ i].value, & $ zvalName); \n" ;
2999
- if ($ arg ->name ) {
3000
- $ code .= "\tattribute_ {$ escapedAttributeName }_ {$ nameSuffix }->args[ $ i].name = zend_string_init( \"{$ arg ->name ->name }\", sizeof( \"{$ arg ->name ->name }\") - 1, 1); \n" ;
3001
- }
3002
- }
3003
- }
3004
- return $ code ;
3005
- }
3006
-
3007
3017
/** @return DocCommentTag[] */
3008
3018
function parseDocComment (DocComment $ comment ): array {
3009
3019
$ commentText = substr ($ comment ->getText (), 2 , -2 );
@@ -3330,7 +3340,7 @@ function parseClass(
3330
3340
3331
3341
foreach ($ class ->attrGroups as $ attrGroup ) {
3332
3342
foreach ($ attrGroup ->attrs as $ attr ) {
3333
- $ attributes [] = [ $ attr ->name ->toString (), $ attr ->args ] ;
3343
+ $ attributes [] = new AttributeInfo ( $ attr ->name ->toString (), $ attr ->args ) ;
3334
3344
switch ($ attr ->name ->toString ()) {
3335
3345
case 'AllowDynamicProperties ' :
3336
3346
$ allowsDynamicProperties = true ;
0 commit comments