Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
17 | class PHPCompatibility_Sniffs_PHP_NewInterfacesSniff extends PHPCompatibility_AbstractNewFeatureSniff |
||
|
|||
18 | { |
||
19 | |||
20 | /** |
||
21 | * A list of new interfaces, not present in older versions. |
||
22 | * |
||
23 | * The array lists : version number with false (not present) or true (present). |
||
24 | * If's sufficient to list the first version where the interface appears. |
||
25 | * |
||
26 | * @var array(string => array(string => int|string|null)) |
||
27 | */ |
||
28 | protected $newInterfaces = array( |
||
29 | 'Traversable' => array( |
||
30 | '4.4' => false, |
||
31 | '5.0' => true, |
||
32 | ), |
||
33 | |||
34 | 'Countable' => array( |
||
35 | '5.0' => false, |
||
36 | '5.1' => true, |
||
37 | ), |
||
38 | 'OuterIterator' => array( |
||
39 | '5.0' => false, |
||
40 | '5.1' => true, |
||
41 | ), |
||
42 | 'RecursiveIterator' => array( |
||
43 | '5.0' => false, |
||
44 | '5.1' => true, |
||
45 | ), |
||
46 | 'SeekableIterator' => array( |
||
47 | '5.0' => false, |
||
48 | '5.1' => true, |
||
49 | ), |
||
50 | 'Serializable' => array( |
||
51 | '5.0' => false, |
||
52 | '5.1' => true, |
||
53 | ), |
||
54 | 'SplObserver' => array( |
||
55 | '5.0' => false, |
||
56 | '5.1' => true, |
||
57 | ), |
||
58 | 'SplSubject' => array( |
||
59 | '5.0' => false, |
||
60 | '5.1' => true, |
||
61 | ), |
||
62 | |||
63 | 'JsonSerializable' => array( |
||
64 | '5.3' => false, |
||
65 | '5.4' => true, |
||
66 | ), |
||
67 | 'SessionHandlerInterface' => array( |
||
68 | '5.3' => false, |
||
69 | '5.4' => true, |
||
70 | ), |
||
71 | |||
72 | 'DateTimeInterface' => array( |
||
73 | '5.4' => false, |
||
74 | '5.5' => true, |
||
75 | ), |
||
76 | |||
77 | 'Throwable' => array( |
||
78 | '5.6' => false, |
||
79 | '7.0' => true, |
||
80 | ), |
||
81 | |||
82 | ); |
||
83 | |||
84 | /** |
||
85 | * A list of methods which cannot be used in combination with particular interfaces. |
||
86 | * |
||
87 | * @var array(string => array(string => string)) |
||
88 | */ |
||
89 | protected $unsupportedMethods = array( |
||
90 | 'Serializable' => array( |
||
91 | '__sleep' => 'http://php.net/serializable', |
||
92 | '__wakeup' => 'http://php.net/serializable', |
||
93 | ), |
||
94 | ); |
||
95 | |||
96 | /** |
||
97 | * Returns an array of tokens this test wants to listen for. |
||
98 | * |
||
99 | * @return array |
||
100 | */ |
||
101 | 22 | public function register() |
|
102 | { |
||
103 | // Handle case-insensitivity of interface names. |
||
104 | 22 | $this->newInterfaces = $this->arrayKeysToLowercase($this->newInterfaces); |
|
105 | 22 | $this->unsupportedMethods = $this->arrayKeysToLowercase($this->unsupportedMethods); |
|
106 | |||
107 | $targets = array( |
||
108 | 22 | T_CLASS, |
|
109 | T_FUNCTION, |
||
110 | 22 | T_CLOSURE, |
|
111 | ); |
||
112 | |||
113 | 22 | if (defined('T_ANON_CLASS')) { |
|
114 | 22 | $targets[] = constant('T_ANON_CLASS'); |
|
115 | } |
||
116 | |||
117 | 22 | return $targets; |
|
118 | |||
119 | }//end register() |
||
120 | |||
121 | |||
122 | /** |
||
123 | * Processes this test, when one of its tokens is encountered. |
||
124 | * |
||
125 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
126 | * @param int $stackPtr The position of the current token in |
||
127 | * the stack passed in $tokens. |
||
128 | * |
||
129 | * @return void |
||
130 | */ |
||
131 | 5 | View Code Duplication | public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
132 | { |
||
133 | 5 | $tokens = $phpcsFile->getTokens(); |
|
134 | |||
135 | 5 | switch ($tokens[$stackPtr]['type']) { |
|
136 | 5 | case 'T_CLASS': |
|
137 | 5 | case 'T_ANON_CLASS': |
|
138 | 5 | $this->processClassToken($phpcsFile, $stackPtr); |
|
139 | 5 | break; |
|
140 | |||
141 | 5 | case 'T_FUNCTION': |
|
142 | 5 | case 'T_CLOSURE': |
|
143 | 5 | $this->processFunctionToken($phpcsFile, $stackPtr); |
|
144 | 5 | break; |
|
145 | |||
146 | default: |
||
147 | // Deliberately left empty. |
||
148 | break; |
||
149 | } |
||
150 | |||
151 | 5 | }//end process() |
|
152 | |||
153 | |||
154 | /** |
||
155 | * Processes this test for when a class token is encountered. |
||
156 | * |
||
157 | * - Detect classes implementing the new interfaces. |
||
158 | * - Detect classes implementing the new interfaces with unsupported functions. |
||
159 | * |
||
160 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
161 | * @param int $stackPtr The position of the current token in |
||
162 | * the stack passed in $tokens. |
||
163 | * |
||
164 | * @return void |
||
165 | */ |
||
166 | 5 | private function processClassToken(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
167 | { |
||
168 | 5 | $interfaces = $this->findImplementedInterfaceNames($phpcsFile, $stackPtr); |
|
169 | |||
170 | 5 | if (is_array($interfaces) === false || $interfaces === array()) { |
|
171 | 5 | return; |
|
172 | } |
||
173 | |||
174 | 5 | $tokens = $phpcsFile->getTokens(); |
|
175 | 5 | $checkMethods = false; |
|
176 | |||
177 | 5 | if (isset($tokens[$stackPtr]['scope_closer'])) { |
|
178 | 5 | $checkMethods = true; |
|
179 | 5 | $scopeCloser = $tokens[$stackPtr]['scope_closer']; |
|
180 | } |
||
181 | |||
182 | 5 | foreach ($interfaces as $interface) { |
|
183 | 5 | $interfaceLc = strtolower($interface); |
|
184 | |||
185 | 5 | if (isset($this->newInterfaces[$interfaceLc]) === true) { |
|
186 | $itemInfo = array( |
||
187 | 5 | 'name' => $interface, |
|
188 | 5 | 'nameLc' => $interfaceLc, |
|
189 | ); |
||
190 | 5 | $this->handleFeature($phpcsFile, $stackPtr, $itemInfo); |
|
191 | } |
||
192 | |||
193 | 5 | if ($checkMethods === true && isset($this->unsupportedMethods[$interfaceLc]) === true) { |
|
194 | 5 | $nextFunc = $stackPtr; |
|
195 | 5 | while (($nextFunc = $phpcsFile->findNext(T_FUNCTION, ($nextFunc + 1), $scopeCloser)) !== false) { |
|
196 | 5 | $funcName = $phpcsFile->getDeclarationName($nextFunc); |
|
197 | 5 | $funcNameLc = strtolower($funcName); |
|
198 | 5 | if ($funcNameLc === '') { |
|
199 | continue; |
||
200 | } |
||
201 | |||
202 | 5 | if (isset($this->unsupportedMethods[$interfaceLc][$funcNameLc]) === true) { |
|
203 | 5 | $error = 'Classes that implement interface %s do not support the method %s(). See %s'; |
|
204 | 5 | $errorCode = $this->stringToErrorCode($interface).'UnsupportedMethod'; |
|
205 | $data = array( |
||
206 | 5 | $interface, |
|
207 | 5 | $funcName, |
|
208 | 5 | $this->unsupportedMethods[$interfaceLc][$funcNameLc], |
|
209 | ); |
||
210 | |||
211 | 5 | $phpcsFile->addError($error, $nextFunc, $errorCode, $data); |
|
212 | } |
||
213 | } |
||
214 | } |
||
215 | } |
||
216 | 5 | }//end processClassToken() |
|
217 | |||
218 | |||
219 | /** |
||
220 | * Processes this test for when a function token is encountered. |
||
221 | * |
||
222 | * - Detect new interfaces when used as a type hint. |
||
223 | * |
||
224 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
225 | * @param int $stackPtr The position of the current token in |
||
226 | * the stack passed in $tokens. |
||
227 | * |
||
228 | * @return void |
||
229 | */ |
||
230 | 5 | View Code Duplication | private function processFunctionToken(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
231 | { |
||
232 | 5 | $typeHints = $this->getTypeHintsFromFunctionDeclaration($phpcsFile, $stackPtr); |
|
233 | 5 | if (empty($typeHints) || is_array($typeHints) === false) { |
|
234 | 5 | return; |
|
235 | } |
||
236 | |||
237 | 5 | foreach ($typeHints as $hint) { |
|
238 | |||
239 | 5 | $typeHintLc = strtolower($hint); |
|
240 | |||
241 | 5 | if (isset($this->newInterfaces[$typeHintLc]) === true) { |
|
242 | $itemInfo = array( |
||
243 | 5 | 'name' => $hint, |
|
244 | 5 | 'nameLc' => $typeHintLc, |
|
245 | ); |
||
246 | 5 | $this->handleFeature($phpcsFile, $stackPtr, $itemInfo); |
|
247 | } |
||
248 | } |
||
249 | 5 | } |
|
250 | |||
251 | |||
252 | /** |
||
253 | * Get the relevant sub-array for a specific item from a multi-dimensional array. |
||
254 | * |
||
255 | * @param array $itemInfo Base information about the item. |
||
256 | * |
||
257 | * @return array Version and other information about the item. |
||
258 | */ |
||
259 | 5 | public function getItemArray(array $itemInfo) |
|
263 | |||
264 | |||
265 | /** |
||
266 | * Get the error message template for this sniff. |
||
267 | * |
||
268 | * @return string |
||
269 | */ |
||
270 | 5 | protected function getErrorMsgTemplate() |
|
274 | |||
275 | |||
276 | }//end class |
||
277 |
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.