Skip to content
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

Squiz.WhiteSpace.ScopeClosingBrace fixer removes HTML content when fixing closing brace alignment #3422

Closed
GlennBags opened this issue Sep 1, 2021 · 12 comments · Fixed by #3447

Comments

@GlennBags
Copy link

Describe the bug
Div end tag removed when reformatting using phpcbf to reformat code to PSR-12 standard

Code sample

// before reformatter
    </div> <?php endif; ?>

// after reformatter
    <?php endif; ?>

Custom ruleset

<?xml version="1.0"?>
<ruleset name="My Custom Standard">
  <description>If you are using a custom ruleset, please enter it here.</description>

  <description>Besides the below, have some exclude patterns that shouldn't affect the outcode.</description>

    <rule ref="PSR12"/>
    <arg name="extensions" value="php" />

</ruleset>

To reproduce
Steps to reproduce the behavior:

  1. Create a file called test.php with the code sample above...
  2. Run php phpcbf.phar --standard=ruleset.xml test.php -v
  3. See code reformatted with the end div removed

Expected behavior
Should not remove HTML

Versions (please complete the following information):

  • OS: MacOS 11.5.2
  • PHP: 8.0.2
  • $ php phpcbf.phar --version
    PHP_CodeSniffer version 3.6.0 (stable) by Squiz (http://www.squiz.net)
  • Standard: PSR12
@jrfnl
Copy link
Contributor

jrfnl commented Sep 2, 2021

@GlennBags The issue cannot be reproduced with the code sample you provided.

@GlennBags
Copy link
Author

OK, apologies, the code sample was incomplete. Please try:

<?php if ($someConditional) : ?>
<div class="container">

</div> <?php endif; ?>

Using the above file as the test.php, I am able to replicate using the following ruleset:

<?xml version="1.0"?>
<ruleset name="Selective Standard">

    <rule ref="PSR12"/>

    <arg name="extensions" value="php" />

</ruleset>

@jrfnl
Copy link
Contributor

jrfnl commented Sep 2, 2021

@GlennBags Thanks, that's better. Reproduced the issue. Thanks for reporting it.

The problem is in the Squiz.WhiteSpace.ScopeClosingBrace sniff which doesn't handle non-whitespace inline HTML tokens on the same line as the scope closer correctly.

This will need looking into.

@jrfnl
Copy link
Contributor

jrfnl commented Oct 6, 2021

@GlennBags I've prepared a fix for this issue. See PR #3447. Testing appreciated.

@GlennBags
Copy link
Author

@GlennBags I've prepared a fix for this issue. See PR #3447. Testing appreciated.

Am I able to check this out to test using composer?

@jrfnl
Copy link
Contributor

jrfnl commented Oct 6, 2021

Am I able to check this out to test using composer?

@GlennBags You should be able to using squizlabs/php_codesniffer:"dev-feature/3422-squiz-scopeclosingbrace-bug-fix", but you will probably have to set a VCS to point to my fork too. 😒

Once PR #3442 has been merged, testing PRs should become easier as you can then download a PHAR file for each PR to use for testing.

@jrfnl
Copy link
Contributor

jrfnl commented Oct 6, 2021

@GlennBags Just thinking - if it helps, I could generate the PHAR file for the PR for you. Let me know if you want that.

@GlennBags
Copy link
Author

GlennBags commented Oct 6, 2021

@jrfnl Yes, apparently I can't get it from composer command line, or am I doing something wrong?

bash-4.2# composer  squizlabs/php_codesniffer:"dev-feature/3422-squiz-scopeclosingbrace-bug-fix" --require-dev

  [Symfony\Component\Console\Exception\CommandNotFoundException]
  There are no commands defined in the "squizlabs/php_codesniffer" namespace.

@GlennBags
Copy link
Author

Well, duh, I'm testing using the phar, so, yeah, a phar file would help.
I haven't finished my first cup of coffee... that's my excuse and I'm sticking to it! heh

@jrfnl
Copy link
Contributor

jrfnl commented Oct 6, 2021

@GlennBags 😂 you were also missing the require keyword in Composer. The things lack of coffee makes us do ;-)

You can download zips containing the phpcs and phpcbf PHAR files for this PR here: https://github.com/jrfnl/PHP_CodeSniffer/actions/runs/1311950956

@GlennBags
Copy link
Author

Awesome, thank you. Works like a charm.
Pre:

<?php if ($someConditional) : ?>
    <div class="container">

    </div> <?php endif; ?>
<?php if ($someConditional) { ?>
    <div class="container">

    </div> <?php } ?>

Post (after running the phpcbf):

<?php if ($someConditional) : ?>
    <div class="container">

    </div> 
<?php endif; ?>
<?php if ($someConditional) { ?>
    <div class="container">

    </div> 
<?php } ?>

@jrfnl
Copy link
Contributor

jrfnl commented Oct 6, 2021

Thanks for testing @GlennBags !

@gsherwood gsherwood changed the title HTML removed when adjusting PHP tag in view file when using phpcbf.phar command line Squiz.WhiteSpace.ScopeClosingBrace fixer removes HTML content when fixing closing brace alignment Nov 1, 2021
gsherwood added a commit that referenced this issue Nov 1, 2021
PHPCS v3 Development automation moved this from Idea Bank to Ready for Release Nov 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
PHPCS v3 Development
Ready for Release
Development

Successfully merging a pull request may close this issue.

3 participants