-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add sniff to detect consecutive empty lines in a file
This is an edited version of the 'EmptyLines' part of Squiz.WhiteSpace.SuperfluousWhitespace sniff (which only looks inside functions). Squiz.WhiteSpace.SuperfluousWhitespace.EmptyLines has also been excluded from the standard. Bug: T120570 Change-Id: I556b209f2660590b007f332307a171ab6099b6ad
- Loading branch information
1 parent
fa58411
commit 3a6709b
Showing
4 changed files
with
76 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
/** | ||
* Check multiple consecutive newlines in a file. | ||
*/ | ||
// @codingStandardsIgnoreStart | ||
class MediaWiki_Sniffs_WhiteSpace_MultipleEmptyLinesSniff | ||
implements PHP_CodeSniffer_Sniff { | ||
// @codingStandardsIgnoreEnd | ||
public function register() { | ||
return array( | ||
T_WHITESPACE | ||
); | ||
} | ||
|
||
public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) { | ||
$tokens = $phpcsFile->getTokens(); | ||
|
||
if ( $stackPtr > 2 | ||
&& $tokens[$stackPtr - 1]['line'] < $tokens[$stackPtr]['line'] | ||
&& $tokens[$stackPtr - 2]['line'] === $tokens[$stackPtr - 1]['line'] | ||
) { | ||
// This is the first whitespace token on a line | ||
// and the line before this one is not empty, | ||
// so this could be the start of a multiple empty line block. | ||
$next = $phpcsFile->findNext( T_WHITESPACE, $stackPtr, null, true ); | ||
$lines = ( $tokens[$next]['line'] - $tokens[$stackPtr]['line'] ); | ||
if ( $lines > 1 ) { | ||
// If the next non T_WHITESPACE token is more than 1 line away, | ||
// then there were multiple empty lines. | ||
$error = 'Multiple empty lines should not exist in a row; found %s consecutive empty lines'; | ||
$fix = $phpcsFile->addFixableError( | ||
$error, | ||
$stackPtr, | ||
'MultipleEmptyLines', | ||
array( $lines ) | ||
); | ||
if ( $fix === true ) { | ||
$phpcsFile->fixer->beginChangeset(); | ||
$i = $stackPtr; | ||
while ( $tokens[$i]['line'] !== $tokens[$next]['line'] ) { | ||
$phpcsFile->fixer->replaceToken( $i, '' ); | ||
$i++; | ||
} | ||
$phpcsFile->fixer->addNewlineBefore( $i ); | ||
$phpcsFile->fixer->endChangeset(); | ||
} | ||
} | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
MediaWiki/Tests/files/WhiteSpace/multiple_empty_lines_fail.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
$a = true; | ||
|
||
|
||
$b = false; | ||
|
||
function wfFoo() { | ||
$a = 1; | ||
|
||
|
||
$b = 2; | ||
} |
10 changes: 10 additions & 0 deletions
10
MediaWiki/Tests/files/WhiteSpace/multiple_empty_lines_pass.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
$a = true; | ||
|
||
$b = false; | ||
|
||
function wfFoo() { | ||
$a = 1; | ||
|
||
$b = 2; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters