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