Skip to content

Support the #[\AllowDynamicProperties] attribute in stubs #8776

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions Zend/zend_builtin_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ ZEND_MINIT_FUNCTION(core) { /* {{{ */
zend_register_default_classes();

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

return SUCCESS;
}
Expand Down
1 change: 1 addition & 0 deletions Zend/zend_builtin_functions.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

/** @generate-class-entries */

#[\AllowDynamicProperties]
class stdClass
{
}
Expand Down
4 changes: 3 additions & 1 deletion Zend/zend_builtin_functions_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions build/gen_stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -2273,6 +2273,8 @@ class ClassInfo {
/** @var bool */
public $isStrictProperties;
/** @var bool */
public $allowsDynamicProperties;
/** @var bool */
public $isNotSerializable;
/** @var Name[] */
public $extends;
Expand Down Expand Up @@ -2305,6 +2307,7 @@ public function __construct(
?SimpleType $enumBackingType,
bool $isDeprecated,
bool $isStrictProperties,
bool $allowsDynamicProperties,
bool $isNotSerializable,
array $extends,
array $implements,
Expand All @@ -2321,6 +2324,7 @@ public function __construct(
$this->enumBackingType = $enumBackingType;
$this->isDeprecated = $isDeprecated;
$this->isStrictProperties = $isStrictProperties;
$this->allowsDynamicProperties = $allowsDynamicProperties;
$this->isNotSerializable = $isNotSerializable;
$this->extends = $extends;
$this->implements = $implements;
Expand Down Expand Up @@ -2409,6 +2413,10 @@ function (Name $item) {
$code .= $property->getDeclaration($allConstInfos);
}

if ($this->allowsDynamicProperties) {
$code .= "\tzend_add_class_attribute(class_entry, zend_ce_allow_dynamic_properties->name, 0);\n";
}

if ($attributeInitializationCode = generateAttributeInitialization($this->funcInfos, $this->cond)) {
$code .= "\n" . $attributeInitializationCode;
}
Expand Down Expand Up @@ -2452,6 +2460,10 @@ private function getFlagsAsString(): string
$flags[] = "ZEND_ACC_NO_DYNAMIC_PROPERTIES";
}

if ($this->allowsDynamicProperties) {
$flags[] = "ZEND_ACC_ALLOW_DYNAMIC_PROPERTIES";
}

if ($this->isNotSerializable) {
$flags[] = "ZEND_ACC_NOT_SERIALIZABLE";
}
Expand Down Expand Up @@ -3273,6 +3285,7 @@ function parseClass(
$isDeprecated = false;
$isStrictProperties = false;
$isNotSerializable = false;
$allowsDynamicProperties = false;

if ($comment) {
$tags = parseDocComment($comment);
Expand All @@ -3289,6 +3302,22 @@ function parseClass(
}
}

foreach ($class->attrGroups as $attrGroup) {
foreach ($attrGroup->attrs as $attr) {
switch ($attr->name->toCodeString()) {
case '\\AllowDynamicProperties':
$allowsDynamicProperties = true;
break;
default:
throw new Exception("Unhandled attribute {$attr->name->toCodeString()}.");
}
}
}

if ($isStrictProperties && $allowsDynamicProperties) {
throw new Exception("A class may not have '@strict-properties' and '#[\\AllowDynamicProperties]' at the same time.");
}

$extends = [];
$implements = [];

Expand Down Expand Up @@ -3319,6 +3348,7 @@ function parseClass(
? SimpleType::fromNode($class->scalarType) : null,
$isDeprecated,
$isStrictProperties,
$allowsDynamicProperties,
$isNotSerializable,
$extends,
$implements,
Expand Down