Skip to content

Commit b4c1682

Browse files
committed
Squiz/EmbeddedPhp: bug fix - close tag intended indent calculation
Yet another indentation calculation bug. If the close tag is not on its own line, but is on a line containing a subsequent line of a star-slash style comment, the indentation would not be calculated correctly as the comment token will include the indentation. Fixed now, includes test.
1 parent d1c63af commit b4c1682

File tree

4 files changed

+38
-1
lines changed

4 files changed

+38
-1
lines changed

src/Standards/Squiz/Sniffs/PHP/EmbeddedPhpSniff.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,16 @@ private function validateMultilineEmbeddedPhp($phpcsFile, $stackPtr, $closingTag
253253
) {
254254
$closerIndent = $indent;
255255
} else {
256-
$first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $closingTag, true);
256+
$first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $closingTag, true);
257+
258+
while ($tokens[$first]['code'] === T_COMMENT
259+
&& $tokens[$first]['content'] !== ltrim($tokens[$first]['content'])
260+
) {
261+
// This is a subsequent line in a star-slash comment containing leading indent.
262+
// We'll need the first line of the comment to correctly determine the indent.
263+
$first = $phpcsFile->findFirstOnLine(T_WHITESPACE, ($first - 1), true);
264+
}
265+
257266
$closerIndent = ($tokens[$first]['column'] - 1);
258267
}
259268

src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.1.inc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,18 @@ echo 'indent is correct - first on line for open tag is open tag';
228228
echo 'indent is incorrect - first on line for open tag is whitespace';
229229
?>
230230

231+
<!--
232+
Safeguard correct close tag indent calculation.
233+
-->
234+
<?php
235+
/* Single line star comments are not problematic. */ ?>
236+
237+
<?php
238+
/* Subsequent lines of inline slash-star comments
239+
contain the indent in the comment token.
240+
This means the calculated indent would be 0 unless we
241+
walk to the first line in the comment. */ ?>
242+
231243
<?php
232244
// This test case file MUST always end with an unclosed long open PHP tag (with this comment) to prevent
233245
// the tests running into the "last PHP closing tag excepted" condition breaking tests.

src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.1.inc.fixed

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,20 @@ echo 'indent is incorrect - first on line for open tag is open tag';
234234
echo 'indent is incorrect - first on line for open tag is whitespace';
235235
?>
236236

237+
<!--
238+
Safeguard correct close tag indent calculation.
239+
-->
240+
<?php
241+
/* Single line star comments are not problematic. */
242+
?>
243+
244+
<?php
245+
/* Subsequent lines of inline slash-star comments
246+
contain the indent in the comment token.
247+
This means the calculated indent would be 0 unless we
248+
walk to the first line in the comment. */
249+
?>
250+
237251
<?php
238252
// This test case file MUST always end with an unclosed long open PHP tag (with this comment) to prevent
239253
// the tests running into the "last PHP closing tag excepted" condition breaking tests.

src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ public function getErrorList($testFile='')
9393
226 => 1,
9494
227 => 2,
9595
228 => 1,
96+
235 => 1,
97+
241 => 1,
9698
];
9799

98100
case 'EmbeddedPhpUnitTest.2.inc':

0 commit comments

Comments
 (0)