1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* PHPCompatibility_Sniffs_PHP_NewArrayStringDereferencingSniff. |
4
|
|
|
* |
5
|
|
|
* PHP version 5.5 |
6
|
|
|
* |
7
|
|
|
* @category PHP |
8
|
|
|
* @package PHPCompatibility |
9
|
|
|
* @author Juliette Reinders Folmer <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* PHPCompatibility_Sniffs_PHP_NewArrayStringDereferencingSniff. |
14
|
|
|
* |
15
|
|
|
* Array and string literals can now be dereferenced directly to access individual elements and characters. |
16
|
|
|
* |
17
|
|
|
* PHP version 5.5 |
18
|
|
|
* |
19
|
|
|
* @category PHP |
20
|
|
|
* @package PHPCompatibility |
21
|
|
|
* @author Juliette Reinders Folmer <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class PHPCompatibility_Sniffs_PHP_NewArrayStringDereferencingSniff extends PHPCompatibility_Sniff |
|
|
|
|
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( |
33
|
|
|
T_ARRAY, |
34
|
|
|
T_OPEN_SHORT_ARRAY, |
35
|
|
|
T_CONSTANT_ENCAPSED_STRING, |
36
|
|
|
); |
37
|
|
|
}//end register() |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Processes this test, when one of its tokens is encountered. |
41
|
|
|
* |
42
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
43
|
|
|
* @param int $stackPtr The position of the current token in |
44
|
|
|
* the stack passed in $tokens. |
45
|
|
|
* |
46
|
|
|
* @return void |
47
|
|
|
*/ |
48
|
|
|
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
49
|
|
|
{ |
50
|
|
|
if ($this->supportsBelow('5.4') === false) { |
51
|
|
|
return; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$tokens = $phpcsFile->getTokens(); |
55
|
|
|
|
56
|
|
|
switch($tokens[$stackPtr]['code']) { |
57
|
|
|
case T_CONSTANT_ENCAPSED_STRING: |
58
|
|
|
$type = 'string literals'; |
59
|
|
|
$end = $stackPtr; |
60
|
|
|
break; |
61
|
|
|
|
62
|
|
View Code Duplication |
case T_ARRAY: |
|
|
|
|
63
|
|
|
if (isset($tokens[$stackPtr]['parenthesis_closer']) === false) { |
64
|
|
|
// Live coding. |
65
|
|
|
return; |
66
|
|
|
} else { |
67
|
|
|
$type = 'arrays'; |
68
|
|
|
$end = $tokens[$stackPtr]['parenthesis_closer']; |
69
|
|
|
} |
70
|
|
|
break; |
71
|
|
|
|
72
|
|
View Code Duplication |
case T_OPEN_SHORT_ARRAY: |
|
|
|
|
73
|
|
|
if (isset($tokens[$stackPtr]['bracket_closer']) === false) { |
74
|
|
|
// Live coding. |
75
|
|
|
return; |
76
|
|
|
} else { |
77
|
|
|
$type = 'arrays'; |
78
|
|
|
$end = $tokens[$stackPtr]['bracket_closer']; |
79
|
|
|
} |
80
|
|
|
break; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
if (isset($type, $end) === false) { |
84
|
|
|
// Shouldn't happen, but for some reason did. |
85
|
|
|
return; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$nextNonEmpty = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($end + 1), null, true, null, true); |
|
|
|
|
89
|
|
|
|
90
|
|
|
if ($nextNonEmpty !== false && |
91
|
|
|
($tokens[$nextNonEmpty]['type'] === 'T_OPEN_SQUARE_BRACKET' || |
92
|
|
|
$tokens[$nextNonEmpty]['type'] === 'T_OPEN_SHORT_ARRAY') // Work around bug #1381 in PHPCS 2.8.1 and lower. |
93
|
|
|
) { |
94
|
|
|
$phpcsFile->addError( |
95
|
|
|
'Direct array dereferencing of %s is not present in PHP version 5.4 or earlier', |
96
|
|
|
$nextNonEmpty, |
97
|
|
|
'Found', |
98
|
|
|
array($type) |
|
|
|
|
99
|
|
|
); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
}//end process() |
103
|
|
|
|
104
|
|
|
}//end class |
105
|
|
|
|
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.