WordPress / WordPress/WordPress-Coding-Standards

Newline required after opening brace triggered for allowed loop syntax

Open
#1,085 18 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Status: Close candidate Type: Bug
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.