Skip to content

Commit 10f660f

Browse files
committed
Fix ?-> in encaps vars without braces
Closes GH-5966.
1 parent 898bb97 commit 10f660f

File tree

3 files changed

+79
-21
lines changed

3 files changed

+79
-21
lines changed

Zend/tests/nullsafe_operator/003.phpt

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,20 @@ var_dump(null?->baz);
1919
var_dump(null?->qux());
2020
var_dump(null?->quux());
2121

22-
var_dump((new Foo)?->bar);
23-
var_dump((new Foo)?->baz);
24-
var_dump((new Foo)?->qux());
22+
var_dump($foo?->bar);
23+
var_dump($foo?->baz);
24+
var_dump($foo?->qux());
2525
try {
26-
var_dump((new Foo)?->quux());
26+
var_dump($foo?->quux());
2727
} catch (Throwable $e) {
2828
var_dump($e->getMessage());
2929
}
3030

31-
var_dump("{$null?->foo}");
32-
var_dump("{$null?->bar}");
33-
var_dump("{$null?->qux()}");
34-
var_dump("{$null?->quux()}");
35-
36-
var_dump("{$foo?->bar}");
37-
var_dump("{$foo?->baz}");
38-
var_dump("{$foo?->qux()}");
31+
var_dump((new Foo)?->bar);
32+
var_dump((new Foo)?->baz);
33+
var_dump((new Foo)?->qux());
3934
try {
40-
var_dump("{$foo?->quux()}");
35+
var_dump((new Foo)?->quux());
4136
} catch (Throwable $e) {
4237
var_dump($e->getMessage());
4338
}
@@ -54,13 +49,9 @@ Warning: Undefined property: Foo::$baz in %s.php on line 20
5449
NULL
5550
string(3) "qux"
5651
string(36) "Call to undefined method Foo::quux()"
57-
string(0) ""
58-
string(0) ""
59-
string(0) ""
60-
string(0) ""
6152
string(3) "bar"
6253

63-
Warning: Undefined property: Foo::$baz in %s.php on line 34
64-
string(0) ""
54+
Warning: Undefined property: Foo::$baz in %s.php on line 29
55+
NULL
6556
string(3) "qux"
6657
string(36) "Call to undefined method Foo::quux()"

Zend/tests/nullsafe_operator/033.phpt

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
--TEST--
2+
Test nullsafe operator in encaps vars
3+
--FILE--
4+
<?php
5+
6+
class Foo {
7+
public $bar = 'bar';
8+
9+
function qux() {
10+
return 'qux';
11+
}
12+
}
13+
14+
$null = null;
15+
$foo = new Foo();
16+
17+
var_dump("{$null?->foo}");
18+
var_dump("{$null?->bar()}");
19+
var_dump("$null?->foo");
20+
var_dump("$null?->bar()");
21+
22+
var_dump("{$foo?->bar}");
23+
var_dump("{$foo?->baz}");
24+
var_dump("{$foo?->qux()}");
25+
try {
26+
var_dump("{$foo?->quux()}");
27+
} catch (Throwable $e) {
28+
var_dump($e->getMessage());
29+
}
30+
31+
var_dump("$foo?->bar");
32+
var_dump("$foo?->baz");
33+
var_dump("$foo?->qux()");
34+
try {
35+
var_dump("$foo?->quux()");
36+
} catch (Throwable $e) {
37+
var_dump($e->getMessage());
38+
}
39+
40+
?>
41+
--EXPECTF--
42+
string(0) ""
43+
string(0) ""
44+
string(0) ""
45+
string(2) "()"
46+
string(3) "bar"
47+
48+
Warning: Undefined property: Foo::$baz in %s.php on line 20
49+
string(0) ""
50+
string(3) "qux"
51+
string(36) "Call to undefined method Foo::quux()"
52+
string(3) "bar"
53+
54+
Warning: Undefined property: Foo::$baz in %s.php on line 29
55+
string(0) ""
56+
57+
Warning: Undefined property: Foo::$qux in %s.php on line 30
58+
string(2) "()"
59+
60+
Warning: Undefined property: Foo::$quux in %s.php on line 32
61+
string(2) "()"

Zend/zend_language_scanner.l

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2257,15 +2257,21 @@ inline_char_handler:
22572257
}
22582258

22592259

2260-
/* Make sure a label character follows "->", otherwise there is no property
2261-
* and "->" will be taken literally
2260+
/* Make sure a label character follows "->" or "?->", otherwise there is no property
2261+
* and "->"/"?->" will be taken literally
22622262
*/
22632263
<ST_DOUBLE_QUOTES,ST_HEREDOC,ST_BACKQUOTE>"$"{LABEL}"->"[a-zA-Z_\x80-\xff] {
22642264
yyless(yyleng - 3);
22652265
yy_push_state(ST_LOOKING_FOR_PROPERTY);
22662266
RETURN_TOKEN_WITH_STR(T_VARIABLE, 1);
22672267
}
22682268

2269+
<ST_DOUBLE_QUOTES,ST_HEREDOC,ST_BACKQUOTE>"$"{LABEL}"?->"[a-zA-Z_\x80-\xff] {
2270+
yyless(yyleng - 4);
2271+
yy_push_state(ST_LOOKING_FOR_PROPERTY);
2272+
RETURN_TOKEN_WITH_STR(T_VARIABLE, 1);
2273+
}
2274+
22692275
/* A [ always designates a variable offset, regardless of what follows
22702276
*/
22712277
<ST_DOUBLE_QUOTES,ST_HEREDOC,ST_BACKQUOTE>"$"{LABEL}"[" {

0 commit comments

Comments
 (0)