WordPress-Coding-Standards: phpcbf breaks php opening and closing tags adding spaces

Bug Description

Using phpcbf on a .php file with many opening and closing php tags both inline and multiline breaks the file completely. The result has < ? php with the spaces and missing closing tags.

Minimal Code Snippet

The issue happens when running this command:

phpcbf --standard=Wordpress-Core myfile.php

… over a file containing this code:

<?php

if (have_rows('page_block')): ?>
  <?php while (have_rows('page_block')):
    the_row(); ?>
    <?php if (get_row_layout() == 'my_block'): ?>
      <?php include get_theme_file_path(
        'my_block.php'
      ); ?>               
    <?php endif; ?>
  <?php endwhile; ?>
<?php endif; ?>

The file was auto-fixed via phpcbf to:

<?php

if ( have_rows( 'page_block' ) ) : ?>
	<?php
	while ( have_rows( 'page_block' ) ) :
		the_row();
		?>
		<?php
		if ( get_row_layout() == 'my_block' ) :
			< ? php
			include get_theme_file_path(
				'my_block.php'
			);
			?>
					 
	<?php endif; ?>
	<?php endwhile; ?>
	<?php
endif;

… while I expected the code to be fixed to:

<?php

if ( have_rows( 'page_block' ) ) {
  while ( have_rows( 'page_block' ) ) {
    the_row();
    if ( get_row_layout() == 'my_block' ) {
      include get_theme_file_path('/theme_extra/theme_blocks/page/timer_suppliers_block.php');
    }
  }
}

Error Code

Custom ruleset

Environment

Question Answer
PHP version 8.0.19
PHP_CodeSniffer version 3.7.1
WPCS version dev
WPCS install type composer local
IDE (if relevant) -

Additional Context (optional)

Tested Against develop branch?

  • I have verified the issue still exists in the develop branch of WPCS.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 16 (12 by maintainers)

Most upvoted comments

@jrfnl If I knew I wouldn’t report two different issues on two different boards, and I would probably propose a fix. I think it’s quite obvious I had no idea those issues were connected.

I understand it can be hard to determine whether things are the same issue, but if it involves the same code snippet, chances are high that the issues are related, if not the same. For a next time, at least please afford us the courtesy of linking the issues together as “possibly related” to safe us wasting time on something which was already being worked on.

Thanks @GaryJones ! As this is an upstream issue, I think we should close the issue here as there is nothing which we can do in WPCS about this. Agreed ?

Tested by changing my composer.json to include:

"squizlabs/php_codesniffer": "dev-feature/pear-functioncallsignature-prevent-fixer-creating-parse-error as 3.9",

and:

    "repositories": [
        {
            "type": "vcs",
            "url": "https://github.com/jrfnl/php_codesniffer"
        }
    ]

And the test file now correctly gets fixed to:

<?php

if ( have_rows( 'page_block' ) ) : ?>
	<?php
	while ( have_rows( 'page_block' ) ) :
		the_row();
		?>
		<?php if ( get_row_layout() == 'my_block' ) : ?>
			<?php
			include get_theme_file_path(
				'my_block.php'
			);
			?>
					 
	<?php endif; ?>
	<?php endwhile; ?>
	<?php
endif;