Skip to content

Squiz ArrayDeclarationSniff process a single-line array containing a multi-line array as a multiline array #1267

@AntoineRoue

Description

@AntoineRoue

Describe the bug

If I use Squiz.Arrays.ArrayDeclaration.NoCommaAfterLast rule with this kind of array below, then the first array will be treated as a multi-line array, and this rule will expect a comma between arrays end tokens.

Code sample

  1. Test code:
$a = [[
    1,
    2,
]];
  1. With Squiz.Arrays.ArrayDeclaration.NoCommaAfterLast rule, this code is expected:
$a = [[
    1,
    2,
],];

Ruleset

<?xml version="1.0"?>
<ruleset name="My Custom Standard">
  <rule ref="Squiz.Arrays.ArrayDeclaration.NoCommaAfterLast"/>
</ruleset>

To reproduce

Steps to reproduce the behavior:

  1. Create a file called test.php with the test code above.
  2. Run phpcs test.php or phpcbf test.php
  3. See that a comma is expected line 4, between both arrays end tokens.
    PHPCS output : ERROR | [x] Comma required after last value in array declaration

Expected behavior

The first array should not be detected as multi-line.

Versions (please complete the following information)

Operating System Ubuntu 24 with WSL 2 with Windows 11
PHP version 8.3
PHP_CodeSniffer version 4.0.0
Standard Squiz
Install type Composer (local)

Addtitional information

In the code, the condition deciding if an array is single or multi-line is at the end of ArrayDeclarationSniff::process:

if ($tokens[$arrayStart]['line'] === $tokens[$arrayEnd]['line']) {

Please confirm

  • I have searched the issue list and am not opening a duplicate issue.
  • I have read the Contribution Guidelines and this is not a support question.
  • 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 4.x branch of PHP_CodeSniffer.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions