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

Generic.ControlStructures.InlineControlStructure confused by mixed short/long tags #2565

Closed
igorsantos07 opened this issue Jul 24, 2019 · 7 comments · Fixed by #2567
Closed
Milestone

Comments

@igorsantos07
Copy link

igorsantos07 commented Jul 24, 2019

Example:

<?php if (true) : ?>
<? endif ?>
<?php if (true) { ?>
<? } ?>

<? if (false) : ?>
<? endif ?>
<?php if (false) { ?>
<?php } ?>

CLI output (reduced for brevity):

$ phpcs -n --standard=PSR2 phpcs-test.php
  1 | ERROR | [x] Inline control structures are not allowed
  3 | ERROR | [x] Inline control structures are not allowed
$ phpcbf -n --standard=PSR2 phpcs-test.php
  /home/me/phpcs-test.php                            FAILED TO FIX

Resulting "fixed" file:

<?php if (true) {
    :;
} ?>
<? endif ?>
<?php if (true) { { { { { { { { { { { { { { { { { { { { { { { { { { { {;}}}}}}}}}}}}}}}}}}}}}}}}}} } ?>
<? } ?>

<? if (false) : ?>
<? endif ?>
<?php if (false) { ?>
<?php } ?>

As can be seen in the false if's, using <?php or <? on both lines do not trigger the phpcs error. I'm not so sure what InlineControlStructure means in those cases (maybe the humanized message doesn't make much sense for those cases?), but we can say for sure that phpcbf got lost with the alt-syntax and nuts with the bracketed one 😂

That's for versions 3.3.2/3.4.2 at PHP 7.2.20.

@igorsantos07 igorsantos07 changed the title InlineControlStructure is confusing? + PHPCBF causes syntax error InlineControlStructure gets confused with mixed short/long tags? + PHPCBF causes syntax error Jul 24, 2019
@gsherwood
Copy link
Member

This is probably due to that second PHP block using short open tags. PHPCS is probably being told it is inline HTML by the PHP tokenizer, so the closing tag of the IF statement is never found. So to PHPCS it looks like a syntax error.

@igorsantos07 igorsantos07 changed the title InlineControlStructure gets confused with mixed short/long tags? + PHPCBF causes syntax error InlineControlStructure gets confused with mixed short/long tags? + PHPCBF gets nuts with the fix Jul 24, 2019
@igorsantos07
Copy link
Author

I've updated the description to reflect new findings. Nonetheless, that's probably not related to the short tags, at least not in the way you mentioned. PHPCS has sniffers for that, and if you remove -n you'll see it's also emitting a warning about that; I just added -n as the warning is not relevant to the bug - in our project, short tags are allowed just fine and the issue also happens.

@igorsantos07
Copy link
Author

Updated to 3.4.2 and the issue is exactly the same.

@jrfnl
Copy link
Contributor

jrfnl commented Jul 24, 2019

Nonetheless, that's probably not related to the short tags

@igorsantos07 Actually it is. Sniffs work independently of each other and as you will see if you use -s as well, the other error is from another sniff.

The system on which you are running PHPCS must be configured with short_open_tag=Off for them not to be recognized by the InlineControlStructure sniff.
The ShortOpenTags sniff makes a special allowance for that to still be able to detect them and warn about them appropriately.

I think the bug fix which is needed here, is for the InlineControlStructure sniff to still throw an error when it detects this, but not try to auto-fix.

@igorsantos07
Copy link
Author

Correct, sometime later I noticed another issue, but that time with an entire class file and phpcs reported it could be a short_open_tag issue as it found no code in that file. I had upgraded recently to PHP 7.2 and my ini file went back to the defaults 🤦‍♂️

I'm not sure what to say here. How should phpcs behave when the code is incomplete? I guess we can simply ignore the fact it was complaining about something weird (inline control structures are forbidden), because in fact the rest of the code was "missing"?
Also, should it be able to double check cases like and maybe warn about the missing config, or the actual hint I got in the full-class file was just a cautionary message with no extra checks? It took me a while to figure out what was going on, as phpcs was running locally but the code is executed in another server (which was properly setup for <?).

Not to mention it's definitely "funny" what it did with the if (true) { haha

@jrfnl
Copy link
Contributor

jrfnl commented Jul 24, 2019

How should phpcs behave when the code is incomplete?

Generally speaking, PHPCS will bow out and not report anything or, at the very least, not try to auto-fix code if it doesn't find the tokens which are expected, as this normally indicates that PHPCS is run during live coding or that there is a parse error.

That way the fixer won't accidentally introduce (additional) syntax errors.

Not to mention it's definitely "funny" what it did with the if (true) { haha

True that 😂

@jrfnl
Copy link
Contributor

jrfnl commented Jul 24, 2019

@igorsantos07 PR #2567 should fix this by bowing out in the precise situation you found yourself in. Hope it helps ;-)

@gsherwood gsherwood added this to the 3.5.0 milestone Jul 25, 2019
@gsherwood gsherwood changed the title InlineControlStructure gets confused with mixed short/long tags? + PHPCBF gets nuts with the fix Generic.ControlStructures.InlineControlStructure confused with mixed short/long tags Jul 25, 2019
@gsherwood gsherwood changed the title Generic.ControlStructures.InlineControlStructure confused with mixed short/long tags Generic.ControlStructures.InlineControlStructure confused by mixed short/long tags Jul 25, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants