Skip to content

Commit 876f900

Browse files
author
Vincent Langlet
committed
✨ Add check for multiple blank line
1 parent 81e880f commit 876f900

File tree

4 files changed

+106
-0
lines changed

4 files changed

+106
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
/**
4+
* Checks that there are not 2 empty lines following each other.
5+
*/
6+
class Symfony3Custom_Sniffs_WhiteSpace_EmptyLinesSniff implements PHP_CodeSniffer_Sniff
7+
{
8+
/**
9+
* A list of tokenizers this sniff supports.
10+
*
11+
* @var array
12+
*/
13+
public $supportedTokenizers = array(
14+
'PHP',
15+
'JS',
16+
'CSS',
17+
);
18+
19+
/**
20+
* Returns an array of tokens this test wants to listen for.
21+
*
22+
* @return array
23+
*/
24+
public function register()
25+
{
26+
return array(T_WHITESPACE);
27+
}
28+
29+
/**
30+
* Processes this test, when one of its tokens is encountered.
31+
*
32+
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
33+
* @param int $stackPtr The position of the current token
34+
* in the stack passed in $tokens.
35+
*
36+
* @return void
37+
*/
38+
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
39+
{
40+
$tokens = $phpcsFile->getTokens();
41+
if ($tokens[$stackPtr]['content'] === $phpcsFile->eolChar
42+
&& isset($tokens[$stackPtr + 1]) === true
43+
&& $tokens[$stackPtr + 1]['content'] === $phpcsFile->eolChar
44+
&& isset($tokens[$stackPtr + 2]) === true
45+
&& $tokens[$stackPtr + 2]['content'] === $phpcsFile->eolChar
46+
) {
47+
$error = 'More than 1 empty lines are not allowed';
48+
$fix = $phpcsFile->addFixableError($error, $stackPtr + 2, 'EmptyLines');
49+
50+
if ($fix === true) {
51+
$phpcsFile->fixer->replaceToken($stackPtr + 2, '');
52+
}
53+
}
54+
}
55+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
$a = 2;
4+
5+
6+
7+
$b = 3;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
$a = 2;
4+
5+
$b = 3;
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
/**
4+
* Unit test class for the EmptyLines sniff.
5+
*
6+
* A sniff unit test checks a .inc file for expected violations of a single
7+
* coding standard. Expected errors and warnings are stored in this class.
8+
*/
9+
class Symfony3Custom_Tests_WhiteSpace_EmptyLinesUnitTest extends AbstractSniffUnitTest
10+
{
11+
/**
12+
* Returns the lines where errors should occur.
13+
*
14+
* The key of the array should represent the line number and the value
15+
* should represent the number of errors that should occur on that line.
16+
*
17+
* @return array<int, int>
18+
*/
19+
public function getErrorList()
20+
{
21+
return array(
22+
5 => 1,
23+
6 => 1,
24+
);
25+
}
26+
27+
/**
28+
* Returns the lines where warnings should occur.
29+
*
30+
* The key of the array should represent the line number and the value
31+
* should represent the number of warnings that should occur on that line.
32+
*
33+
* @return array(int => int)
34+
*/
35+
protected function getWarningList()
36+
{
37+
return array();
38+
}
39+
}

0 commit comments

Comments
 (0)