5 |
aurelien |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* Generic_Sniffs_Functions_OpeningFunctionBraceKernighanRitchieSniff.
|
|
|
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: OpeningFunctionBraceKernighanRitchieSniff.php,v 1.5 2008/05/05 03:59:12 squiz Exp $
|
|
|
14 |
* @link http://pear.php.net/package/PHP_CodeSniffer
|
|
|
15 |
*/
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* Generic_Sniffs_Functions_OpeningFunctionBraceKernighanRitchieSniff.
|
|
|
19 |
*
|
|
|
20 |
* Checks that the opening brace of a function is on the same line
|
|
|
21 |
* as the function declaration.
|
|
|
22 |
*
|
|
|
23 |
* @category PHP
|
|
|
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.2.0RC1
|
|
|
30 |
* @link http://pear.php.net/package/PHP_CodeSniffer
|
|
|
31 |
*/
|
|
|
32 |
class Generic_Sniffs_Functions_OpeningFunctionBraceKernighanRitchieSniff implements PHP_CodeSniffer_Sniff
|
|
|
33 |
{
|
|
|
34 |
|
|
|
35 |
|
|
|
36 |
/**
|
|
|
37 |
* Registers the tokens that this sniff wants to listen for.
|
|
|
38 |
*
|
|
|
39 |
* @return void
|
|
|
40 |
*/
|
|
|
41 |
public function register()
|
|
|
42 |
{
|
|
|
43 |
return array(T_FUNCTION);
|
|
|
44 |
|
|
|
45 |
}//end register()
|
|
|
46 |
|
|
|
47 |
|
|
|
48 |
/**
|
|
|
49 |
* Processes this test, when one of its tokens is encountered.
|
|
|
50 |
*
|
|
|
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.
|
|
|
54 |
*
|
|
|
55 |
* @return void
|
|
|
56 |
*/
|
|
|
57 |
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
|
|
|
58 |
{
|
|
|
59 |
$tokens = $phpcsFile->getTokens();
|
|
|
60 |
|
|
|
61 |
if (isset($tokens[$stackPtr]['scope_opener']) === false) {
|
|
|
62 |
return;
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
$openingBrace = $tokens[$stackPtr]['scope_opener'];
|
|
|
66 |
|
|
|
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'];
|
|
|
72 |
|
|
|
73 |
$lineDifference = ($braceLine - $functionLine);
|
|
|
74 |
|
|
|
75 |
if ($lineDifference > 0) {
|
|
|
76 |
$error = 'Opening brace should be on the same line as the declaration';
|
|
|
77 |
$phpcsFile->addError($error, $openingBrace);
|
|
|
78 |
return;
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
// Checks that the closing parenthesis and the opening brace are
|
|
|
82 |
// separated by a whitespace character.
|
|
|
83 |
$closerColumn = $tokens[$tokens[$stackPtr]['parenthesis_closer']]['column'];
|
|
|
84 |
$braceColumn = $tokens[$openingBrace]['column'];
|
|
|
85 |
|
|
|
86 |
$columnDifference = ($braceColumn - $closerColumn);
|
|
|
87 |
|
|
|
88 |
if ($columnDifference !== 2) {
|
|
|
89 |
$error = 'Expected 1 space between the closing parenthesis and the opening brace; found '.($columnDifference - 1).'.';
|
|
|
90 |
$phpcsFile->addError($error, $openingBrace);
|
|
|
91 |
return;
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
// Check that a tab was not used instead of a space.
|
|
|
95 |
$spaceTokenPtr = ($tokens[$stackPtr]['parenthesis_closer'] + 1);
|
|
|
96 |
$spaceContent = $tokens[$spaceTokenPtr]['content'];
|
|
|
97 |
if ($spaceContent !== ' ') {
|
|
|
98 |
$error = 'Expected a single space character between closing parenthesis and opening brace; found "'.$spaceContent.'".';
|
|
|
99 |
$phpcsFile->addError($error, $openingBrace);
|
|
|
100 |
return;
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
}//end process()
|
|
|
104 |
|
|
|
105 |
|
|
|
106 |
}//end class
|
|
|
107 |
|
|
|
108 |
?>
|