Handle explicitly nullable types in optional-before-required deprecation

The exception for null default values here exists to keep compatibility
with PHP < 7.1 where "Foo $bar = null" was the canonical way to create
a nullable parameter. If the parameter is actually "?Foo $bar = null",
then clearly compatibility with PHP < 7.1 is not a concern, and we
can throw a deprecation notice.
This commit is contained in:
Nikita Popov 2021-05-18 14:00:25 +02:00
parent afc4d67c8b
commit c939bd2f10
2 changed files with 5 additions and 2 deletions

View file

@ -5,7 +5,7 @@ Required parameter after optional is deprecated
function test($testA = null, $testB = null, $testC) {}
function test2(Type $test2A = null, $test2B = null, $test2C) {}
function test3(Type $test3A = null, Type2 $test3B = null, $test3C) {}
function test3(Type $test3A = null, ?Type2 $test3B = null, $test3C) {}
?>
--EXPECTF--
@ -14,3 +14,5 @@ Deprecated: Optional parameter $testA declared before required parameter $testC
Deprecated: Optional parameter $testB declared before required parameter $testC is implicitly treated as a required parameter in %s on line %d
Deprecated: Optional parameter $test2B declared before required parameter $test2C is implicitly treated as a required parameter in %s on line %d
Deprecated: Optional parameter $test3B declared before required parameter $test3C is implicitly treated as a required parameter in %s on line %d

View file

@ -6558,7 +6558,8 @@ void zend_compile_params(zend_ast *ast, zend_ast *return_type_ast, uint32_t fall
/* Ignore parameters of the form "Type $param = null".
* This is the PHP 5 style way of writing "?Type $param", so allow it for now. */
bool is_implicit_nullable =
type_ast && Z_TYPE(default_node.u.constant) == IS_NULL;
type_ast && !(type_ast->attr & ZEND_TYPE_NULLABLE)
&& Z_TYPE(default_node.u.constant) == IS_NULL;
if (!is_implicit_nullable) {
zend_ast *required_param_ast = list->child[last_required_param];
zend_error(E_DEPRECATED,