-
-
Notifications
You must be signed in to change notification settings - Fork 66
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.PHP.EmbeddedPhp false positive when using PHP short open tag #588
Comments
@rodrigoprimo Nice find. Low priority to fix as it is uncommon for short open tags (not echo) to be enabled, let alone used.
I don't think the directive Checking with the current condition + a content check for |
Thanks for checking, @jrfnl. I agree that this is a low-priority issue, as you said. It seems somewhat straightforward to fix it, so I might give it a try if I have some time in the next few days. |
Noting here that I found a scenario where this bug, combined with another sniff, can cause a parse error. Running the following PHPCBF command against the same code sample used in the description of the issue will result in invalid PHP file.
Here is the resulting PHP file: <?php phpecho 'This is a short open tag'; ?> Here is the part of the
On a first pass, |
@rodrigoprimo Good catch, still low prio. Having said that, might be good to check if the |
@jrfnl, good point, I checked, and the sniff does check that: PHP_CodeSniffer/src/Standards/Generic/Sniffs/PHP/DisallowShortOpenTagSniff.php Lines 61 to 65 in 84acf4e
PHP_CodeSniffer/src/Standards/Generic/Tests/PHP/DisallowShortOpenTagUnitTest.2.inc Line 7 in 8b7beb1
PHP_CodeSniffer/src/Standards/Generic/Tests/PHP/DisallowShortOpenTagUnitTest.2.inc.fixed Line 7 in 8b7beb1
The problem only happens when the two sniffs are executed together. So it seems to be a case of conflict between the fixers. I'm assuming it is not worth investigating this further as the issue in |
Closing as fixed via #591 |
Describe the bug
When the
short_open_tag
ini directive is enabled, the sniffSquiz.PHP.EmbeddedPhp
raises a false positive error for code that uses a single-line statement with the short open tag.Code sample
To reproduce
Steps to reproduce the behavior:
short_open_tag
ini directive in the PHP configuration file.test.php
with the code sample above.phpcs --standard=Squiz --sniffs=Squiz.PHP.EmbeddedPhp test.php
Expected behavior
PHPCS should report no errors.
Versions (please complete the following information)
Additional context
The sniff assumes that there is always a space after the PHP open tag that is part of the content of the
T_OPEN_TAG
token. That is true for the PHP long open tag, but not for the PHP short open tag. When processing a short open tag, the sniff "sees" an extra space that doesn't exist, causing it to report the error, saying that it expected one space but found two.Here is the code which was added in 3172a21:
PHP_CodeSniffer/src/Standards/Squiz/Sniffs/PHP/EmbeddedPhpSniff.php
Lines 381 to 386 in ac5fd07
I believe the sniff should be fixed by changing the if condition highlighted above to differentiate between the short open tag and the long open tag when the directive
short_open_tag
is enabled. This could be done by checking the value of$tokens[$stackPtr]['cotent']
. I wonder if there is a better way to do it.Please confirm
master
branch of PHP_CodeSniffer.The text was updated successfully, but these errors were encountered: