Description
Describe the bug
When the short_open_tag
ini directive is enabled, the sniff Squiz.PHP.EmbeddedPhp
raises a false positive error for code that uses a single-line statement with the short open tag.
Code sample
<? echo 'This is a short open tag'; ?>
To reproduce
Steps to reproduce the behavior:
- Enable the
short_open_tag
ini directive in the PHP configuration file. - Create a file called
test.php
with the code sample above. - Run
phpcs --standard=Squiz --sniffs=Squiz.PHP.EmbeddedPhp test.php
- See the error message displayed
FILE: test.php
----------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
----------------------------------------------------------------------
9 | ERROR | [x] Expected 1 space after opening PHP tag; 2 found
----------------------------------------------------------------------
PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------
Expected behavior
PHPCS should report no errors.
Versions (please complete the following information)
Operating System | Ubuntu 24.04 |
PHP version | 8.3 |
PHP_CodeSniffer version | master |
Standard | Squiz |
Install type | git clone |
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
- I have searched the issue list and am not opening a duplicate issue.
- I have read the Contribution Guidelines and this is not a support question.
- I confirm that this bug is a bug in PHP_CodeSniffer and not in one of the external standards.
- I have verified the issue still exists in the
master
branch of PHP_CodeSniffer.