WordPress/WordPress-Coding-Standards

Incorrect array indentation with a single line comment inside the array

aymanrady opened this issue · 0 comments

Bug Description

Extra tabs are inserted on the next line after the comment.

Minimal Code Snippet

The issue happens when running this command:

phpcbf --standard=WordPress

... over a file containing this code:

array(
// a comment
	'value'
);

The file was auto-fixed via phpcbf to:

<?php

array(
	// a comment
		'a',
);

... while I expected the code to be fixed to:

<?php

array(
	// a comment
	'a',
);

Error Code

WordPress.Arrays.ArrayIndentation

Environment

Question Answer
PHP version 8.2.22
PHP_CodeSniffer version 3.10.2
WordPressCS version 3.1.0
PHPCSUtils version 1.0.12
PHPCSExtra version 1.2.1
WordPressCS install type composer project local

Additional Context (optional)

Ran unit tests with this patch

diff --git a/WordPress/Tests/Arrays/ArrayIndentationUnitTest.1.inc b/WordPress/Tests/Arrays/ArrayIndentationUnitTest.1.inc
index 75dd1162..65f2fe6a 100644
--- a/WordPress/Tests/Arrays/ArrayIndentationUnitTest.1.inc
+++ b/WordPress/Tests/Arrays/ArrayIndentationUnitTest.1.inc
@@ -429,6 +429,11 @@ $my_array = [
        'something_else',
 ];
 
+$my_array = [
+// A comment
+       'value1'
+];
+
 /*
  * Issue #1692: Test distinguishing between short array and short list.
  */
diff --git a/WordPress/Tests/Arrays/ArrayIndentationUnitTest.1.inc.fixed b/WordPress/Tests/Arrays/ArrayIndentationUnitTest.1.inc.fixed
index 99a98589..254ddc96 100644
--- a/WordPress/Tests/Arrays/ArrayIndentationUnitTest.1.inc.fixed
+++ b/WordPress/Tests/Arrays/ArrayIndentationUnitTest.1.inc.fixed
@@ -429,6 +429,11 @@ $my_array = [
        'something_else',
 ];
 
+$my_array = [
+       // A comment
+       'value1'
+];
+
 /*
  * Issue #1692: Test distinguishing between short array and short list.
  */
There was 1 failure:

1) WordPressCS\WordPress\Tests\Arrays\ArrayIndentationUnitTest::testSniff
[LINE 433] Expected 0 error(s) in ArrayIndentationUnitTest.1.inc but found 1 error(s). The error(s) found were:
 -> Array item not aligned correctly; expected 4 spaces but found 0 (WordPress.Arrays.ArrayIndentation.ItemNotAligned)
[LINE 434] Expected 0 error(s) in ArrayIndentationUnitTest.1.inc but found 1 error(s). The error(s) found were:
 -> Multi-line array item not aligned correctly; expected 8 spaces, but found 4 (WordPress.Arrays.ArrayIndentation.MultiLineArrayItemNotAligned)
Fixed version of ArrayIndentationUnitTest.1.inc does not match expected version in ArrayIndentationUnitTest.1.inc.fixed; the diff is
--- WordPress/Tests/Arrays/ArrayIndentationUnitTest.1.inc.fixed
+++ PHP_CodeSniffer
@@ -431,7 +431,7 @@
 
 $my_array = [
        // A comment
-       'value1'
+               'value1'
 ];
 
 /*

/home/aymanrady/Development/open-source/WordPress-Coding-Standards/vendor/squizlabs/php_codesniffer/tests/Standards/AbstractSniffUnitTest.php:214
/home/aymanrady/Development/open-source/WordPress-Coding-Standards/vendor/squizlabs/php_codesniffer/tests/TestSuite7.php:28

Tested Against develop Branch?

  • I have verified the issue still exists in the develop branch of WordPressCS.