Skip to content

Squiz.Arrays.ArrayDeclaration sniff gives incorrect NoComma error for multiline string values #584

@JDGrimes

Description

@JDGrimes

Consider this code (I didn't write it):

$var = array(
    'tab_template'      => '
        <li>%s</li>',
    'panel_template'        => '
        <div id="%s">
            %s
        </div>',
);

You will get two Squiz.Arrays.ArrayDeclaration.NoComma errors, even though both array values are followed by commas.

This change fixes it for me, but I don't know if it breaks anything else:

Index: CodeSniffer/Standards/Squiz/Sniffs/Arrays/ArrayDeclarationSniff.php
<+>UTF-8
===================================================================
--- CodeSniffer/Standards/Squiz/Sniffs/Arrays/ArrayDeclarationSniff.php (revision 8b75b9c1d842642ecf8997bc3691f0077a5f2546)
+++ CodeSniffer/Standards/Squiz/Sniffs/Arrays/ArrayDeclarationSniff.php (revision )
@@ -837,7 +837,7 @@
                 }
             }//end for

-            if ($nextComma === false || ($tokens[$nextComma]['line'] !== $valueLine)) {
+            if ($nextComma === false) {
                 $error = 'Each line in an array declaration must end in a comma';
                 $fix   = $phpcsFile->addFixableError($error, $index['value'], 'NoComma');

Related: #582.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions