1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* PHPCompatibility_Sniffs_PHP_EmptyNonVariableSniff. |
4
|
|
|
* |
5
|
|
|
* PHP version 5.5 |
6
|
|
|
* |
7
|
|
|
* @category PHP |
8
|
|
|
* @package PHPCompatibility |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* PHPCompatibility_Sniffs_PHP_EmptyNonVariableSniff. |
13
|
|
|
* |
14
|
|
|
* Verify that nothing but variables are passed to empty(). |
15
|
|
|
* |
16
|
|
|
* PHP version 5.5 |
17
|
|
|
* |
18
|
|
|
* @category PHP |
19
|
|
|
* @package PHPCompatibility |
20
|
|
|
* @author Juliette Reinders Folmer <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class PHPCompatibility_Sniffs_PHP_EmptyNonVariableSniff extends PHPCompatibility_Sniff |
|
|
|
|
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* List of tokens to check against. |
26
|
|
|
* |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
protected $tokenBlackList = array(); |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Returns an array of tokens this test wants to listen for. |
33
|
|
|
* |
34
|
|
|
* @return array |
35
|
|
|
*/ |
36
|
|
|
public function register() |
37
|
|
|
{ |
38
|
|
|
// Set the token blacklist only once. |
39
|
|
|
$this->tokenBlackList = array_unique(array_merge( |
40
|
|
|
PHP_CodeSniffer_Tokens::$assignmentTokens, |
41
|
|
|
PHP_CodeSniffer_Tokens::$equalityTokens, |
42
|
|
|
PHP_CodeSniffer_Tokens::$comparisonTokens, |
43
|
|
|
PHP_CodeSniffer_Tokens::$operators, |
44
|
|
|
PHP_CodeSniffer_Tokens::$booleanOperators, |
45
|
|
|
PHP_CodeSniffer_Tokens::$castTokens, |
46
|
|
|
array(T_OPEN_PARENTHESIS, T_STRING_CONCAT) |
47
|
|
|
)); |
48
|
|
|
|
49
|
|
|
return array(T_EMPTY); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Processes this test, when one of its tokens is encountered. |
54
|
|
|
* |
55
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
56
|
|
|
* @param int $stackPtr The position of the current token in the |
57
|
|
|
* stack passed in $tokens. |
58
|
|
|
* |
59
|
|
|
* @return void |
60
|
|
|
*/ |
61
|
|
|
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
62
|
|
|
{ |
63
|
|
|
if ($this->supportsBelow('5.4') === false) { |
64
|
|
|
return; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$tokens = $phpcsFile->getTokens(); |
68
|
|
|
|
69
|
|
|
$open = $phpcsFile->findNext(T_OPEN_PARENTHESIS, $stackPtr, null, false, null, true); |
70
|
|
|
if ($open === false || isset($tokens[$open]['parenthesis_closer']) === false) { |
71
|
|
|
return; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$close = $tokens[$open]['parenthesis_closer']; |
75
|
|
|
|
76
|
|
|
// If no variable at all was found, then it's definitely a no-no. |
77
|
|
|
$hasVariable = $phpcsFile->findNext(T_VARIABLE, $open + 1, $close); |
78
|
|
|
if ($hasVariable === false) { |
79
|
|
|
$this->addError($phpcsFile, $stackPtr); |
80
|
|
|
return; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
// Check if the variable found is at the right level. Deeper levels are always an error. |
84
|
|
|
if (isset($tokens[$open + 1]['nested_parenthesis'], $tokens[$hasVariable]['nested_parenthesis'])) { |
85
|
|
|
$nestingLevel = count($tokens[$open + 1]['nested_parenthesis']); |
86
|
|
|
if (count($tokens[$hasVariable]['nested_parenthesis']) !== $nestingLevel) { |
87
|
|
|
$this->addError($phpcsFile, $stackPtr); |
88
|
|
|
return; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
// Ok, so the first variable is at the right level, now are there any |
93
|
|
|
// blacklisted tokens within the empty() ? |
94
|
|
|
$hasBadToken = $phpcsFile->findNext($this->tokenBlackList, $open + 1, $close); |
95
|
|
|
if ($hasBadToken !== false) { |
96
|
|
|
$this->addError($phpcsFile, $stackPtr); |
97
|
|
|
return; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Add the error message. |
104
|
|
|
* |
105
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
106
|
|
|
* @param int $stackPtr The position of the current token in the |
107
|
|
|
* stack passed in $tokens. |
108
|
|
|
* |
109
|
|
|
* @return void |
110
|
|
|
*/ |
111
|
|
|
protected function addError($phpcsFile, $stackPtr) |
112
|
|
|
{ |
113
|
|
|
$error = 'Only variables can be passed to empty() prior to PHP 5.5.'; |
114
|
|
|
$phpcsFile->addError($error, $stackPtr, 'Found'); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.