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

PEAR/FunctionDeclaration: prevent fixer conflict for unfinished closures/live coding #827

Merged

Conversation

jrfnl
Copy link
Member

@jrfnl jrfnl commented Feb 16, 2025

Description

The PEAR.Functions.FunctionDeclaration sniff contained code to protect against a fixer conflict for unfinished closures, however, this code did not work correctly as an unfinished closure will generally also not have a function body, which "undoes" the protection via the scope opener check.

In other words, the fixer conflict still existed and would result in one part of the sniff trying to add a space between the function keyword and the open parenthesis, while another part of the sniff would be removing that space again.

        => Fixing file: 1/1 violations remaining
        PEAR.Functions.FunctionDeclaration:124 replaced token 11 (T_WHITESPACE on line 7) " (" => "("
        => Fixing file: 1/1 violations remaining [made 1 pass]...
        * fixed 1 violations, starting loop 2 *
        PEAR.Functions.FunctionDeclaration:94 replaced token 10 (T_FUNCTION on line 7) "function" => "function "
        => Fixing file: 1/1 violations remaining [made 2 passes]...
        * fixed 1 violations, starting loop 3 *
        PEAR.Functions.FunctionDeclaration:124 replaced token 11 (T_WHITESPACE on line 7) " (" => "("
        => Fixing file: 1/1 violations remaining [made 3 passes]...
        * fixed 1 violations, starting loop 4 *
        PEAR.Functions.FunctionDeclaration:94 replaced token 10 (T_FUNCTION on line 7) "function" => "function "
        => Fixing file: 1/1 violations remaining [made 4 passes]...
        * fixed 1 violations, starting loop 5 *

Fixed now by verifying if the function is named instead. That way we can be sure it's not a closure.

Includes test.

Suggested changelog entry

PEAR.Functions.FunctionDeclaration could get into a fixer conflict over an unfinished closure during live coding

Related issues/external references

Builds on the previously pulled fix from PR #816.

Related to #152

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

…res/live coding

The `PEAR.Functions.FunctionDeclaration` sniff contained code to protect against a fixer conflict for unfinished closures, however, this code did not work correctly as an unfinished closure will generally also not have a function body, which "undoes" the protection via the scope opener check.

In other words, the fixer conflict still existed and would result in one part of the sniff trying to _add_ a space between the `function` keyword and the open parenthesis, while another part of the sniff would be removing that space again.

```
        => Fixing file: 1/1 violations remaining
        PEAR.Functions.FunctionDeclaration:124 replaced token 11 (T_WHITESPACE on line 7) " (" => "("
        => Fixing file: 1/1 violations remaining [made 1 pass]...
        * fixed 1 violations, starting loop 2 *
        PEAR.Functions.FunctionDeclaration:94 replaced token 10 (T_FUNCTION on line 7) "function" => "function "
        => Fixing file: 1/1 violations remaining [made 2 passes]...
        * fixed 1 violations, starting loop 3 *
        PEAR.Functions.FunctionDeclaration:124 replaced token 11 (T_WHITESPACE on line 7) " (" => "("
        => Fixing file: 1/1 violations remaining [made 3 passes]...
        * fixed 1 violations, starting loop 4 *
        PEAR.Functions.FunctionDeclaration:94 replaced token 10 (T_FUNCTION on line 7) "function" => "function "
        => Fixing file: 1/1 violations remaining [made 4 passes]...
        * fixed 1 violations, starting loop 5 *
```

Fixed now by verifying if the function is named instead. That way we can be sure it's not a closure.

Includes test.

Builds on the previously pulled fix from PR 816.

Related to #152
@jrfnl
Copy link
Member Author

jrfnl commented Feb 18, 2025

Thanks for reviewing @fredden !

@jrfnl jrfnl merged commit f947742 into master Feb 18, 2025
59 checks passed
@jrfnl jrfnl deleted the feature/pear-functiondeclaration-prevent-fixer-conflict branch February 18, 2025 01:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants