Skip to content

Commit 877dbbb

Browse files
committed
zend_ast: Surround function by parens when exporting calls to function stored in property
The extra parentheses are needed to disambiguate method calls from calls to a function stored in a property. Fixes #22373.
1 parent 0fff3cc commit 877dbbb

3 files changed

Lines changed: 25 additions & 1 deletion

File tree

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ PHP NEWS
2828
invalid variable names). (timwolla)
2929
. Fixed bug GH-22291 (AST pretty printing does not correctly handle braces
3030
in string interpolation). (timwolla)
31+
. Fixed bug GH-22373 (AST pretty-printing drops meaningful parentheses
32+
surrounding property access). (timwolla)
3133

3234
- BCMath:
3335
. Added NUL-byte validation to BCMath functions. (jorgsowa)

Zend/zend_ast.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2535,7 +2535,7 @@ static ZEND_COLD void zend_ast_export_ex(smart_str *str, zend_ast *ast, int prio
25352535
break;
25362536
case ZEND_AST_CALL: {
25372537
zend_ast *left = ast->child[0];
2538-
if (left->kind == ZEND_AST_ARROW_FUNC || left->kind == ZEND_AST_CLOSURE) {
2538+
if (left->kind == ZEND_AST_ARROW_FUNC || left->kind == ZEND_AST_CLOSURE || left->kind == ZEND_AST_PROP) {
25392539
smart_str_appendc(str, '(');
25402540
zend_ast_export_ns_name(str, left, 0, indent);
25412541
smart_str_appendc(str, ')');
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
--TEST--
2+
GH-22292: AST pretty-printing drops meaningful parentheses surrounding property access
3+
--FILE--
4+
<?php
5+
6+
class Foo {
7+
public function __construct(
8+
public Closure $f = strrev(...),
9+
) {
10+
try {
11+
assert(($this->f)('abc') !== 'cba');
12+
} catch (Error $e) {
13+
echo $e->getMessage(), PHP_EOL;
14+
}
15+
}
16+
}
17+
18+
new Foo();
19+
20+
?>
21+
--EXPECT--
22+
assert(($this->f)('abc') !== 'cba')

0 commit comments

Comments
 (0)