1<?php
2/**
3 * This file is part of the CodeAnalysis add-on for PHP_CodeSniffer.
4 *
5 * PHP version 5
6 *
7 * @category  PHP
8 * @package   PHP_CodeSniffer
9 * @author    Manuel Pichler <mapi@manuel-pichler.de>
10 * @author    Greg Sherwood <gsherwood@squiz.net>
11 * @copyright 2007-2014 Manuel Pichler. All rights reserved.
12 * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
13 * @link      http://pear.php.net/package/PHP_CodeSniffer
14 */
15
16/**
17 * This sniff class detected empty statement.
18 *
19 * This sniff implements the common algorithm for empty statement body detection.
20 * A body is considered as empty if it is completely empty or it only contains
21 * whitespace characters and/or comments.
22 *
23 * <code>
24 * stmt {
25 *   // foo
26 * }
27 * stmt (conditions) {
28 *   // foo
29 * }
30 * </code>
31 *
32 * @category  PHP
33 * @package   PHP_CodeSniffer
34 * @author    Manuel Pichler <mapi@manuel-pichler.de>
35 * @author    Greg Sherwood <gsherwood@squiz.net>
36 * @copyright 2007-2014 Manuel Pichler. All rights reserved.
37 * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
38 * @version   Release: @package_version@
39 * @link      http://pear.php.net/package/PHP_CodeSniffer
40 */
41class Generic_Sniffs_CodeAnalysis_EmptyStatementSniff implements PHP_CodeSniffer_Sniff
42{
43
44
45    /**
46     * Registers the tokens that this sniff wants to listen for.
47     *
48     * @return int[]
49     */
50    public function register()
51    {
52        return array(
53                T_CATCH,
54                T_DO,
55                T_ELSE,
56                T_ELSEIF,
57                T_FOR,
58                T_FOREACH,
59                T_IF,
60                T_SWITCH,
61                T_TRY,
62                T_WHILE,
63               );
64
65    }//end register()
66
67
68    /**
69     * Processes this test, when one of its tokens is encountered.
70     *
71     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
72     * @param int                  $stackPtr  The position of the current token
73     *                                        in the stack passed in $tokens.
74     *
75     * @return void
76     */
77    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
78    {
79        $tokens = $phpcsFile->getTokens();
80        $token  = $tokens[$stackPtr];
81
82        // Skip statements without a body.
83        if (isset($token['scope_opener']) === false) {
84            return;
85        }
86
87        $next = $phpcsFile->findNext(
88            PHP_CodeSniffer_Tokens::$emptyTokens,
89            ($token['scope_opener'] + 1),
90            ($token['scope_closer'] - 1),
91            true
92        );
93
94        if ($next !== false) {
95            return;
96        }
97
98        // Get token identifier.
99        $name  = strtoupper($token['content']);
100        $error = 'Empty %s statement detected';
101        $phpcsFile->addError($error, $stackPtr, 'Detected'.$name, array($name));
102
103    }//end process()
104
105
106}//end class
107