Skip to content

Commit 26cacb9

Browse files
Merge pull request #21 from VincentLanglet/staging
🐛 Handle first line case
2 parents 55146a4 + 246714d commit 26cacb9

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

Symfony3Custom/Sniffs/WhiteSpace/EmptyLinesSniff.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,23 @@ public function register()
2929
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
3030
{
3131
$tokens = $phpcsFile->getTokens();
32+
33+
// Special case for the first line
34+
if (isset($tokens[$stackPtr - 1])
35+
&& 'T_OPEN_TAG' === $tokens[$stackPtr - 1]['type']
36+
&& $tokens[$stackPtr]['content'] === $phpcsFile->eolChar
37+
&& isset($tokens[$stackPtr + 1]) === true
38+
&& $tokens[$stackPtr + 1]['content'] === $phpcsFile->eolChar
39+
) {
40+
$error = 'More than 1 empty lines are not allowed';
41+
$fix = $phpcsFile->addFixableError($error, $stackPtr + 1, 'EmptyLines');
42+
43+
if (true === $fix) {
44+
$phpcsFile->fixer->replaceToken($stackPtr + 1, '');
45+
}
46+
}
47+
48+
// General case
3249
if ($tokens[$stackPtr]['content'] === $phpcsFile->eolChar
3350
&& isset($tokens[$stackPtr + 1]) === true
3451
&& $tokens[$stackPtr + 1]['content'] === $phpcsFile->eolChar

Symfony3Custom/Tests/WhiteSpace/EmptyLinesUnitTest.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
34
$a = 2;
45

56

Symfony3Custom/Tests/WhiteSpace/EmptyLinesUnitTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ class Symfony3Custom_Tests_WhiteSpace_EmptyLinesUnitTest extends AbstractSniffUn
1919
public function getErrorList()
2020
{
2121
return array(
22-
5 => 1,
22+
3 => 1,
2323
6 => 1,
24+
7 => 1,
2425
);
2526
}
2627

0 commit comments

Comments
 (0)