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_Sniff |
||
|
|||
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) |
||
130 | |||
131 | |||
132 | /** |
||
133 | * Generates a error or warning for this sniff. |
||
134 | * |
||
135 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
136 | * @param int $stackPtr The position of the declare statement |
||
137 | * in the token array. |
||
138 | * @param string $directive The directive. |
||
139 | * |
||
140 | * @return void |
||
141 | */ |
||
142 | protected function maybeAddError($phpcsFile, $stackPtr, $directive) |
||
184 | |||
185 | |||
186 | /** |
||
187 | * Generates a error or warning for this sniff. |
||
188 | * |
||
189 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
190 | * @param int $stackPtr The position of the execution directive value |
||
191 | * in the token array. |
||
192 | * @param string $directive The directive. |
||
193 | * |
||
194 | * @return void |
||
195 | */ |
||
196 | protected function addWarningOnInvalidValue($phpcsFile, $stackPtr, $directive) |
||
197 | { |
||
198 | $tokens = $phpcsFile->getTokens(); |
||
199 | |||
200 | $value = $tokens[$stackPtr]['content']; |
||
201 | if ($tokens[$stackPtr]['code'] === T_CONSTANT_ENCAPSED_STRING) { |
||
202 | $value = $this->stripQuotes($value); |
||
203 | } |
||
204 | |||
205 | $isError = false; |
||
206 | if (isset($this->newDirectives[$directive]['valid_values'])) { |
||
207 | if (in_array($value, $this->newDirectives[$directive]['valid_values']) === false) { |
||
208 | $isError = true; |
||
209 | } |
||
210 | } |
||
211 | else if (isset($this->newDirectives[$directive]['valid_value_callback'])) { |
||
212 | $valid = call_user_func(array($this, $this->newDirectives[$directive]['valid_value_callback']), $value); |
||
213 | if ($valid === false) { |
||
214 | $isError = true; |
||
215 | } |
||
216 | } |
||
217 | |||
218 | if ($isError === true) { |
||
219 | $error = 'The execution directive %s does not seem to have a valid value. Please review. Found: %s'; |
||
220 | $data = array( |
||
221 | $directive, |
||
222 | $value, |
||
223 | ); |
||
224 | $phpcsFile->addWarning($error, $stackPtr, 'InvalidDirectiveValueFound', $data); |
||
225 | } |
||
226 | }// addErrorOnInvalidValue() |
||
227 | |||
228 | |||
229 | /** |
||
230 | * Check whether a value is numeric. |
||
231 | * |
||
232 | * Callback function to test whether the value for an execution directive is valid. |
||
233 | * |
||
234 | * @param mixed $value The value to test. |
||
235 | * |
||
236 | * @return bool |
||
237 | */ |
||
238 | protected function isNumeric($value) |
||
242 | |||
243 | |||
244 | /** |
||
245 | * Check whether a value is valid encoding. |
||
246 | * |
||
247 | * Callback function to test whether the value for an execution directive is valid. |
||
248 | * |
||
249 | * @param mixed $value The value to test. |
||
250 | * |
||
251 | * @return bool |
||
252 | */ |
||
253 | protected function validEncoding($value) |
||
272 | |||
273 | }//end class |
||
274 |
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.