3
* Generic_Sniffs_Functions_OpeningFunctionBraceKernighanRitchieSniff.
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_OpeningFunctionBraceKernighanRitchieSniff.
19
* Checks that the opening brace of a function is on the same line
20
* as the 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_OpeningFunctionBraceKernighanRitchieSniff 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 the same line as the declaration';
76
$phpcsFile->addError($error, $openingBrace, 'BraceOnNewLine');
80
$closeBracket = $tokens[$stackPtr]['parenthesis_closer'];
81
if ($tokens[($closeBracket + 1)]['code'] !== T_WHITESPACE) {
83
} else if ($tokens[($closeBracket + 1)]['content'] === "\t") {
86
$length = strlen($tokens[($closeBracket + 1)]['content']);
90
$error = 'Expected 1 space after closing parenthesis; found %s';
91
$data = array($length);
92
$phpcsFile->addError($error, $closeBracket, 'SpaceAfterBracket', $data);
96
$closeBrace = $tokens[$stackPtr]['scope_opener'];
97
if ($tokens[($closeBrace - 1)]['code'] !== T_WHITESPACE) {
99
} else if ($tokens[($closeBrace - 1)]['content'] === "\t") {
102
$length = strlen($tokens[($closeBrace - 1)]['content']);
106
$error = 'Expected 1 space before opening brace; found %s';
107
$data = array($length);
108
$phpcsFile->addError($error, $openingBrace, 'SpaceBeforeBrace', $data);