Skip to content

Commit 0a5b7c8

Browse files
committed
Make nested ternary without parentheses a compile error
This was deprecated in PHP 7.4.
1 parent 9bf1198 commit 0a5b7c8

File tree

6 files changed

+42
-18
lines changed

6 files changed

+42
-18
lines changed

UPGRADING

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,8 @@ PHP 8.0 UPGRADE NOTES
208208
recognized as a namespaced name, `Foo \ Bar` will not. Conversely, reserved
209209
keywords are now permitted as namespace segments.
210210
RFC: https://wiki.php.net/rfc/namespaced_names_as_token
211+
. Nested ternaries now require explicit parentheses.
212+
RFC: https://wiki.php.net/rfc/ternary_associativity
211213

212214
- COM:
213215
. Removed the ability to import case-insensitive constants from type

Zend/tests/ternary_associativity.phpt

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,22 @@
11
--TEST--
2-
Using ternary associativity is deprecated
2+
Allowed nested ternary cases
33
--FILE--
44
<?php
55

6-
1 ? 2 : 3 ? 4 : 5; // deprecated
76
(1 ? 2 : 3) ? 4 : 5; // ok
87
1 ? 2 : (3 ? 4 : 5); // ok
98

10-
// While the associativity of ?: is also incorrect, it will not cause a
11-
// functional difference, only some unnecessary checks.
129
1 ?: 2 ?: 3; // ok
1310
(1 ?: 2) ?: 3; // ok
1411
1 ?: (2 ?: 3); // ok
1512

16-
1 ?: 2 ? 3 : 4; // deprecated
1713
(1 ?: 2) ? 3 : 4; // ok
1814
1 ?: (2 ? 3 : 4); // ok
1915

20-
1 ? 2 : 3 ?: 4; // deprecated
2116
(1 ? 2 : 3) ?: 4; // ok
2217
1 ? 2 : (3 ?: 4); // ok
2318

2419
?>
25-
--EXPECTF--
26-
Deprecated: Unparenthesized `a ? b : c ? d : e` is deprecated. Use either `(a ? b : c) ? d : e` or `a ? b : (c ? d : e)` in %s on line 3
27-
28-
Deprecated: Unparenthesized `a ?: b ? c : d` is deprecated. Use either `(a ?: b) ? c : d` or `a ?: (b ? c : d)` in %s on line 13
29-
30-
Deprecated: Unparenthesized `a ? b : c ?: d` is deprecated. Use either `(a ? b : c) ?: d` or `a ? b : (c ?: d)` in %s on line 17
20+
===DONE===
21+
--EXPECT--
22+
===DONE===
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
--TEST--
2+
Forbidden nested ternary, case 1
3+
--FILE--
4+
<?php
5+
6+
1 ? 2 : 3 ? 4 : 5;
7+
8+
?>
9+
--EXPECTF--
10+
Fatal error: Unparenthesized `a ? b : c ? d : e` is not supported. Use either `(a ? b : c) ? d : e` or `a ? b : (c ? d : e)` in %s on line %d
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
--TEST--
2+
Forbidden nested ternary, case 2
3+
--FILE--
4+
<?php
5+
6+
1 ?: 2 ? 3 : 4;
7+
8+
?>
9+
--EXPECTF--
10+
Fatal error: Unparenthesized `a ?: b ? c : d` is not supported. Use either `(a ?: b) ? c : d` or `a ?: (b ? c : d)` in %s on line %d
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
--TEST--
2+
Forbidden nested ternary, case 3
3+
--FILE--
4+
<?php
5+
6+
1 ? 2 : 3 ?: 4;
7+
8+
?>
9+
--EXPECTF--
10+
Fatal error: Unparenthesized `a ? b : c ?: d` is not supported. Use either `(a ? b : c) ?: d` or `a ? b : (c ?: d)` in %s on line %d

Zend/zend_compile.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8234,18 +8234,18 @@ void zend_compile_conditional(znode *result, zend_ast *ast) /* {{{ */
82348234
&& cond_ast->attr != ZEND_PARENTHESIZED_CONDITIONAL) {
82358235
if (cond_ast->child[1]) {
82368236
if (true_ast) {
8237-
zend_error(E_DEPRECATED,
8238-
"Unparenthesized `a ? b : c ? d : e` is deprecated. "
8237+
zend_error(E_COMPILE_ERROR,
8238+
"Unparenthesized `a ? b : c ? d : e` is not supported. "
82398239
"Use either `(a ? b : c) ? d : e` or `a ? b : (c ? d : e)`");
82408240
} else {
8241-
zend_error(E_DEPRECATED,
8242-
"Unparenthesized `a ? b : c ?: d` is deprecated. "
8241+
zend_error(E_COMPILE_ERROR,
8242+
"Unparenthesized `a ? b : c ?: d` is not supported. "
82438243
"Use either `(a ? b : c) ?: d` or `a ? b : (c ?: d)`");
82448244
}
82458245
} else {
82468246
if (true_ast) {
8247-
zend_error(E_DEPRECATED,
8248-
"Unparenthesized `a ?: b ? c : d` is deprecated. "
8247+
zend_error(E_COMPILE_ERROR,
8248+
"Unparenthesized `a ?: b ? c : d` is not supported. "
82498249
"Use either `(a ?: b) ? c : d` or `a ?: (b ? c : d)`");
82508250
} else {
82518251
/* This case is harmless: (a ?: b) ?: c always produces the same result

0 commit comments

Comments
 (0)