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 |
||
22 | class NewExecutionDirectivesSniff extends AbstractNewFeatureSniff |
||
23 | { |
||
24 | |||
25 | /** |
||
26 | * A list of new execution directives |
||
27 | * |
||
28 | * The array lists : version number with false (not present) or true (present). |
||
29 | * If the execution order is conditional, add the condition as a string to the version nr. |
||
30 | * If's sufficient to list the first version where the execution directive appears. |
||
31 | * |
||
32 | * @var array(string => array(string => int|string|null)) |
||
33 | */ |
||
34 | protected $newDirectives = array( |
||
35 | 'ticks' => array( |
||
36 | '3.1' => false, |
||
37 | '4.0' => true, |
||
38 | 'valid_value_callback' => 'isNumeric', |
||
39 | ), |
||
40 | 'encoding' => array( |
||
41 | '5.2' => false, |
||
42 | '5.3' => '--enable-zend-multibyte', // Directive ignored unless. |
||
43 | '5.4' => true, |
||
44 | 'valid_value_callback' => 'validEncoding', |
||
45 | ), |
||
46 | 'strict_types' => array( |
||
47 | '5.6' => false, |
||
48 | '7.0' => true, |
||
49 | 'valid_values' => array(1), |
||
50 | ), |
||
51 | ); |
||
52 | |||
53 | |||
54 | /** |
||
55 | * Tokens to ignore when trying to find the value for the directive. |
||
56 | * |
||
57 | * @var array |
||
58 | */ |
||
59 | protected $ignoreTokens = array(); |
||
60 | |||
61 | |||
62 | /** |
||
63 | * Returns an array of tokens this test wants to listen for. |
||
64 | * |
||
65 | * @return array |
||
66 | */ |
||
67 | public function register() |
||
74 | |||
75 | |||
76 | /** |
||
77 | * Processes this test, when one of its tokens is encountered. |
||
78 | * |
||
79 | * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
80 | * @param int $stackPtr The position of the current token in |
||
81 | * the stack passed in $tokens. |
||
82 | * |
||
83 | * @return void |
||
84 | */ |
||
85 | public function process(\PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
138 | |||
139 | |||
140 | /** |
||
141 | * Determine whether an error/warning should be thrown for an item based on collected information. |
||
142 | * |
||
143 | * @param array $errorInfo Detail information about an item. |
||
144 | * |
||
145 | * @return bool |
||
146 | */ |
||
147 | protected function shouldThrowError(array $errorInfo) |
||
151 | |||
152 | |||
153 | /** |
||
154 | * Get the relevant sub-array for a specific item from a multi-dimensional array. |
||
155 | * |
||
156 | * @param array $itemInfo Base information about the item. |
||
157 | * |
||
158 | * @return array Version and other information about the item. |
||
159 | */ |
||
160 | public function getItemArray(array $itemInfo) |
||
164 | |||
165 | |||
166 | /** |
||
167 | * Get an array of the non-PHP-version array keys used in a sub-array. |
||
168 | * |
||
169 | * @return array |
||
170 | */ |
||
171 | protected function getNonVersionArrayKeys() |
||
178 | |||
179 | |||
180 | /** |
||
181 | * Retrieve the relevant detail (version) information for use in an error message. |
||
182 | * |
||
183 | * @param array $itemArray Version and other information about the item. |
||
184 | * @param array $itemInfo Base information about the item. |
||
185 | * |
||
186 | * @return array |
||
187 | */ |
||
188 | public function getErrorInfo(array $itemArray, array $itemInfo) |
||
208 | |||
209 | |||
210 | /** |
||
211 | * Get the error message template for this sniff. |
||
212 | * |
||
213 | * @return string |
||
214 | */ |
||
215 | protected function getErrorMsgTemplate() |
||
219 | |||
220 | |||
221 | /** |
||
222 | * Generates the error or warning for this item. |
||
223 | * |
||
224 | * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
225 | * @param int $stackPtr The position of the relevant token in |
||
226 | * the stack. |
||
227 | * @param array $itemInfo Base information about the item. |
||
228 | * @param array $errorInfo Array with detail (version) information |
||
229 | * relevant to the item. |
||
230 | * |
||
231 | * @return void |
||
232 | */ |
||
233 | public function addError(\PHP_CodeSniffer_File $phpcsFile, $stackPtr, array $itemInfo, array $errorInfo) |
||
250 | |||
251 | |||
252 | /** |
||
253 | * Generates a error or warning for this sniff. |
||
254 | * |
||
255 | * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
256 | * @param int $stackPtr The position of the execution directive value |
||
257 | * in the token array. |
||
258 | * @param string $directive The directive. |
||
259 | * |
||
260 | * @return void |
||
261 | */ |
||
262 | protected function addWarningOnInvalidValue(\PHP_CodeSniffer_File $phpcsFile, $stackPtr, $directive) |
||
294 | |||
295 | |||
296 | /** |
||
297 | * Check whether a value is numeric. |
||
298 | * |
||
299 | * Callback function to test whether the value for an execution directive is valid. |
||
300 | * |
||
301 | * @param mixed $value The value to test. |
||
302 | * |
||
303 | * @return bool |
||
304 | */ |
||
305 | protected function isNumeric($value) |
||
309 | |||
310 | |||
311 | /** |
||
312 | * Check whether a value is a valid encoding. |
||
313 | * |
||
314 | * Callback function to test whether the value for an execution directive is valid. |
||
315 | * |
||
316 | * @param mixed $value The value to test. |
||
317 | * |
||
318 | * @return bool |
||
319 | */ |
||
320 | protected function validEncoding($value) |
||
334 | |||
335 | |||
336 | }//end class |
||
337 |