squizlabs / PHP_CodeSniffer

PHP_CodeSniffer tokenizes PHP files and detects violations of a defined set of coding standards.
BSD 3-Clause "New" or "Revised" License
10.67k stars 1.48k forks source link

phpcbf fails to fix function call when there is a trailing comma immediately before a closing parenthesis #3836

Closed scorgn closed 1 year ago

scorgn commented 1 year ago

Describe the bug

phpcbf fails to fix a file when it has a call to a function where all the following are true:

The two conflicting rules are:

Code samples

phpcbf will fail to fix the following code samples:

<?php
var_dump('foobar',);
<?php
var_dump('foobar'
   ,

);
<?php
Foo
::bar('foobar',);
<?php
Foo::{
'bar'
}('foobar',);

However phpcbf will successfully fix the following code sample without failing:

<?php
var_dump
('foobar',);

To reproduce

Steps to reproduce the behavior:

  1. Create a file called test.php with any of the failing code samples above...
  2. Run phpcbf --standard=PSR2 --sniffs=Generic.Functions.FunctionCallArgumentSpacing,PSR2.Methods.FunctionCallSignature test.php
  3. See error message displayed
    PHPCBF RESULT SUMMARY
    ----------------------------------------------------------------------------------------
    FILE                                                                    FIXED  REMAINING
    ----------------------------------------------------------------------------------------
    .../test.php     FAILED TO FIX
    ----------------------------------------------------------------------------------------
    A TOTAL OF 0 ERRORS WERE FIXED IN 1 FILE
    ----------------------------------------------------------------------------------------
    PHPCBF FAILED TO FIX 1 FILE
    ----------------------------------------------------------------------------------------

Expected behavior

It may not be appropriate for phpcs to remove the trailing comma, so my expected behavior would be that phpcs does not mark the line as automatically fixable and that phpcbf does not attempt to fix the issue - and that phpcbf does not fail to fix the file.

Versions (please complete the following information)

Operating System MacOS 12.6.5
PHP version PHP 8.0.28
PHP_CodeSniffer version PHP_CodeSniffer version 3.7.2 (stable) by Squiz (http://www.squiz.net)
Standard PSR2
Install type Composer (local)

Please confirm:

jrfnl commented 1 year ago

Closing as duplicate of #3477 and #3707, which were both already fixed via #3805.

@scorgn I don't know why you checked the "I have verified the issue still exists in the master branch of PHP_CodeSniffer." (or the "I have searched the issue list and am not opening a duplicate issue.") checkboxes ? Any particular reason ? I honestly cannot reproduce the issue anymore with the current master branch, so I don't understand how you could... ?

scorgn commented 1 year ago

Apologies, I'm now realizing that my .zshrc kicked in and was still using the global installation of phpcs rather than the one in the local folder.

I did search through existing issues but I wasn't able to find those, I will try to be more diligent looking in the future.

jrfnl commented 1 year ago

@scorgn Thanks for getting back to me. The fix will be included in the next (3.8.0) release.