Skip to content

Commit ad96ebb

Browse files
committed
Control structure sniffs for the Squiz coding standard
git-svn-id: http://svn.php.net/repository/pear/packages/PHP_CodeSniffer/trunk@225434 c90b9560-bf6c-de11-be94-00142212c4b1
1 parent bce506d commit ad96ebb

21 files changed

+1699
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
/**
3+
* Verifies that control statements conform to their coding standards.
4+
*
5+
* PHP version 5
6+
*
7+
* @category PHP
8+
* @package PHP_CodeSniffer
9+
* @author Greg Sherwood <gsherwood@squiz.net>
10+
* @author Marc McIntyre <mmcintyre@squiz.net>
11+
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
12+
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
13+
* @version CVS: $Id$
14+
* @link http://pear.php.net/package/PHP_CodeSniffer
15+
*/
16+
17+
require_once 'PHP/CodeSniffer/Standards/AbstractPatternSniff.php';
18+
19+
/**
20+
* Verifies that control statements conform to their coding standards.
21+
*
22+
* @category PHP
23+
* @package PHP_CodeSniffer
24+
* @author Greg Sherwood <gsherwood@squiz.net>
25+
* @author Marc McIntyre <mmcintyre@squiz.net>
26+
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
27+
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
28+
* @version Release: @package_version@
29+
* @link http://pear.php.net/package/PHP_CodeSniffer
30+
*/
31+
class Squiz_Sniffs_ControlStructures_ControlSignatureSniff extends PHP_CodeSniffer_Standards_AbstractPatternSniff
32+
{
33+
34+
35+
/**
36+
* Returns the patterns that this test wishes to verify.
37+
*
38+
* @return array(string)
39+
*/
40+
protected function getPatterns()
41+
{
42+
return array(
43+
"try {\n...} catch (...) {\n",
44+
"do {\n...} while (...);\n",
45+
"while (...) {\n",
46+
"for (...) {\n",
47+
"if (...) {\n",
48+
"foreach (...) {\n",
49+
"} else if (...) {\n",
50+
"} else {\n",
51+
"do {\n",
52+
);
53+
54+
}//end getPatterns()
55+
56+
57+
}//end class
58+
59+
?>
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
/**
3+
* Squiz_Sniffs_ControlStructures_ElseIfDeclarationSniff.
4+
*
5+
* PHP version 5
6+
*
7+
* @category PHP
8+
* @package PHP_CodeSniffer
9+
* @author Greg Sherwood <gsherwood@squiz.net>
10+
* @author Marc McIntyre <mmcintyre@squiz.net>
11+
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
12+
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
13+
* @version CVS: $Id$
14+
* @link http://pear.php.net/package/PHP_CodeSniffer
15+
*/
16+
17+
require_once 'PHP/CodeSniffer/Sniff.php';
18+
19+
/**
20+
* Squiz_Sniffs_ControlStructures_ElseIfDeclarationSniff.
21+
*
22+
* Verifies that there are not elseif statements. The else and the if should
23+
* be separated by a space.
24+
*
25+
* @category PHP
26+
* @package PHP_CodeSniffer
27+
* @author Greg Sherwood <gsherwood@squiz.net>
28+
* @author Marc McIntyre <mmcintyre@squiz.net>
29+
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
30+
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
31+
* @version Release: @package_version@
32+
* @link http://pear.php.net/package/PHP_CodeSniffer
33+
*/
34+
class Squiz_Sniffs_ControlStructures_ElseIfDeclarationSniff implements PHP_CodeSniffer_Sniff
35+
{
36+
37+
38+
/**
39+
* Returns an array of tokens this test wants to listen for.
40+
*
41+
* @return array
42+
*/
43+
public function register()
44+
{
45+
return array(T_ELSEIF);
46+
47+
}//end register()
48+
49+
50+
/**
51+
* Processes this test, when one of its tokens is encountered.
52+
*
53+
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
54+
* @param int $stackPtr The position of the current token in the
55+
* stack passed in $tokens.
56+
*
57+
* @return void
58+
*/
59+
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
60+
{
61+
$error = 'Usage of ELSEIF not allowed. Use ELSE IF instead.';
62+
$phpcsFile->addError($error, $stackPtr);
63+
64+
}//end process()
65+
66+
67+
}//end class
68+
69+
70+
?>
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
<?php
2+
/**
3+
* Squiz_Sniffs_ControlStructures_ForEachLoopDeclarationSniff.
4+
*
5+
* PHP version 5
6+
*
7+
* @category PHP
8+
* @package PHP_CodeSniffer
9+
* @author Greg Sherwood <gsherwood@squiz.net>
10+
* @author Marc McIntyre <mmcintyre@squiz.net>
11+
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
12+
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
13+
* @version CVS: $Id$
14+
* @link http://pear.php.net/package/PHP_CodeSniffer
15+
*/
16+
17+
require_once 'PHP/CodeSniffer/Sniff.php';
18+
19+
/**
20+
* Squiz_Sniffs_ControlStructures_ForEachLoopDeclarationSniff.
21+
*
22+
* Verifies that there is a spce between each condition of foreach loops.
23+
*
24+
* @category PHP
25+
* @package PHP_CodeSniffer
26+
* @author Greg Sherwood <gsherwood@squiz.net>
27+
* @author Marc McIntyre <mmcintyre@squiz.net>
28+
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
29+
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
30+
* @version Release: @package_version@
31+
* @link http://pear.php.net/package/PHP_CodeSniffer
32+
*/
33+
class Squiz_Sniffs_ControlStructures_ForEachLoopDeclarationSniff implements PHP_CodeSniffer_Sniff
34+
{
35+
36+
37+
/**
38+
* Returns an array of tokens this test wants to listen for.
39+
*
40+
* @return array
41+
*/
42+
public function register()
43+
{
44+
return array(
45+
T_FOREACH,
46+
);
47+
48+
}//end register()
49+
50+
51+
/**
52+
* Processes this test, when one of its tokens is encountered.
53+
*
54+
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
55+
* @param int $stackPtr The position of the current token in the
56+
* stack passed in $tokens.
57+
*
58+
* @return void
59+
*/
60+
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
61+
{
62+
$tokens = $phpcsFile->getTokens();
63+
64+
$errors = array();
65+
66+
$openingBracket = $phpcsFile->findNext(T_OPEN_PARENTHESIS, $stackPtr);
67+
$closingBracket = $tokens[$openingBracket]['parenthesis_closer'];
68+
69+
if ($tokens[$openingBracket + 1]['code'] === T_WHITESPACE) {
70+
$errors[] = 'Space found after opening bracket of FOREACH loop';
71+
}
72+
73+
if ($tokens[$closingBracket - 1]['code'] === T_WHITESPACE) {
74+
$errors[] = 'Space found before closing bracket of FOREACH loop';
75+
}
76+
77+
$asToken = $phpcsFile->findNext(T_AS, $openingBracket);
78+
$doubleArrow = $phpcsFile->findNext(T_DOUBLE_ARROW, $openingBracket, $closingBracket);
79+
80+
if ($doubleArrow !== false) {
81+
if ($tokens[$doubleArrow - 1]['code'] !== T_WHITESPACE) {
82+
$errors[] = 'Expected 1 space before "=>"; 0 found';
83+
} else {
84+
if (strlen($tokens[$doubleArrow - 1]['content']) !== 1) {
85+
$spaces = strlen($tokens[$doubleArrow - 1]['content']);
86+
$errors[] = "Expected 1 space before \"=>\"; $spaces found";
87+
}
88+
89+
}
90+
91+
if ($tokens[$doubleArrow + 1]['code'] !== T_WHITESPACE) {
92+
$errors[] = 'Expected 1 space after "=>"; 0 found';
93+
} else {
94+
if (strlen($tokens[$doubleArrow + 1]['content']) !== 1) {
95+
$spaces = strlen($tokens[$doubleArrow + 1]['content']);
96+
$errors[] = "Expected 1 space after \"=>\"; $spaces found";
97+
}
98+
99+
}
100+
101+
}//end if
102+
103+
if ($tokens[$asToken + 1]['code'] !== T_WHITESPACE) {
104+
$errors[] = 'Expected 1 space before "as"; 0 found';
105+
} else {
106+
if (strlen($tokens[$asToken - 1]['content']) !== 1) {
107+
$spaces = strlen($tokens[$asToken - 1]['content']);
108+
$errors[] = "Expected 1 space before \"as\"; $spaces found";
109+
}
110+
}
111+
112+
if ($tokens[$asToken + 1]['code'] !== T_WHITESPACE) {
113+
$errors[] = 'Expected 1 space after "as"; 0 found';
114+
} else {
115+
if (strlen($tokens[$asToken + 1]['content']) !== 1) {
116+
$spaces = strlen($tokens[$asToken + 1]['content']);
117+
$errors[] = "Expected 1 space after \"as\"; $spaces found";
118+
}
119+
120+
}
121+
122+
foreach ($errors as $error) {
123+
$phpcsFile->addError($error, $stackPtr);
124+
}
125+
126+
}//end process()
127+
128+
129+
}//end class
130+
131+
?>
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?php
2+
/**
3+
* Squiz_Sniffs_ControlStructures_ForLoopDeclarationSniff.
4+
*
5+
* PHP version 5
6+
*
7+
* @category PHP
8+
* @package PHP_CodeSniffer
9+
* @author Greg Sherwood <gsherwood@squiz.net>
10+
* @author Marc McIntyre <mmcintyre@squiz.net>
11+
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
12+
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
13+
* @version CVS: $Id$
14+
* @link http://pear.php.net/package/PHP_CodeSniffer
15+
*/
16+
17+
require_once 'PHP/CodeSniffer/Sniff.php';
18+
19+
/**
20+
* Squiz_Sniffs_ControlStructures_ForLoopDeclarationSniff.
21+
*
22+
* Verifies that there is a spce between each condition of for loops.
23+
*
24+
* @category PHP
25+
* @package PHP_CodeSniffer
26+
* @author Greg Sherwood <gsherwood@squiz.net>
27+
* @author Marc McIntyre <mmcintyre@squiz.net>
28+
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
29+
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
30+
* @version Release: @package_version@
31+
* @link http://pear.php.net/package/PHP_CodeSniffer
32+
*/
33+
class Squiz_Sniffs_ControlStructures_ForLoopDeclarationSniff implements PHP_CodeSniffer_Sniff
34+
{
35+
36+
37+
/**
38+
* Returns an array of tokens this test wants to listen for.
39+
*
40+
* @return array
41+
*/
42+
public function register()
43+
{
44+
return array(
45+
T_FOR,
46+
);
47+
48+
}//end register()
49+
50+
51+
/**
52+
* Processes this test, when one of its tokens is encountered.
53+
*
54+
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
55+
* @param int $stackPtr The position of the current token in the
56+
* stack passed in $tokens.
57+
*
58+
* @return void
59+
*/
60+
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
61+
{
62+
$tokens = $phpcsFile->getTokens();
63+
64+
$errors = array();
65+
66+
$openingBracket = $phpcsFile->findNext(T_OPEN_PARENTHESIS, $stackPtr);
67+
$closingBracket = $tokens[$openingBracket]['parenthesis_closer'];
68+
69+
if ($tokens[$openingBracket + 1]['code'] === T_WHITESPACE) {
70+
$errors[] = 'Space found after opening bracket of FOR loop';
71+
}
72+
73+
if ($tokens[$closingBracket - 1]['code'] === T_WHITESPACE) {
74+
$errors[] = 'Space found before closing bracket of FOR loop';
75+
}
76+
77+
$firstSemicolon = $phpcsFile->findNext(T_SEMICOLON, $openingBracket);
78+
$secondSemicolon = $phpcsFile->findNext(T_SEMICOLON, $firstSemicolon + 1);
79+
80+
// Check whitespace around each of the tokens.
81+
if ($tokens[$firstSemicolon - 1]['code'] === T_WHITESPACE) {
82+
$errors[] = 'Space found before first semicolon of FOR loop';
83+
}
84+
85+
if ($tokens[$firstSemicolon + 1]['code'] !== T_WHITESPACE) {
86+
$errors[] = 'Expected 1 space after first semicolon of FOR loop; 0 found';
87+
} else {
88+
if (strlen($tokens[$firstSemicolon + 1]['content']) !== 1) {
89+
$spaces = strlen($tokens[$firstSemicolon + 1]['content']);
90+
$errors[] = "Expected 1 space after first semicolon of FOR loop; $spaces found";
91+
}
92+
}
93+
94+
if ($tokens[$secondSemicolon - 1]['code'] === T_WHITESPACE) {
95+
$errors[] = 'Space found before second semicolon of FOR loop';
96+
}
97+
98+
if ($tokens[$secondSemicolon + 1]['code'] !== T_WHITESPACE) {
99+
$errors[] = 'Expected 1 space after second semicolon of FOR loop; 0 found';
100+
} else {
101+
if (strlen($tokens[$secondSemicolon + 1]['content']) !== 1) {
102+
$spaces = strlen($tokens[$firstSemicolon + 1]['content']);
103+
$errors[] = "Expected 1 space after second semicolon of FOR loop; $spaces found";
104+
}
105+
}
106+
107+
foreach ($errors as $error) {
108+
$phpcsFile->addError($error, $stackPtr);
109+
}
110+
111+
}//end process()
112+
113+
114+
}//end class
115+
116+
?>

0 commit comments

Comments
 (0)