Skip to content

Fix '?' in ReflectionNamedType::getName() from ReflectionProperty::getSettableType() #19201

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 2 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
6 changes: 3 additions & 3 deletions ext/reflection/php_reflection.c
Original file line number Diff line number Diff line change
Expand Up @@ -6517,7 +6517,7 @@ ZEND_METHOD(ReflectionProperty, getSettableType)
/* Get-only virtual property can never be written to. */
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);
reflection_type_factory(never_type, return_value, 1);
return;
}

Expand All @@ -6527,15 +6527,15 @@ ZEND_METHOD(ReflectionProperty, getSettableType)
if (!ZEND_TYPE_IS_SET(arg_info->type)) {
RETURN_NULL();
}
reflection_type_factory(arg_info->type, return_value, 0);
reflection_type_factory(arg_info->type, return_value, 1);
return;
}

/* Fall back to property type */
if (!ZEND_TYPE_IS_SET(ref->prop->type)) {
RETURN_NULL();
}
reflection_type_factory(ref->prop->type, return_value, 0);
reflection_type_factory(ref->prop->type, return_value, 1);
}

/* {{{ Returns whether property has a type */
Expand Down
56 changes: 56 additions & 0 deletions ext/reflection/tests/gh19187.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
--TEST--
GH-19187: ReflectionNamedType::getName() should not include nullable type
--FILE--
<?php

class C {
public string|null $a {
set => $value;
}
public string|null $b {
set(string|null $value) => $value;
}
public string|null $c {
get => $this->c;
}
}

foreach ((new ReflectionClass(C::class))->getProperties() as $r) {
$type = $r->getType();
echo $type, "\n";
echo $type->getName(), "\n";
var_dump($type->allowsNull());
echo "\n";

$settableType = $r->getSettableType();
echo $settableType, "\n";
echo $settableType->getName(), "\n";
var_dump($settableType->allowsNull());
echo "\n";
}

?>
--EXPECT--
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, can you also include the output of __toString() and allowsNull() to confirm that the nullability is still communicated?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Also added a test for the fallback path when no set is present.

?string
string
bool(true)

?string
string
bool(true)

?string
string
bool(true)

?string
string
bool(true)

?string
string
bool(true)

?string
string
bool(true)