-
-
Notifications
You must be signed in to change notification settings - Fork 493
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
Newline required after opening brace triggered for allowed loop syntax #1085
Comments
Where you put the <?php
if ( have_posts() ) :
?>
<div class="hfeed">
<?php
while ( have_posts() ) :
the_post();
?>
<article id="post-<?php the_ID() ?>" class="<?php post_class() ?>">
<!-- ... -->
</article>
<?php
endwhile;
?>
</div>
<?php
endif;
?> Would likely pass. |
Maybe I mislabeled the ticket.
My opinion is that new lines should not be required for these cases. |
Updated the ticket title (the first title I wrote before diving deeper into the issue to debug) |
This was previously discussed in #1000 and the requirement for PHP open/close tags to be on their own lines for multi-statement embedded PHP has been added explicitly to the WP Core Handbook: So, no, this is not "allowed in the handbook". |
The reference @jrfnl gave leads to:
Though, to be fair, that's a little confusing when it relates to, for instance |
Yeah, they are allowed to both be on the same line, but if only one of them is on the line, then it has to be by itself without other code. Which is why "multi-line" is the important word there. But this isn't about the tags, it is about brace style. And the handbook does indeed include this example: <?php if ( have_posts() ) : ?>
<div class="hfeed">
<?php while ( have_posts() ) : the_post(); ?>
<article id="post-<?php the_ID() ?>" class="<?php post_class() ?>">
<!-- ... -->
</article>
<?php endwhile; ?>
</div>
<?php endif; ?> https://make.wordpress.org/core/handbook/best-practices/coding-standards/php/#brace-style The third line ( It could be changed to this:
And that would also pass. But I think this pattern is common enough that we might want to consider making a special allowance for it. |
Actually, it's not about "multi-line", but about "multi-statement". This is not clear enough in the handbook and I think it would be good to clarify this there. Regarding the brace style example - IMHO it would be better to change that code snippet in the handbook to the below: <?php
while ( have_posts() ) :
the_post();
?>
The reason the handbook was adjusted and this sniff was added to the As it's been decided to add it, I would very much be in favour of applying it consistently and not having semi-random "exceptions". |
I'm in agreement here - just the wording / example in the Handbook needs tweaking to make it clearer. |
For some more context, Codex actually displays this loop format as well (acknowledging that parts of the Codex are outdated). My point was only that this is very common out in the wild, and many learning resources I have seen people use have taught this style. Not against change, but just wanted to raise that this is very common, and may be worth looking at further. |
I'm fine with updating the handbook and codex, I just wanted to make sure that we're all on the same page with regard to the fact that this has basically been the de facto standard up to now. Personally, I think that one statement per line is far more readable; it's easy to miss the second statement. So I'm happy to just say this isn't really feasible technically to add an exception for, and move on. |
Could it be a |
I disagree about this being specific to loop or needing an exception for it. In my opinion rule that is specifically about braces formatting is irrelevant to alternative syntax. Alternative syntax is better suited for being mixed in HTML in some cases, exactly because braces and rigid formatting can get in the way of producing neat meaningful readable template. I think this rule should just stay away from alternative |
I agree with @Rarst here. With HTML templating, you sometimes need to avoid extra whitespace. While you can get around this sniff by |
Any progress on this? I agree that an exception should be added. The Loop is such a fundamental part of Wordpress development that an exception makes sense. |
The WordPress coding standards state:
The current version of the standard does not allow this though.
Newline required after opening brace
is thrown for the following code snippets:<?php while ( have_posts() ) : the_post(); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
Running
phpcbf
on these two examples will produce the following, respectively:The text was updated successfully, but these errors were encountered: