1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* PHPCompatibility_Sniffs_PHP_EmptyNonVariableSniff. |
4
|
|
|
* |
5
|
|
|
* PHP version 5.4 |
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.4 |
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
|
|
|
/** |
26
|
|
|
* Returns an array of tokens this test wants to listen for. |
27
|
|
|
* |
28
|
|
|
* @return array |
29
|
|
|
*/ |
30
|
|
|
public function register() |
31
|
|
|
{ |
32
|
|
|
return array(T_EMPTY); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Processes this test, when one of its tokens is encountered. |
37
|
|
|
* |
38
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
39
|
|
|
* @param int $stackPtr The position of the current token in the |
40
|
|
|
* stack passed in $tokens. |
41
|
|
|
* |
42
|
|
|
* @return void |
43
|
|
|
*/ |
44
|
|
|
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
45
|
|
|
{ |
46
|
|
|
if ($this->supportsBelow('5.4') === false) { |
47
|
|
|
return; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$tokens = $phpcsFile->getTokens(); |
51
|
|
|
|
52
|
|
|
$open = $phpcsFile->findNext(T_OPEN_PARENTHESIS, $stackPtr, null, false, null, true); |
53
|
|
|
if ($open === false || isset($tokens[$open]['parenthesis_closer']) === false) { |
54
|
|
|
return; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$close = $tokens[$open]['parenthesis_closer']; |
58
|
|
|
|
59
|
|
|
// If no variable at all was found, then it's definitely a no-no. |
60
|
|
|
$hasVariable = $phpcsFile->findNext(T_VARIABLE, $open + 1, $close); |
61
|
|
|
if ($hasVariable === false) { |
62
|
|
|
$this->addError($phpcsFile, $stackPtr); |
63
|
|
|
return; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
// Check if the variable found is at the right level. Deeper levels are always an error. |
67
|
|
|
if (isset($tokens[$open + 1]['nested_parenthesis'], $tokens[$hasVariable]['nested_parenthesis'])) { |
68
|
|
|
$nestingLevel = count($tokens[$open + 1]['nested_parenthesis']); |
69
|
|
|
if (count($tokens[$hasVariable]['nested_parenthesis']) !== $nestingLevel) { |
70
|
|
|
$this->addError($phpcsFile, $stackPtr); |
71
|
|
|
return; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
// Ok, so the first variable is at the right level, now are there open |
76
|
|
|
// parenthesis within the empty() ? |
77
|
|
|
$hasSubLevel = $phpcsFile->findNext(T_OPEN_PARENTHESIS, $open + 1, $close); |
78
|
|
|
if ($hasSubLevel !== false) { |
79
|
|
|
$this->addError($phpcsFile, $stackPtr); |
80
|
|
|
return; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Add the error message. |
87
|
|
|
* |
88
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
89
|
|
|
* @param int $stackPtr The position of the current token in the |
90
|
|
|
* stack passed in $tokens. |
91
|
|
|
* |
92
|
|
|
* @return void |
93
|
|
|
*/ |
94
|
|
|
protected function addError($phpcsFile, $stackPtr) |
95
|
|
|
{ |
96
|
|
|
$error = 'Only variables can be passed to empty() prior to PHP 5.5.'; |
97
|
|
|
$phpcsFile->addError($error, $stackPtr, 'Found'); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
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.