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

Fixer breaks the file when code violates both PEAR.Functions.FunctionDeclaration and Squiz.WhiteSpace.ScopeClosingBrace #3736

Closed
ODAEL opened this issue Jan 4, 2023 · 3 comments · Fixed by PHPCSStandards/PHP_CodeSniffer#52

Comments

@ODAEL
Copy link

ODAEL commented Jan 4, 2023

Describe the bug
If there is a code that violates both PEAR.Functions.FunctionDeclaration and Squiz.WhiteSpace.ScopeClosingBrace sniffs, the fixer (CBF) will break the file because of deleting the extra brace.

Code sample

<?php

class Test
{
    public function __construct(
        protected int $id
    )
    {}
}

Custom ruleset

<?xml version="1.0"?>
<ruleset name="My Custom Standard">
    <rule ref="PEAR.Functions.FunctionDeclaration"/>
    <rule ref="Squiz.WhiteSpace.ScopeClosingBrace"/>
</ruleset>

To reproduce
Steps to reproduce the behavior:

  1. Create a file called test.php with the code sample above...
  2. Run phpcbf test.php ...
  3. See test.php
<?php

class Test
{
    public function __construct(
        protected int $id
    ) {
    
    }

Expected behavior
Fixer should not "eat" the closing brace of the class.

Versions (please complete the following information):

  • OS: MacOS 12.6
  • PHP: 8.1
  • PHPCS: 3.7.1
  • Standard: PEAR.Functions.FunctionDeclaration, Squiz.WhiteSpace.ScopeClosingBrace
@jrfnl
Copy link
Contributor

jrfnl commented Jan 5, 2023

Thanks for reporting this. I can confirm the bug and it is unrelated to the Squiz.WhiteSpace.ScopeClosingBrace sniff and solely caused by the PEAR.Functions.FunctionDeclaration sniff in combination with the {} (i.e. nothing between the open and close brace).

This can be confirmed by using the following code sample:

// Removes the close brace of the function declaration.
class Test
{
    public function __construct(
        protected int $id
    )
    {}
}

// Fixes fine, no problems.
class Test
{
    public function __construct(
        protected int $id
    )
    { }
}

// Fixes fine, no problems.
class Test
{
    public function __construct(
        protected int $id
    )
    {
		// comment
    }
}

The changeset for the first two code samples is as follows:

        => Changeset started by PEAR.Functions.FunctionDeclaration:332
                Q: PEAR.Functions.FunctionDeclaration:333 replaced token 24 (T_CLOSE_PARENTHESIS on line 7) ")" => ") {"
                Q: PEAR.Functions.FunctionDeclaration:351 replaced token 27 (T_OPEN_CURLY_BRACKET on line 8) "{" => ""
                Q: PEAR.Functions.FunctionDeclaration:353 replaced token 28 (T_CLOSE_CURLY_BRACKET on line 8) "}" => ""
                A: PEAR.Functions.FunctionDeclaration:357 replaced token 24 (T_CLOSE_PARENTHESIS on line 7) ")" => ") {"
                A: PEAR.Functions.FunctionDeclaration:357 replaced token 27 (T_OPEN_CURLY_BRACKET on line 8) "{" => ""
                A: PEAR.Functions.FunctionDeclaration:357 replaced token 28 (T_CLOSE_CURLY_BRACKET on line 8) "}" => ""
        => Changeset ended: 3 changes applied
        => Changeset started by PEAR.Functions.FunctionDeclaration:332
                Q: PEAR.Functions.FunctionDeclaration:333 replaced token 55 (T_CLOSE_PARENTHESIS on line 15) ")" => ") {"
                Q: PEAR.Functions.FunctionDeclaration:351 replaced token 58 (T_OPEN_CURLY_BRACKET on line 16) "{" => ""
                Q: PEAR.Functions.FunctionDeclaration:353 replaced token 59 (T_WHITESPACE on line 16) " }" => "}"
                A: PEAR.Functions.FunctionDeclaration:357 replaced token 55 (T_CLOSE_PARENTHESIS on line 15) ")" => ") {"
                A: PEAR.Functions.FunctionDeclaration:357 replaced token 58 (T_OPEN_CURLY_BRACKET on line 16) "{" => ""
                A: PEAR.Functions.FunctionDeclaration:357 replaced token 59 (T_WHITESPACE on line 16) " }" => "}"

And the problem is clearly visible in the first changeset:

                Q: PEAR.Functions.FunctionDeclaration:353 replaced token 28 (T_CLOSE_CURLY_BRACKET on line 8) "}" => ""

@jrfnl
Copy link
Contributor

jrfnl commented Jan 5, 2023

PR #3739 should fix this. Testing appreciated.

@jrfnl
Copy link
Contributor

jrfnl commented Dec 8, 2023

FYI: the fix for this issue is included in today's PHP_CodeSniffer 3.8.0 release.

As per #3932, development on PHP_CodeSniffer will continue in the PHPCSStandards/PHP_CodeSniffer repository. If you want to stay informed, you may want to start "watching" that repo (or watching releases from that repo).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment