Skip to content

Commit bac3be5

Browse files
committed
Squiz/EmbeddedPhp: bug fix - fixer adds stray new line
When the close tag of a previous multi-line embedded PHP snippet and the open tag of the next multi-line embedded PHP snippet are on the same line, the sniff would add a stray blank line with trailing whitespace between the two tokens due to the fixers both running in the same loop. This commit adds a small tweak preventing this stray blank line from being added. Includes unit tests proving the issue and safeguarding the fix.
1 parent 9fbbe04 commit bac3be5

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,13 @@ private function validateMultilineEmbeddedPhp($phpcsFile, $stackPtr, $closingTag
262262
}
263263
}
264264

265+
if ($tokens[$firstContentAfterBlock]['code'] === T_OPEN_TAG) {
266+
// Next token is a PHP open tag which will also have thrown an error.
267+
// Prevent both fixers running in the same loop by making sure the token is "touched" during this loop.
268+
// This prevents a stray new line being added between the close and open tags.
269+
$phpcsFile->fixer->replaceToken($firstContentAfterBlock, $tokens[$firstContentAfterBlock]['content']);
270+
}
271+
265272
$phpcsFile->fixer->endChangeset();
266273
}//end if
267274
}//end if

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,19 @@ Make sure the "content after closer" fixer does not leave trailing space behind.
168168
echo $closerNeedsOwnLine;
169169
?> <?php echo $i; ?>
170170

171+
<!--
172+
Make sure the fixer does not add stray new lines when there are consecutive PHP blocks.
173+
-->
174+
<?php
175+
// Do something.
176+
?><?php
177+
echo 'embedded';
178+
?> <?php
179+
echo 'embedded';
180+
?><?php
181+
echo 'embedded';
182+
?>
183+
171184
<?php
172185
// This test case file MUST always end with an unclosed long open PHP tag (with this comment) to prevent
173186
// the tests running into the "last PHP closing tag excepted" condition breaking tests.

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,22 @@ Make sure the "content after closer" fixer does not leave trailing space behind.
166166
?>
167167
<?php echo $i; ?>
168168

169+
<!--
170+
Make sure the fixer does not add stray new lines when there are consecutive PHP blocks.
171+
-->
172+
<?php
173+
// Do something.
174+
?>
175+
<?php
176+
echo 'embedded';
177+
?>
178+
<?php
179+
echo 'embedded';
180+
?>
181+
<?php
182+
echo 'embedded';
183+
?>
184+
169185
<?php
170186
// This test case file MUST always end with an unclosed long open PHP tag (with this comment) to prevent
171187
// the tests running into the "last PHP closing tag excepted" condition breaking tests.

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ public function getErrorList()
7474
158 => 1,
7575
165 => 1,
7676
169 => 1,
77+
175 => 1,
78+
176 => 2,
79+
178 => 1,
80+
179 => 1,
81+
180 => 2,
82+
181 => 1,
7783
];
7884

7985
}//end getErrorList()

0 commit comments

Comments
 (0)