Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
JDGrimes opened this issue May 3, 2015 · 6 comments

Comments

@JDGrimes
Copy link
Contributor

JDGrimes commented May 3, 2015

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.

@aik099
Copy link
Contributor

aik099 commented May 3, 2015

That's tricky, because it's a multi-line string, that is tokenized as single token and only line number it has is when it starts.

JDGrimes added a commit to WordPress/WordPress-Coding-Standards that referenced this issue May 3, 2015
This basically synchronizes it with `Squiz.Arrays.ArrayDeclaration`.
The upstream version is similar, except that we exclude a few errors.
Unfortunately we have to actually comment out the code rather than just
using the upstream sniff and `<exclude>` in our ruleset, due to a bug
(squizlabs/PHP_CodeSniffer#582). (I’ve also included a fix for another
bug, squizlabs/PHP_CodeSniffer#584.) Because of this, we cannot yet
eliminate duplicated logic from this child sniff.

I think this pretty much maintains the previous behavior, except that I
have added a warning for extra whitespace at the edges of multiline
arrays.
@JDGrimes
Copy link
Contributor Author

JDGrimes commented May 3, 2015

That's tricky, because it's a multi-line string, that is tokenized as single token and only line number it has is when it starts.

Nope, it isn't tokenized as a single token, each line is a separate token.

@aik099
Copy link
Contributor

aik099 commented May 3, 2015

Then instead of removing || ($tokens[$nextComma]['line'] !== $valueLine) you should be checking line of last token belonging to that array value.

@JDGrimes
Copy link
Contributor Author

JDGrimes commented May 3, 2015

Yeah, but I can't figure out what that check is even for. I'm not sure it's needed.

@gsherwood
Copy link
Member

Thanks for reporting. Issue is fixed now. Just needed to treat multi-line strings as one argument, like the sniff does for other multi-line values such as closures.

@JDGrimes
Copy link
Contributor Author

JDGrimes commented May 4, 2015

Thanks for the quick fix @gsherwood! 🎉

vrestihnat pushed a commit to vrestihnat/PHP_CodeSniffer_Slevomat that referenced this issue Sep 17, 2024
…r-php-add-tests-heredoc-nowdoc

Tokenizer/PHP: add tests for heredoc/nowdoc tokenization
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants