Subversion Repositories Applications.framework

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
5 aurelien 1
<?php
2
/**
3
 * Generic_Sniffs_Formatting_DisallowMultipleStatementsSniff.
4
 *
5
 * PHP version 5
6
 *
7
 * @category  PHP
8
 * @package   PHP_CodeSniffer
9
 * @author    Greg Sherwood <gsherwood@squiz.net>
10
 * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
11
 * @license   http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
12
 * @version   CVS: $Id: DisallowMultipleStatementsSniff.php,v 1.1 2008/06/24 06:37:54 squiz Exp $
13
 * @link      http://pear.php.net/package/PHP_CodeSniffer
14
 */
15
 
16
/**
17
 * Generic_Sniffs_Formatting_DisallowMultipleStatementsSniff.
18
 *
19
 * Ensures each statement is on a line by itself.
20
 *
21
 * @category  PHP
22
 * @package   PHP_CodeSniffer
23
 * @author    Greg Sherwood <gsherwood@squiz.net>
24
 * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
25
 * @license   http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
26
 * @version   Release: 1.2.0RC1
27
 * @link      http://pear.php.net/package/PHP_CodeSniffer
28
 */
29
class Generic_Sniffs_Formatting_DisallowMultipleStatementsSniff implements PHP_CodeSniffer_Sniff
30
{
31
 
32
 
33
    /**
34
     * Returns an array of tokens this test wants to listen for.
35
     *
36
     * @return array
37
     */
38
    public function register()
39
    {
40
        return array(T_SEMICOLON);
41
 
42
    }//end register()
43
 
44
 
45
    /**
46
     * Processes this test, when one of its tokens is encountered.
47
     *
48
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
49
     * @param int                  $stackPtr  The position of the current token in
50
     *                                        the stack passed in $tokens.
51
     *
52
     * @return void
53
     */
54
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
55
    {
56
        $tokens = $phpcsFile->getTokens();
57
 
58
        $prev = $phpcsFile->findPrevious(T_SEMICOLON, ($stackPtr - 1));
59
        if ($prev === false) {
60
            return;
61
        }
62
 
63
        // Ignore multiple statements in a FOR condition.
64
        if (isset($tokens[$stackPtr]['nested_parenthesis']) === true) {
65
            foreach ($tokens[$stackPtr]['nested_parenthesis'] as $bracket) {
66
                $owner = $tokens[$bracket]['parenthesis_owner'];
67
                if ($tokens[$owner]['code'] === T_FOR) {
68
                    return;
69
                }
70
            }
71
        }
72
 
73
        if ($tokens[$prev]['line'] === $tokens[$stackPtr]['line']) {
74
            $error = 'Each PHP statement must be on a line by itself';
75
            $phpcsFile->addError($error, $stackPtr);
76
            return;
77
        }
78
 
79
    }//end process()
80
 
81
 
82
}//end class
83
 
84
?>