Skip to content

Commit 17c7bc8

Browse files
author
Vincent Langlet
committed
✨ Add new rules
1 parent eb18941 commit 17c7bc8

File tree

5 files changed

+133
-0
lines changed

5 files changed

+133
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace Symfony3Custom\Sniffs\Classes;
4+
5+
use PHP_CodeSniffer\Files\File;
6+
7+
class ClassDeclarationSniff
8+
{
9+
/**
10+
* @return array
11+
*/
12+
public function register()
13+
{
14+
return array(
15+
T_CLASS,
16+
T_INTERFACE,
17+
T_TRAIT,
18+
);
19+
}
20+
21+
/**
22+
* Processes this test, when one of its tokens is encountered.
23+
*
24+
* @param File $phpcsFile The file being scanned.
25+
* @param int $stackPtr The position of the current token
26+
* in the stack passed in $tokens.
27+
*
28+
* @return void
29+
*/
30+
public function process(File $phpcsFile, $stackPtr)
31+
{
32+
// Just in case.
33+
$tokens = $phpcsFile->getTokens();
34+
$openingBrace = $tokens[$stackPtr]['scope_opener'];
35+
36+
if (isset($openingBrace) === false) {
37+
return;
38+
}
39+
40+
$nextElement = $phpcsFile->findNext(array(T_WHITESPACE), $openingBrace + 1, null, true);
41+
42+
if ($tokens[$openingBrace]['line'] + 1 < $tokens[$nextElement]['line']) {
43+
$fix = $phpcsFile->addFixableError(
44+
'The opening brace should not be followed by a blank line',
45+
$openingBrace,
46+
'Invalid'
47+
);
48+
49+
if (true === $fix) {
50+
$phpcsFile->fixer->replaceToken($openingBrace + 1, '');
51+
}
52+
}
53+
}
54+
}
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)