Skip to content

Commit c939bd2

Browse files
committed
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.
1 parent afc4d67 commit c939bd2

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

Zend/tests/required_param_after_optional.phpt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Required parameter after optional is deprecated
55

66
function test($testA = null, $testB = null, $testC) {}
77
function test2(Type $test2A = null, $test2B = null, $test2C) {}
8-
function test3(Type $test3A = null, Type2 $test3B = null, $test3C) {}
8+
function test3(Type $test3A = null, ?Type2 $test3B = null, $test3C) {}
99

1010
?>
1111
--EXPECTF--
@@ -14,3 +14,5 @@ Deprecated: Optional parameter $testA declared before required parameter $testC
1414
Deprecated: Optional parameter $testB declared before required parameter $testC is implicitly treated as a required parameter in %s on line %d
1515

1616
Deprecated: Optional parameter $test2B declared before required parameter $test2C is implicitly treated as a required parameter in %s on line %d
17+
18+
Deprecated: Optional parameter $test3B declared before required parameter $test3C is implicitly treated as a required parameter in %s on line %d

Zend/zend_compile.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6558,7 +6558,8 @@ void zend_compile_params(zend_ast *ast, zend_ast *return_type_ast, uint32_t fall
65586558
/* Ignore parameters of the form "Type $param = null".
65596559
* This is the PHP 5 style way of writing "?Type $param", so allow it for now. */
65606560
bool is_implicit_nullable =
6561-
type_ast && Z_TYPE(default_node.u.constant) == IS_NULL;
6561+
type_ast && !(type_ast->attr & ZEND_TYPE_NULLABLE)
6562+
&& Z_TYPE(default_node.u.constant) == IS_NULL;
65626563
if (!is_implicit_nullable) {
65636564
zend_ast *required_param_ast = list->child[last_required_param];
65646565
zend_error(E_DEPRECATED,

0 commit comments

Comments
 (0)