WordPress / WordPress/WordPress-Coding-Standards

HTML with PHP in templates is badly formatted

Open
#1,889 0 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
PHP
Stars
2.8k
Forks
521
Avg merge
5d 20h
Merged PRs (30d)
1

Description

Bug Description

HTML with PHP in templates is badly formatted
I will show you one example, but to be honest, it is even worse with large files 🙈

Code Snippet

Sample messy code before the fix (yes i know it's really a mess but i did it just for the test)
								<?php	if ( $count_rest ) : ?>
				<?php if ( $count_rest == 1 ) : ?>
	<div class="row row-cols-1 row-cols-md-2 text-center">
<?php	endif; ?>

				<?php the_excerpt(); ?>

				<?php if ( $query_blog->current_post + 1 == $query_blog->post_count ) : ?>
			</div>
										<?php endif; ?>
							<?php endif; ?>
Sample code after the fix (still a total disaster)
<?php	if ( $count_rest ) : ?>
									<?php if ( $count_rest == 1 ) : ?>
	<div class="row row-cols-1 row-cols-md-2 text-center">
<?php	endif; ?>

									<?php the_excerpt(); ?>

									<?php if ( $query_blog->current_post + 1 == $query_blog->post_count ) : ?>
			</div>
										<?php endif; ?>
							<?php endif; ?>
Expected behavior (more or less):
<?php if ( $count_rest ) : ?>
	<?php if ( $count_rest == 1 ) : ?>
		<div class="row row-cols-1 row-cols-md-2 text-center">
	<?php endif; ?>
	<?php the_excerpt(); ?>
	<?php if ( $query_blog->current_post + 1 == $query_blog->post_count ) : ?>
		</div>
	<?php endif; ?>
<?php endif; ?>

Environment

Question Answer
PHP version PHP 7.4.4 (cli) (built: Mar 17 2020 13:49:19) ( ZTS Visual C++ 2017 x64 ), Copyright (c) The PHP Group, Zend Engine v3.4.0, Copyright (c) Zend Technologies
PHP_CodeSniffer version PHP_CodeSniffer version 3.5.5 (stable) by Squiz (http://www.squiz.net)
WPCS version latest
WPCS install type Composer global
IDE (if relevant) No IDE. Just tested on N++ with cmd commands

Additional Context

Command that i use:
phpcbf --standard=Wordpress test.php

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 the provided PHP/HTML snippets and the command phpcbf --standard=Wordpress test.php. Start by tracing how PHP_CodeSniffer formats mixed PHP and HTML, then add or update a focused regression test for nested PHP control blocks; done means the formatter produces indentation matching the expected example.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.