3
* Generic_Sniffs_Methods_OpeningMethodBraceBsdAllmanSniff.
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: OpeningFunctionBraceBsdAllmanSniff.php,v 1.8 2008/05/05 03:59:12 squiz Exp $
14
* @link http://pear.php.net/package/PHP_CodeSniffer
18
* Generic_Sniffs_Functions_OpeningFunctionBraceBsdAllmanSniff.
20
* Checks that the opening brace of a function is on the line after the
21
* function declaration.
24
* @package PHP_CodeSniffer
25
* @author Greg Sherwood <gsherwood@squiz.net>
26
* @author Marc McIntyre <mmcintyre@squiz.net>
27
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
28
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
29
* @version Release: 1.1.0
30
* @link http://pear.php.net/package/PHP_CodeSniffer
32
class Generic_Sniffs_Functions_OpeningFunctionBraceBsdAllmanSniff implements PHP_CodeSniffer_Sniff
37
* Registers the tokens that this sniff wants to listen for.
41
public function register()
43
return array(T_FUNCTION);
49
* Processes this test, when one of its tokens is encountered.
51
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
52
* @param int $stackPtr The position of the current token in the
53
* stack passed in $tokens.
57
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
59
$tokens = $phpcsFile->getTokens();
61
if (isset($tokens[$stackPtr]['scope_opener']) === false) {
65
$openingBrace = $tokens[$stackPtr]['scope_opener'];
67
// The end of the function occurs at the end of the argument list. Its
68
// like this because some people like to break long function declarations
69
// over multiple lines.
70
$functionLine = $tokens[$tokens[$stackPtr]['parenthesis_closer']]['line'];
71
$braceLine = $tokens[$openingBrace]['line'];
73
$lineDifference = ($braceLine - $functionLine);
75
if ($lineDifference === 0) {
76
$error = 'Opening brace should be on a new line';
77
$phpcsFile->addError($error, $openingBrace);
81
if ($lineDifference > 1) {
83
if (($lineDifference - 1) !== 1) {
87
$error = 'Opening brace should be on the line after the declaration; found '.($lineDifference - 1).' blank '.$ender;
88
$phpcsFile->addError($error, $openingBrace);
92
// We need to actually find the first piece of content on this line,
93
// as if this is a method with tokens before it (public, static etc)
94
// or an if with an else before it, then we need to start the scope
95
// checking from there, rather than the current token.
96
$lineStart = $stackPtr;
97
while (($lineStart = $phpcsFile->findPrevious(array(T_WHITESPACE), ($lineStart - 1), null, false)) !== false) {
98
if (strpos($tokens[$lineStart]['content'], $phpcsFile->eolChar) !== false) {
103
// We found a new line, now go forward and find the first non-whitespace
105
$lineStart = $phpcsFile->findNext(array(T_WHITESPACE), $lineStart, null, true);
107
// The opening brace is on the correct line, now it needs to be
108
// checked to be correctly indented.
109
$startColumn = $tokens[$lineStart]['column'];
110
$braceIndent = $tokens[$openingBrace]['column'];
112
if ($braceIndent !== $startColumn) {
113
$error = 'Opening brace indented incorrectly; expected '.($startColumn - 1).' spaces, found '.($braceIndent - 1);
114
$phpcsFile->addError($error, $openingBrace);
b'\\ No newline at end of file'