Squiz.WhiteSpace.LanguageConstructSpacing does not properly check for tabs and newlines by michalbundyra · Pull Request #1573 · squizlabs/PHP_CodeSniffer · GitHub
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.WhiteSpace.LanguageConstructSpacing does not properly check for tabs and newlines #1573

Merged

Conversation

michalbundyra
Copy link
Contributor

As we discussed in #1337 (comment) I've created this PR to issues noted in PR #1337

It could be another whitespace character, like "\t", "\n", ...
Now it will be always converted to space.
@michalbundyra
Copy link
Contributor Author

@gmponos please have a look

@@ -30,3 +30,10 @@ return;
return $blah;
return $blah;
return($blah);

return $tab;
return
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that this change here will raise a lot of issues. According to my understanding (maybe I am wrong) the goal of this sniff is to ensure that there are no two spaces between language constructs and $variables/functions etc.

So as I understand it the goal is to avoid this:

return  ['test' => 'value'];

and not this:

return  
    [
        'test' => 'value'
    ];

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have right, but sniff description says:

Ensures all language constructs contain a single space between themselves and their content.

And I think that it is exactly what the sniff should do, we didn't have covered that case previously in tests, so I think @gsherwood is gonna decide what to do.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That could be avoided by checking for ' ' or $phpcsFile->eolChar

Copy link
Contributor

@glen-84 glen-84 May 6, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To work around this problem, you can use:

return (
    [
        'test' => 'value'
    ]
);

Edit: Or just:

return [
    'test' => 'value'
];

But in my case I have stuff like:

return
    $qb->from(Entities\Player::class, 'p')
        ->leftJoin('p.user', 'u')
        ->select('p', 'u')
        ->where('CASE WHEN p.name IS NULL THEN u.username ELSE p.name END = ?1')
        ->setParameter(1, $name)
        ->getQuery()
        ->getOneOrNullResult();

... so the parentheses work better.

$content = $tokens[($stackPtr + 1)]['content'];
if ($content !== ' ') {
$error = 'Language constructs must be followed by a single space; expected 1 space but found "%s"';
$data = array($tokenContent = Util\Common::prepareForOutput($content));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally I avoid assignments that can be tricky to the eye. I would've added the assignment on a different line.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that I see it more carefully the $tokenContent is not used anywhere. So why is it assinged to a variable first?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was my mistake, the variable shouldn't be there, thanks. Now removed.

@gsherwood gsherwood added this to the 3.2.0 milestone Jul 24, 2017
return $tab;
return
$newLine;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if this is gonna be merged I believe it should cover one more test case. What happens if there is a single space after return and then a new line.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gmponos I've added this test case and it seems to be fine 😄 Please see 5324d96

@gmponos
Copy link
Contributor

gmponos commented Nov 25, 2017

@gsherwood can you check on this PR I need for another one?

@gsherwood
Copy link
Member

The intention of this sniff is for a newline to be banned. That's why a return followed by space newline is auto-fixed by removing the newline character. It's just an old sniff I wrote before I knew better, so this fix for tabs and newlines looks correct.

@gsherwood gsherwood changed the title Hotfix - Squiz.WhiteSpace.LanguageConstructSpacing - tabs and new lines Squiz.WhiteSpace.LanguageConstructSpacing does not properly check for tabs and newlines Nov 27, 2017
@gsherwood gsherwood merged commit 5324d96 into squizlabs:master Nov 27, 2017
@gsherwood
Copy link
Member

Thanks for this fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants