WordPress / WordPress/WordPress-Coding-Standards

Incorrect array indentation with a single line comment inside the array

Open
#2,474 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
PHP
Stars
2.8k
Forks
521
Avg merge
5d 20h
Merged PRs (30d)
1

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with WordPress/Tests/Arrays/ArrayIndentationUnitTest.1.inc and its .fixed counterpart, then run the ArrayIndentationUnitTest to reproduce the reported errors. Trace the WordPress.Arrays.ArrayIndentation behavior for a single-line comment inside an array; done means the item and comment use the expected indentation and the test passes.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.