1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* PHPCompatibility_Sniffs_PHP_NewMagicMethodsSniff. |
4
|
|
|
* |
5
|
|
|
* @category PHP |
6
|
|
|
* @package PHPCompatibility |
7
|
|
|
* @author Juliette Reinders Folmer <[email protected]> |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* PHPCompatibility_Sniffs_PHP_NewMagicMethodsSniff. |
12
|
|
|
* |
13
|
|
|
* Warns for non-magic behaviour of magic methods prior to becoming magic. |
14
|
|
|
* |
15
|
|
|
* @category PHP |
16
|
|
|
* @package PHPCompatibility |
17
|
|
|
* @author Juliette Reinders Folmer <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
class PHPCompatibility_Sniffs_PHP_NewMagicMethodsSniff extends PHPCompatibility_Sniff |
|
|
|
|
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* A list of new magic methods, not considered magic in older versions. |
24
|
|
|
* |
25
|
|
|
* Method names in the array should be all *lowercase*. |
26
|
|
|
* The array lists : version number with false (not magic) or true (magic). |
27
|
|
|
* If's sufficient to list the first version where the method became magic. |
28
|
|
|
* |
29
|
|
|
* @var array(string => array(string => int|string|null)) |
30
|
|
|
*/ |
31
|
|
|
protected $newMagicMethods = array( |
32
|
|
|
'__get' => array( // verified |
33
|
|
|
'4.4' => false, |
34
|
|
|
'5.0' => true, |
35
|
|
|
), |
36
|
|
|
|
37
|
|
|
'__isset' => array( // verified |
38
|
|
|
'5.0' => false, |
39
|
|
|
'5.1' => true, |
40
|
|
|
), |
41
|
|
|
'__unset' => array( // verified |
42
|
|
|
'5.0' => false, |
43
|
|
|
'5.1' => true, |
44
|
|
|
), |
45
|
|
|
'__set_state' => array( // verified |
46
|
|
|
'5.0' => false, |
47
|
|
|
'5.1' => true, |
48
|
|
|
), |
49
|
|
|
|
50
|
|
|
'__callstatic' => array( // verified |
51
|
|
|
'5.2' => false, |
52
|
|
|
'5.3' => true, |
53
|
|
|
), |
54
|
|
|
'__invoke' => array( // verified |
55
|
|
|
'5.2' => false, |
56
|
|
|
'5.3' => true, |
57
|
|
|
), |
58
|
|
|
|
59
|
|
|
'__debuginfo' => array( // verified |
60
|
|
|
'5.5' => false, |
61
|
|
|
'5.6' => true, |
62
|
|
|
), |
63
|
|
|
|
64
|
|
|
// Special case - only became properly magical in 5.2.0, |
65
|
|
|
// before that it was only called for echo and print. |
66
|
|
|
'__tostring' => array( |
67
|
|
|
'5.1' => false, |
68
|
|
|
'5.2' => true, |
69
|
|
|
'message' => 'The method %s() was not truly magical in PHP version %s and earlier. The associated magic functionality will only be called when directly combined with echo or print.', |
70
|
|
|
), |
71
|
|
|
); |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Returns an array of tokens this test wants to listen for. |
76
|
|
|
* |
77
|
|
|
* @return array |
78
|
|
|
*/ |
79
|
|
|
public function register() |
80
|
|
|
{ |
81
|
|
|
return array(T_FUNCTION); |
82
|
|
|
|
83
|
|
|
}//end register() |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Processes this test, when one of its tokens is encountered. |
88
|
|
|
* |
89
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
90
|
|
|
* @param int $stackPtr The position of the current token in the |
91
|
|
|
* stack passed in $tokens. |
92
|
|
|
* |
93
|
|
|
* @return void |
94
|
|
|
*/ |
95
|
|
|
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
96
|
|
|
{ |
97
|
|
|
$functionName = $phpcsFile->getDeclarationName($stackPtr); |
98
|
|
|
$functionNameLc = strtolower($functionName); |
99
|
|
|
|
100
|
|
|
if (isset($this->newMagicMethods[$functionNameLc]) === false) { |
101
|
|
|
return; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
if ($this->inClassScope($phpcsFile, $stackPtr, false) === false) { |
105
|
|
|
return; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$lastVersionBelow = ''; |
109
|
|
|
foreach ($this->newMagicMethods[$functionNameLc] as $version => $magic) { |
110
|
|
|
if ($version !== 'message' && $magic === false && $this->supportsBelow($version)) { |
111
|
|
|
$lastVersionBelow = $version; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
if ($lastVersionBelow !== '') { |
116
|
|
|
$error = 'The method %s() was not magical in PHP version %s and earlier. The associated magic functionality will not be invoked.'; |
117
|
|
|
if (empty($this->newMagicMethods[$functionNameLc]['message']) === false) { |
118
|
|
|
$error = $this->newMagicMethods[$functionNameLc]['message']; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$data = array( |
122
|
|
|
$functionName, |
123
|
|
|
$lastVersionBelow, |
124
|
|
|
); |
125
|
|
|
|
126
|
|
|
$phpcsFile->addWarning($error, $stackPtr, 'Found', $data); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
}//end process() |
130
|
|
|
|
131
|
|
|
}//end class |
132
|
|
|
|
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.