WordPress / WordPress/WordPress-Coding-Standards
Newline required after opening brace triggered for allowed loop syntax
Nobody has claimed this yet.
- Dominant language
- PHP
- Stars
- 2.8k
- Forks
- 521
- Avg merge
- 5d 20h
- Merged PRs (30d)
- 1
Description
The WordPress coding standards state:
You are free to use the alternative syntax for control structures (e.g. if/endif, while/endwhile)—especially in your templates where PHP code is embedded within HTML, for instance:
<?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; ?>
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:
<?php
while ( have_posts() ) :
the_post();
?>
<?php
if ( have_posts() ) :
while ( have_posts() ) :
the_post();
?>
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the reported behavior with PHP_CodeSniffer and phpcbf using the two alternative-syntax snippets in the issue. Trace the rule that emits “Newline required after opening brace,” then add coverage for these loop forms and verify phpcbf no longer rewrites valid syntax incorrectly.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- php
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100