Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 6 additions & 3 deletions src/Standards/Squiz/Sniffs/PHP/EmbeddedPhpSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -379,8 +379,11 @@ private function validateInlineEmbeddedPhp($phpcsFile, $stackPtr, $closeTag)
}

// Check that there is one, and only one space at the start of the statement.
$leadingSpace = 0;
if ($tokens[$stackPtr]['code'] === T_OPEN_TAG) {
$leadingSpace = 0;
$isLongOpenTag = $tokens[$stackPtr]['code'] === T_OPEN_TAG
&& stripos($tokens[$stackPtr]['content'], '<?php') === 0;

if ($isLongOpenTag === true) {
// The long open tag token in a single line tag set always contains a single space after it.
$leadingSpace = 1;
}
Expand All @@ -394,7 +397,7 @@ private function validateInlineEmbeddedPhp($phpcsFile, $stackPtr, $closeTag)
$data = [$leadingSpace];
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpacingAfterOpen', $data);
if ($fix === true) {
if ($tokens[$stackPtr]['code'] === T_OPEN_TAG) {
if ($isLongOpenTag === true) {
$phpcsFile->fixer->replaceToken(($stackPtr + 1), '');
} else if ($tokens[($stackPtr + 1)]['code'] === T_WHITESPACE) {
// Short open tag with too much whitespace.
Expand Down
4 changes: 4 additions & 0 deletions src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.1.inc
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,10 @@ echo 'the PHP tag is correctly indented as an indent less than the previous code
echo 'the PHP tag is incorrectly indented as the indent is more than 4 different from the indent of the previous code';
?>

<?PHP echo 'Uppercase long open tag'; ?>

<?PHP echo 'Extra space after uppercase long open tag '; ?>

<?php
// This test case file MUST always end with an unclosed long open PHP tag (with this comment) to prevent
// the tests running into the "last PHP closing tag excepted" condition breaking tests.
Expand Down
4 changes: 4 additions & 0 deletions src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.1.inc.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,10 @@ echo 'the PHP tag is correctly indented as an indent less than the previous code
echo 'the PHP tag is incorrectly indented as the indent is more than 4 different from the indent of the previous code';
?>

<?PHP echo 'Uppercase long open tag'; ?>

<?PHP echo 'Extra space after uppercase long open tag '; ?>

<?php
// This test case file MUST always end with an unclosed long open PHP tag (with this comment) to prevent
// the tests running into the "last PHP closing tag excepted" condition breaking tests.
Expand Down
25 changes: 25 additions & 0 deletions src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.24.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?

// This test case file MUST always start with a open PHP tag set (with this comment) to prevent
// the tests running into the "first PHP open tag excepted" condition breaking the tests.
// Tests related to that "first PHP open tag excepted" condition should go in separate files.

// This test case file only deals with SHORT OPEN TAGS.

?>

<?
/* Contrary to the long open tag token, the short open tag token does not contain a space after the
tag and the sniff should handle it accordingly. The test below protects against regressions
related to https://github.com/PHPCSStandards/PHP_CodeSniffer/issues/588. */
?>
<? echo 'one space after short open tag'; ?>

<? echo 'two spaces after short open tag'; ?>

<?echo 'without space after short open tag'; ?>

<?
// This test case file MUST always end with an unclosed open PHP tag (with this comment) to prevent
// the tests running into the "last PHP closing tag excepted" condition breaking tests.
// Tests related to that "last PHP closing tag excepted" condition should go in separate files.
25 changes: 25 additions & 0 deletions src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.24.inc.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?

// This test case file MUST always start with a open PHP tag set (with this comment) to prevent
// the tests running into the "first PHP open tag excepted" condition breaking the tests.
// Tests related to that "first PHP open tag excepted" condition should go in separate files.

// This test case file only deals with SHORT OPEN TAGS.

?>

<?
/* Contrary to the long open tag token, the short open tag token does not contain a space after the
tag and the sniff should handle it accordingly. The test below protects against regressions
related to https://github.com/PHPCSStandards/PHP_CodeSniffer/issues/588. */
?>
<? echo 'one space after short open tag'; ?>

<? echo 'two spaces after short open tag'; ?>

<? echo 'without space after short open tag'; ?>

<?
// This test case file MUST always end with an unclosed open PHP tag (with this comment) to prevent
// the tests running into the "last PHP closing tag excepted" condition breaking tests.
// Tests related to that "last PHP closing tag excepted" condition should go in separate files.
12 changes: 12 additions & 0 deletions src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ public function getErrorList($testFile='')
258 => 1,
263 => 1,
264 => 1,
270 => 1,
];

case 'EmbeddedPhpUnitTest.2.inc':
Expand Down Expand Up @@ -190,6 +191,17 @@ public function getErrorList($testFile='')
22 => 2,
];

case 'EmbeddedPhpUnitTest.24.inc':
$shortOpenTagDirective = (bool) ini_get('short_open_tag');
if ($shortOpenTagDirective === true) {
return [
18 => 1,
20 => 1,
];
} else {
return [];
}

default:
return [];
}//end switch
Expand Down