Skip to content
Closed
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
22 changes: 22 additions & 0 deletions ext/zend_test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -1045,6 +1045,28 @@ static ZEND_METHOD(_ZendTestClass, takesUnionType)
RETURN_NULL();
}

static ZEND_METHOD(_ZendTestClass, returnByRefIntProp)
{
ZEND_PARSE_PARAMETERS_NONE();

zend_object *obj = Z_OBJ_P(ZEND_THIS);
zend_string *name = zend_string_init("intProp", strlen("intProp"), false);
zval *int_prop = obj->handlers->get_property_ptr_ptr(obj, name, BP_VAR_W, NULL);
zend_string_release_ex(name, false);

ZEND_ASSERT(int_prop);
ZEND_ASSERT(!Z_ISERROR_P(int_prop));

if (!Z_ISREF_P(int_prop)) {
zend_property_info *prop_info = zend_get_property_info_for_slot(obj, int_prop);
ZEND_ASSERT(ZEND_TYPE_IS_SET(prop_info->type));
ZVAL_MAKE_REF(int_prop);
ZEND_REF_ADD_TYPE_SOURCE(Z_REF_P(int_prop), prop_info);
}

ZVAL_COPY(return_value, int_prop);
}

// Returns a newly allocated DNF type `Iterator|(Traversable&Countable)`.
//
// We need to generate it "manually" because gen_stubs.php does not support codegen for DNF types ATM.
Expand Down
2 changes: 2 additions & 0 deletions ext/zend_test/test.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ public function returnsThrowable(): Throwable {}
static public function variadicTest(string|Iterator ...$elements) : static {}

public function takesUnionType(stdclass|Iterator $arg): void {}

public function &returnByRefIntProp(): int {}
}

class _ZendTestMagicCall
Expand Down
7 changes: 6 additions & 1 deletion ext/zend_test/test_arginfo.h

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

51 changes: 51 additions & 0 deletions ext/zend_test/tests/gen_stub_test_return_by_ref.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
--TEST--
gen_stub.php: Test that return by ref flag is set
--EXTENSIONS--
zend_test
--FILE--
<?php

$reflectionMethod = new ReflectionMethod(_ZendTestClass::class, "returnByRefIntProp");
var_dump($reflectionMethod->returnsReference());

$o = new _ZendTestClass();
var_dump($o);
$i =& $o->returnByRefIntProp();
var_dump($i);
$i = 24;
var_dump($i);
var_dump($o);

?>
--EXPECT--
bool(true)
object(_ZendTestClass)#2 (3) {
["intProp"]=>
int(123)
["classProp"]=>
NULL
["classUnionProp"]=>
NULL
["classIntersectionProp"]=>
uninitialized(Traversable&Countable)
["readonlyProp"]=>
uninitialized(int)
["dnfProperty"]=>
uninitialized(Iterator|(Traversable&Countable))
}
int(123)
int(24)
object(_ZendTestClass)#2 (3) {
["intProp"]=>
&int(24)
["classProp"]=>
NULL
["classUnionProp"]=>
NULL
["classIntersectionProp"]=>
uninitialized(Traversable&Countable)
["readonlyProp"]=>
uninitialized(int)
["dnfProperty"]=>
uninitialized(Iterator|(Traversable&Countable))
}