1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* PHPCompatibility_Sniffs_PHP_NewFunctionArrayDereferencingSniff. |
4
|
|
|
* |
5
|
|
|
* @category PHP |
6
|
|
|
* @package PHPCompatibility |
7
|
|
|
* @author Wim Godden <[email protected]> |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* PHPCompatibility_Sniffs_PHP_NewFunctionArrayDereferencingSniff. |
12
|
|
|
* |
13
|
|
|
* @category PHP |
14
|
|
|
* @package PHPCompatibility |
15
|
|
|
* @author Wim Godden <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class PHPCompatibility_Sniffs_PHP_NewFunctionArrayDereferencingSniff extends PHPCompatibility_Sniff |
|
|
|
|
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Returns an array of tokens this test wants to listen for. |
21
|
|
|
* |
22
|
|
|
* @return array |
23
|
|
|
*/ |
24
|
|
|
public function register() |
25
|
|
|
{ |
26
|
|
|
return array(T_STRING); |
27
|
|
|
}//end register() |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Processes this test, when one of its tokens is encountered. |
31
|
|
|
* |
32
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
33
|
|
|
* @param int $stackPtr The position of the current token in |
34
|
|
|
* the stack passed in $tokens. |
35
|
|
|
* |
36
|
|
|
* @return void |
37
|
|
|
*/ |
38
|
|
|
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
39
|
|
|
{ |
40
|
|
|
if ($this->supportsBelow('5.3') === false) { |
41
|
|
|
return; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$tokens = $phpcsFile->getTokens(); |
45
|
|
|
|
46
|
|
|
// Next non-empty token should be the open parenthesis. |
47
|
|
|
$openParenthesis = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 1), null, true, null, true); |
48
|
|
|
if ($openParenthesis === false || $tokens[$openParenthesis]['code'] !== T_OPEN_PARENTHESIS) { |
49
|
|
|
return; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
// Don't throw errors during live coding. |
53
|
|
|
if (isset($tokens[$openParenthesis]['parenthesis_closer']) === false) { |
54
|
|
|
return; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
// Is this T_STRING really a function or method call ? |
58
|
|
|
$prevToken = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr - 1), null, true); |
59
|
|
|
if($prevToken !== false && in_array($tokens[$prevToken]['code'], array(T_DOUBLE_COLON, T_OBJECT_OPERATOR), true) === false) { |
60
|
|
|
$ignore = array( |
61
|
|
|
T_FUNCTION, |
62
|
|
|
T_CONST, |
63
|
|
|
T_USE, |
64
|
|
|
T_NEW, |
65
|
|
|
T_CLASS, |
66
|
|
|
T_INTERFACE, |
67
|
|
|
); |
68
|
|
|
|
69
|
|
|
if (in_array($tokens[$prevToken]['code'], $ignore) === true) { |
70
|
|
|
// Not a call to a PHP function or method. |
71
|
|
|
return; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$closeParenthesis = $tokens[$openParenthesis]['parenthesis_closer']; |
76
|
|
|
$nextNonEmpty = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($closeParenthesis + 1), null, true, null, true); |
77
|
|
|
if ($nextNonEmpty !== false && $tokens[$nextNonEmpty]['type'] === 'T_OPEN_SQUARE_BRACKET') { |
78
|
|
|
$phpcsFile->addError( |
79
|
|
|
'Function array dereferencing is not present in PHP version 5.3 or earlier', |
80
|
|
|
$nextNonEmpty, |
81
|
|
|
'Found' |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
}//end process() |
86
|
|
|
}//end class |
87
|
|
|
|
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.