| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* Squiz_Sniffs_WhiteSpace_FunctionOpeningBraceSpaceSniff.
|
|
|
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: FunctionOpeningBraceSpaceSniff.php 267850 2008-10-27 03:00:03Z squiz $
|
|
|
14 |
* @link http://pear.php.net/package/PHP_CodeSniffer
|
|
|
15 |
*/
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* Squiz_Sniffs_WhiteSpace_FunctionOpeningBraceSpaceSniff.
|
|
|
19 |
*
|
|
|
20 |
* Checks that there is no empty line after the opening brace of a function.
|
|
|
21 |
*
|
|
|
22 |
* @category PHP
|
|
|
23 |
* @package PHP_CodeSniffer
|
|
|
24 |
* @author Greg Sherwood <gsherwood@squiz.net>
|
|
|
25 |
* @author Marc McIntyre <mmcintyre@squiz.net>
|
|
|
26 |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
|
|
|
27 |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
|
|
|
28 |
* @version Release: 1.2.1
|
|
|
29 |
* @link http://pear.php.net/package/PHP_CodeSniffer
|
|
|
30 |
*/
|
|
|
31 |
class Squiz_Sniffs_WhiteSpace_FunctionOpeningBraceSpaceSniff implements PHP_CodeSniffer_Sniff
|
|
|
32 |
{
|
|
|
33 |
|
|
|
34 |
/**
|
|
|
35 |
* A list of tokenizers this sniff supports.
|
|
|
36 |
*
|
|
|
37 |
* @var array
|
|
|
38 |
*/
|
|
|
39 |
public $supportedTokenizers = array(
|
|
|
40 |
'PHP',
|
|
|
41 |
'JS',
|
|
|
42 |
);
|
|
|
43 |
|
|
|
44 |
|
|
|
45 |
/**
|
|
|
46 |
* Returns an array of tokens this test wants to listen for.
|
|
|
47 |
*
|
|
|
48 |
* @return array
|
|
|
49 |
*/
|
|
|
50 |
public function register()
|
|
|
51 |
{
|
|
|
52 |
return array(T_FUNCTION);
|
|
|
53 |
|
|
|
54 |
}//end register()
|
|
|
55 |
|
|
|
56 |
|
|
|
57 |
/**
|
|
|
58 |
* Processes this test, when one of its tokens is encountered.
|
|
|
59 |
*
|
|
|
60 |
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
|
|
|
61 |
* @param int $stackPtr The position of the current token
|
|
|
62 |
* in the stack passed in $tokens.
|
|
|
63 |
*
|
|
|
64 |
* @return void
|
|
|
65 |
*/
|
|
|
66 |
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
|
|
|
67 |
{
|
|
|
68 |
$tokens = $phpcsFile->getTokens();
|
|
|
69 |
|
|
|
70 |
if (isset($tokens[$stackPtr]['scope_opener']) === false) {
|
|
|
71 |
// Probably an interface method.
|
|
|
72 |
return;
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
$openBrace = $tokens[$stackPtr]['scope_opener'];
|
|
|
76 |
$nextContent = $phpcsFile->findNext(T_WHITESPACE, ($openBrace + 1), null, true);
|
|
|
77 |
|
|
|
78 |
if ($nextContent === $tokens[$stackPtr]['scope_closer']) {
|
|
|
79 |
// The next bit of content is the closing brace, so this
|
|
|
80 |
// is an empty function and should have a blank line
|
|
|
81 |
// between the opening and closing braces.
|
|
|
82 |
return;
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
$braceLine = $tokens[$openBrace]['line'];
|
|
|
86 |
$nextLine = $tokens[$nextContent]['line'];
|
|
|
87 |
|
|
|
88 |
$found = ($nextLine - $braceLine - 1);
|
|
|
89 |
if ($found > 0) {
|
|
|
90 |
$error = "Expected 0 blank lines after opening function brace; $found found";
|
|
|
91 |
$phpcsFile->addError($error, $openBrace);
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
if ($phpcsFile->tokenizerType === 'JS') {
|
|
|
95 |
// Do some additional checking before the function brace.
|
|
|
96 |
$nestedFunction = ($phpcsFile->hasCondition($stackPtr, T_FUNCTION) === true || isset($tokens[$stackPtr]['nested_parenthesis']) === true);
|
|
|
97 |
|
|
|
98 |
$functionLine = $tokens[$tokens[$stackPtr]['parenthesis_closer']]['line'];
|
|
|
99 |
$lineDifference = ($braceLine - $functionLine);
|
|
|
100 |
|
|
|
101 |
if ($nestedFunction === true) {
|
|
|
102 |
if ($lineDifference > 0) {
|
|
|
103 |
$error = "Expected 0 blank lines before openning brace of nested function; $found found";
|
|
|
104 |
$phpcsFile->addError($error, $openBrace);
|
|
|
105 |
}
|
|
|
106 |
} else {
|
|
|
107 |
if ($lineDifference === 0) {
|
|
|
108 |
$error = 'Opening brace should be on a new line';
|
|
|
109 |
$phpcsFile->addError($error, $openBrace);
|
|
|
110 |
return;
|
|
|
111 |
}
|
|
|
112 |
|
|
|
113 |
if ($lineDifference > 1) {
|
|
|
114 |
$ender = 'line';
|
|
|
115 |
if (($lineDifference - 1) !== 1) {
|
|
|
116 |
$ender .= 's';
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
$error = 'Opening brace should be on the line after the declaration; found '.($lineDifference - 1).' blank '.$ender;
|
|
|
120 |
$phpcsFile->addError($error, $openBrace);
|
|
|
121 |
return;
|
|
|
122 |
}
|
|
|
123 |
}//end if
|
|
|
124 |
}//end if
|
|
|
125 |
|
|
|
126 |
}//end process()
|
|
|
127 |
|
|
|
128 |
|
|
|
129 |
}//end class
|
|
|
130 |
|
|
|
131 |
?>
|