Subversion Repositories Applications.framework

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
5 aurelien 1
<?php
2
/**
3
 * This file is part of the CodeAnalysis addon for PHP_CodeSniffer.
4
 *
5
 * PHP version 5
6
 *
7
 * @category  PHP
8
 * @package   PHP_CodeSniffer
9
 * @author    Greg Sherwood <gsherwood@squiz.net>
10
 * @author    Manuel Pichler <mapi@manuel-pichler.de>
11
 * @copyright 2007-2008 Manuel Pichler. All rights reserved.
12
 * @license   http://www.opensource.org/licenses/bsd-license.php  BSD License
13
 * @version   CVS: $Id: EmptyStatementSniff.php,v 1.1 2008/02/06 02:38:36 squiz Exp $
14
 * @link      http://pear.php.net/package/PHP_CodeSniffer
15
 */
16
 
17
/**
18
 * This sniff class detected empty statement.
19
 *
20
 * This sniff implements the common algorithm for empty statement body detection.
21
 * A body is considered as empty if it is completely empty or it only contains
22
 * whitespace characters and|or comments.
23
 *
24
 * <code>
25
 * stmt {
26
 *   // foo
27
 * }
28
 * stmt (conditions) {
29
 *   // foo
30
 * }
31
 * </code>
32
 *
33
 * Statements covered by this sniff are <b>catch</b>, <b>do</b>, <b>else</b>,
34
 * <b>elsif</b>, <b>for</b>, <b>foreach<b>, <b>if</b>, <b>switch</b>, <b>try</b>
35
 * and <b>while</b>.
36
 *
37
 * @category  PHP
38
 * @package   PHP_CodeSniffer
39
 * @author    Manuel Pichler <mapi@manuel-pichler.de>
40
 * @copyright 2007-2008 Manuel Pichler. All rights reserved.
41
 * @license   http://www.opensource.org/licenses/bsd-license.php  BSD License
42
 * @version   Release: 1.2.0RC1
43
 * @link      http://pear.php.net/package/PHP_CodeSniffer
44
 */
45
class Generic_Sniffs_CodeAnalysis_EmptyStatementSniff implements PHP_CodeSniffer_Sniff
46
{
47
 
48
    /**
49
     * List of block tokens that this sniff covers.
50
     *
51
     * The key of this hash identifies the required token while the boolean
52
     * value says mark an error or mark a warning.
53
     *
54
     * @type array<boolean>
55
     * @var array(integer=>boolean) $_tokens
56
     */
57
    private $_tokens = array(
58
                        T_CATCH   => true,
59
                        T_DO      => false,
60
                        T_ELSE    => false,
61
                        T_ELSEIF  => false,
62
                        T_FOR     => false,
63
                        T_FOREACH => false,
64
                        T_IF      => false,
65
                        T_SWITCH  => false,
66
                        T_TRY     => false,
67
                        T_WHILE   => false,
68
                       );
69
 
70
 
71
    /**
72
     * Registers the tokens that this sniff wants to listen for.
73
     *
74
     * @return array(integer)
75
     */
76
    public function register()
77
    {
78
        return array_keys($this->_tokens);
79
 
80
    }//end register()
81
 
82
 
83
    /**
84
     * Processes this test, when one of its tokens is encountered.
85
     *
86
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
87
     * @param int                  $stackPtr  The position of the current token
88
     *                                        in the stack passed in $tokens.
89
     *
90
     * @return void
91
     */
92
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
93
    {
94
        $tokens = $phpcsFile->getTokens();
95
        $token  = $tokens[$stackPtr];
96
 
97
        // Skip for-statements without body.
98
        if (isset($token['scope_opener']) === false) {
99
            return;
100
        }
101
 
102
        $next = ++$token['scope_opener'];
103
        $end  = --$token['scope_closer'];
104
 
105
        $emptyBody = true;
106
        for (; $next <= $end; ++$next) {
107
            if (in_array($tokens[$next]['code'], PHP_CodeSniffer_Tokens::$emptyTokens) === false) {
108
                $emptyBody = false;
109
                break;
110
            }
111
        }
112
 
113
        if ($emptyBody === true) {
114
            // Get token identifier.
115
            $name  = $phpcsFile->getTokensAsString($stackPtr, 1);
116
            $error = sprintf('Empty %s statement detected', strtoupper($name));
117
            if ($this->_tokens[$token['code']] === true) {
118
                $phpcsFile->addError($error, $stackPtr);
119
            } else {
120
                $phpcsFile->addWarning($error, $stackPtr);
121
            }
122
        }
123
 
124
    }//end process()
125
 
126
 
127
}//end class
128
 
129
?>