-
-
Notifications
You must be signed in to change notification settings - Fork 495
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
phpcbf breaks php opening and closing tags adding spaces #2083
Comments
I can confirm that with the PHPCS 3.6.2, and WPCS 2.3.0 on PHP 7.4.26 I got the same result. So this is a bug (at least as far as the opening tag being broken). As far as changing the alternative syntax of block structures to braces ones, I don't think phpcbf will do that when core standards are applied, because the alternative syntax ( I think you should look into DisallowAlternativeSyntaxSniff in the If you add it to your <?xml version="1.0"?>
<ruleset name="Test">
<description>Test ruleset</description>
<rule ref="WordPress-Core" />
<!-- Additional arguments. -->
<arg value="sp"/>
<arg name="cache"/>
<arg name="basepath" value="."/>
<arg name="parallel" value="8"/>
<arg name="extensions" value="php"/>
<rule ref="Universal.ControlStructures.DisallowAlternativeSyntax" />
</ruleset> The fix will produce: <?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 } ?>
<?php } ?>
<?php
} You'd still need to check the extra unnecessary PHP tags (there could probably be some setting or sniff for that as well,didn't poke too deep into it 🤷🏼♂️). |
I didn't really expect to change the alternative syntax (exaggerated with that expected code block 😁 )but "DisallowAlternativeSyntaxSniff" is really interesting and thanks for that! I tried to figure out what triggers the bug with the opening php tag but had no luck. It seems that removing |
If the starting snippet uses tabs consistently, then the fixer doesn't break that opening tag. It seems to treat the control syntax alternative syntax as a ternary condition. In pass 1:
By pass 2, the
The
Pass 4:
|
Nice debugging @GaryJones ! Are you working on this or would you like me to pick this up ? Sounds like a tokenizer bug in PHPCS, but I'd need to investigate to be sure. |
Smaller code sample to reproduce this with (and still reproducible): <?php if (get_row_layout() == 'my_block'): ?>
<?php include get_theme_file_path(
'my_block.php'
); ?>
<?php endif; ?> |
Actually not a tokenizer issue, but as @giorgosbotis already surmised in #2083 (comment), a problem with the
|
Upstream PR squizlabs/PHP_CodeSniffer#3667 should fix this. Testing appreciated. |
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; |
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 the commit as well. I can also confirm it fixes the issue and it works just fine, even with multiple similar if/else statements. Thank you @jrfnl ! |
Thanks for confirming @giorgosbotis ! In that case, I'm closing the issue and we'll just have to wait for the PHPCS version in which my fix will be merged, to be released. |
Apparently the OP also reported this issue upstream with a different description, so now my PR can be binned.... |
@jrfnl I'm sorry but it was not the same issue. The issue I reported on phpcs board was a runtime error I got using only PEAR standard. It had nothing to do with breaking php tags and it was reproducible without Wordpress standards. |
@giorgosbotis Except they were the same issue.... See the fix committed and the fix I pulled. They are effectively the same. |
@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. |
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:
... over a file containing this code:
The file was auto-fixed via
phpcbf
to:... while I expected the code to be fixed to:
Error Code
Custom ruleset
Environment
Additional Context (optional)
Tested Against
develop
branch?develop
branch of WPCS.The text was updated successfully, but these errors were encountered: