Skip to content

Commit 79b8067

Browse files
committed
Add namespace spacing sniff
1 parent 1bcad5c commit 79b8067

File tree

4 files changed

+60
-2
lines changed

4 files changed

+60
-2
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace PSR2R\Sniffs\WhiteSpace;
4+
5+
use PHP_CodeSniffer\Files\File;
6+
use PSR2R\Tools\AbstractSniff;
7+
8+
/**
9+
* Checks that the namespace declaration and body has correct spacing.
10+
*
11+
* @author Mark Scherer
12+
* @license MIT
13+
*/
14+
class NamespaceSpacingSniff extends AbstractSniff {
15+
16+
/**
17+
* @inheritDoc
18+
*/
19+
public function register() {
20+
return [T_NAMESPACE];
21+
}
22+
23+
/**
24+
* @inheritDoc
25+
*/
26+
public function process(File $phpcsFile, $stackPtr) {
27+
$tokens = $phpcsFile->getTokens();
28+
29+
$beforeIndex = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
30+
$beforeLine = $tokens[$beforeIndex]['line'];
31+
32+
if ($beforeLine === $tokens[$stackPtr]['line'] - 2) {
33+
return;
34+
}
35+
36+
$error = 'There must be one blank line before the namespace declaration';
37+
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'BlankLineBefore');
38+
if (!$fix) {
39+
return;
40+
}
41+
42+
if ($beforeLine < $tokens[$stackPtr]['line'] - 2) {
43+
$phpcsFile->fixer->beginChangeset();
44+
$phpcsFile->fixer->replaceToken($stackPtr - 1, '');
45+
$phpcsFile->fixer->endChangeset();
46+
47+
return;
48+
}
49+
50+
$phpcsFile->fixer->beginChangeset();
51+
$phpcsFile->fixer->addNewline($beforeIndex);
52+
$phpcsFile->fixer->endChangeset();
53+
}
54+
55+
}

PSR2R/Tools/Traits/CommentingTrait.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
namespace PSR2R\Tools\Traits;
34

45
use PHP_CodeSniffer\Files\File;

PSR2R/Tools/Traits/NamespaceTrait.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
namespace PSR2R\Tools\Traits;
34

45
use PHP_CodeSniffer\Files\File;

docs/sniffs.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# PSR2R Code Sniffer
22

33

4-
The PSR2R standard contains 127 sniffs
4+
The PSR2R standard contains 128 sniffs
55

66
Generic (21 sniffs)
77
-------------------
@@ -55,7 +55,7 @@ PSR2 (4 sniffs)
5555
- PSR2.Namespaces.NamespaceDeclaration
5656
- PSR2.Namespaces.UseDeclaration
5757

58-
PSR2R (68 sniffs)
58+
PSR2R (69 sniffs)
5959
-----------------
6060
- PSR2R.Classes.BraceOnSameLine
6161
- PSR2R.Classes.ClassCreateInstance
@@ -120,6 +120,7 @@ PSR2R (68 sniffs)
120120
- PSR2R.WhiteSpace.EmptyLines
121121
- PSR2R.WhiteSpace.LanguageConstructSpacing
122122
- PSR2R.WhiteSpace.MethodSpacing
123+
- PSR2R.WhiteSpace.NamespaceSpacing
123124
- PSR2R.WhiteSpace.ObjectAttributeSpacing
124125
- PSR2R.WhiteSpace.OperatorSpacing
125126
- PSR2R.WhiteSpace.TabAndSpace

0 commit comments

Comments
 (0)