]> granicus.if.org Git - php/commitdiff
Improve handling of `#[` attributes in `php -a`
authorTyson Andre <tysonandre775@hotmail.com>
Sun, 6 Sep 2020 13:43:09 +0000 (09:43 -0400)
committerTyson Andre <tysonandre775@hotmail.com>
Sun, 6 Sep 2020 14:40:26 +0000 (10:40 -0400)
`php -a` treats lines starting with `#` as comments when deciding if
the provided statement is valid.

So it passed `#[MyAttr]` to the parser after the user hits enter,
causing a syntax error for multi-line statements..

With this patch, the following snippet is parsed correctly

```
php > #[Attr]
php > function x() { }
php > var_export((new ReflectionFunction('x'))->getAttributes()[0]->getName());
'Attr'
```

Followup to GH-6085

Closes GH-6086

ext/readline/readline_cli.c

index d7e6f20c9fbd8fbff70da87c680bef26251f7eea..2930796ae7605580a47e70d80a5ef9291656b76a 100644 (file)
@@ -250,6 +250,10 @@ static int cli_is_valid_code(char *code, size_t len, zend_string **prompt) /* {{
                                                code_type = dstring;
                                                break;
                                        case '#':
+                                               if (code[i+1] == '[') {
+                                                       valid_end = 0;
+                                                       break;
+                                               }
                                                code_type = comment_line;
                                                break;
                                        case '/':