Skip to content

Commit 77ebd28

Browse files
committed
Squiz/EmbeddedPhp: opener on own line fixer should remove trailing spaces
As things were, the "opener on own line / content after" fixer would always leave at least one trailing space behind. For most codebases, this means the sniff would always need to be run in conjunction with a sniff which removes trailing spaces and that there is always a second fixer round needed: the first to fix the embedded PHP formatting, the second to allow the other sniff to remove the trailing spaces introduced by the fixes made by this sniff. It also makes updating the test case file fiddly. Secondly, if the space between the open tag and the first content was larger than exactly one space, the sniff would also need an extra fixer round to fix the indent of the "first content", which was moved to the next line and given indent, but with the extra space having being moved as well, the indent would now be too large. This commit fixes the "opener on own line / content after" fixer to remove trailing spaces and prevent the potential extra fixer loop to fix the indent. Covered by pre-existing tests + an additional newly added test.
1 parent b2f4d32 commit 77ebd28

File tree

4 files changed

+24
-3
lines changed

4 files changed

+24
-3
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,16 @@ private function validateMultilineEmbeddedPhp($phpcsFile, $stackPtr, $closingTag
9797
if ($fix === true) {
9898
$first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $stackPtr, true);
9999
$padding = (strlen($tokens[$first]['content']) - strlen(ltrim($tokens[$first]['content'])));
100+
100101
$phpcsFile->fixer->beginChangeset();
102+
$phpcsFile->fixer->replaceToken($stackPtr, rtrim($tokens[$stackPtr]['content']));
101103
$phpcsFile->fixer->addNewline($stackPtr);
102104
$phpcsFile->fixer->addContent($stackPtr, str_repeat(' ', $padding));
105+
106+
if ($tokens[($stackPtr + 1)]['code'] === T_WHITESPACE) {
107+
$phpcsFile->fixer->replaceToken(($stackPtr + 1), '');
108+
}
109+
103110
$phpcsFile->fixer->endChangeset();
104111
}
105112
} else {

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,12 @@ or indentation if there is code _after_ the removed empty tag.
145145
<?php if (true) { ?><?php echo $i; ?> <?php
146146
?> <?php } ?>
147147

148+
<!--
149+
Make sure the "content after opener" fixer does not leave trailing space behind.
150+
-->
151+
<?php echo $openerNeedsOwnLine;
152+
?>
153+
148154
<?php
149155
// This test case file MUST always end with an unclosed long open PHP tag (with this comment) to prevent
150156
// the tests running into the "last PHP closing tag excepted" condition breaking tests.

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
<?php
3333
echo $closerNeedsOwnLine;
3434
?>
35-
<?php
35+
<?php
3636
echo $openerNeedsOwnLine;
3737
?>
3838

@@ -51,12 +51,12 @@ function test()
5151
foreach ($root->section as $section) {
5252
?>
5353
<table>
54-
<?php
54+
<?php
5555
if ($foo) {
5656
?>
5757
<tr>
5858
</tr>
59-
<?php
59+
<?php
6060
}
6161
?>
6262
<?php
@@ -139,6 +139,13 @@ or indentation if there is code _after_ the removed empty tag.
139139

140140
<?php if (true) { ?><?php echo $i; ?> <?php } ?>
141141

142+
<!--
143+
Make sure the "content after opener" fixer does not leave trailing space behind.
144+
-->
145+
<?php
146+
echo $openerNeedsOwnLine;
147+
?>
148+
142149
<?php
143150
// This test case file MUST always end with an unclosed long open PHP tag (with this comment) to prevent
144151
// the tests running into the "last PHP closing tag excepted" condition breaking tests.

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public function getErrorList()
7070
138 => 1,
7171
142 => 1,
7272
145 => 1,
73+
151 => 1,
7374
];
7475

7576
}//end getErrorList()

0 commit comments

Comments
 (0)