3
* Generic_Sniffs_Formatting_DisallowMultipleStatementsSniff.
8
* @package PHP_CodeSniffer
9
* @author Greg Sherwood <gsherwood@squiz.net>
10
* @copyright 2006-2012 Squiz Pty Ltd (ABN 77 084 670 600)
11
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
12
* @link http://pear.php.net/package/PHP_CodeSniffer
16
* Generic_Sniffs_Formatting_DisallowMultipleStatementsSniff.
18
* Ensures each statement is on a line by itself.
21
* @package PHP_CodeSniffer
22
* @author Greg Sherwood <gsherwood@squiz.net>
23
* @copyright 2006-2012 Squiz Pty Ltd (ABN 77 084 670 600)
24
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
25
* @version Release: 1.5.0RC2
26
* @link http://pear.php.net/package/PHP_CodeSniffer
28
class Generic_Sniffs_Formatting_DisallowMultipleStatementsSniff implements PHP_CodeSniffer_Sniff
33
* Returns an array of tokens this test wants to listen for.
37
public function register()
39
return array(T_SEMICOLON);
45
* Processes this test, when one of its tokens is encountered.
47
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
48
* @param int $stackPtr The position of the current token in
49
* the stack passed in $tokens.
53
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
55
$tokens = $phpcsFile->getTokens();
57
$prev = $phpcsFile->findPrevious(T_SEMICOLON, ($stackPtr - 1));
58
if ($prev === false) {
62
// Ignore multiple statements in a FOR condition.
63
if (isset($tokens[$stackPtr]['nested_parenthesis']) === true) {
64
foreach ($tokens[$stackPtr]['nested_parenthesis'] as $bracket) {
65
if (isset($tokens[$bracket]['parenthesis_owner']) === false) {
66
// Probably a closure sitting inside a function call.
70
$owner = $tokens[$bracket]['parenthesis_owner'];
71
if ($tokens[$owner]['code'] === T_FOR) {
77
if ($tokens[$prev]['line'] === $tokens[$stackPtr]['line']) {
78
$error = 'Each PHP statement must be on a line by itself';
79
$phpcsFile->addError($error, $stackPtr, 'SameLine');