| 1 |
lars |
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 286785 2009-08-04 05:19:21Z squiz $
|
|
|
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.1
|
|
|
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 |
* @var array
|
|
|
55 |
*/
|
|
|
56 |
protected $checkedTokens = array(
|
|
|
57 |
T_CATCH => true,
|
|
|
58 |
T_DO => false,
|
|
|
59 |
T_ELSE => false,
|
|
|
60 |
T_ELSEIF => false,
|
|
|
61 |
T_FOR => false,
|
|
|
62 |
T_FOREACH => false,
|
|
|
63 |
T_IF => false,
|
|
|
64 |
T_SWITCH => false,
|
|
|
65 |
T_TRY => false,
|
|
|
66 |
T_WHILE => false,
|
|
|
67 |
);
|
|
|
68 |
|
|
|
69 |
|
|
|
70 |
/**
|
|
|
71 |
* Registers the tokens that this sniff wants to listen for.
|
|
|
72 |
*
|
|
|
73 |
* @return array(integer)
|
|
|
74 |
*/
|
|
|
75 |
public function register()
|
|
|
76 |
{
|
|
|
77 |
return array_keys($this->checkedTokens);
|
|
|
78 |
|
|
|
79 |
}//end register()
|
|
|
80 |
|
|
|
81 |
|
|
|
82 |
/**
|
|
|
83 |
* Processes this test, when one of its tokens is encountered.
|
|
|
84 |
*
|
|
|
85 |
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
|
|
|
86 |
* @param int $stackPtr The position of the current token
|
|
|
87 |
* in the stack passed in $tokens.
|
|
|
88 |
*
|
|
|
89 |
* @return void
|
|
|
90 |
*/
|
|
|
91 |
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
|
|
|
92 |
{
|
|
|
93 |
$tokens = $phpcsFile->getTokens();
|
|
|
94 |
$token = $tokens[$stackPtr];
|
|
|
95 |
|
|
|
96 |
// Skip for-statements without body.
|
|
|
97 |
if (isset($token['scope_opener']) === false) {
|
|
|
98 |
return;
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
$next = ++$token['scope_opener'];
|
|
|
102 |
$end = --$token['scope_closer'];
|
|
|
103 |
|
|
|
104 |
$emptyBody = true;
|
|
|
105 |
for (; $next <= $end; ++$next) {
|
|
|
106 |
if (in_array($tokens[$next]['code'], PHP_CodeSniffer_Tokens::$emptyTokens) === false) {
|
|
|
107 |
$emptyBody = false;
|
|
|
108 |
break;
|
|
|
109 |
}
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
if ($emptyBody === true) {
|
|
|
113 |
// Get token identifier.
|
|
|
114 |
$name = $phpcsFile->getTokensAsString($stackPtr, 1);
|
|
|
115 |
$error = sprintf('Empty %s statement detected', strtoupper($name));
|
|
|
116 |
if ($this->checkedTokens[$token['code']] === true) {
|
|
|
117 |
$phpcsFile->addError($error, $stackPtr);
|
|
|
118 |
} else {
|
|
|
119 |
$phpcsFile->addWarning($error, $stackPtr);
|
|
|
120 |
}
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
}//end process()
|
|
|
124 |
|
|
|
125 |
|
|
|
126 |
}//end class
|
|
|
127 |
|
|
|
128 |
?>
|