gregdev
gregdev

Reputation: 1943

php-cs-fixer doesn't indent HTML inside PHP control structures

I'm trying to use php-cs-fixer with a WordPress project, which means I (unfortunately) have files with a mix of PHP and HTML. I'm using the @PSR12 ruleset.

I'm having trouble with getting HTML within PHP control structures to indent correctly. Take this example snippet:

<?php if (!empty($related_posts)) : ?>
    <div class="module--related_posts alignfull has-2-columns has-hover-state slider-on-mobile">
        <h3 class="has-text-align-center">Related <?= esc_html($title) ?></h3>
    </div>
<?php endif ?>

php-cs-fixer reformats it to:

<?php if (!empty($related_posts)) : ?>
<div class="module--related_posts alignfull has-2-columns has-hover-state slider-on-mobile">
    <h3 class="has-text-align-center">Related <?= esc_html($title) ?>
    </h3>
</div>
<?php endif ?>

Note the closing h3 tag has been moved to a new line, and the first-level indent within the if statement body has been removed.

The h3 issue I can live with, as this is resolved if I put the opening tag on its own line:

<h3 class="has-text-align-center">
    Related <?= esc_html($title) ?>
</h3>

...but the lack of indent within the if statement is going to do my head in. The same thing happens with for and while statements.

Is there a rule in php-cs-fixer that I've overlooked that will resolve this?

Upvotes: 2

Views: 1027

Answers (2)

Movahhedi
Movahhedi

Reputation: 584

The Answer of keradus on the "PHP code does not align with html code" issue:

PHP CS Fixer was never written with supporting mixed html/php file. Some fixers are supporting one php part and one html part inside single file, but not big mix of them, like in template files. If we would like to support template files, we would need to not only detect and track concrete fixers, but also provide some big integration test (like we do for Sf ruleset) that it remains to work for most important rules. Before that happen, I would not claim that we officially support html/php mixed-files.

And in another answer:

we do not aim to fix mixed file (PHP and HTML in single file)

Upvotes: 2

gregdev
gregdev

Reputation: 1943

Not really an answer but I ended up just switching to PHP_CodeSniffer to get around this.

Upvotes: 1

Related Questions