Skip to content

Commit 21cfb84

Browse files
✨ Add check for namespace
1 parent 990863b commit 21cfb84

File tree

4 files changed

+142
-0
lines changed

4 files changed

+142
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
namespace SymfonyCustom\Sniffs\Namespaces;
4+
5+
use PHP_CodeSniffer\Files\File;
6+
use PHP_CodeSniffer\Sniffs\Sniff;
7+
8+
/**
9+
* Ensure there is a blank line before namespace.
10+
* PSR2 checks only for blank line after namespace.
11+
*/
12+
class NamespaceDeclarationSniff implements Sniff
13+
{
14+
/**
15+
* Returns an array of tokens this test wants to listen for.
16+
*
17+
* @return array
18+
*/
19+
public function register()
20+
{
21+
return [T_NAMESPACE];
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 in the stack passed in $tokens.
29+
*
30+
* @return void
31+
*/
32+
public function process(File $phpcsFile, $stackPtr)
33+
{
34+
$tokens = $phpcsFile->getTokens();
35+
36+
for ($i = $stackPtr - 1; $i > 0; $i--) {
37+
if ($tokens[$i]['line'] === $tokens[$stackPtr]['line']) {
38+
continue;
39+
}
40+
41+
break;
42+
}
43+
44+
// The $i var now points to the last token on the line before the
45+
// namespace declaration, which must be a blank line.
46+
$previous = $phpcsFile->findPrevious(T_WHITESPACE, $i, null, true);
47+
if (false === $previous) {
48+
return;
49+
}
50+
51+
$diff = ($tokens[$i]['line'] - $tokens[$previous]['line']);
52+
if (1 === $diff) {
53+
return;
54+
}
55+
56+
if ($diff < 0) {
57+
$diff = 0;
58+
}
59+
60+
$error = 'There must be one blank line before the namespace declaration';
61+
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'BlankLineBefore');
62+
63+
if (true === $fix) {
64+
if (0 === $diff) {
65+
$phpcsFile->fixer->addNewlineBefore($stackPtr);
66+
} else {
67+
$phpcsFile->fixer->beginChangeset();
68+
for ($x = $stackPtr - 1; $x > $previous; $x--) {
69+
if ($tokens[$x]['line'] === $tokens[$previous]['line']) {
70+
break;
71+
}
72+
73+
$phpcsFile->fixer->replaceToken($x, '');
74+
}
75+
76+
$phpcsFile->fixer->addNewlineBefore($stackPtr);
77+
$phpcsFile->fixer->endChangeset();
78+
}
79+
}
80+
}
81+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
namespace MyProject;
3+
4+
namespace Foo;
5+
6+
7+
namespace Toto;
8+
;namespace FooBar;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace MyProject;
4+
5+
namespace Foo;
6+
7+
namespace Toto;
8+
;
9+
10+
namespace FooBar;
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace SymfonyCustom\Tests\Namespaces;
4+
5+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
6+
7+
/**
8+
* Unit test class for the UnusedUse sniff.
9+
*
10+
* @group SymfonyCustom
11+
*/
12+
class NamespaceDeclarationUnitTest 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 [
25+
2 => 1,
26+
7 => 1,
27+
8 => 1,
28+
];
29+
}
30+
31+
/**
32+
* Returns the lines where warnings should occur.
33+
*
34+
* The key of the array should represent the line number and the value
35+
* should represent the number of errors that should occur on that line.
36+
*
37+
* @return array(int => int)
38+
*/
39+
public function getWarningList()
40+
{
41+
return [];
42+
}
43+
}

0 commit comments

Comments
 (0)