1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* PHPCompatibility_Sniffs_PHP_NewInterfacesSniff. |
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_NewInterfacesSniff. |
14
|
|
|
* |
15
|
|
|
* @category PHP |
16
|
|
|
* @package PHPCompatibility |
17
|
|
|
* @author Juliette Reinders Folmer <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
class PHPCompatibility_Sniffs_PHP_NewInterfacesSniff extends PHPCompatibility_Sniff |
|
|
|
|
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* A list of new interfaces, not present in older versions. |
24
|
|
|
* |
25
|
|
|
* The array lists : version number with false (not present) or true (present). |
26
|
|
|
* If's sufficient to list the first version where the interface appears. |
27
|
|
|
* |
28
|
|
|
* @var array(string => array(string => int|string|null)) |
29
|
|
|
*/ |
30
|
|
|
protected $newInterfaces = array( |
31
|
|
|
'Countable' => array( |
32
|
|
|
'5.0' => false, |
33
|
|
|
'5.1' => true |
34
|
|
|
), |
35
|
|
|
'OuterIterator' => array( |
36
|
|
|
'5.0' => false, |
37
|
|
|
'5.1' => true |
38
|
|
|
), |
39
|
|
|
'RecursiveIterator' => array( |
40
|
|
|
'5.0' => false, |
41
|
|
|
'5.1' => true |
42
|
|
|
), |
43
|
|
|
'SeekableIterator' => array( |
44
|
|
|
'5.0' => false, |
45
|
|
|
'5.1' => true |
46
|
|
|
), |
47
|
|
|
'Serializable' => array( |
48
|
|
|
'5.0' => false, |
49
|
|
|
'5.1' => true, |
50
|
|
|
), |
51
|
|
|
'SplObserver' => array( |
52
|
|
|
'5.0' => false, |
53
|
|
|
'5.1' => true |
54
|
|
|
), |
55
|
|
|
'SplSubject' => array( |
56
|
|
|
'5.0' => false, |
57
|
|
|
'5.1' => true |
58
|
|
|
), |
59
|
|
|
|
60
|
|
|
'JsonSerializable' => array( |
61
|
|
|
'5.3' => false, |
62
|
|
|
'5.4' => true |
63
|
|
|
), |
64
|
|
|
'SessionHandlerInterface' => array( |
65
|
|
|
'5.3' => false, |
66
|
|
|
'5.4' => true |
67
|
|
|
), |
68
|
|
|
|
69
|
|
|
); |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* A list of methods which cannot be used in combination with particular interfaces. |
73
|
|
|
* |
74
|
|
|
* @var array(string => array(string => string)) |
75
|
|
|
*/ |
76
|
|
|
protected $unsupportedMethods = array( |
77
|
|
|
'Serializable' => array( |
78
|
|
|
'__sleep' => 'http://php.net/serializable', |
79
|
|
|
'__wakeup' => 'http://php.net/serializable', |
80
|
|
|
), |
81
|
|
|
); |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Returns an array of tokens this test wants to listen for. |
85
|
|
|
* |
86
|
|
|
* @return array |
87
|
|
|
*/ |
88
|
|
|
public function register() |
89
|
|
|
{ |
90
|
|
|
// Handle case-insensitivity of class names. |
91
|
|
|
$keys = array_keys( $this->newInterfaces ); |
92
|
|
|
$keys = array_map( 'strtolower', $keys ); |
93
|
|
|
$this->newInterfaces = array_combine( $keys, $this->newInterfaces ); |
94
|
|
|
|
95
|
|
|
$keys = array_keys( $this->unsupportedMethods ); |
96
|
|
|
$keys = array_map( 'strtolower', $keys ); |
97
|
|
|
$this->unsupportedMethods = array_combine( $keys, $this->unsupportedMethods ); |
98
|
|
|
|
99
|
|
|
return array(T_CLASS); |
100
|
|
|
|
101
|
|
|
}//end register() |
102
|
|
|
|
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Processes this test, when one of its tokens is encountered. |
106
|
|
|
* |
107
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
108
|
|
|
* @param int $stackPtr The position of the current token in |
109
|
|
|
* the stack passed in $tokens. |
110
|
|
|
* |
111
|
|
|
* @return void |
112
|
|
|
*/ |
113
|
|
|
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
114
|
|
|
{ |
115
|
|
|
$interfaces = $this->findImplementedInterfaceNames($phpcsFile, $stackPtr); |
116
|
|
|
|
117
|
|
|
if (is_array($interfaces) === false || $interfaces === array()) { |
118
|
|
|
return; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$tokens = $phpcsFile->getTokens(); |
122
|
|
|
$checkMethods = false; |
123
|
|
|
|
124
|
|
|
if(isset($tokens[$stackPtr]['scope_closer'])) { |
125
|
|
|
$checkMethods = true; |
126
|
|
|
$scopeCloser = $tokens[$stackPtr]['scope_closer']; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
foreach ($interfaces as $interface) { |
130
|
|
|
$lcInterface = strtolower($interface); |
131
|
|
|
if (isset($this->newInterfaces[$lcInterface]) === true) { |
132
|
|
|
$this->addError($phpcsFile, $stackPtr, $interface); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
if ($checkMethods === true && isset($this->unsupportedMethods[$lcInterface]) === true) { |
136
|
|
|
$nextFunc = $stackPtr; |
137
|
|
|
while (($nextFunc = $phpcsFile->findNext(T_FUNCTION, ($nextFunc + 1), $scopeCloser)) !== false) { |
|
|
|
|
138
|
|
|
$funcNamePos = $phpcsFile->findNext(T_STRING, $nextFunc); |
139
|
|
|
$funcName = strtolower($tokens[$funcNamePos]['content']); |
140
|
|
|
|
141
|
|
|
if (isset($this->unsupportedMethods[$lcInterface][$funcName]) === true) { |
142
|
|
|
$error = 'Classes that implement interface %s do not support the method %s(). See %s'; |
143
|
|
|
$data = array( |
144
|
|
|
$interface, |
145
|
|
|
$tokens[$funcNamePos]['content'], |
146
|
|
|
$this->unsupportedMethods[$lcInterface][$funcName], |
147
|
|
|
); |
148
|
|
|
$phpcsFile->addError($error, $funcNamePos, 'UnsupportedMethod', $data); |
|
|
|
|
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
}//end process() |
155
|
|
|
|
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Generates the error or warning for this sniff. |
159
|
|
|
* |
160
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
161
|
|
|
* @param int $stackPtr The position of the function |
162
|
|
|
* in the token array. |
163
|
|
|
* @param string $interface The name of the interface. |
164
|
|
|
* |
165
|
|
|
* @return void |
166
|
|
|
*/ |
167
|
|
View Code Duplication |
protected function addError($phpcsFile, $stackPtr, $interface) |
|
|
|
|
168
|
|
|
{ |
169
|
|
|
$interfaceLc = strtolower($interface); |
170
|
|
|
$error = ''; |
171
|
|
|
|
172
|
|
|
$isError = false; |
173
|
|
|
foreach ($this->newInterfaces[$interfaceLc] as $version => $present) { |
174
|
|
|
if ($this->supportsBelow($version)) { |
175
|
|
|
if ($present === false) { |
176
|
|
|
$isError = true; |
177
|
|
|
$error .= 'not present in PHP version ' . $version . ' or earlier'; |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
if (strlen($error) > 0) { |
183
|
|
|
$error = 'The built-in interface ' . $interface . ' is ' . $error; |
184
|
|
|
|
185
|
|
|
if ($isError === true) { |
186
|
|
|
$phpcsFile->addError($error, $stackPtr); |
187
|
|
|
} else { |
188
|
|
|
$phpcsFile->addWarning($error, $stackPtr); |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
}//end addError() |
193
|
|
|
|
194
|
|
|
}//end class |
195
|
|
|
|
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.