Skip to content

Commit ff472ce

Browse files
authored
Support the #[\AllowDynamicProperties] attribute in stubs (#8776)
* Support the `#[\AllowDynamicProperties]` attribute in stubs * Use `#[\AllowDynamicProperties]` attribute in stubs * Disallow applying both `@strict-properties` and `#[\AllowDynamicProperties]`
1 parent 68c2343 commit ff472ce

4 files changed

+34
-3
lines changed

Zend/zend_builtin_functions.c

-2
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ ZEND_MINIT_FUNCTION(core) { /* {{{ */
3838
zend_register_default_classes();
3939

4040
zend_standard_class_def = register_class_stdClass();
41-
zend_add_class_attribute(zend_standard_class_def, zend_ce_allow_dynamic_properties->name, 0);
42-
zend_standard_class_def->ce_flags |= ZEND_ACC_ALLOW_DYNAMIC_PROPERTIES;
4341

4442
return SUCCESS;
4543
}

Zend/zend_builtin_functions.stub.php

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
/** @generate-class-entries */
44

5+
#[\AllowDynamicProperties]
56
class stdClass
67
{
78
}

Zend/zend_builtin_functions_arginfo.h

+3-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/gen_stub.php

+30
Original file line numberDiff line numberDiff line change
@@ -2273,6 +2273,8 @@ class ClassInfo {
22732273
/** @var bool */
22742274
public $isStrictProperties;
22752275
/** @var bool */
2276+
public $allowsDynamicProperties;
2277+
/** @var bool */
22762278
public $isNotSerializable;
22772279
/** @var Name[] */
22782280
public $extends;
@@ -2305,6 +2307,7 @@ public function __construct(
23052307
?SimpleType $enumBackingType,
23062308
bool $isDeprecated,
23072309
bool $isStrictProperties,
2310+
bool $allowsDynamicProperties,
23082311
bool $isNotSerializable,
23092312
array $extends,
23102313
array $implements,
@@ -2321,6 +2324,7 @@ public function __construct(
23212324
$this->enumBackingType = $enumBackingType;
23222325
$this->isDeprecated = $isDeprecated;
23232326
$this->isStrictProperties = $isStrictProperties;
2327+
$this->allowsDynamicProperties = $allowsDynamicProperties;
23242328
$this->isNotSerializable = $isNotSerializable;
23252329
$this->extends = $extends;
23262330
$this->implements = $implements;
@@ -2409,6 +2413,10 @@ function (Name $item) {
24092413
$code .= $property->getDeclaration($allConstInfos);
24102414
}
24112415

2416+
if ($this->allowsDynamicProperties) {
2417+
$code .= "\tzend_add_class_attribute(class_entry, zend_ce_allow_dynamic_properties->name, 0);\n";
2418+
}
2419+
24122420
if ($attributeInitializationCode = generateAttributeInitialization($this->funcInfos, $this->cond)) {
24132421
$code .= "\n" . $attributeInitializationCode;
24142422
}
@@ -2452,6 +2460,10 @@ private function getFlagsAsString(): string
24522460
$flags[] = "ZEND_ACC_NO_DYNAMIC_PROPERTIES";
24532461
}
24542462

2463+
if ($this->allowsDynamicProperties) {
2464+
$flags[] = "ZEND_ACC_ALLOW_DYNAMIC_PROPERTIES";
2465+
}
2466+
24552467
if ($this->isNotSerializable) {
24562468
$flags[] = "ZEND_ACC_NOT_SERIALIZABLE";
24572469
}
@@ -3273,6 +3285,7 @@ function parseClass(
32733285
$isDeprecated = false;
32743286
$isStrictProperties = false;
32753287
$isNotSerializable = false;
3288+
$allowsDynamicProperties = false;
32763289

32773290
if ($comment) {
32783291
$tags = parseDocComment($comment);
@@ -3289,6 +3302,22 @@ function parseClass(
32893302
}
32903303
}
32913304

3305+
foreach ($class->attrGroups as $attrGroup) {
3306+
foreach ($attrGroup->attrs as $attr) {
3307+
switch ($attr->name->toCodeString()) {
3308+
case '\\AllowDynamicProperties':
3309+
$allowsDynamicProperties = true;
3310+
break;
3311+
default:
3312+
throw new Exception("Unhandled attribute {$attr->name->toCodeString()}.");
3313+
}
3314+
}
3315+
}
3316+
3317+
if ($isStrictProperties && $allowsDynamicProperties) {
3318+
throw new Exception("A class may not have '@strict-properties' and '#[\\AllowDynamicProperties]' at the same time.");
3319+
}
3320+
32923321
$extends = [];
32933322
$implements = [];
32943323

@@ -3319,6 +3348,7 @@ function parseClass(
33193348
? SimpleType::fromNode($class->scalarType) : null,
33203349
$isDeprecated,
33213350
$isStrictProperties,
3351+
$allowsDynamicProperties,
33223352
$isNotSerializable,
33233353
$extends,
33243354
$implements,

0 commit comments

Comments
 (0)