Skip to content

Commit d1c63af

Browse files
committed
Squiz/EmbeddedPhp: bug fix - open tag on own line intended indent calculation
Basically, this is the same bug as fixed in a previous commit for the intended content indent calculation. The indentation of the first token in the file would be used to calculate the intended indentation, instead of the indentation for the first token on the line containing the PHP open tag. Fixed now, includes test. Note: The only reason the test doesn't actually show the bug is because of the earlier "fixer adds stray new line" bugfix in this PR, which means that the fixer for the previous close tag will prevent the buggy fixer from running. Without _that_ fix, this bug would be visible when running with `-vvv` (indent would get set to 32 and then corrected to 4 on the next fixer round via the `OpenTagIndent` fixer).
1 parent 0fff3b5 commit d1c63af

File tree

4 files changed

+31
-4
lines changed

4 files changed

+31
-4
lines changed

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,13 @@ private function validateMultilineEmbeddedPhp($phpcsFile, $stackPtr, $closingTag
185185
$error = 'Opening PHP tag must be on a line by itself';
186186
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'ContentBeforeOpen');
187187
if ($fix === true) {
188-
$first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $stackPtr);
188+
$padding = 0;
189+
$first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $stackPtr);
189190
if ($first === false) {
190-
$first = $phpcsFile->findFirstOnLine(T_INLINE_HTML, $stackPtr);
191-
$padding = (strlen($tokens[$first]['content']) - strlen(ltrim($tokens[$first]['content'])));
191+
$first = $phpcsFile->findFirstOnLine(T_INLINE_HTML, $stackPtr);
192+
if ($first !== false) {
193+
$padding = (strlen($tokens[$first]['content']) - strlen(ltrim($tokens[$first]['content'])));
194+
}
192195
} else {
193196
$padding = ($tokens[($first + 1)]['column'] - 1);
194197
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@ echo 'indent is correct - first on line for open tag is open tag';
1414
echo 'indent is incorrect - first on line for open tag is inline HTML';
1515
?>
1616

17+
<!--
18+
Open tag indent calculation should not look at first token in the stack, but only at the line containing the PHP open tag for the block.
19+
-->
20+
<?php
21+
// This is only here so we can have a close tag as the first token on the next line.
22+
?><?php
23+
// The PHP open tag should be fixed to be at the very start of the line without indent.
24+
// And the indent of this comment should be seen as correct.
25+
?>
26+
1727
<?php
1828
// This test case file MUST always end with an unclosed long open PHP tag (with this comment) to prevent
1929
// the tests running into the "last PHP closing tag excepted" condition breaking tests.

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,17 @@ echo 'indent is correct - first on line for open tag is open tag';
1414
echo 'indent is incorrect - first on line for open tag is inline HTML';
1515
?>
1616

17+
<!--
18+
Open tag indent calculation should not look at first token in the stack, but only at the line containing the PHP open tag for the block.
19+
-->
20+
<?php
21+
// This is only here so we can have a close tag as the first token on the next line.
22+
?>
23+
<?php
24+
// The PHP open tag should be fixed to be at the very start of the line without indent.
25+
// And the indent of this comment should be seen as correct.
26+
?>
27+
1728
<?php
1829
// This test case file MUST always end with an unclosed long open PHP tag (with this comment) to prevent
1930
// the tests running into the "last PHP closing tag excepted" condition breaking tests.

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,10 @@ public function getErrorList($testFile='')
178178
return [12 => 2];
179179

180180
case 'EmbeddedPhpUnitTest.22.inc':
181-
return [14 => 1];
181+
return [
182+
14 => 1,
183+
22 => 2,
184+
];
182185

183186
default:
184187
return [];

0 commit comments

Comments
 (0)