Skip to content

Commit

Permalink
Only modify tokens if the content changed (cleaner debug output)
Browse files Browse the repository at this point in the history
  • Loading branch information
gsherwood committed Mar 8, 2020
1 parent daa440f commit af90533
Showing 1 changed file with 20 additions and 16 deletions.
36 changes: 20 additions & 16 deletions src/Standards/Generic/Sniffs/Files/LineEndingsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,23 +110,27 @@ public function process(File $phpcsFile, $stackPtr)
}

for ($i = 0; $i < $phpcsFile->numTokens; $i++) {
if (isset($tokens[($i + 1)]) === false
|| $tokens[($i + 1)]['line'] > $tokens[$i]['line']
if (isset($tokens[($i + 1)]) === true
&& $tokens[($i + 1)]['line'] <= $tokens[$i]['line']
) {
// Token is the last on a line.
if (isset($tokens[$i]['orig_content']) === true) {
$tokenContent = $tokens[$i]['orig_content'];
} else {
$tokenContent = $tokens[$i]['content'];
}

if ($tokenContent === '') {
// Special case for JS/CSS close tag.
continue;
}

$newContent = rtrim($tokenContent, "\r\n");
$newContent .= $eolChar;
continue;
}

// Token is the last on a line.
if (isset($tokens[$i]['orig_content']) === true) {
$tokenContent = $tokens[$i]['orig_content'];
} else {
$tokenContent = $tokens[$i]['content'];
}

if ($tokenContent === '') {
// Special case for JS/CSS close tag.
continue;
}

$newContent = rtrim($tokenContent, "\r\n");
$newContent .= $eolChar;
if ($tokenContent !== $newContent) {
$phpcsFile->fixer->replaceToken($i, $newContent);
}
}//end for
Expand Down

0 comments on commit af90533

Please sign in to comment.