| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* Squiz_Sniffs_ControlStructures_LongConditionClosingCommentSniff.
|
|
|
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: LongConditionClosingCommentSniff.php 265574 2008-08-28 04:04:02Z squiz $
|
|
|
14 |
* @link http://pear.php.net/package/PHP_CodeSniffer
|
|
|
15 |
*/
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* Squiz_Sniffs_ControlStructures_LongConditionClosingCommentSniff.
|
|
|
19 |
*
|
|
|
20 |
* @category PHP
|
|
|
21 |
* @package PHP_CodeSniffer
|
|
|
22 |
* @author Greg Sherwood <gsherwood@squiz.net>
|
|
|
23 |
* @author Marc McIntyre <mmcintyre@squiz.net>
|
|
|
24 |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
|
|
|
25 |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
|
|
|
26 |
* @version Release: 1.2.1
|
|
|
27 |
* @link http://pear.php.net/package/PHP_CodeSniffer
|
|
|
28 |
*/
|
|
|
29 |
class Squiz_Sniffs_Commenting_LongConditionClosingCommentSniff implements PHP_CodeSniffer_Sniff
|
|
|
30 |
{
|
|
|
31 |
|
|
|
32 |
/**
|
|
|
33 |
* A list of tokenizers this sniff supports.
|
|
|
34 |
*
|
|
|
35 |
* @var array
|
|
|
36 |
*/
|
|
|
37 |
public $supportedTokenizers = array(
|
|
|
38 |
'PHP',
|
|
|
39 |
'JS',
|
|
|
40 |
);
|
|
|
41 |
|
|
|
42 |
/**
|
|
|
43 |
* The openers that we are interested in.
|
|
|
44 |
*
|
|
|
45 |
* @var array(int)
|
|
|
46 |
*/
|
|
|
47 |
private static $_openers = array(
|
|
|
48 |
T_SWITCH,
|
|
|
49 |
T_IF,
|
|
|
50 |
T_FOR,
|
|
|
51 |
T_FOREACH,
|
|
|
52 |
T_WHILE,
|
|
|
53 |
);
|
|
|
54 |
|
|
|
55 |
/**
|
|
|
56 |
* The length that a code block must be before
|
|
|
57 |
* requiring a closing comment.
|
|
|
58 |
*
|
|
|
59 |
* @var int
|
|
|
60 |
*/
|
|
|
61 |
protected $lineLimit = 20;
|
|
|
62 |
|
|
|
63 |
|
|
|
64 |
/**
|
|
|
65 |
* Returns an array of tokens this test wants to listen for.
|
|
|
66 |
*
|
|
|
67 |
* @return array
|
|
|
68 |
*/
|
|
|
69 |
public function register()
|
|
|
70 |
{
|
|
|
71 |
return array(T_CLOSE_CURLY_BRACKET);
|
|
|
72 |
|
|
|
73 |
}//end register()
|
|
|
74 |
|
|
|
75 |
|
|
|
76 |
/**
|
|
|
77 |
* Processes this test, when one of its tokens is encountered.
|
|
|
78 |
*
|
|
|
79 |
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
|
|
|
80 |
* @param int $stackPtr The position of the current token in the
|
|
|
81 |
* stack passed in $tokens.
|
|
|
82 |
*
|
|
|
83 |
* @return void
|
|
|
84 |
*/
|
|
|
85 |
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
|
|
|
86 |
{
|
|
|
87 |
$tokens = $phpcsFile->getTokens();
|
|
|
88 |
|
|
|
89 |
if (isset($tokens[$stackPtr]['scope_condition']) === false) {
|
|
|
90 |
// No scope condition. It is a function closer.
|
|
|
91 |
return;
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
$startCondition = $tokens[$tokens[$stackPtr]['scope_condition']];
|
|
|
95 |
$startBrace = $tokens[$tokens[$stackPtr]['scope_opener']];
|
|
|
96 |
$endBrace = $tokens[$stackPtr];
|
|
|
97 |
|
|
|
98 |
// We are only interested in some code blocks.
|
|
|
99 |
if (in_array($startCondition['code'], self::$_openers) === false) {
|
|
|
100 |
return;
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
if ($startCondition['code'] === T_IF) {
|
|
|
104 |
// If this is actually and ELSE IF, skip it as the brace
|
|
|
105 |
// will be checked by the original IF.
|
|
|
106 |
$else = $phpcsFile->findPrevious(T_WHITESPACE, ($tokens[$stackPtr]['scope_condition'] - 1), null, true);
|
|
|
107 |
if ($tokens[$else]['code'] === T_ELSE) {
|
|
|
108 |
return;
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
// IF statements that have an ELSE block need to use
|
|
|
112 |
// "end if" rather than "end else" or "end elseif".
|
|
|
113 |
do {
|
|
|
114 |
$nextToken = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
|
|
|
115 |
if ($tokens[$nextToken]['code'] === T_ELSE || $tokens[$nextToken]['code'] === T_ELSEIF) {
|
|
|
116 |
// Check for ELSE IF (2 tokens) as opposed to ELSEIF (1 token).
|
|
|
117 |
if ($tokens[$nextToken]['code'] === T_ELSE && isset($tokens[$nextToken]['scope_closer']) === false) {
|
|
|
118 |
$nextToken = $phpcsFile->findNext(T_WHITESPACE, ($nextToken + 1), null, true);
|
|
|
119 |
if ($tokens[$nextToken]['code'] !== T_IF) {
|
|
|
120 |
break;
|
|
|
121 |
}
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
// The end brace becomes the ELSE's end brace.
|
|
|
125 |
$stackPtr = $tokens[$nextToken]['scope_closer'];
|
|
|
126 |
$endBrace = $tokens[$stackPtr];
|
|
|
127 |
} else {
|
|
|
128 |
break;
|
|
|
129 |
}
|
|
|
130 |
} while (isset($tokens[$nextToken]['scope_closer']) === true);
|
|
|
131 |
}//end if
|
|
|
132 |
|
|
|
133 |
$lineDifference = ($endBrace['line'] - $startBrace['line']);
|
|
|
134 |
if ($lineDifference < $this->lineLimit) {
|
|
|
135 |
return;
|
|
|
136 |
}
|
|
|
137 |
|
|
|
138 |
$expected = '//end '.$startCondition['content'];
|
|
|
139 |
|
|
|
140 |
$comment = $phpcsFile->findNext(array(T_COMMENT), $stackPtr, null, false);
|
|
|
141 |
if (($comment === false) || ($tokens[$comment]['line'] !== $endBrace['line'])) {
|
|
|
142 |
$error = "End comment for long condition not found; expected \"$expected\"";
|
|
|
143 |
$phpcsFile->addError($error, $stackPtr);
|
|
|
144 |
return;
|
|
|
145 |
}
|
|
|
146 |
|
|
|
147 |
if (($comment - $stackPtr) !== 1) {
|
|
|
148 |
$error = "Space found before closing comment; expected \"$expected\"";
|
|
|
149 |
$phpcsFile->addError($error, $stackPtr);
|
|
|
150 |
}
|
|
|
151 |
|
|
|
152 |
if ((strpos(trim($tokens[$comment]['content']), $expected)) === false) {
|
|
|
153 |
$found = trim($tokens[$comment]['content']);
|
|
|
154 |
$error = "Incorrect closing comment; expected \"$expected\" but found \"$found\"";
|
|
|
155 |
$phpcsFile->addError($error, $stackPtr);
|
|
|
156 |
return;
|
|
|
157 |
}
|
|
|
158 |
|
|
|
159 |
}//end process()
|
|
|
160 |
|
|
|
161 |
|
|
|
162 |
}//end class
|
|
|
163 |
|
|
|
164 |
|
|
|
165 |
?>
|