Php_codesniffer: PHP close tag after opening brace

Created on 27 Oct 2017  路  7Comments  路  Source: squizlabs/PHP_CodeSniffer

OpeningFunctionBraceKernighanRitchieSniff disallows the following:

<?php foo(function ($bar) { ?>
    <div><?php echo $bar; ?></div>
<?php }); ?>

and I think it should be allowed. In fact, it's as easy fix:

https://github.com/squizlabs/PHP_CodeSniffer/blob/master/src/Standards/Generic/Sniffs/Functions/OpeningFunctionBraceKernighanRitchieSniff.php#L124
should change from:

- if ($next === $tokens[$stackPtr]['scope_closer']) {
+ if ($next === $tokens[$stackPtr]['scope_closer'] || $tokens[$next]['code'] == T_CLOSE_TAG) {

I know this project deals with just PHP and not HTML. However, we have hundreds of these and was hoping you would accept a PR for this.

All 7 comments

It would also be nice if it handled this sort of situation:

<?php if ($someVar): ?>
<div><?php echo $someOtherVar; ?></div>
<?php endif; ?>

@josephzidell I don't get any errors from the sniff when running on your test code. Do you get errors when running on that code alone or is it contained inside a larger file? And what PHPCS version are you using?

@mwgamble The OpeningFunctionBraceKernighanRitchie sniff only deals with function braces. You probably want changes to a different sniff, but you'd have to report that separately with more info.

@gsherwood I get the error either way. The message is "Opening brace must be the last content on the line". I'm currently using v 3.1.1.

Thanks, I've figured it out. This sniff doesn't check closure by default, so you have probably turned that on, or are using a sniff that has turned it on. I was using the default setting so couldn't see the error message. I'll take a look into this now.

I appreciate you taking the time to address this

Looked like a good change to me, and your code worked fine. Committed now. Thanks a lot.

Cheers!

Was this page helpful?
0 / 5 - 0 ratings