PHP-CS-Fixer: PHP code does not align with html code

I’ve a problem with aligning html within a php file. I use a Atom Editor plugin to automatically run php-cs-fixer.

PHP: 7.2.4 PHP-CS-Fixer: 2.11.1 PHP-CS-Fixer Conf: https://hastebin.com/ugimareluv.xml ATOM: 1.25.1 Beautify: 0.32.2 Beautify Conf: https://hastebin.com/irokawonaq.scala

Sample of PHP code:

  • before running PHP CS Fixer (no changes):
....

ob_start();
?>
  <div id="portfolio">
    <div class="container" hidden>
    ........
    </div>
    <div class="portfolio-grid row no-gutters">
      <div class="col-12 col-sm-6 col-md-4 col-xl-3 sizer" style="min-height:0;height:0;"></div>
      <?php
      $query_array = getQueryArray($params);
      $query_results = new \WP_Query($query_array);
      if($query_results->have_posts()):
        while($query_results->have_posts()): $query_results->the_post();

        $terms = wp_get_post_terms(get_the_ID(), 'portfolio-category' );
        $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );

        $t = array();
        foreach($terms as $term) $t[] = $term->slug;
        ?>
....
  • unexpected changes applied when running PHP CS Fixer:
....
  ob_start(); ?>
  <div id="portfolio">
    <div class="container" hidden>
    ........
    </div>
    <div class="portfolio-grid row no-gutters">
      <div class="col-12 col-sm-6 col-md-4 col-xl-3 sizer" style="min-height:0;height:0;"></div>
      <?php
    $query_array = getQueryArray($params);
  $query_results = new \WP_Query($query_array);
  if ($query_results->have_posts()):
    while ($query_results->have_posts()): $query_results->the_post();

  $terms = wp_get_post_terms(get_the_ID(), 'portfolio-category');
  $url = wp_get_attachment_url(get_post_thumbnail_id($post->ID));

  $t = array();
  foreach ($terms as $term) {
    $t[] = $term->slug;
  } ?>
....
  • with the changes you expected instead: It should just normally align the php code with the html code.

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 2
  • Comments: 26 (17 by maintainers)

Most upvoted comments

I think it’s important to stress, maybe in the README or issue template, that mixed PHP and HTML files are not explicitly supported. It would be good to get a compiled list of every issue that relates to mixed PHP and HTML in case there’s a common detail that can be fixed quite easily.

@kubawerlos That is not possible, I can not rewrite a CMS… I think there is a big use for that feature. Many CMS like Wordpress or as I mentioned Joomla, are using PHP without a templating engine. That developers should be respected as well.

And as I can say about me, I‘m not a fan of the alternative style or implementing a templating engine in every project I create!

I couldn’t care less about files with mixed content - if one is mixing HTML with PHP he should not care about coding style - he has bigger problem and that is obviously having HTML mixed with PHP.

There is enough to do with monolitic files right now.

Personally I believe it would be better to make it configurable:

  • skip non-monolithic files (default)
  • opt-in: allow non-monolithic files and continue like it was in v3 (without official support, users’ responsibility)
  • opt-in: exception on non-monolithic files (debatable, I am not sure if this is actually helpful)

Anyway, drop the support but allow people to use it like they use it now, because it somehow works for them, even if there are some issues. Removing this quasi-support completely can lead to users drop, which is not something we want. But it’s only my opinion.

@Wirone

@realjjaveweb Please have in mind that Fixer is focused on modern PHP, that does not mix code with HTML markup (see this comment), that’s why we closed all the issues related to HTML some time ago. There’s no point in commenting issue closed 2 years ago 😉.

I very strongly disagree, actually, I was hoping that maybe you’ve changed your mind after a few years and I was really hoping for this issue to be reopened, not closed.

The comment you’ve linked is inherently wrong with the argumentation - not all PHP&HTML mixed files are mixing logic with view - many of them are very simple view files, I have even seen project that leveraged <?= $value ?> in its PHP templates - and there was no logic, really a simple view.

Now I understand there is a good intention of discouraging mixing logic into view, however, removing it completely is using a sledge hammer on a fly - sure it will work, but it also destroys everything around that has nothing to do with it. I would rather suggest enforcing the not-mixing policy (like forbidding class definitions when there’s a mixed file) than removing the option to do simple efficient templating with PHP (we don’t want to get back to “echo” times…).

Should I open a new issue then?

@keradus maybe implement this functionality as a plugin in the future?

And do you have a example of plugins that can part php part html formating?

i understand why its not support html with php

but instead i found my solution whenever i need to mix php with html i use alternate syntax and it is much cooler

By the way thank you everyone