1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* PHPCompatibility_Sniffs_PHP_NewAnonymousClasses. |
4
|
|
|
* |
5
|
|
|
* PHP version 7.0 |
6
|
|
|
* |
7
|
|
|
* @category PHP |
8
|
|
|
* @package PHPCompatibility |
9
|
|
|
* @author Wim Godden <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* PHPCompatibility_Sniffs_PHP_NewAnonymousClasses. |
14
|
|
|
* |
15
|
|
|
* Anonymous classes are supported in PHP 7.0 |
16
|
|
|
* |
17
|
|
|
* PHP version 7.0 |
18
|
|
|
* |
19
|
|
|
* @category PHP |
20
|
|
|
* @package PHPCompatibility |
21
|
|
|
* @author Wim Godden <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class PHPCompatibility_Sniffs_PHP_NewAnonymousClassesSniff extends PHPCompatibility_Sniff |
|
|
|
|
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Tokens which in various PHP versions indicate the `class` keyword. |
28
|
|
|
* |
29
|
|
|
* The dedicated anonymous class token is added from the `register()` |
30
|
|
|
* method if the token is available. |
31
|
|
|
* |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
private $indicators = array( |
35
|
|
|
T_CLASS => T_CLASS, |
36
|
|
|
); |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Returns an array of tokens this test wants to listen for. |
40
|
|
|
* |
41
|
|
|
* @return array |
42
|
|
|
*/ |
43
|
3 |
|
public function register() |
44
|
|
|
{ |
45
|
3 |
|
if (defined('T_ANON_CLASS')) { |
46
|
3 |
|
$this->indicators[T_ANON_CLASS] = T_ANON_CLASS; |
47
|
3 |
|
} |
48
|
|
|
|
49
|
3 |
|
return array(T_NEW); |
50
|
|
|
}//end register() |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Processes this test, when one of its tokens is encountered. |
55
|
|
|
* |
56
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
57
|
|
|
* @param int $stackPtr The position of the current token in the |
58
|
|
|
* stack passed in $tokens. |
59
|
|
|
* |
60
|
|
|
* @return void |
61
|
|
|
*/ |
62
|
2 |
|
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
63
|
|
|
{ |
64
|
2 |
|
if ($this->supportsBelow('5.6') === false) { |
65
|
1 |
|
return; |
66
|
|
|
} |
67
|
|
|
|
68
|
1 |
|
$tokens = $phpcsFile->getTokens(); |
69
|
1 |
|
$nextNonEmpty = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, $stackPtr + 1, null, true, null, true); |
70
|
1 |
|
if ($nextNonEmpty === false || isset($this->indicators[$tokens[$nextNonEmpty]['code']]) === false) { |
71
|
1 |
|
return; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
// Still here ? In that case, it is an anonymous class. |
75
|
1 |
|
$phpcsFile->addError( |
76
|
1 |
|
'Anonymous classes are not supported in PHP 5.6 or earlier', |
77
|
1 |
|
$stackPtr, |
78
|
|
|
'Found' |
79
|
1 |
|
); |
80
|
|
|
|
81
|
1 |
|
}//end process() |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
}//end class |
85
|
|
|
|
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.