-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathEmptyStatementSniff.php
More file actions
109 lines (92 loc) · 2.68 KB
/
EmptyStatementSniff.php
File metadata and controls
109 lines (92 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
/**
* This sniff class detected empty statement.
*
* This sniff implements the common algorithm for empty statement body detection.
* A body is considered as empty if it is completely empty or it only contains
* whitespace characters and/or comments.
*
* <code>
* stmt {
* // foo
* }
* stmt (conditions) {
* // foo
* }
* </code>
*
* @author Manuel Pichler <mapi@manuel-pichler.de>
* @author Greg Sherwood <gsherwood@squiz.net>
* @copyright 2007-2014 Manuel Pichler. All rights reserved.
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
*/
namespace PHP_CodeSniffer\Standards\Generic\Sniffs\CodeAnalysis;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Util\Tokens;
class EmptyStatementSniff implements Sniff
{
/**
* Whether to allow statements that contain only comments.
*
* @var boolean
*/
public $allowComments = false;
/**
* Registers the tokens that this sniff wants to listen for.
*
* @return int[]
*/
public function register()
{
return [
T_TRY,
T_CATCH,
T_FINALLY,
T_DO,
T_ELSE,
T_ELSEIF,
T_FOR,
T_FOREACH,
T_IF,
T_SWITCH,
T_WHILE,
];
}//end register()
/**
* Processes this test, when one of its tokens is encountered.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
*
* @return void
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$token = $tokens[$stackPtr];
// Skip statements without a body.
if (isset($token['scope_opener']) === false) {
return;
}
if ($this->allowComments === true) {
$emptyTokens = ([T_WHITESPACE => T_WHITESPACE] + Tokens::$phpcsCommentTokens);
} else {
$emptyTokens = Tokens::$emptyTokens;
}
$next = $phpcsFile->findNext(
$emptyTokens,
($token['scope_opener'] + 1),
$token['scope_closer'],
true
);
if ($next !== false) {
return;
}
// Get token identifier.
$name = strtoupper($token['content']);
$error = 'Empty %s statement detected';
$phpcsFile->addError($error, $stackPtr, 'Detected'.ucfirst(strtolower($name)), [$name]);
}//end process()
}//end class