Skip to content

Fix ?-> in encaps vars without braces #5966

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
29 changes: 10 additions & 19 deletions Zend/tests/nullsafe_operator/003.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,20 @@ var_dump(null?->baz);
var_dump(null?->qux());
var_dump(null?->quux());

var_dump((new Foo)?->bar);
var_dump((new Foo)?->baz);
var_dump((new Foo)?->qux());
var_dump($foo?->bar);
var_dump($foo?->baz);
var_dump($foo?->qux());
try {
var_dump((new Foo)?->quux());
var_dump($foo?->quux());
} catch (Throwable $e) {
var_dump($e->getMessage());
}

var_dump("{$null?->foo}");
var_dump("{$null?->bar}");
var_dump("{$null?->qux()}");
var_dump("{$null?->quux()}");

var_dump("{$foo?->bar}");
var_dump("{$foo?->baz}");
var_dump("{$foo?->qux()}");
var_dump((new Foo)?->bar);
var_dump((new Foo)?->baz);
var_dump((new Foo)?->qux());
try {
var_dump("{$foo?->quux()}");
var_dump((new Foo)?->quux());
} catch (Throwable $e) {
var_dump($e->getMessage());
}
Expand All @@ -54,13 +49,9 @@ Warning: Undefined property: Foo::$baz in %s.php on line 20
NULL
string(3) "qux"
string(36) "Call to undefined method Foo::quux()"
string(0) ""
string(0) ""
string(0) ""
string(0) ""
string(3) "bar"

Warning: Undefined property: Foo::$baz in %s.php on line 34
string(0) ""
Warning: Undefined property: Foo::$baz in %s.php on line 29
NULL
string(3) "qux"
string(36) "Call to undefined method Foo::quux()"
61 changes: 61 additions & 0 deletions Zend/tests/nullsafe_operator/031.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
--TEST--
Test nullsafe operator in encaps vars
--FILE--
<?php

class Foo {
public $bar = 'bar';

function qux() {
return 'qux';
}
}

$null = null;
$foo = new Foo();

var_dump("{$null?->foo}");
var_dump("{$null?->bar()}");
var_dump("$null?->foo");
var_dump("$null?->bar()");

var_dump("{$foo?->bar}");
var_dump("{$foo?->baz}");
var_dump("{$foo?->qux()}");
try {
var_dump("{$foo?->quux()}");
} catch (Throwable $e) {
var_dump($e->getMessage());
}

var_dump("$foo?->bar");
var_dump("$foo?->baz");
var_dump("$foo?->qux()");
try {
var_dump("$foo?->quux()");
} catch (Throwable $e) {
var_dump($e->getMessage());
}

?>
--EXPECTF--
string(0) ""
string(0) ""
string(0) ""
string(2) "()"
string(3) "bar"

Warning: Undefined property: Foo::$baz in %s.php on line 20
string(0) ""
string(3) "qux"
string(36) "Call to undefined method Foo::quux()"
string(3) "bar"

Warning: Undefined property: Foo::$baz in %s.php on line 29
string(0) ""

Warning: Undefined property: Foo::$qux in %s.php on line 30
string(2) "()"

Warning: Undefined property: Foo::$quux in %s.php on line 32
string(2) "()"
10 changes: 8 additions & 2 deletions Zend/zend_language_scanner.l
Original file line number Diff line number Diff line change
Expand Up @@ -2259,15 +2259,21 @@ inline_char_handler:
}


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

<ST_DOUBLE_QUOTES,ST_HEREDOC,ST_BACKQUOTE>"$"{LABEL}"?->"[a-zA-Z_\x80-\xff] {
yyless(yyleng - 4);
yy_push_state(ST_LOOKING_FOR_PROPERTY);
RETURN_TOKEN_WITH_STR(T_VARIABLE, 1);
}

/* A [ always designates a variable offset, regardless of what follows
*/
<ST_DOUBLE_QUOTES,ST_HEREDOC,ST_BACKQUOTE>"$"{LABEL}"[" {
Expand Down