-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Closed
PHPCSStandards/PHP_CodeSniffer
#93Milestone
Description
Describe the bug
In the (intentionally bad) snippet of code below, I have a method defined on the same line as its class. When running PHPCS on it with the Squiz.WhiteSpace.FunctionSpacing
sniff enabled (ruleset below), it reports that 1 blank line is expected before the method. However, if I then try to autofix that with phpcbf, it tries to add the line in the wrong place, thus going into an infinite loop because it detects that the issue still hasn't been fixed; it only aborts upon reaching the limit of 50 passes.
Code sample
<?php
class Person { public function __construct($name){} }
Custom ruleset
<?xml version="1.0"?>
<ruleset>
<arg name="tab-width" value="4" />
<rule ref="Squiz.WhiteSpace.FunctionSpacing">
<properties>
<property name="spacing" value="1" />
</properties>
</rule>
</ruleset>
To reproduce
Steps to reproduce the behavior:
- Create a file called
test.php
with the code sample above - Run
phpcs test.php
- See error message displayed
----------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
----------------------------------------------------------------------
3 | ERROR | [x] Expected 1 blank line before function; 0 found
----------------------------------------------------------------------
PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------
- Run
phpcbf -vvv test.php
- See that it fails to fix the file. The full output shows that it's adding the line right after the PHP open tag:
(... all the previous passes...)
---START FILE CONTENT---
1|<?php
2|
3|
4|
5|
6|
7|
8|
9|
10|
11|
12|
13|
14|
15|
16|
17|
18|
19|
20|
21|
22|
23|
24|
25|
26|
27|
28|class Person { public function __construct($name){} }
29|
30|
--- END FILE CONTENT ---
Squiz.WhiteSpace.FunctionSpacing:342 replaced token 0 (T_OPEN_TAG on line 1) "<?php\n" => "<?php\n\n"
=> Fixing file: 1/1 violations remaining [made 50 passes]...
* fixed 1 violations, starting loop 51 *
*** Reached maximum number of loops with 1 violations left unfixed ***
ERROR in 33ms
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
Ideally, the issue should be fixed. Or if that's hard to do, at least PHPCS should not go into an endless loop, and instead report the issue as unfixable.
Versions (please complete the following information)
Operating System | Ubuntu 22.04 |
PHP version | 8.2.11 |
PHP_CodeSniffer version | master |
Standard | See above |
Install type | Composer local |
Please confirm:
- I have searched the issue list and am not opening a duplicate issue.
- I confirm that this bug is a bug in PHP_CodeSniffer and not in one of the external standards.
- I have verified the issue still exists in the
master
branch of PHP_CodeSniffer.