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-2012 Squiz Pty Ltd (ABN 77 084 670 600)
12
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
13
* @link http://pear.php.net/package/PHP_CodeSniffer
17
* Generic_Sniffs_Functions_OpeningFunctionBraceBsdAllmanSniff.
19
* Checks that the opening brace of a function is on the line after the
20
* function declaration.
23
* @package PHP_CodeSniffer
24
* @author Greg Sherwood <gsherwood@squiz.net>
25
* @author Marc McIntyre <mmcintyre@squiz.net>
26
* @copyright 2006-2012 Squiz Pty Ltd (ABN 77 084 670 600)
27
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
28
* @version Release: 1.5.0RC2
29
* @link http://pear.php.net/package/PHP_CodeSniffer
31
class Generic_Sniffs_Functions_OpeningFunctionBraceBsdAllmanSniff implements PHP_CodeSniffer_Sniff
36
* Registers the tokens that this sniff wants to listen for.
40
public function register()
42
return array(T_FUNCTION);
48
* Processes this test, when one of its tokens is encountered.
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.
56
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
58
$tokens = $phpcsFile->getTokens();
60
if (isset($tokens[$stackPtr]['scope_opener']) === false) {
64
$openingBrace = $tokens[$stackPtr]['scope_opener'];
66
// The end of the function occurs at the end of the argument list. Its
67
// like this because some people like to break long function declarations
68
// over multiple lines.
69
$functionLine = $tokens[$tokens[$stackPtr]['parenthesis_closer']]['line'];
70
$braceLine = $tokens[$openingBrace]['line'];
72
$lineDifference = ($braceLine - $functionLine);
74
if ($lineDifference === 0) {
75
$error = 'Opening brace should be on a new line';
76
$phpcsFile->addError($error, $openingBrace, 'BraceOnSameLine');
80
if ($lineDifference > 1) {
81
$error = 'Opening brace should be on the line after the declaration; found %s blank line(s)';
82
$data = array(($lineDifference - 1));
83
$phpcsFile->addError($error, $openingBrace, 'BraceSpacing', $data);
87
// We need to actually find the first piece of content on this line,
88
// as if this is a method with tokens before it (public, static etc)
89
// or an if with an else before it, then we need to start the scope
90
// checking from there, rather than the current token.
91
$lineStart = $stackPtr;
92
while (($lineStart = $phpcsFile->findPrevious(array(T_WHITESPACE), ($lineStart - 1), null, false)) !== false) {
93
if (strpos($tokens[$lineStart]['content'], $phpcsFile->eolChar) !== false) {
98
// We found a new line, now go forward and find the first non-whitespace
100
$lineStart = $phpcsFile->findNext(array(T_WHITESPACE), $lineStart, null, true);
102
// The opening brace is on the correct line, now it needs to be
103
// checked to be correctly indented.
104
$startColumn = $tokens[$lineStart]['column'];
105
$braceIndent = $tokens[$openingBrace]['column'];
107
if ($braceIndent !== $startColumn) {
108
$error = 'Opening brace indented incorrectly; expected %s spaces, found %s';
113
$phpcsFile->addError($error, $openingBrace, 'BraceIndent', $data);