Skip to content

Virtual props xmlreader #15125

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
Aug 26, 2024
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: 1 addition & 1 deletion Zend/tests/property_hooks/unserialize.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ object(Test)#2 (1) {
Test::$prop3::get
Test::$prop3::set

Warning: unserialize(): Cannot unserialize value for hooked property Test::$prop3 in %s on line %d
Warning: unserialize(): Cannot unserialize value for virtual property Test::$prop3 in %s on line %d

Warning: unserialize(): Error at offset 26 of 32 bytes in %s on line %d
bool(false)
Expand Down
11 changes: 11 additions & 0 deletions build/gen_stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -2916,6 +2916,7 @@ class PropertyInfo extends VariableLike
public ?Expr $defaultValue;
public ?string $defaultValueString;
public bool $isDocReadonly;
public bool $isVirtual;

/**
* @var AttributeInfo[] $attributes
Expand All @@ -2929,6 +2930,7 @@ public function __construct(
?Expr $defaultValue,
?string $defaultValueString,
bool $isDocReadonly,
bool $isVirtual,
?string $link,
?int $phpVersionIdMinimumCompatibility,
array $attributes,
Expand All @@ -2939,6 +2941,7 @@ public function __construct(
$this->defaultValue = $defaultValue;
$this->defaultValueString = $defaultValueString;
$this->isDocReadonly = $isDocReadonly;
$this->isVirtual = $isVirtual;
parent::__construct($flags, $type, $phpDocType, $link, $phpVersionIdMinimumCompatibility, $attributes, $exposedDocComment);
}

Expand Down Expand Up @@ -3054,6 +3057,10 @@ protected function getFlagsByPhpVersion(): array
$flags = $this->addFlagForVersionsAbove($flags, "ZEND_ACC_READONLY", PHP_82_VERSION_ID);
}

if ($this->isVirtual) {
$flags = $this->addFlagForVersionsAbove($flags, "ZEND_ACC_VIRTUAL", PHP_84_VERSION_ID);
}

return $flags;
}

Expand Down Expand Up @@ -4433,6 +4440,7 @@ function parseProperty(
): PropertyInfo {
$phpDocType = null;
$isDocReadonly = false;
$isVirtual = false;
$link = null;

if ($comments) {
Expand All @@ -4444,6 +4452,8 @@ function parseProperty(
$isDocReadonly = true;
} elseif ($tag->name === 'link') {
$link = $tag->value;
} elseif ($tag->name === 'virtual') {
$isVirtual = true;
}
}
}
Expand Down Expand Up @@ -4472,6 +4482,7 @@ function parseProperty(
$property->default,
$property->default ? $prettyPrinter->prettyPrintExpr($property->default) : null,
$isDocReadonly,
$isVirtual,
$link,
$phpVersionIdMinimumCompatibility,
$attributes,
Expand Down
2 changes: 1 addition & 1 deletion ext/reflection/php_reflection.c
Original file line number Diff line number Diff line change
Expand Up @@ -6041,7 +6041,7 @@ ZEND_METHOD(ReflectionProperty, getSettableType)
}

/* Get-only virtual property can never be written to. */
if ((prop->flags & ZEND_ACC_VIRTUAL) && !prop->hooks[ZEND_PROPERTY_HOOK_SET]) {
if (prop->hooks && (prop->flags & ZEND_ACC_VIRTUAL) && !prop->hooks[ZEND_PROPERTY_HOOK_SET]) {
zend_type never_type = ZEND_TYPE_INIT_CODE(IS_NEVER, 0, 0);
reflection_type_factory(never_type, return_value, 0);
return;
Expand Down
2 changes: 1 addition & 1 deletion ext/standard/var_unserializer.re
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ static int is_property_visibility_changed(zend_class_entry *ce, zval *key)
return 1;
} else {
php_error_docref(NULL, E_WARNING,
"Cannot unserialize value for hooked property %s::$%s",
"Cannot unserialize value for virtual property %s::$%s",
ZSTR_VAL(existing_propinfo->ce->name), Z_STRVAL_P(key));
zval_ptr_dtor_str(key);
return -1;
Expand Down
14 changes: 14 additions & 0 deletions ext/xmlreader/php_xmlreader.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,32 +99,46 @@ class XMLReader
public const int SUBST_ENTITIES = UNKNOWN;


/** @virtual */
public int $attributeCount;

/** @virtual */
public string $baseURI;

/** @virtual */
public int $depth;

/** @virtual */
public bool $hasAttributes;

/** @virtual */
public bool $hasValue;

/** @virtual */
public bool $isDefault;

/** @virtual */
public bool $isEmptyElement;

/** @virtual */
public string $localName;

/** @virtual */
public string $name;

/** @virtual */
public string $namespaceURI;

/** @virtual */
public int $nodeType;

/** @virtual */
public string $prefix;

/** @virtual */
public string $value;

/** @virtual */
public string $xmlLang;

/** @tentative-return-type */
Expand Down
30 changes: 15 additions & 15 deletions ext/xmlreader/php_xmlreader_arginfo.h

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

34 changes: 34 additions & 0 deletions ext/xmlreader/tests/virtual_properties.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
--TEST--
Virtual property tests
--EXTENSIONS--
xmlreader
--FILE--
<?php

$rc = new ReflectionClass(XMLReader::class);
$prop = $rc->getProperty("nodeType");
var_dump($prop->isVirtual());
var_dump($prop->getSettableType());
var_dump($prop->getHooks());
var_dump($prop->getRawValue(new XMLReader));
var_dump($prop->getValue(new XMLReader));

$reader = XMLReader::XML("<root>hi</root>");
var_dump(json_encode($reader));
var_export($reader); echo "\n";
var_dump(get_object_vars($reader));

?>
--EXPECTF--
bool(true)
object(ReflectionNamedType)#%d (0) {
}
array(0) {
}
int(0)
int(0)
string(2) "{}"
\XMLReader::__set_state(array(
))
array(0) {
}
Loading