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-2014 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-2014 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: 2.3.3
29
* @link http://pear.php.net/package/PHP_CodeSniffer
31
class Generic_Sniffs_Functions_OpeningFunctionBraceKernighanRitchieSniff implements PHP_CodeSniffer_Sniff
36
* Should this sniff check function braces?
40
public $checkFunctions = true;
43
* Should this sniff check closure braces?
47
public $checkClosures = false;
51
* Registers the tokens that this sniff wants to listen for.
55
public function register()
66
* Processes this test, when one of its tokens is encountered.
68
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
69
* @param int $stackPtr The position of the current token in the
70
* stack passed in $tokens.
74
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
76
$tokens = $phpcsFile->getTokens();
78
if (isset($tokens[$stackPtr]['scope_opener']) === false) {
82
if (($tokens[$stackPtr]['code'] === T_FUNCTION
83
&& (bool) $this->checkFunctions === false)
84
|| ($tokens[$stackPtr]['code'] === T_CLOSURE
85
&& (bool) $this->checkClosures === false)
90
$openingBrace = $tokens[$stackPtr]['scope_opener'];
91
$closeBracket = $tokens[$stackPtr]['parenthesis_closer'];
92
if ($tokens[$stackPtr]['code'] === T_CLOSURE) {
93
$use = $phpcsFile->findNext(T_USE, ($closeBracket + 1), $tokens[$stackPtr]['scope_opener']);
95
$openBracket = $phpcsFile->findNext(T_OPEN_PARENTHESIS, ($use + 1));
96
$closeBracket = $tokens[$openBracket]['parenthesis_closer'];
100
$functionLine = $tokens[$closeBracket]['line'];
101
$braceLine = $tokens[$openingBrace]['line'];
103
$lineDifference = ($braceLine - $functionLine);
105
if ($lineDifference > 0) {
106
$phpcsFile->recordMetric($stackPtr, 'Function opening brace placement', 'new line');
107
$error = 'Opening brace should be on the same line as the declaration';
108
$fix = $phpcsFile->addFixableError($error, $openingBrace, 'BraceOnNewLine');
110
$phpcsFile->fixer->beginChangeset();
111
$phpcsFile->fixer->addContent($closeBracket, ' {');
112
$phpcsFile->fixer->replaceToken($openingBrace, '');
113
$phpcsFile->fixer->endChangeset();
117
$phpcsFile->recordMetric($stackPtr, 'Function opening brace placement', 'same line');
119
$next = $phpcsFile->findNext(T_WHITESPACE, ($openingBrace + 1), null, true);
120
if ($tokens[$next]['line'] === $tokens[$openingBrace]['line']) {
121
if ($next === $tokens[$stackPtr]['scope_closer']) {
122
// Ignore empty functions.
126
$error = 'Opening brace must be the last content on the line';
127
$fix = $phpcsFile->addFixableError($error, $openingBrace, 'ContentAfterBrace');
129
$phpcsFile->fixer->addNewline($openingBrace);
133
// Only continue checking if the opening brace looks good.
134
if ($lineDifference > 0) {
138
if ($tokens[($closeBracket + 1)]['code'] !== T_WHITESPACE) {
140
} else if ($tokens[($closeBracket + 1)]['content'] === "\t") {
143
$length = strlen($tokens[($closeBracket + 1)]['content']);
147
$error = 'Expected 1 space after closing parenthesis; found %s';
148
$data = array($length);
149
$fix = $phpcsFile->addFixableError($error, $closeBracket, 'SpaceAfterBracket', $data);
151
if ($length === 0 || $length === '\t') {
152
$phpcsFile->fixer->addContent($closeBracket, ' ');
154
$phpcsFile->fixer->replaceToken(($closeBracket + 1), ' ');