Skip to content

Fix Lazy Objects Reflection API: add ReflectionProperty::isLazy() #16342

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

Closed
wants to merge 3 commits into from
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
140 changes: 140 additions & 0 deletions Zend/tests/lazy_objects/isLazy.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
--TEST--
Lazy Objects: ReflectionProperty::isLazy()
--FILE--
<?php

#[AllowDynamicProperties]
class C {
public static $staticProp;
public int $typed;
public $untyped;
public $virtual {
get {}
}
}

function testProps(ReflectionClass $reflector, object $obj) {
foreach (['staticProp', 'typed', 'untyped', 'virtual', 'dynamic'] as $name) {
if ('dynamic' === $name) {
$tmp = new C();
$tmp->dynamic = 1;
$pr = new ReflectionProperty($tmp, $name);
} else {
$pr = $reflector->getProperty($name);
}
printf("%s: %d\n", $name, $pr->isLazy($obj));
}
}

$reflector = new ReflectionClass(C::class);

print "# Ghost\n";

$obj = $reflector->newLazyGhost(function () { });

testProps($reflector, $obj);

$pr = $reflector->getProperty('typed');
$pr->skipLazyInitialization($obj);
printf("typed (skipped): %d\n", $pr->isLazy($obj));

print "# Initialized Ghost\n";

$reflector->initializeLazyObject($obj);

testProps($reflector, $obj);

print "# Proxy\n";

$obj = $reflector->newLazyProxy(function () {
return new C();
});

testProps($reflector, $obj);

$pr = $reflector->getProperty('typed');
$pr->skipLazyInitialization($obj);
printf("typed (skipped prop): %d\n", $pr->isLazy($obj));

print "# Initialized Proxy\n";

$reflector->initializeLazyObject($obj);

testProps($reflector, $obj);

print "# Nested Proxy\n";

$nested = new C();
$obj = $reflector->newLazyProxy(function () use ($nested) {
return $nested;
});
$reflector->initializeLazyObject($obj);
$reflector->resetAsLazyProxy($nested, function () {
return new C();
});

testProps($reflector, $obj);

print "# Nested Proxy (nested initialized)\n";

$nested = new C();
$obj = $reflector->newLazyProxy(function () use ($nested) {
return $nested;
});
$reflector->initializeLazyObject($obj);
$reflector->resetAsLazyProxy($nested, function () {
return new C();
});
$reflector->initializeLazyObject($nested);

testProps($reflector, $obj);

print "# Internal\n";

$obj = (new DateTime())->diff(new DateTime());
$reflector = new ReflectionClass(DateInterval::class);
$pr = new ReflectionProperty($obj, 'y');
printf("y: %d\n", $pr->isLazy($obj));

?>
--EXPECT--
# Ghost
staticProp: 0
typed: 1
untyped: 1
virtual: 0
dynamic: 0
typed (skipped): 0
# Initialized Ghost
staticProp: 0
typed: 0
untyped: 0
virtual: 0
dynamic: 0
# Proxy
staticProp: 0
typed: 1
untyped: 1
virtual: 0
dynamic: 0
typed (skipped prop): 0
# Initialized Proxy
staticProp: 0
typed: 0
untyped: 0
virtual: 0
dynamic: 0
# Nested Proxy
staticProp: 0
typed: 1
untyped: 1
virtual: 0
dynamic: 0
# Nested Proxy (nested initialized)
staticProp: 0
typed: 0
untyped: 0
virtual: 0
dynamic: 0
# Internal
y: 0
24 changes: 24 additions & 0 deletions ext/reflection/php_reflection.c
Original file line number Diff line number Diff line change
Expand Up @@ -6271,6 +6271,30 @@ ZEND_METHOD(ReflectionProperty, skipLazyInitialization)
}
}

ZEND_METHOD(ReflectionProperty, isLazy)
{
reflection_object *intern;
property_reference *ref;
zend_object *object;

GET_REFLECTION_OBJECT_PTR(ref);

ZEND_PARSE_PARAMETERS_START(1, 1) {
Z_PARAM_OBJ_OF_CLASS(object, intern->ce)
} ZEND_PARSE_PARAMETERS_END();

if (!ref->prop || ref->prop->flags & (ZEND_ACC_STATIC | ZEND_ACC_VIRTUAL)) {
RETURN_FALSE;
}

while (zend_object_is_lazy_proxy(object)
&& zend_lazy_object_initialized(object)) {
object = zend_lazy_object_get_instance(object);
}

RETURN_BOOL(Z_PROP_FLAG_P(OBJ_PROP(object, ref->prop->offset)) & IS_PROP_LAZY);
}

/* {{{ Returns true if property was initialized */
ZEND_METHOD(ReflectionProperty, isInitialized)
{
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 @@ -502,6 +502,8 @@ public function setRawValueWithoutLazyInitialization(object $object, mixed $valu

public function skipLazyInitialization(object $object): void {}

public function isLazy(object $object): bool {}

/** @tentative-return-type */
public function isInitialized(?object $object = null): bool {}

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.