Skip to content

reflection: Add ReflectionGenerator::isClosed() #14358

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
May 29, 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
1 change: 1 addition & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ PHP NEWS
(nielsdos)
. Make ReflectionGenerator::getFunction() legal after generator termination.
(timwolla)
. Added ReflectionGenerator::isClosed(). (timwolla)

- SimpleXML:
. Fixed bug GH-12192 (SimpleXML infinite loop when getName() is called
Expand Down
1 change: 1 addition & 0 deletions UPGRADING
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ PHP 8.4 UPGRADE NOTES
now returns the attached doc comments.
. ReflectionConstant was introduced.
. ReflectionClassConstant::isDeprecated() was introduced.
. ReflectionGenerator::isClosed() was introduced.

- Standard:
. stream_bucket_make_writeable() and stream_bucket_new() will now return a
Expand Down
14 changes: 13 additions & 1 deletion ext/reflection/php_reflection.c
Original file line number Diff line number Diff line change
Expand Up @@ -2271,7 +2271,7 @@ ZEND_METHOD(ReflectionGenerator, __construct)

#define REFLECTION_CHECK_VALID_GENERATOR(ex) \
if (!ex) { \
_DO_THROW("Cannot fetch information from a terminated Generator"); \
_DO_THROW("Cannot fetch information from a closed Generator"); \
RETURN_THROWS(); \
}

Expand Down Expand Up @@ -2403,6 +2403,18 @@ ZEND_METHOD(ReflectionGenerator, getExecutingGenerator)
}
/* }}} */

ZEND_METHOD(ReflectionGenerator, isClosed)
{
zend_generator *generator = (zend_generator *) Z_OBJ(Z_REFLECTION_P(ZEND_THIS)->obj);
zend_execute_data *ex = generator->execute_data;

if (zend_parse_parameters_none() == FAILURE) {
RETURN_THROWS();
}

RETURN_BOOL(ex == NULL);
}

/* {{{ Constructor. Throws an Exception in case the given method does not exist */
ZEND_METHOD(ReflectionParameter, __construct)
{
Expand Down
2 changes: 2 additions & 0 deletions ext/reflection/php_reflection.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ public function getThis(): ?object {}

/** @tentative-return-type */
public function getExecutingGenerator(): Generator {}

public function isClosed(): bool {}
}

class ReflectionMethod extends ReflectionFunctionAbstract
Expand Down
6 changes: 5 additions & 1 deletion ext/reflection/php_reflection_arginfo.h

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

2 changes: 1 addition & 1 deletion ext/reflection/tests/027.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ try {
}
?>
--EXPECT--
Cannot fetch information from a terminated Generator
Cannot fetch information from a closed Generator
46 changes: 46 additions & 0 deletions ext/reflection/tests/ReflectionGenerator_isClosed.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
--TEST--
ReflectionGenerator::isClosed
--FILE--
<?php

function empty_generator() {
return;
yield;
}

$gens = [
(function() {
yield;
})(),
empty_generator(),
];

foreach ($gens as $gen) {
$ref = new ReflectionGenerator($gen);
var_dump($ref->getExecutingLine());
var_dump($ref->isClosed());
var_dump($ref->getExecutingLine());
foreach ($gen as $dummy);
var_dump($ref->isClosed());
try {
var_dump($ref->getExecutingLine());
} catch (\Exception $e) {
echo $e->getMessage(), PHP_EOL;
}

echo PHP_EOL;
}

?>
--EXPECTF--
int(10)
bool(false)
int(10)
bool(true)
Cannot fetch information from a closed Generator

int(4)
bool(false)
int(4)
bool(true)
Cannot fetch information from a closed Generator
42 changes: 42 additions & 0 deletions ext/reflection/tests/ReflectionGenerator_isClosed_002.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
--TEST--
ReflectionGenerator::isClosed: ->valid() terminates an empty generator.
--FILE--
<?php

function empty_generator() {
return;
yield;
}

$gens = [
(function() {
yield;
})(),
empty_generator(),
];

foreach ($gens as $gen) {
$ref = new ReflectionGenerator($gen);
var_dump($ref->isClosed());
var_dump($gen->valid());
var_dump($ref->isClosed());
try {
var_dump($ref->getExecutingLine());
} catch (\Exception $e) {
echo $e->getMessage(), PHP_EOL;
}

echo PHP_EOL;
}

?>
--EXPECTF--
bool(false)
bool(true)
bool(false)
int(11)

bool(false)
bool(false)
bool(true)
Cannot fetch information from a closed Generator
Loading