Skip to content

Improve handling of #[ attributes in php -a #6085

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

Conversation

TysonAndre
Copy link
Contributor

@TysonAndre TysonAndre commented Sep 5, 2020

a.k.a. "The handling of #[ in php -a is terrible, are we sure we're OK with it?"

PHP treats #ini_setting=value as a call to
ini_set('ini_setting', 'value') (only in php -a) ,
and silently skips undeclared settings.

This is a problem due to #[ becoming supported attribute syntax:

  • #[Attr] const X = 123; (this is not a valid place to put an attribute)
    This does not create a constant.
  • #[Attr] function test($x=false){} also contains =.
    This does not create a function.

Instead, only treat lines starting with # as a special case
when the next character isn't [

PHP treats `#ini_setting=value` as a call to
`ini_set('ini_setting', 'value')`,
and silently skips undeclared settings.

This is a problem due to `#[` becoming supported attribute syntax:

- `#[Attr] const X = 123;` (this is not a valid place to put an attribute)
  This does not create a constant.
- `#[Attr] function test($x=false){}` also contains `=`.
  This does not create a function.

Instead, only treat lines starting with `#` as a special case
when the next character isn't `[`
@TysonAndre TysonAndre force-pushed the readline-attribute-eval branch from b3dd7f1 to 674945f Compare September 5, 2020 21:11
@php-pulls php-pulls closed this in 9439ca5 Sep 5, 2020
TysonAndre added a commit to TysonAndre/php-src that referenced this pull request Sep 6, 2020
`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 phpGH-6085
php-pulls pushed a commit that referenced this pull request Sep 6, 2020
`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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants