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/CharacterBeforePHPOpeningTag: prevent false positive in mixed PHP/HTML files #3704

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,8 @@ http://pear.php.net/dtd/package-2.0.xsd">
<file baseinstalldir="PHP/CodeSniffer" name="CharacterBeforePHPOpeningTagUnitTest.1.inc" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="CharacterBeforePHPOpeningTagUnitTest.2.inc" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="CharacterBeforePHPOpeningTagUnitTest.3.inc" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="CharacterBeforePHPOpeningTagUnitTest.4.inc" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="CharacterBeforePHPOpeningTagUnitTest.5.inc" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="CharacterBeforePHPOpeningTagUnitTest.php" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="ClosingPHPTagUnitTest.1.inc" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="ClosingPHPTagUnitTest.2.inc" role="test" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ class CharacterBeforePHPOpeningTagSniff implements Sniff
*/
public function register()
{
return [T_OPEN_TAG];
return [
T_OPEN_TAG,
T_OPEN_TAG_WITH_ECHO,
];

}//end register()

Expand All @@ -52,10 +55,15 @@ public function register()
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
if ($tokens[$stackPtr]['code'] === T_OPEN_TAG_WITH_ECHO) {
// Mixed PHP/HTML file, ignore.
return $phpcsFile->numTokens;
}

$expected = 0;
if ($stackPtr > 0) {
// Allow a byte-order mark.
$tokens = $phpcsFile->getTokens();
foreach ($this->bomDefinitions as $bomName => $expectedBomHex) {
$bomByteLength = (strlen($expectedBomHex) / 2);
$htmlBomHex = bin2hex(substr($tokens[0]['content'], 0, $bomByteLength));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Foo
<?= $var ?>


<?= $var ?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?= $var ?>

<?php
// Some code