Skip to content

Fix ReflectionType::__toString() BC break #2136

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 1 commit 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 Zend/zend_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -5102,7 +5102,7 @@ void zend_compile_params(zend_ast *ast, zend_ast *return_type_ast) /* {{{ */
arg_infos->class_name = NULL;

if (return_type_ast->attr & ZEND_TYPE_NULLABLE) {
arg_infos->allow_null = 1;
arg_infos->allow_null = 2;
return_type_ast->attr &= ~ZEND_TYPE_NULLABLE;
}

Expand Down Expand Up @@ -5194,10 +5194,10 @@ void zend_compile_params(zend_ast *ast, zend_ast *return_type_ast) /* {{{ */
&& (Z_TYPE(default_node.u.constant) == IS_NULL
|| (Z_TYPE(default_node.u.constant) == IS_CONSTANT
&& strcasecmp(Z_STRVAL(default_node.u.constant), "NULL") == 0));
zend_bool is_explicitly_nullable = (type_ast->attr & ZEND_TYPE_NULLABLE) == ZEND_TYPE_NULLABLE;
zend_bool is_explicitly_nullable = ((type_ast->attr & ZEND_TYPE_NULLABLE) == ZEND_TYPE_NULLABLE) << 1;

op_array->fn_flags |= ZEND_ACC_HAS_TYPE_HINTS;
arg_info->allow_null = has_null_default || is_explicitly_nullable;
arg_info->allow_null = has_null_default | is_explicitly_nullable;

type_ast->attr &= ~ZEND_TYPE_NULLABLE;
zend_compile_typename(type_ast, arg_info);
Expand Down
2 changes: 1 addition & 1 deletion ext/reflection/php_reflection.c
Original file line number Diff line number Diff line change
Expand Up @@ -3031,7 +3031,7 @@ ZEND_METHOD(reflection_type, __toString)

str = reflection_type_name(param);

if (param->arg_info->allow_null) {
if (param->arg_info->allow_null & 2) {
Copy link
Contributor

Choose a reason for hiding this comment

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

shouldn't you define a constant instead of using a magic number here ?

size_t orig_len = ZSTR_LEN(str);
str = zend_string_extend(str, orig_len + 1, 0);
memmove(ZSTR_VAL(str) + 1, ZSTR_VAL(str), orig_len + 1);
Expand Down
8 changes: 4 additions & 4 deletions ext/reflection/tests/ReflectionType_001.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
ReflectionParameter::get/hasType and ReflectionType tests
--FILE--
<?php
function foo(stdClass $a, array $b, callable $c, stdClass $d = null, $e = null, string $f, bool $g, int $h, float $i, NotExisting $j) { }
function foo(stdClass $a, array $b, ?callable $c, stdClass $d = null, $e = null, string $f, bool $g, int $h, float $i, NotExisting $j) { }

function bar(): stdClass { return new stdClass; }

Expand Down Expand Up @@ -89,14 +89,14 @@ bool(true)
string(5) "array"
** Function 0 - Parameter 2
bool(true)
bool(false)
bool(true)
string(8) "callable"
bool(true)
string(9) "?callable"
** Function 0 - Parameter 3
bool(true)
bool(true)
bool(false)
string(9) "?stdClass"
string(8) "stdClass"
** Function 0 - Parameter 4
bool(false)
** Function 0 - Parameter 5
Expand Down