Skip to content

Commit 46f7102

Browse files
committed
Squiz/EmbeddedPhp: bug fix - open tag intended indent calculation
Basically, this is the same bug as fixed in a previous commit for the intended close tag indent calculation. If the first token on the line before the open tag would be a subsequent line for a star-slash comment, the indentation would not be calculated correctly, but would always be set to `0`, even when there was indentation. Fixed now, includes test.
1 parent b4c1682 commit 46f7102

File tree

4 files changed

+68
-1
lines changed

4 files changed

+68
-1
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,11 +200,19 @@ private function validateMultilineEmbeddedPhp($phpcsFile, $stackPtr, $closingTag
200200
}
201201
} else {
202202
// Find the first token on the first non-empty line we find.
203-
for ($first = ($stackPtr - 1); $first > 0; $first--) {
203+
for ($first = ($lastContentBeforeBlock - 1); $first > 0; $first--) {
204204
if ($tokens[$first]['line'] === $tokens[$stackPtr]['line']) {
205205
continue;
206206
} else if (trim($tokens[$first]['content']) !== '') {
207207
$first = $phpcsFile->findFirstOnLine([], $first, true);
208+
if ($tokens[$first]['code'] === T_COMMENT
209+
&& $tokens[$first]['content'] !== ltrim($tokens[$first]['content'])
210+
) {
211+
// This is a subsequent line in a star-slash comment containing leading indent.
212+
// We'll need the first line of the comment to correctly determine the indent.
213+
continue;
214+
}
215+
208216
break;
209217
}
210218
}

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,31 @@ Safeguard correct close tag indent calculation.
240240
This means the calculated indent would be 0 unless we
241241
walk to the first line in the comment. */ ?>
242242

243+
<!--
244+
Safeguard correct open tag indent calculation when the previous non-empty line contains a comment.
245+
-->
246+
<?php
247+
/* Subsequent lines of inline slash-star comments
248+
contain the indent in the comment token. */ ?>
249+
<?php
250+
echo 'the PHP tag is correctly indented as an indent less than the previous code indent is accepted';
251+
252+
/* Subsequent lines of inline slash-star comments
253+
contain the indent in the comment token. */ ?>
254+
<?php
255+
echo 'the PHP tag is correctly indented';
256+
257+
/* Subsequent lines of inline slash-star comments
258+
contain the indent in the comment token. */ ?>
259+
<?php
260+
echo 'the PHP tag is correctly indented as a diff of 4 is accepted';
261+
262+
/* Subsequent lines of inline slash-star comments
263+
contain the indent in the comment token. */ ?>
264+
<?php
265+
echo 'the PHP tag is incorrectly indented as the indent is more than 4 different from the indent of the previous code';
266+
?>
267+
243268
<?php
244269
// This test case file MUST always end with an unclosed long open PHP tag (with this comment) to prevent
245270
// the tests running into the "last PHP closing tag excepted" condition breaking tests.

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,35 @@ Safeguard correct close tag indent calculation.
248248
walk to the first line in the comment. */
249249
?>
250250

251+
<!--
252+
Safeguard correct open tag indent calculation when the previous non-empty line contains a comment.
253+
-->
254+
<?php
255+
/* Subsequent lines of inline slash-star comments
256+
contain the indent in the comment token. */
257+
?>
258+
<?php
259+
echo 'the PHP tag is correctly indented as an indent less than the previous code indent is accepted';
260+
261+
/* Subsequent lines of inline slash-star comments
262+
contain the indent in the comment token. */
263+
?>
264+
<?php
265+
echo 'the PHP tag is correctly indented';
266+
267+
/* Subsequent lines of inline slash-star comments
268+
contain the indent in the comment token. */
269+
?>
270+
<?php
271+
echo 'the PHP tag is correctly indented as a diff of 4 is accepted';
272+
273+
/* Subsequent lines of inline slash-star comments
274+
contain the indent in the comment token. */
275+
?>
276+
<?php
277+
echo 'the PHP tag is incorrectly indented as the indent is more than 4 different from the indent of the previous code';
278+
?>
279+
251280
<?php
252281
// This test case file MUST always end with an unclosed long open PHP tag (with this comment) to prevent
253282
// the tests running into the "last PHP closing tag excepted" condition breaking tests.

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ public function getErrorList($testFile='')
9595
228 => 1,
9696
235 => 1,
9797
241 => 1,
98+
248 => 1,
99+
253 => 1,
100+
258 => 1,
101+
263 => 1,
102+
264 => 1,
98103
];
99104

100105
case 'EmbeddedPhpUnitTest.2.inc':

0 commit comments

Comments
 (0)