Skip to content
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

Short list not tokenized correctly in control structures without braces #3632

Conversation

jrfnl
Copy link
Contributor

@jrfnl jrfnl commented Jul 14, 2022

Originally reported in sirbrillig/phpcs-variable-analysis#263.

When the short list syntax would be used as the first contents within a control structure without braces, the square brackets would be tokenized as plain square brackets, not as short array brackets.

if ( true )
    [ $a ] = [ 'hi' ]; // The first `[` in this line was tokenized incorrectly.

return $a ?? '';

Fixed now by checking whether the preceding parenthesis closer has an owner and if that owner could be a scope owner.

Includes unit tests.
Includes updating the data providers to use named data sets to allow for easier debugging.

…thout braces

Originally reported in sirbrillig/phpcs-variable-analysis 263.

When the short list syntax would be used as the first contents within a control structure without braces, the square brackets would be tokenized as plain square brackets, not as short array brackets.

```php
if ( true )
    [ $a ] = [ 'hi' ]; // The first `[` in this line was tokenized incorrectly.

return $a ?? '';
```

Fixed now by checking whether the preceding parenthesis closer has an owner and if that owner could be a scope owner.

Includes unit tests.
Includes updating the data providers to use named data sets to allow for easier debugging.
sirbrillig added a commit to sirbrillig/phpcs-variable-analysis that referenced this pull request Aug 12, 2022
sirbrillig added a commit to sirbrillig/phpcs-variable-analysis that referenced this pull request Aug 13, 2022
* Add tests for inline if list destructuring

* Support treating T_OPEN_SQUARE_BRACKET as a list opener

* Add test for foreach with list destructure

* Better differentiate between list destructuring and array access

* Use the same technique for finding list assignments as phpcs might

See squizlabs/PHP_CodeSniffer#3632
@gsherwood gsherwood added this to Idea Bank in PHPCS v3 Development via automation Sep 15, 2022
@gsherwood gsherwood added this to the 3.7.2 milestone Sep 15, 2022
@gsherwood gsherwood changed the title Tokenizer/PHP: bug fix for short list tokens in control structures without braces Short list not tokenized correctly in control structures without braces Sep 15, 2022
gsherwood added a commit that referenced this pull request Sep 15, 2022
@gsherwood gsherwood merged commit 5a85210 into squizlabs:master Sep 15, 2022
PHPCS v3 Development automation moved this from Idea Bank to Ready for Release Sep 15, 2022
@gsherwood
Copy link
Member

Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
PHPCS v3 Development
Ready for Release
Development

Successfully merging this pull request may close these issues.

None yet

2 participants