1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* PHPCompatibility_Sniffs_PHP_VariableVariables. |
4
|
|
|
* |
5
|
|
|
* PHP version 7.0 |
6
|
|
|
* |
7
|
|
|
* @category PHP |
8
|
|
|
* @package PHPCompatibility |
9
|
|
|
* @author Juliette Reinders Folmer <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* PHPCompatibility_Sniffs_PHP_VariableVariables. |
14
|
|
|
* |
15
|
|
|
* The interpretation of variable variables has changed in PHP 7.0. |
16
|
|
|
* |
17
|
|
|
* PHP version 7.0 |
18
|
|
|
* |
19
|
|
|
* @category PHP |
20
|
|
|
* @package PHPCompatibility |
21
|
|
|
* @author Juliette Reinders Folmer <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class PHPCompatibility_Sniffs_PHP_VariableVariablesSniff extends PHPCompatibility_Sniff |
|
|
|
|
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Returns an array of tokens this test wants to listen for. |
27
|
|
|
* |
28
|
|
|
* @return array |
29
|
|
|
*/ |
30
|
25 |
|
public function register() |
31
|
|
|
{ |
32
|
25 |
|
return array(T_VARIABLE); |
33
|
|
|
|
34
|
|
|
}//end register() |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Processes this test, when one of its tokens is encountered. |
38
|
|
|
* |
39
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
40
|
|
|
* @param int $stackPtr The position of the current token |
41
|
|
|
* in the stack passed in $tokens. |
42
|
|
|
* |
43
|
|
|
* @return void |
44
|
|
|
*/ |
45
|
2 |
|
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
46
|
|
|
{ |
47
|
2 |
|
if ($this->supportsAbove('7.0') === false) { |
48
|
1 |
|
return; |
49
|
|
|
} |
50
|
|
|
|
51
|
1 |
|
$tokens = $phpcsFile->getTokens(); |
52
|
|
|
|
53
|
|
|
// Verify that the next token is a square open bracket. If not, bow out. |
54
|
1 |
|
$nextToken = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 1), null, true, null, true); |
55
|
|
|
|
56
|
1 |
|
if ($nextToken === false || $tokens[$nextToken]['code'] !== T_OPEN_SQUARE_BRACKET || isset($tokens[$nextToken]['bracket_closer']) === false) { |
57
|
1 |
|
return; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
// The previous token has to be a $, -> or ::. |
61
|
1 |
|
if (isset($tokens[($stackPtr - 1)]) === false || in_array($tokens[($stackPtr - 1)]['code'], array(T_DOLLAR, T_OBJECT_OPERATOR, T_DOUBLE_COLON), true) === false) { |
62
|
1 |
|
return; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
// For static object calls, it only applies when this is a function call. |
66
|
1 |
|
if ($tokens[($stackPtr - 1)]['code'] === T_DOUBLE_COLON) { |
67
|
1 |
|
$hasBrackets = $tokens[$nextToken]['bracket_closer']; |
68
|
1 |
|
while (($hasBrackets = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($hasBrackets + 1), null, true, null, true)) !== false) { |
69
|
1 |
|
if ($tokens[$hasBrackets]['code'] === T_OPEN_SQUARE_BRACKET) { |
70
|
1 |
|
if (isset($tokens[$hasBrackets]['bracket_closer'])) { |
71
|
1 |
|
$hasBrackets = $tokens[$hasBrackets]['bracket_closer']; |
72
|
1 |
|
continue; |
73
|
|
|
} else { |
74
|
|
|
// Live coding. |
75
|
|
|
return; |
76
|
|
|
} |
77
|
|
|
|
78
|
1 |
|
} elseif ($tokens[$hasBrackets]['code'] === T_OPEN_PARENTHESIS) { |
79
|
|
|
// Caught! |
80
|
1 |
|
break; |
81
|
|
|
|
82
|
|
|
} else { |
83
|
|
|
// Not a function call, so bow out. |
84
|
1 |
|
return; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
// Now let's also prevent false positives when used with self and static which still work fine. |
89
|
1 |
|
$classToken = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr - 2), null, true, null, true); |
90
|
1 |
|
if ($classToken !== false) { |
91
|
1 |
|
if ($tokens[$classToken]['code'] === T_STATIC || $tokens[$classToken]['code'] === T_SELF) { |
92
|
1 |
|
return; |
93
|
|
|
} |
94
|
1 |
|
elseif ($tokens[$classToken]['code'] === T_STRING && $tokens[$classToken]['content'] === 'self') { |
95
|
|
|
return; |
96
|
|
|
} |
97
|
1 |
|
} |
98
|
1 |
|
} |
99
|
|
|
|
100
|
1 |
|
$phpcsFile->addError( |
101
|
1 |
|
'Indirect access to variables, properties and methods will be evaluated strictly in left-to-right order since PHP 7.0. Use curly braces to remove ambiguity.', |
102
|
1 |
|
$stackPtr, |
103
|
|
|
'Found' |
104
|
1 |
|
); |
105
|
|
|
|
106
|
1 |
|
}//end process() |
107
|
|
|
|
108
|
|
|
|
109
|
|
|
}//end class |
110
|
|
|
|
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.