| 1 |
lars |
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: ForEachLoopDeclarationSniff.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_ForEachLoopDeclarationSniff.
|
|
|
19 |
*
|
|
|
20 |
* Verifies that there is a space between each condition of foreach 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_ForEachLoopDeclarationSniff implements PHP_CodeSniffer_Sniff
|
|
|
32 |
{
|
|
|
33 |
|
|
|
34 |
|
|
|
35 |
/**
|
|
|
36 |
* Returns an array of tokens this test wants to listen for.
|
|
|
37 |
*
|
|
|
38 |
* @return array
|
|
|
39 |
*/
|
|
|
40 |
public function register()
|
|
|
41 |
{
|
|
|
42 |
return array(T_FOREACH);
|
|
|
43 |
|
|
|
44 |
}//end register()
|
|
|
45 |
|
|
|
46 |
|
|
|
47 |
/**
|
|
|
48 |
* Processes this test, when one of its tokens is encountered.
|
|
|
49 |
*
|
|
|
50 |
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
|
|
|
51 |
* @param int $stackPtr The position of the current token in the
|
|
|
52 |
* stack passed in $tokens.
|
|
|
53 |
*
|
|
|
54 |
* @return void
|
|
|
55 |
*/
|
|
|
56 |
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
|
|
|
57 |
{
|
|
|
58 |
$tokens = $phpcsFile->getTokens();
|
|
|
59 |
$errors = array();
|
|
|
60 |
|
|
|
61 |
$openingBracket = $phpcsFile->findNext(T_OPEN_PARENTHESIS, $stackPtr);
|
|
|
62 |
$closingBracket = $tokens[$openingBracket]['parenthesis_closer'];
|
|
|
63 |
|
|
|
64 |
if ($tokens[($openingBracket + 1)]['code'] === T_WHITESPACE) {
|
|
|
65 |
$errors[] = 'Space found after opening bracket of FOREACH loop';
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
if ($tokens[($closingBracket - 1)]['code'] === T_WHITESPACE) {
|
|
|
69 |
$errors[] = 'Space found before closing bracket of FOREACH loop';
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
$asToken = $phpcsFile->findNext(T_AS, $openingBracket);
|
|
|
73 |
$content = $tokens[$asToken]['content'];
|
|
|
74 |
if ($content !== strtolower($content)) {
|
|
|
75 |
$expected = strtolower($content);
|
|
|
76 |
$errors[] = "AS keyword must be lowercase; expected \"$expected\" but found \"$content\"";
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
$doubleArrow = $phpcsFile->findNext(T_DOUBLE_ARROW, $openingBracket, $closingBracket);
|
|
|
80 |
|
|
|
81 |
if ($doubleArrow !== false) {
|
|
|
82 |
if ($tokens[($doubleArrow - 1)]['code'] !== T_WHITESPACE) {
|
|
|
83 |
$errors[] = 'Expected 1 space before "=>"; 0 found';
|
|
|
84 |
} else {
|
|
|
85 |
if (strlen($tokens[($doubleArrow - 1)]['content']) !== 1) {
|
|
|
86 |
$spaces = strlen($tokens[($doubleArrow - 1)]['content']);
|
|
|
87 |
$errors[] = "Expected 1 space before \"=>\"; $spaces found";
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
if ($tokens[($doubleArrow + 1)]['code'] !== T_WHITESPACE) {
|
|
|
93 |
$errors[] = 'Expected 1 space after "=>"; 0 found';
|
|
|
94 |
} else {
|
|
|
95 |
if (strlen($tokens[($doubleArrow + 1)]['content']) !== 1) {
|
|
|
96 |
$spaces = strlen($tokens[($doubleArrow + 1)]['content']);
|
|
|
97 |
$errors[] = "Expected 1 space after \"=>\"; $spaces found";
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
}//end if
|
|
|
103 |
|
|
|
104 |
if ($tokens[($asToken - 1)]['code'] !== T_WHITESPACE) {
|
|
|
105 |
$errors[] = 'Expected 1 space before "as"; 0 found';
|
|
|
106 |
} else {
|
|
|
107 |
if (strlen($tokens[($asToken - 1)]['content']) !== 1) {
|
|
|
108 |
$spaces = strlen($tokens[($asToken - 1)]['content']);
|
|
|
109 |
$errors[] = "Expected 1 space before \"as\"; $spaces found";
|
|
|
110 |
}
|
|
|
111 |
}
|
|
|
112 |
|
|
|
113 |
if ($tokens[($asToken + 1)]['code'] !== T_WHITESPACE) {
|
|
|
114 |
$errors[] = 'Expected 1 space after "as"; 0 found';
|
|
|
115 |
} else {
|
|
|
116 |
if (strlen($tokens[($asToken + 1)]['content']) !== 1) {
|
|
|
117 |
$spaces = strlen($tokens[($asToken + 1)]['content']);
|
|
|
118 |
$errors[] = "Expected 1 space after \"as\"; $spaces found";
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
foreach ($errors as $error) {
|
|
|
124 |
$phpcsFile->addError($error, $stackPtr);
|
|
|
125 |
}
|
|
|
126 |
|
|
|
127 |
}//end process()
|
|
|
128 |
|
|
|
129 |
|
|
|
130 |
}//end class
|
|
|
131 |
|
|
|
132 |
?>
|