Php-cs-fixer: PHP code does not align with html code

Created on 15 Apr 2018  Â·  16Comments  Â·  Source: FriendsOfPHP/PHP-CS-Fixer

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.

Most helpful comment

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.

All 16 comments

I am having the same problem in vscode

php 7.1.11
PHP-CS-Fixer: 2.11.1
vscode:-1.22.2

```PHP

return PhpCsFixer\Config::create()
->setRules(array(
'@PSR2' => true,
'array_syntax' => array('syntax' => 'short'),
'array_indentation' => true,
'combine_consecutive_unsets' => true,
'method_separation' => true,
'no_multiline_whitespace_before_semicolons' => true,
'single_quote' => true,

    'binary_operator_spaces' => array(
        'align_double_arrow' => false,
        'align_equals' => false,
    ),
    // 'blank_line_after_opening_tag' => true,
    // 'blank_line_before_return' => true,
    'braces' => array(
        'allow_single_line_closure' => true,
    ),
    // 'cast_spaces' => true,
    // 'class_definition' => array('singleLine' => true),
    'concat_space' => array('spacing' => 'one'),
    'declare_equal_normalize' => true,
    'function_typehint_space' => true,
    'hash_to_slash_comment' => true,
    'include' => true,
    'lowercase_cast' => true,
    // 'native_function_casing' => true,
    // 'new_with_braces' => true,
    // 'no_blank_lines_after_class_opening' => true,
    // 'no_blank_lines_after_phpdoc' => true,
    // 'no_empty_comment' => true,
    // 'no_empty_phpdoc' => true,
    // 'no_empty_statement' => true,
    'no_extra_consecutive_blank_lines' => array(
        'curly_brace_block',
        'extra',
        'parenthesis_brace_block',
        'square_brace_block',
        'throw',
        'use',
    ),
    // 'no_leading_import_slash' => true,
    // 'no_leading_namespace_whitespace' => true,
    // 'no_mixed_echo_print' => array('use' => 'echo'),
    'no_multiline_whitespace_around_double_arrow' => true,
    // 'no_short_bool_cast' => true,
    // 'no_singleline_whitespace_before_semicolons' => true,
    'no_spaces_around_offset' => true,
    // 'no_trailing_comma_in_list_call' => true,
    // 'no_trailing_comma_in_singleline_array' => true,
    // 'no_unneeded_control_parentheses' => true,
    'no_unused_imports' => true,
    'no_whitespace_before_comma_in_array' => true,
    'no_whitespace_in_blank_line' => true,
    // 'normalize_index_brace' => true,
    'object_operator_without_whitespace' => true,
    // 'php_unit_fqcn_annotation' => true,
    // 'phpdoc_align' => true,
    // 'phpdoc_annotation_without_dot' => true,
    // 'phpdoc_indent' => true,
    // 'phpdoc_inline_tag' => true,
    // 'phpdoc_no_access' => true,
    // 'phpdoc_no_alias_tag' => true,
    // 'phpdoc_no_empty_return' => true,
    // 'phpdoc_no_package' => true,
    // 'phpdoc_no_useless_inheritdoc' => true,
    // 'phpdoc_return_self_reference' => true,
    // 'phpdoc_scalar' => true,
    // 'phpdoc_separation' => true,
    // 'phpdoc_single_line_var_spacing' => true,
    // 'phpdoc_summary' => true,
    // 'phpdoc_to_comment' => true,
    // 'phpdoc_trim' => true,
    // 'phpdoc_types' => true,
    // 'phpdoc_var_without_name' => true,
    // 'pre_increment' => true,
    // 'return_type_declaration' => true,
    // 'self_accessor' => true,
    // 'short_scalar_cast' => true,
    'single_blank_line_before_namespace' => true,
    // 'single_class_element_per_statement' => true,
    // 'space_after_semicolon' => true,
    // 'standardize_not_equals' => true,
    'ternary_operator_spaces' => true,
    // 'trailing_comma_in_multiline_array' => true,
    'trim_array_spaces' => true,
    'unary_operator_spaces' => true,
    'whitespace_after_comma_in_array' => true,
))
->setIndent("  ")
->setLineEnding("\n")

;

yes it's horribly broken

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.
cc @FriendsOfPHP/php-cs-fixer


currently, there are too many opened issues/PR to handle everything by core team. Do you want to help dealing with mixed files, @rattkin ?

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.

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

@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?

sorry @muuvmuuv , I'm not fully understand you here. what do you mean by a plugin here?
plugin for vscode?

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.

@ntzm , the thing is - do we want to have that support or not? there are already some rules that requires fixing file to be monolitic (explicitly no html content at all).
if, in future, we want to - first step IMO would be to create a PR with integration test, see how much it's failing, and then decide, is it small enough to simply fix it, or it's so big that we would add a note in readme

@keradus oh sry, I mean option. As I think this function will decrease performance, some users that use alternative syntax doesn't want to have this.

i don't believe that would be an option (like a configuration) for whole tool. it's every single rule that is affected by that would have to learn how to work properly with mixed content. some of them could be easy (fix indentation detection), some harder (move private methods after public methods, but I really doubt one would do that in template file...)

Hmm okay, I understand your point. Do you know any alternative fixer that supports mixed fixing? @keradus

When do you think you will test that functionality to give us an insight into how hard it would be to implement this fully?

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.

@kubawerlos and how do you think I should handle templating for CMS? I‘m working on a template for Joomla right now and there I have huge HTML/PHP mixed files. I can‘t get rid of them.

Here is an example of an index.php: https://hastebin.com/licopedatu.bash

@kubawerlos Yes. OpenCart as well. It's not possible for us to replace a huge e-shop that took three years to build. Sure new stuff has elegant templating systems, but what about legacy (legacy = 3 years old)

So, what's most important you have the will to improve :+1:

When I approach legacy code I create 2 configurations - the "light" version and "strict" one with more rules - at the beginning all files under "light" check, and then, step by step move them under the "strict" when working with.

Or maybe instead of light/strict one for pure PHP files and one for mixed content.

@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!

Was this page helpful?
0 / 5 - 0 ratings