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_NewExecutionDirectivesSniff extends PHPCompatibility_AbstractNewFeatureSniff |
||
|
|||
18 | { |
||
19 | |||
20 | /** |
||
21 | * A list of new execution directives |
||
22 | * |
||
23 | * The array lists : version number with false (not present) or true (present). |
||
24 | * If the execution order is conditional, add the condition as a string to the version nr. |
||
25 | * If's sufficient to list the first version where the execution directive appears. |
||
26 | * |
||
27 | * @var array(string => array(string => int|string|null)) |
||
28 | */ |
||
29 | protected $newDirectives = array ( |
||
30 | 'ticks' => array( |
||
31 | '3.1' => false, |
||
32 | '4.0' => true, |
||
33 | 'valid_value_callback' => 'isNumeric', |
||
34 | ), |
||
35 | 'encoding' => array( |
||
36 | '5.2' => false, |
||
37 | '5.3' => '--enable-zend-multibyte', // Directive ignored unless. |
||
38 | '5.4' => true, |
||
39 | 'valid_value_callback' => 'validEncoding', |
||
40 | ), |
||
41 | 'strict_types' => array( |
||
42 | '5.6' => false, |
||
43 | '7.0' => true, |
||
44 | 'valid_values' => array(1), |
||
45 | ), |
||
46 | ); |
||
47 | |||
48 | |||
49 | /** |
||
50 | * Tokens to ignore when trying to find the value for the directive. |
||
51 | * |
||
52 | * @var array |
||
53 | */ |
||
54 | protected $ignoreTokens = array(); |
||
55 | |||
56 | |||
57 | /** |
||
58 | * Returns an array of tokens this test wants to listen for. |
||
59 | * |
||
60 | * @return array |
||
61 | */ |
||
62 | public function register() |
||
69 | |||
70 | |||
71 | /** |
||
72 | * Processes this test, when one of its tokens is encountered. |
||
73 | * |
||
74 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
75 | * @param int $stackPtr The position of the current token in |
||
76 | * the stack passed in $tokens. |
||
77 | * |
||
78 | * @return void |
||
79 | */ |
||
80 | public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
134 | |||
135 | |||
136 | /** |
||
137 | * Determine whether an error/warning should be thrown for an item based on collected information. |
||
138 | * |
||
139 | * @param array $errorInfo Detail information about an item. |
||
140 | * |
||
141 | * @return bool |
||
142 | */ |
||
143 | protected function shouldThrowError(array $errorInfo) |
||
147 | |||
148 | |||
149 | /** |
||
150 | * Get the relevant sub-array for a specific item from a multi-dimensional array. |
||
151 | * |
||
152 | * @param array $itemInfo Base information about the item. |
||
153 | * |
||
154 | * @return array Version and other information about the item. |
||
155 | */ |
||
156 | public function getItemArray(array $itemInfo) |
||
160 | |||
161 | |||
162 | /** |
||
163 | * Get an array of the non-PHP-version array keys used in a sub-array. |
||
164 | * |
||
165 | * @return array |
||
166 | */ |
||
167 | protected function getNonVersionArrayKeys() |
||
174 | |||
175 | |||
176 | /** |
||
177 | * Retrieve the relevant detail (version) information for use in an error message. |
||
178 | * |
||
179 | * @param array $itemArray Version and other information about the item. |
||
180 | * @param array $itemInfo Base information about the item. |
||
181 | * |
||
182 | * @return array |
||
183 | */ |
||
184 | public function getErrorInfo(array $itemArray, array $itemInfo) |
||
204 | |||
205 | |||
206 | /** |
||
207 | * Get the error message template for this sniff. |
||
208 | * |
||
209 | * @return string |
||
210 | */ |
||
211 | protected function getErrorMsgTemplate() |
||
215 | |||
216 | |||
217 | /** |
||
218 | * Generates the error or warning for this item. |
||
219 | * |
||
220 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
221 | * @param int $stackPtr The position of the relevant token in |
||
222 | * the stack. |
||
223 | * @param array $itemInfo Base information about the item. |
||
224 | * @param array $errorInfo Array with detail (version) information |
||
225 | * relevant to the item. |
||
226 | * |
||
227 | * @return void |
||
228 | */ |
||
229 | public function addError(PHP_CodeSniffer_File $phpcsFile, $stackPtr, array $itemInfo, array $errorInfo) |
||
246 | |||
247 | |||
248 | /** |
||
249 | * Generates a error or warning for this sniff. |
||
250 | * |
||
251 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
252 | * @param int $stackPtr The position of the execution directive value |
||
253 | * in the token array. |
||
254 | * @param string $directive The directive. |
||
255 | * |
||
256 | * @return void |
||
257 | */ |
||
258 | protected function addWarningOnInvalidValue($phpcsFile, $stackPtr, $directive) |
||
291 | |||
292 | |||
293 | /** |
||
294 | * Check whether a value is numeric. |
||
295 | * |
||
296 | * Callback function to test whether the value for an execution directive is valid. |
||
297 | * |
||
298 | * @param mixed $value The value to test. |
||
299 | * |
||
300 | * @return bool |
||
301 | */ |
||
302 | protected function isNumeric($value) |
||
306 | |||
307 | |||
308 | /** |
||
309 | * Check whether a value is valid encoding. |
||
310 | * |
||
311 | * Callback function to test whether the value for an execution directive is valid. |
||
312 | * |
||
313 | * @param mixed $value The value to test. |
||
314 | * |
||
315 | * @return bool |
||
316 | */ |
||
317 | protected function validEncoding($value) |
||
336 | |||
337 | |||
338 | }//end class |
||
339 |
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.