Skip to content

Commit 31b207c

Browse files
Merge pull request #30 from VincentLanglet/staging
✨ Add new rules
2 parents eb18941 + ad8d937 commit 31b207c

File tree

5 files changed

+136
-0
lines changed

5 files changed

+136
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace Symfony3Custom\Sniffs\Classes;
4+
5+
use PHP_CodeSniffer\Files\File;
6+
7+
/**
8+
* Checks that there are not empty lines following class declaration.
9+
*/
10+
class ClassDeclarationSniff
11+
{
12+
/**
13+
* @return array
14+
*/
15+
public function register()
16+
{
17+
return array(
18+
T_CLASS,
19+
T_INTERFACE,
20+
T_TRAIT,
21+
);
22+
}
23+
24+
/**
25+
* Processes this test, when one of its tokens is encountered.
26+
*
27+
* @param File $phpcsFile The file being scanned.
28+
* @param int $stackPtr The position of the current token
29+
* in the stack passed in $tokens.
30+
*
31+
* @return void
32+
*/
33+
public function process(File $phpcsFile, $stackPtr)
34+
{
35+
// Just in case.
36+
$tokens = $phpcsFile->getTokens();
37+
$openingBrace = $tokens[$stackPtr]['scope_opener'];
38+
39+
if (isset($openingBrace) === false) {
40+
return;
41+
}
42+
43+
$nextElement = $phpcsFile->findNext(array(T_WHITESPACE), $openingBrace + 1, null, true);
44+
45+
if ($tokens[$openingBrace]['line'] + 1 < $tokens[$nextElement]['line']) {
46+
$fix = $phpcsFile->addFixableError(
47+
'The opening brace should not be followed by a blank line',
48+
$openingBrace,
49+
'Invalid'
50+
);
51+
52+
if (true === $fix) {
53+
$phpcsFile->fixer->replaceToken($openingBrace + 1, '');
54+
}
55+
}
56+
}
57+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
class ClassTest
4+
{
5+
6+
}
7+
8+
class ClassTest2
9+
{
10+
}
11+
12+
class ClassTest3
13+
{
14+
15+
16+
public $a = 1;
17+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
class ClassTest
4+
{
5+
}
6+
7+
class ClassTest2
8+
{
9+
}
10+
11+
class ClassTest3
12+
{
13+
public $a = 1;
14+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace Symfony3Custom\Tests\Classes;
4+
5+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
6+
7+
/**
8+
* Unit test class for the ClassDeclaration sniff.
9+
*
10+
* @group Symfony3Custom
11+
*/
12+
class ClassDeclarationUnitTest extends AbstractSniffUnitTest
13+
{
14+
/**
15+
* Returns the lines where errors should occur.
16+
*
17+
* The key of the array should represent the line number and the value
18+
* should represent the number of errors that should occur on that line.
19+
*
20+
* @return array<int, int>
21+
*/
22+
public function getErrorList()
23+
{
24+
return array(
25+
4 => 1,
26+
13 => 1,
27+
);
28+
}
29+
30+
/**
31+
* Returns the lines where warnings should occur.
32+
*
33+
* The key of the array should represent the line number and the value
34+
* should represent the number of errors that should occur on that line.
35+
*
36+
* @return array(int => int)
37+
*/
38+
public function getWarningList()
39+
{
40+
return array();
41+
}
42+
}

docs/standards.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,12 @@ we do not respect this rule:
101101
<rule ref="Symfony3Custom.WhiteSpace.OpenBracketSpacing"/>
102102
```
103103

104+
- Do not use blank lines after class openers `{`
105+
106+
```
107+
<rule ref="Symfony3Custom.Classes.ClassDeclaration"/>
108+
```
109+
104110
- Do not use multiple following blank lines
105111

106112
```

0 commit comments

Comments
 (0)