Skip to content

Commit 9f76a00

Browse files
committed
Added the namespace sniffs for the PSR2 standard. These include all namespaces and use statement rules.
1 parent e31c7d6 commit 9f76a00

File tree

8 files changed

+378
-0
lines changed

8 files changed

+378
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
/**
3+
* PSR2_Sniffs_Namespaces_NamespaceDeclarationSniff.
4+
*
5+
* PHP version 5
6+
*
7+
* @category PHP
8+
* @package PHP_CodeSniffer
9+
* @author Greg Sherwood <[email protected]>
10+
* @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
11+
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
12+
* @link http://pear.php.net/package/PHP_CodeSniffer
13+
*/
14+
15+
/**
16+
* PSR2_Sniffs_Namespaces_NamespaceDeclarationSniff.
17+
*
18+
* Ensures namespaces are declared correctly.
19+
*
20+
* @category PHP
21+
* @package PHP_CodeSniffer
22+
* @author Greg Sherwood <[email protected]>
23+
* @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
24+
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
25+
* @version Release: @package_version@
26+
* @link http://pear.php.net/package/PHP_CodeSniffer
27+
*/
28+
class PSR2_Sniffs_Namespaces_NamespaceDeclarationSniff implements PHP_CodeSniffer_Sniff
29+
{
30+
31+
32+
/**
33+
* Returns an array of tokens this test wants to listen for.
34+
*
35+
* @return array
36+
*/
37+
public function register()
38+
{
39+
return array(T_NAMESPACE);
40+
41+
}//end register()
42+
43+
44+
/**
45+
* Processes this test, when one of its tokens is encountered.
46+
*
47+
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
48+
* @param int $stackPtr The position of the current token in
49+
* the stack passed in $tokens.
50+
*
51+
* @return void
52+
*/
53+
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
54+
{
55+
$tokens = $phpcsFile->getTokens();
56+
57+
for ($i = ($stackPtr + 1); $i < $phpcsFile->numTokens; $i++) {
58+
if ($tokens[$i]['line'] === $tokens[$stackPtr]['line']) {
59+
continue;
60+
}
61+
62+
break;
63+
}
64+
65+
// The $i var now points to the first token on the line after the
66+
// namespace delcaration, which must be a blank line.
67+
$next = $phpcsFile->findNext(T_WHITESPACE, $i, $phpcsFile->numTokens, true);
68+
if ($tokens[$next]['line'] === $tokens[$i]['line']) {
69+
$error = 'There must be one blank line after the namespace declaration';
70+
$phpcsFile->addError($error, $stackPtr, 'BlankLineAfter');
71+
}
72+
73+
}//end process()
74+
75+
76+
}//end class
77+
78+
79+
?>
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
/**
3+
* PSR2_Sniffs_Namespaces_UseDeclarationSniff.
4+
*
5+
* PHP version 5
6+
*
7+
* @category PHP
8+
* @package PHP_CodeSniffer
9+
* @author Greg Sherwood <[email protected]>
10+
* @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
11+
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
12+
* @link http://pear.php.net/package/PHP_CodeSniffer
13+
*/
14+
15+
/**
16+
* PSR2_Sniffs_Namespaces_UseDeclarationSniff.
17+
*
18+
* Ensures USE blocks are declared correctly.
19+
*
20+
* @category PHP
21+
* @package PHP_CodeSniffer
22+
* @author Greg Sherwood <[email protected]>
23+
* @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
24+
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
25+
* @version Release: @package_version@
26+
* @link http://pear.php.net/package/PHP_CodeSniffer
27+
*/
28+
class PSR2_Sniffs_Namespaces_UseDeclarationSniff implements PHP_CodeSniffer_Sniff
29+
{
30+
31+
32+
/**
33+
* Returns an array of tokens this test wants to listen for.
34+
*
35+
* @return array
36+
*/
37+
public function register()
38+
{
39+
return array(T_USE);
40+
41+
}//end register()
42+
43+
44+
/**
45+
* Processes this test, when one of its tokens is encountered.
46+
*
47+
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
48+
* @param int $stackPtr The position of the current token in
49+
* the stack passed in $tokens.
50+
*
51+
* @return void
52+
*/
53+
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
54+
{
55+
$tokens = $phpcsFile->getTokens();
56+
57+
// Only one USE declaration allowed per statement.
58+
$next = $phpcsFile->findNext(array(T_COMMA, T_SEMICOLON), ($stackPtr + 1));
59+
if ($tokens[$next]['code'] === T_COMMA) {
60+
$error = 'There must be one USE keyword per declaration';
61+
$phpcsFile->addError($error, $stackPtr, 'MultipleDeclarations');
62+
}
63+
64+
// Make sure this USE comes after the first namespace declaration.
65+
$prev = $phpcsFile->findPrevious(T_NAMESPACE, ($stackPtr - 1));
66+
if ($prev !== false) {
67+
$first = $phpcsFile->findNext(T_NAMESPACE, 1);
68+
if ($prev !== $first) {
69+
$error = 'USE declarations must go after the first namespace declaration';
70+
$phpcsFile->addError($error, $stackPtr, 'UseAfterNamespace');
71+
}
72+
}
73+
74+
// Only interested in the last USE statement from here onwards.
75+
$nextUse = $phpcsFile->findNext(T_USE, ($stackPtr + 1));
76+
if ($nextUse !== false) {
77+
return;
78+
}
79+
80+
$end = $phpcsFile->findNext(T_SEMICOLON, ($stackPtr + 1));
81+
$next = $phpcsFile->findNext(T_WHITESPACE, ($end + 1), null, true);
82+
$diff = ($tokens[$next]['line'] - $tokens[$end]['line'] - 1);
83+
if ($diff !== 1) {
84+
if ($diff < 0) {
85+
$diff = 0;
86+
}
87+
88+
$error = 'There must be one blank line after the last USE statement; %s found;';
89+
$data = array($diff);
90+
$phpcsFile->addError($error, $stackPtr, 'SpaceAfterLastUse', $data);
91+
}
92+
93+
}//end process()
94+
95+
96+
}//end class
97+
98+
99+
?>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
namespace Vendor\Package;
3+
4+
use FooClass;
5+
6+
namespace Vendor\Package;
7+
use BarClass as Bar;
8+
9+
?>
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
/**
3+
* Unit test class for the NamespaceDeclaration sniff.
4+
*
5+
* PHP version 5
6+
*
7+
* @category PHP
8+
* @package PHP_CodeSniffer
9+
* @author Greg Sherwood <[email protected]>
10+
* @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
11+
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
12+
* @link http://pear.php.net/package/PHP_CodeSniffer
13+
*/
14+
15+
/**
16+
* Unit test class for the NamespaceDeclaration sniff.
17+
*
18+
* A sniff unit test checks a .inc file for expected violations of a single
19+
* coding standard. Expected errors and warnings are stored in this class.
20+
*
21+
* @category PHP
22+
* @package PHP_CodeSniffer
23+
* @author Greg Sherwood <[email protected]>
24+
* @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
25+
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
26+
* @version Release: @package_version@
27+
* @link http://pear.php.net/package/PHP_CodeSniffer
28+
*/
29+
class PSR2_Tests_Namespaces_NamespaceDeclarationUnitTest extends AbstractSniffUnitTest
30+
{
31+
32+
33+
/**
34+
* Returns the lines where errors should occur.
35+
*
36+
* The key of the array should represent the line number and the value
37+
* should represent the number of errors that should occur on that line.
38+
*
39+
* @return array(int => int)
40+
*/
41+
public function getErrorList()
42+
{
43+
return array(
44+
6 => 1,
45+
);
46+
47+
}//end getErrorList()
48+
49+
50+
/**
51+
* Returns the lines where warnings should occur.
52+
*
53+
* The key of the array should represent the line number and the value
54+
* should represent the number of warnings that should occur on that line.
55+
*
56+
* @return array(int => int)
57+
*/
58+
public function getWarningList()
59+
{
60+
return array();
61+
62+
}//end getWarningList()
63+
64+
65+
}//end class
66+
67+
?>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
namespace MyProject;
3+
4+
use BarClass as Bar;
5+
use My\Full\Classname as Another;
6+
7+
8+
use Something;
9+
use SomethingElse;
10+
11+
// Comment here.
12+
use LastThing;
13+
14+
class Foo {
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
namespace MyProject;
3+
4+
use BarClass as Bar;
5+
use My\Full\Classname as Another, My\Full\NSname;
6+
7+
8+
namespace AnotherProject;
9+
10+
use ArrayObject;
11+
12+
13+
$foo = 'bar';
14+
15+
?>
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
/**
3+
* Unit test class for the UseDeclaration sniff.
4+
*
5+
* PHP version 5
6+
*
7+
* @category PHP
8+
* @package PHP_CodeSniffer
9+
* @author Greg Sherwood <[email protected]>
10+
* @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
11+
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
12+
* @link http://pear.php.net/package/PHP_CodeSniffer
13+
*/
14+
15+
/**
16+
* Unit test class for the UseDeclaration sniff.
17+
*
18+
* A sniff unit test checks a .inc file for expected violations of a single
19+
* coding standard. Expected errors and warnings are stored in this class.
20+
*
21+
* @category PHP
22+
* @package PHP_CodeSniffer
23+
* @author Greg Sherwood <[email protected]>
24+
* @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
25+
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
26+
* @version Release: @package_version@
27+
* @link http://pear.php.net/package/PHP_CodeSniffer
28+
*/
29+
class PSR2_Tests_Namespaces_UseDeclarationUnitTest extends AbstractSniffUnitTest
30+
{
31+
32+
33+
/**
34+
* Returns the lines where errors should occur.
35+
*
36+
* The key of the array should represent the line number and the value
37+
* should represent the number of errors that should occur on that line.
38+
*
39+
* @return array(int => int)
40+
*/
41+
public function getErrorList($testFile='')
42+
{
43+
switch ($testFile) {
44+
case 'UseDeclarationUnitTest.2.inc':
45+
return array(
46+
5 => 1,
47+
10 => 2,
48+
);
49+
break;
50+
default:
51+
return array();
52+
break;
53+
}//end switch
54+
55+
}//end getErrorList()
56+
57+
58+
/**
59+
* Returns the lines where warnings should occur.
60+
*
61+
* The key of the array should represent the line number and the value
62+
* should represent the number of warnings that should occur on that line.
63+
*
64+
* @return array(int => int)
65+
*/
66+
public function getWarningList()
67+
{
68+
return array();
69+
70+
}//end getWarningList()
71+
72+
73+
}//end class
74+
75+
?>

package.xml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -950,6 +950,14 @@ http://pear.php.net/dtd/package-2.0.xsd">
950950
<tasks:replace from="@package_version@" to="version" type="package-info" />
951951
</file>
952952
</dir>
953+
<dir name="Namespaces">
954+
<file baseinstalldir="PHP" name="NamespaceDeclarationSniff.php" role="php">
955+
<tasks:replace from="@package_version@" to="version" type="package-info" />
956+
</file>
957+
<file baseinstalldir="PHP" name="UseDeclarationSniff.php" role="php">
958+
<tasks:replace from="@package_version@" to="version" type="package-info" />
959+
</file>
960+
</dir>
953961
</dir>
954962
<dir name="Tests">
955963
<dir name="Classes">
@@ -974,6 +982,17 @@ http://pear.php.net/dtd/package-2.0.xsd">
974982
<tasks:replace from="@package_version@" to="version" type="package-info" />
975983
</file>
976984
</dir>
985+
<dir name="Namespaces">
986+
<file baseinstalldir="PHP" name="NamespaceDeclarationUnitTest.inc" role="test" />
987+
<file baseinstalldir="PHP" name="NamespaceDeclarationUnitTest.php" role="test">
988+
<tasks:replace from="@package_version@" to="version" type="package-info" />
989+
</file>
990+
<file baseinstalldir="PHP" name="UseDeclarationUnitTest.1.inc" role="test" />
991+
<file baseinstalldir="PHP" name="UseDeclarationUnitTest.2.inc" role="test" />
992+
<file baseinstalldir="PHP" name="UseDeclarationUnitTest.php" role="test">
993+
<tasks:replace from="@package_version@" to="version" type="package-info" />
994+
</file>
995+
</dir>
977996
</dir>
978997
<file baseinstalldir="PHP" name="ruleset.xml" role="php" />
979998
</dir>

0 commit comments

Comments
 (0)