|
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_AbstractNewFeatureSniff |
|
|
|
|
|
|
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
|
|
|
$itemInfo = array( |
|
109
|
|
|
'name' => $functionName, |
|
110
|
|
|
'nameLc' => $functionNameLc, |
|
111
|
|
|
); |
|
112
|
|
|
$this->handleFeature($phpcsFile, $stackPtr, $itemInfo); |
|
113
|
|
|
|
|
114
|
|
|
}//end process() |
|
115
|
|
|
|
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Get the relevant sub-array for a specific item from a multi-dimensional array. |
|
119
|
|
|
* |
|
120
|
|
|
* @param array $itemInfo Base information about the item. |
|
121
|
|
|
* |
|
122
|
|
|
* @return array Version and other information about the item. |
|
123
|
|
|
*/ |
|
124
|
|
|
public function getItemArray(array $itemInfo) |
|
125
|
|
|
{ |
|
126
|
|
|
return $this->newMagicMethods[$itemInfo['nameLc']]; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Get an array of the non-PHP-version array keys used in a sub-array. |
|
132
|
|
|
* |
|
133
|
|
|
* @return array |
|
134
|
|
|
*/ |
|
135
|
|
|
protected function getNonVersionArrayKeys() |
|
136
|
|
|
{ |
|
137
|
|
|
return array('message'); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Retrieve the relevant detail (version) information for use in an error message. |
|
143
|
|
|
* |
|
144
|
|
|
* @param array $itemArray Version and other information about the item. |
|
145
|
|
|
* @param array $itemInfo Base information about the item. |
|
146
|
|
|
* |
|
147
|
|
|
* @return array |
|
148
|
|
|
*/ |
|
149
|
|
|
public function getErrorInfo(array $itemArray, array $itemInfo) |
|
150
|
|
|
{ |
|
151
|
|
|
$errorInfo = parent::getErrorInfo($itemArray, $itemInfo); |
|
152
|
|
|
$errorInfo['error'] = false; // Warning, not error. |
|
153
|
|
|
$errorInfo['message'] = ''; |
|
154
|
|
|
|
|
155
|
|
|
if (empty($itemArray['message']) === false) { |
|
156
|
|
|
$errorInfo['message'] = $itemArray['message']; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
return $errorInfo; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Get the error message template for this sniff. |
|
165
|
|
|
* |
|
166
|
|
|
* @return string |
|
167
|
|
|
*/ |
|
168
|
|
|
protected function getErrorMsgTemplate() |
|
169
|
|
|
{ |
|
170
|
|
|
return 'The method %s() was not magical in PHP version %s and earlier. The associated magic functionality will not be invoked.'; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Allow for concrete child classes to filter the error message before it's passed to PHPCS. |
|
176
|
|
|
* |
|
177
|
|
|
* @param string $error The error message which was created. |
|
178
|
|
|
* @param array $itemInfo Base information about the item this error message applied to. |
|
179
|
|
|
* @param array $errorInfo Detail information about an item this error message applied to. |
|
180
|
|
|
* |
|
181
|
|
|
* @return string |
|
182
|
|
|
*/ |
|
183
|
|
|
protected function filterErrorMsg($error, array $itemInfo, array $errorInfo) |
|
184
|
|
|
{ |
|
185
|
|
|
if ($errorInfo['message'] !== '') { |
|
186
|
|
|
$error = $errorInfo['message']; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
return $error; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
|
|
193
|
|
|
}//end class |
|
194
|
|
|
|
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.