Declare tentative return types for ext/simplexml

Closes GH-7003
This commit is contained in:
Máté Kocsis committed 2021-05-26 11:23:38 +02:00
1 parent 312b72908d
commit 37a3c9bc8d
8 files changed
+159 -73

No files matched your search

+9 -6
View File
@@ -2005,7 +2005,8 @@ PHP_METHOD(SimpleXMLElement, current)
}
if (Z_ISUNDEF(sxe->iter.data)) {
return; /* return NULL */
zend_throw_error(NULL, "Iterator not initialized or already consumed");
RETURN_THROWS();
}
data = &sxe->iter.data;
@@ -2025,16 +2026,18 @@ PHP_METHOD(SimpleXMLElement, key)
}
if (Z_ISUNDEF(sxe->iter.data)) {
RETURN_FALSE;
zend_throw_error(NULL, "Iterator not initialized or already consumed");
RETURN_THROWS();
}
intern = Z_SXEOBJ_P(&sxe->iter.data);
if (intern != NULL && intern->node != NULL) {
curnode = (xmlNodePtr)((php_libxml_node_ptr *)intern->node)->node;
RETURN_STRINGL((char*)curnode->name, xmlStrlen(curnode->name));
if (intern == NULL || intern->node == NULL) {
zend_throw_error(NULL, "Iterator not initialized or already consumed");
RETURN_THROWS();
}
RETURN_FALSE;
curnode = (xmlNodePtr)((php_libxml_node_ptr *)intern->node)->node;
RETURN_STRINGL((char*)curnode->name, xmlStrlen(curnode->name));
}
/* }}} */
+38 -38
View File
@@ -10,69 +10,69 @@ function simplexml_import_dom(SimpleXMLElement|DOMNode $node, ?string $class_nam
class SimpleXMLElement implements Stringable, Countable, RecursiveIterator
{
/** @return array|null|false */
public function xpath(string $expression) {}
/** @tentative-return-type */
public function xpath(string $expression): array|null|false {}
/** @return bool */
public function registerXPathNamespace(string $prefix, string $namespace) {}
/** @tentative-return-type */
public function registerXPathNamespace(string $prefix, string $namespace): bool {}
/** @return string|bool */
public function asXML(?string $filename = null) {}
/** @tentative-return-type */
public function asXML(?string $filename = null): string|bool {}
/**
* @return string|bool
* @tentative-return-type
* @alias SimpleXMLElement::asXML
*/
public function saveXML(?string $filename = null) {}
public function saveXML(?string $filename = null): string|bool {}
/** @return array */
public function getNamespaces(bool $recursive = false) {}
/** @tentative-return-type */
public function getNamespaces(bool $recursive = false): array {}
/** @return array|false */
public function getDocNamespaces(bool $recursive = false, bool $fromRoot = true) {}
/** @tentative-return-type */
public function getDocNamespaces(bool $recursive = false, bool $fromRoot = true): array|false {}
/** @return SimpleXMLElement|null */
public function children(?string $namespaceOrPrefix = null, bool $isPrefix = false) {}
/** @tentative-return-type */
public function children(?string $namespaceOrPrefix = null, bool $isPrefix = false): ?SimpleXMLElement {}
/** @return SimpleXMLElement|null */
public function attributes(?string $namespaceOrPrefix = null, bool $isPrefix = false) {}
/** @tentative-return-type */
public function attributes(?string $namespaceOrPrefix = null, bool $isPrefix = false): ?SimpleXMLElement {}
public function __construct(string $data, int $options = 0, bool $dataIsURL = false, string $namespaceOrPrefix = "", bool $isPrefix = false) {}
/** @return SimpleXMLElement|null */
public function addChild(string $qualifiedName, ?string $value = null, ?string $namespace = null) {}
/** @tentative-return-type */
public function addChild(string $qualifiedName, ?string $value = null, ?string $namespace = null): ?SimpleXMLElement {}
/** @return void */
public function addAttribute(string $qualifiedName, string $value, ?string $namespace = null) {}
/** @tentative-return-type */
public function addAttribute(string $qualifiedName, ?string $value = null, ?string $namespace = null): void {}
/** @return string */
public function getName() {}
/** @tentative-return-type */
public function getName(): string {}
public function __toString(): string {}
/** @return int */
public function count() {}
/** @tentative-return-type */
public function count(): int {}
/** @return void */
public function rewind() {}
/** @tentative-return-type */
public function rewind(): void {}
/** @return bool */
public function valid() {}
/** @tentative-return-type */
public function valid(): bool {}
/** @return SimpleXMLElement|null */
public function current() {}
/** @tentative-return-type */
public function current(): SimpleXMLElement {}
/** @return string|false */
public function key() {}
/** @tentative-return-type */
public function key(): string {}
/** @return void */
public function next() {}
/** @tentative-return-type */
public function next(): void {}
/** @return bool */
public function hasChildren() {}
/** @tentative-return-type */
public function hasChildren(): bool {}
/** @return SimpleXMLElement|null */
public function getChildren() {}
/** @tentative-return-type */
public function getChildren(): ?SimpleXMLElement {}
}
class SimpleXMLIterator extends SimpleXMLElement
+23 -18
View File
@@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: a15b1397538882bf618a4963193a63be8d893842 */
* Stub hash: 2adc13ce6717fe2255ea7d9a3df56eeb8838d61d */
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_TYPE_MASK_EX(arginfo_simplexml_load_file, 0, 1, SimpleXMLElement, MAY_BE_FALSE)
ZEND_ARG_TYPE_INFO(0, filename, IS_STRING, 0)
@@ -22,31 +22,31 @@ ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_simplexml_import_dom, 0, 1, Simpl
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, class_name, IS_STRING, 1, "SimpleXMLElement::class")
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_SimpleXMLElement_xpath, 0, 0, 1)
ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_TYPE_MASK_EX(arginfo_class_SimpleXMLElement_xpath, 0, 1, MAY_BE_ARRAY|MAY_BE_NULL|MAY_BE_FALSE)
ZEND_ARG_TYPE_INFO(0, expression, IS_STRING, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_SimpleXMLElement_registerXPathNamespace, 0, 0, 2)
ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_TYPE_INFO_EX(arginfo_class_SimpleXMLElement_registerXPathNamespace, 0, 2, _IS_BOOL, 0)
ZEND_ARG_TYPE_INFO(0, prefix, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, namespace, IS_STRING, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_SimpleXMLElement_asXML, 0, 0, 0)
ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_TYPE_MASK_EX(arginfo_class_SimpleXMLElement_asXML, 0, 0, MAY_BE_STRING|MAY_BE_BOOL)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, filename, IS_STRING, 1, "null")
ZEND_END_ARG_INFO()
#define arginfo_class_SimpleXMLElement_saveXML arginfo_class_SimpleXMLElement_asXML
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_SimpleXMLElement_getNamespaces, 0, 0, 0)
ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_TYPE_INFO_EX(arginfo_class_SimpleXMLElement_getNamespaces, 0, 0, IS_ARRAY, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, recursive, _IS_BOOL, 0, "false")
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_SimpleXMLElement_getDocNamespaces, 0, 0, 0)
ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_TYPE_MASK_EX(arginfo_class_SimpleXMLElement_getDocNamespaces, 0, 0, MAY_BE_ARRAY|MAY_BE_FALSE)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, recursive, _IS_BOOL, 0, "false")
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, fromRoot, _IS_BOOL, 0, "true")
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_SimpleXMLElement_children, 0, 0, 0)
ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_OBJ_INFO_EX(arginfo_class_SimpleXMLElement_children, 0, 0, SimpleXMLElement, 1)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, namespaceOrPrefix, IS_STRING, 1, "null")
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, isPrefix, _IS_BOOL, 0, "false")
ZEND_END_ARG_INFO()
@@ -61,39 +61,44 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_class_SimpleXMLElement___construct, 0, 0, 1)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, isPrefix, _IS_BOOL, 0, "false")
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_SimpleXMLElement_addChild, 0, 0, 1)
ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_OBJ_INFO_EX(arginfo_class_SimpleXMLElement_addChild, 0, 1, SimpleXMLElement, 1)
ZEND_ARG_TYPE_INFO(0, qualifiedName, IS_STRING, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, value, IS_STRING, 1, "null")
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, namespace, IS_STRING, 1, "null")
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_SimpleXMLElement_addAttribute, 0, 0, 2)
ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_TYPE_INFO_EX(arginfo_class_SimpleXMLElement_addAttribute, 0, 1, IS_VOID, 0)
ZEND_ARG_TYPE_INFO(0, qualifiedName, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, value, IS_STRING, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, value, IS_STRING, 1, "null")
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, namespace, IS_STRING, 1, "null")
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_SimpleXMLElement_getName, 0, 0, 0)
ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_TYPE_INFO_EX(arginfo_class_SimpleXMLElement_getName, 0, 0, IS_STRING, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_SimpleXMLElement___toString, 0, 0, IS_STRING, 0)
ZEND_END_ARG_INFO()
#define arginfo_class_SimpleXMLElement_count arginfo_class_SimpleXMLElement_getName
ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_TYPE_INFO_EX(arginfo_class_SimpleXMLElement_count, 0, 0, IS_LONG, 0)
ZEND_END_ARG_INFO()
#define arginfo_class_SimpleXMLElement_rewind arginfo_class_SimpleXMLElement_getName
ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_TYPE_INFO_EX(arginfo_class_SimpleXMLElement_rewind, 0, 0, IS_VOID, 0)
ZEND_END_ARG_INFO()
#define arginfo_class_SimpleXMLElement_valid arginfo_class_SimpleXMLElement_getName
ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_TYPE_INFO_EX(arginfo_class_SimpleXMLElement_valid, 0, 0, _IS_BOOL, 0)
ZEND_END_ARG_INFO()
#define arginfo_class_SimpleXMLElement_current arginfo_class_SimpleXMLElement_getName
ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_OBJ_INFO_EX(arginfo_class_SimpleXMLElement_current, 0, 0, SimpleXMLElement, 0)
ZEND_END_ARG_INFO()
#define arginfo_class_SimpleXMLElement_key arginfo_class_SimpleXMLElement_getName
#define arginfo_class_SimpleXMLElement_next arginfo_class_SimpleXMLElement_getName
#define arginfo_class_SimpleXMLElement_next arginfo_class_SimpleXMLElement_rewind
#define arginfo_class_SimpleXMLElement_hasChildren arginfo_class_SimpleXMLElement_getName
#define arginfo_class_SimpleXMLElement_hasChildren arginfo_class_SimpleXMLElement_valid
#define arginfo_class_SimpleXMLElement_getChildren arginfo_class_SimpleXMLElement_getName
ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_OBJ_INFO_EX(arginfo_class_SimpleXMLElement_getChildren, 0, 0, SimpleXMLElement, 1)
ZEND_END_ARG_INFO()
ZEND_FUNCTION(simplexml_load_file);
+1 -1
View File
@@ -5,7 +5,7 @@ simplexml
--FILE--
<?php
class SXE extends SimpleXmlElement {
public function count() {
public function count(): int {
echo "Called Count!\n";
return parent::count();
}
+39
View File
@@ -0,0 +1,39 @@
--TEST--
SimpleXML: invoking current() after the iterator has already been consumed
--EXTENSIONS--
simplexml
--FILE--
<?php
$xml =<<<EOF
<?xml version='1.0'?>
<root>
<elem/>
</root>
EOF;
$sxe = simplexml_load_string($xml);
try {
$sxe->current();
} catch (Error $exception) {
echo $exception->getMessage() . "\n";
}
for ($sxe->rewind(); $sxe->valid(); $sxe->next()) {
var_dump($sxe->key(), $sxe->current());
}
try {
$sxe->current();
} catch (Error $exception) {
echo $exception->getMessage() . "\n";
}
?>
--EXPECT--
Iterator not initialized or already consumed
string(4) "elem"
object(SimpleXMLElement)#3 (0) {
}
Iterator not initialized or already consumed
+39
View File
@@ -0,0 +1,39 @@
--TEST--
SimpleXML: invoking key() after the iterator has already been consumed
--EXTENSIONS--
simplexml
--FILE--
<?php
$xml =<<<EOF
<?xml version='1.0'?>
<root>
<elem/>
</root>
EOF;
$sxe = simplexml_load_string($xml);
try {
$sxe->key();
} catch (Error $exception) {
echo $exception->getMessage() . "\n";
}
for ($sxe->rewind(); $sxe->valid(); $sxe->next()) {
var_dump($sxe->key(), $sxe->current());
}
try {
$sxe->key();
} catch (Error $exception) {
echo $exception->getMessage() . "\n";
}
?>
--EXPECT--
Iterator not initialized or already consumed
string(4) "elem"
object(SimpleXMLElement)#3 (0) {
}
Iterator not initialized or already consumed
+9 -9
View File
@@ -35,37 +35,37 @@ EOF;
class SXETest extends SimpleXMLIterator
{
function rewind()
function rewind(): void
{
echo __METHOD__ . "\n";
return parent::rewind();
parent::rewind();
}
function valid()
function valid(): bool
{
echo __METHOD__ . "\n";
return parent::valid();
}
function current()
function current(): SimpleXMLElement
{
echo __METHOD__ . "\n";
return parent::current();
}
function key()
function key(): string
{
echo __METHOD__ . "\n";
return parent::key();
}
function next()
function next(): void
{
echo __METHOD__ . "\n";
return parent::next();
parent::next();
}
function hasChildren()
function hasChildren(): bool
{
echo __METHOD__ . "\n";
return parent::hasChildren();
}
function getChildren()
function getChildren(): ?SimpleXMLIterator
{
echo __METHOD__ . "\n";
return parent::getChildren();
+1 -1
View File
@@ -17,7 +17,7 @@ EOF;
class SXETest extends SimpleXMLIterator
{
function count()
function count(): int
{
echo __METHOD__ . "\n";
return parent::count();