Skip to content

Commit 872add0

Browse files
authored
Merge pull request #423 from PHPCSStandards/feature/pear-scopeclosingbrace-fix-fixer-conflict
PEAR/ScopeClosingBrace: prevent fixer conflict with itself
2 parents e0fc8ef + 436d243 commit 872add0

File tree

4 files changed

+27
-10
lines changed

4 files changed

+27
-10
lines changed

src/Standards/PEAR/Sniffs/WhiteSpace/ScopeClosingBraceSniff.php

+13-10
Original file line numberDiff line numberDiff line change
@@ -93,16 +93,19 @@ public function process(File $phpcsFile, $stackPtr)
9393
}
9494

9595
// Check that the closing brace is on it's own line.
96-
$lastContent = $phpcsFile->findPrevious(
97-
[
98-
T_WHITESPACE,
99-
T_INLINE_HTML,
100-
T_OPEN_TAG,
101-
],
102-
($scopeEnd - 1),
103-
$scopeStart,
104-
true
105-
);
96+
for ($lastContent = ($scopeEnd - 1); $lastContent > $scopeStart; $lastContent--) {
97+
if ($tokens[$lastContent]['code'] === T_WHITESPACE || $tokens[$lastContent]['code'] === T_OPEN_TAG) {
98+
continue;
99+
}
100+
101+
if ($tokens[$lastContent]['code'] === T_INLINE_HTML
102+
&& ltrim($tokens[$lastContent]['content']) === ''
103+
) {
104+
continue;
105+
}
106+
107+
break;
108+
}
106109

107110
if ($tokens[$lastContent]['line'] === $tokens[$scopeEnd]['line']) {
108111
$error = 'Closing brace must be on a line by itself';

src/Standards/PEAR/Tests/WhiteSpace/ScopeClosingBraceUnitTest.inc

+6
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,9 @@ enum Suits {}
162162
enum Cards
163163
{
164164
}
165+
166+
?>
167+
<!-- Prevent fixer conflict with itself when the scope closer is preceded by non-empty/non-indentation inline HTML. -->
168+
<?php if ($someConditional) : ?>
169+
<div class="container">
170+
</div> <?php endif;

src/Standards/PEAR/Tests/WhiteSpace/ScopeClosingBraceUnitTest.inc.fixed

+7
Original file line numberDiff line numberDiff line change
@@ -168,3 +168,10 @@ enum Suits {
168168
enum Cards
169169
{
170170
}
171+
172+
?>
173+
<!-- Prevent fixer conflict with itself when the scope closer is preceded by non-empty/non-indentation inline HTML. -->
174+
<?php if ($someConditional) : ?>
175+
<div class="container">
176+
</div> <?php
177+
endif;

src/Standards/PEAR/Tests/WhiteSpace/ScopeClosingBraceUnitTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public function getErrorList()
4949
154 => 1,
5050
160 => 1,
5151
164 => 1,
52+
170 => 1,
5253
];
5354

5455
}//end getErrorList()

0 commit comments

Comments
 (0)