| 1 |
lars |
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: ForLoopDeclarationSniff.php 289611 2009-10-13 07:52:23Z sebastian $
|
|
|
14 |
* @link http://pear.php.net/package/PHP_CodeSniffer
|
|
|
15 |
*/
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* Squiz_Sniffs_ControlStructures_ForLoopDeclarationSniff.
|
|
|
19 |
*
|
|
|
20 |
* Verifies that there is a space between each condition of for loops.
|
|
|
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: 1.2.1
|
|
|
29 |
* @link http://pear.php.net/package/PHP_CodeSniffer
|
|
|
30 |
*/
|
|
|
31 |
class Squiz_Sniffs_ControlStructures_ForLoopDeclarationSniff implements PHP_CodeSniffer_Sniff
|
|
|
32 |
{
|
|
|
33 |
|
|
|
34 |
/**
|
|
|
35 |
* A list of tokenizers this sniff supports.
|
|
|
36 |
*
|
|
|
37 |
* @var array
|
|
|
38 |
*/
|
|
|
39 |
public $supportedTokenizers = array(
|
|
|
40 |
'PHP',
|
|
|
41 |
'JS',
|
|
|
42 |
);
|
|
|
43 |
|
|
|
44 |
|
|
|
45 |
/**
|
|
|
46 |
* Returns an array of tokens this test wants to listen for.
|
|
|
47 |
*
|
|
|
48 |
* @return array
|
|
|
49 |
*/
|
|
|
50 |
public function register()
|
|
|
51 |
{
|
|
|
52 |
return array(T_FOR);
|
|
|
53 |
|
|
|
54 |
}//end register()
|
|
|
55 |
|
|
|
56 |
|
|
|
57 |
/**
|
|
|
58 |
* Processes this test, when one of its tokens is encountered.
|
|
|
59 |
*
|
|
|
60 |
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
|
|
|
61 |
* @param int $stackPtr The position of the current token in the
|
|
|
62 |
* stack passed in $tokens.
|
|
|
63 |
*
|
|
|
64 |
* @return void
|
|
|
65 |
*/
|
|
|
66 |
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
|
|
|
67 |
{
|
|
|
68 |
$tokens = $phpcsFile->getTokens();
|
|
|
69 |
$errors = array();
|
|
|
70 |
|
|
|
71 |
$openingBracket = $phpcsFile->findNext(T_OPEN_PARENTHESIS, $stackPtr);
|
|
|
72 |
if ($openingBracket === false) {
|
|
|
73 |
$error = 'Possible parse error: no opening parenthesis for FOR keyword';
|
|
|
74 |
$phpcsFile->addWarning($error, $stackPtr);
|
|
|
75 |
return;
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
$closingBracket = $tokens[$openingBracket]['parenthesis_closer'];
|
|
|
79 |
|
|
|
80 |
if ($tokens[($openingBracket + 1)]['code'] === T_WHITESPACE) {
|
|
|
81 |
$errors[] = 'Space found after opening bracket of FOR loop';
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
if ($tokens[($closingBracket - 1)]['code'] === T_WHITESPACE) {
|
|
|
85 |
$errors[] = 'Space found before closing bracket of FOR loop';
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
$firstSemicolon = $phpcsFile->findNext(T_SEMICOLON, $openingBracket, $closingBracket);
|
|
|
89 |
|
|
|
90 |
// Check whitespace around each of the tokens.
|
|
|
91 |
if ($firstSemicolon !== false) {
|
|
|
92 |
if ($tokens[($firstSemicolon - 1)]['code'] === T_WHITESPACE) {
|
|
|
93 |
$errors[] = 'Space found before first semicolon of FOR loop';
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
if ($tokens[($firstSemicolon + 1)]['code'] !== T_WHITESPACE) {
|
|
|
97 |
$errors[] = 'Expected 1 space after first semicolon of FOR loop; 0 found';
|
|
|
98 |
} else {
|
|
|
99 |
if (strlen($tokens[($firstSemicolon + 1)]['content']) !== 1) {
|
|
|
100 |
$spaces = strlen($tokens[($firstSemicolon + 1)]['content']);
|
|
|
101 |
$errors[] = "Expected 1 space after first semicolon of FOR loop; $spaces found";
|
|
|
102 |
}
|
|
|
103 |
}
|
|
|
104 |
|
|
|
105 |
$secondSemicolon = $phpcsFile->findNext(T_SEMICOLON, ($firstSemicolon + 1));
|
|
|
106 |
|
|
|
107 |
if ($secondSemicolon !== false) {
|
|
|
108 |
if ($tokens[($secondSemicolon - 1)]['code'] === T_WHITESPACE) {
|
|
|
109 |
$errors[] = 'Space found before second semicolon of FOR loop';
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
if ($tokens[($secondSemicolon + 1)]['code'] !== T_WHITESPACE) {
|
|
|
113 |
$errors[] = 'Expected 1 space after second semicolon of FOR loop; 0 found';
|
|
|
114 |
} else {
|
|
|
115 |
if (strlen($tokens[($secondSemicolon + 1)]['content']) !== 1) {
|
|
|
116 |
$spaces = strlen($tokens[($firstSemicolon + 1)]['content']);
|
|
|
117 |
$errors[] = "Expected 1 space after second semicolon of FOR loop; $spaces found";
|
|
|
118 |
}
|
|
|
119 |
}
|
|
|
120 |
}//end if
|
|
|
121 |
}//end if
|
|
|
122 |
|
|
|
123 |
foreach ($errors as $error) {
|
|
|
124 |
$phpcsFile->addError($error, $stackPtr);
|
|
|
125 |
}
|
|
|
126 |
|
|
|
127 |
}//end process()
|
|
|
128 |
|
|
|
129 |
|
|
|
130 |
}//end class
|
|
|
131 |
|
|
|
132 |
?>
|