5 |
aurelien |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* PEAR_Sniffs_Whitespace_ScopeClosingBraceSniff.
|
|
|
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: ScopeClosingBraceSniff.php,v 1.10 2008/12/08 05:59:29 squiz Exp $
|
|
|
14 |
* @link http://pear.php.net/package/PHP_CodeSniffer
|
|
|
15 |
*/
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* PEAR_Sniffs_Whitespace_ScopeClosingBraceSniff.
|
|
|
19 |
*
|
|
|
20 |
* Checks that the closing braces of scopes are aligned correctly.
|
|
|
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.0RC1
|
|
|
29 |
* @link http://pear.php.net/package/PHP_CodeSniffer
|
|
|
30 |
*/
|
|
|
31 |
class PEAR_Sniffs_WhiteSpace_ScopeClosingBraceSniff 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 PHP_CodeSniffer_Tokens::$scopeOpeners;
|
|
|
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 All the tokens found in the document.
|
|
|
51 |
* @param int $stackPtr The position of the current token
|
|
|
52 |
* in the stack passed in $tokens.
|
|
|
53 |
*
|
|
|
54 |
* @return void
|
|
|
55 |
*/
|
|
|
56 |
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
|
|
|
57 |
{
|
|
|
58 |
$tokens = $phpcsFile->getTokens();
|
|
|
59 |
|
|
|
60 |
// If this is an inline condition (ie. there is no scope opener), then
|
|
|
61 |
// return, as this is not a new scope.
|
|
|
62 |
if (isset($tokens[$stackPtr]['scope_closer']) === false) {
|
|
|
63 |
return;
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
// We need to actually find the first piece of content on this line,
|
|
|
67 |
// as if this is a method with tokens before it (public, static etc)
|
|
|
68 |
// or an if with an else before it, then we need to start the scope
|
|
|
69 |
// checking from there, rather than the current token.
|
|
|
70 |
$lineStart = ($stackPtr - 1);
|
|
|
71 |
for ($lineStart; $lineStart > 0; $lineStart--) {
|
|
|
72 |
if (strpos($tokens[$lineStart]['content'], $phpcsFile->eolChar) !== false) {
|
|
|
73 |
break;
|
|
|
74 |
}
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
// We found a new line, now go forward and find the first non-whitespace
|
|
|
78 |
// token.
|
|
|
79 |
$lineStart= $phpcsFile->findNext(
|
|
|
80 |
array(T_WHITESPACE),
|
|
|
81 |
($lineStart + 1),
|
|
|
82 |
null,
|
|
|
83 |
true
|
|
|
84 |
);
|
|
|
85 |
|
|
|
86 |
$startColumn = $tokens[$lineStart]['column'];
|
|
|
87 |
$scopeStart = $tokens[$stackPtr]['scope_opener'];
|
|
|
88 |
$scopeEnd = $tokens[$stackPtr]['scope_closer'];
|
|
|
89 |
|
|
|
90 |
// Check that the closing brace is on it's own line.
|
|
|
91 |
$lastContent = $phpcsFile->findPrevious(
|
|
|
92 |
array(T_WHITESPACE),
|
|
|
93 |
($scopeEnd - 1),
|
|
|
94 |
$scopeStart,
|
|
|
95 |
true
|
|
|
96 |
);
|
|
|
97 |
|
|
|
98 |
if ($tokens[$lastContent]['line'] === $tokens[$scopeEnd]['line']) {
|
|
|
99 |
$error = 'Closing brace must be on a line by itself';
|
|
|
100 |
$phpcsFile->addError($error, $scopeEnd);
|
|
|
101 |
return;
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
// Check now that the closing brace is lined up correctly.
|
|
|
105 |
$braceIndent = $tokens[$scopeEnd]['column'];
|
|
|
106 |
$isBreakCloser = ($tokens[$scopeEnd]['code'] === T_BREAK);
|
|
|
107 |
if (in_array($tokens[$stackPtr]['code'], array(T_CASE, T_DEFAULT)) === true
|
|
|
108 |
&& $isBreakCloser === true
|
|
|
109 |
) {
|
|
|
110 |
// BREAK statements should be indented 4 spaces from the
|
|
|
111 |
// CASE or DEFAULT statement.
|
|
|
112 |
if ($braceIndent !== ($startColumn + 4)) {
|
|
|
113 |
$error = 'Break statement indented incorrectly; expected '.($startColumn + 3).' spaces, found '.($braceIndent - 1);
|
|
|
114 |
$phpcsFile->addError($error, $scopeEnd);
|
|
|
115 |
}
|
|
|
116 |
} else {
|
|
|
117 |
if ($braceIndent !== $startColumn) {
|
|
|
118 |
$error = 'Closing brace indented incorrectly; expected '.($startColumn - 1).' spaces, found '.($braceIndent - 1);
|
|
|
119 |
$phpcsFile->addError($error, $scopeEnd);
|
|
|
120 |
}
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
}//end process()
|
|
|
124 |
|
|
|
125 |
|
|
|
126 |
}//end class
|
|
|
127 |
|
|
|
128 |
?>
|