Skip to content

Commit

Permalink
Add sniff to detect consecutive empty lines in a file
Browse files Browse the repository at this point in the history
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
polybuildr authored and legoktm committed Jan 6, 2016
1 parent fa58411 commit 3a6709b
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 1 deletion.
50 changes: 50 additions & 0 deletions MediaWiki/Sniffs/WhiteSpace/MultipleEmptyLinesSniff.php
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 MediaWiki/Tests/files/WhiteSpace/multiple_empty_lines_fail.php
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 MediaWiki/Tests/files/WhiteSpace/multiple_empty_lines_pass.php
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;
}
5 changes: 4 additions & 1 deletion MediaWiki/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,10 @@
<rule ref="Squiz.WhiteSpace.ScopeClosingBrace" />
<rule ref="Squiz.WhiteSpace.ScopeKeywordSpacing" />
<rule ref="Squiz.WhiteSpace.SemicolonSpacing" />
<rule ref="Squiz.WhiteSpace.SuperfluousWhitespace" />
<rule ref="Squiz.WhiteSpace.SuperfluousWhitespace">
<!-- Implemented as MediaWiki.WhiteSpace.MultipleEmptyLines -->
<exclude name="Squiz.WhiteSpace.SuperfluousWhitespace.EmptyLines" />
</rule>

<rule ref="Zend.Files.ClosingTag" />
</ruleset>

0 comments on commit 3a6709b

Please sign in to comment.