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:
Complex classes like PHPCompatibility_Sniff often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use PHPCompatibility_Sniff, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | abstract class PHPCompatibility_Sniff implements PHP_CodeSniffer_Sniff |
||
|
|||
23 | { |
||
24 | |||
25 | /** |
||
26 | * List of functions using hash algorithm as parameter (always the first parameter). |
||
27 | * |
||
28 | * Used by the new/removed hash algorithm sniffs. |
||
29 | * Key is the function name, value is the 1-based parameter position in the function call. |
||
30 | * |
||
31 | * @var array |
||
32 | */ |
||
33 | protected $hashAlgoFunctions = array( |
||
34 | 'hash_file' => 1, |
||
35 | 'hash_hmac_file' => 1, |
||
36 | 'hash_hmac' => 1, |
||
37 | 'hash_init' => 1, |
||
38 | 'hash_pbkdf2' => 1, |
||
39 | 'hash' => 1, |
||
40 | ); |
||
41 | |||
42 | |||
43 | /** |
||
44 | * List of functions which take an ini directive as parameter (always the first parameter). |
||
45 | * |
||
46 | * Used by the new/removed ini directives sniffs. |
||
47 | * Key is the function name, value is the 1-based parameter position in the function call. |
||
48 | * |
||
49 | * @var array |
||
50 | */ |
||
51 | protected $iniFunctions = array( |
||
52 | 'ini_get' => 1, |
||
53 | 'ini_set' => 1, |
||
54 | ); |
||
55 | |||
56 | |||
57 | /* The testVersion configuration variable may be in any of the following formats: |
||
58 | * 1) Omitted/empty, in which case no version is specified. This effectively |
||
59 | * disables all the checks provided by this standard. |
||
60 | * 2) A single PHP version number, e.g. "5.4" in which case the standard checks that |
||
61 | * the code will run on that version of PHP (no deprecated features or newer |
||
62 | * features being used). |
||
63 | * 3) A range, e.g. "5.0-5.5", in which case the standard checks the code will run |
||
64 | * on all PHP versions in that range, and that it doesn't use any features that |
||
65 | * were deprecated by the final version in the list, or which were not available |
||
66 | * for the first version in the list. |
||
67 | * PHP version numbers should always be in Major.Minor format. Both "5", "5.3.2" |
||
68 | * would be treated as invalid, and ignored. |
||
69 | * This standard doesn't support checking against PHP4, so the minimum version that |
||
70 | * is recognised is "5.0". |
||
71 | */ |
||
72 | |||
73 | private function getTestVersion() |
||
114 | |||
115 | View Code Duplication | public function supportsAbove($phpVersion) |
|
128 | |||
129 | View Code Duplication | public function supportsBelow($phpVersion) |
|
142 | |||
143 | |||
144 | /** |
||
145 | * Add a PHPCS message to the output stack as either a warning or an error. |
||
146 | * |
||
147 | * @param PHP_CodeSniffer_File $phpcsFile The file the message applies to. |
||
148 | * @param string $message The message. |
||
149 | * @param int $stackPtr The position of the token |
||
150 | * the message relates to. |
||
151 | * @param bool $isError Whether to report the message as an |
||
152 | * 'error' or 'warning'. |
||
153 | * Defaults to true (error). |
||
154 | * @param string $code The error code for the message. |
||
155 | * Defaults to 'Found'. |
||
156 | * @param array $data Optional input for the data replacements. |
||
157 | * |
||
158 | * @return void |
||
159 | */ |
||
160 | public function addMessage($phpcsFile, $message, $stackPtr, $isError, $code = 'Found', $data = array()) |
||
168 | |||
169 | |||
170 | /** |
||
171 | * Convert an arbitrary string to an alphanumeric string with underscores. |
||
172 | * |
||
173 | * Pre-empt issues with arbitrary strings being used as error codes in XML and PHP. |
||
174 | * |
||
175 | * @param string $baseString Arbitrary string. |
||
176 | * |
||
177 | * @return string |
||
178 | */ |
||
179 | public function stringToErrorCode($baseString) |
||
183 | |||
184 | |||
185 | /** |
||
186 | * Strip quotes surrounding an arbitrary string. |
||
187 | * |
||
188 | * Intended for use with the content of a T_CONSTANT_ENCAPSED_STRING. |
||
189 | * |
||
190 | * @param string $string The raw string. |
||
191 | * |
||
192 | * @return string String without quotes around it. |
||
193 | */ |
||
194 | public function stripQuotes($string) { |
||
197 | |||
198 | |||
199 | /** |
||
200 | * Make all top level array keys in an array lowercase. |
||
201 | * |
||
202 | * @param array $array Initial array. |
||
203 | * |
||
204 | * @return array Same array, but with all lowercase top level keys. |
||
205 | */ |
||
206 | public function arrayKeysToLowercase($array) |
||
212 | |||
213 | |||
214 | /** |
||
215 | * Returns the name(s) of the interface(s) that the specified class implements. |
||
216 | * |
||
217 | * Returns FALSE on error or if there are no implemented interface names. |
||
218 | * |
||
219 | * {@internal Duplicate of same method as introduced in PHPCS 2.7. |
||
220 | * Once the minimum supported PHPCS version for this sniff library goes beyond |
||
221 | * that, this method can be removed and call to it replaced with |
||
222 | * `$phpcsFile->findImplementedInterfaceNames($stackPtr)` calls.}} |
||
223 | * |
||
224 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
225 | * @param int $stackPtr The position of the class token. |
||
226 | * |
||
227 | * @return array|false |
||
228 | */ |
||
229 | public function findImplementedInterfaceNames(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
276 | |||
277 | |||
278 | /** |
||
279 | * Checks if a function call has parameters. |
||
280 | * |
||
281 | * Expects to be passed the T_STRING stack pointer for the function call. |
||
282 | * If passed a T_STRING which is *not* a function call, the behaviour is unreliable. |
||
283 | * |
||
284 | * @link https://github.com/wimg/PHPCompatibility/issues/120 |
||
285 | * @link https://github.com/wimg/PHPCompatibility/issues/152 |
||
286 | * |
||
287 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
288 | * @param int $stackPtr The position of the function call token. |
||
289 | * |
||
290 | * @return bool |
||
291 | */ |
||
292 | public function doesFunctionCallHaveParameters(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
325 | |||
326 | |||
327 | /** |
||
328 | * Count the number of parameters a function call has been passed. |
||
329 | * |
||
330 | * Expects to be passed the T_STRING stack pointer for the function call. |
||
331 | * If passed a T_STRING which is *not* a function call, the behaviour is unreliable. |
||
332 | * |
||
333 | * @link https://github.com/wimg/PHPCompatibility/issues/111 |
||
334 | * @link https://github.com/wimg/PHPCompatibility/issues/114 |
||
335 | * @link https://github.com/wimg/PHPCompatibility/issues/151 |
||
336 | * |
||
337 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
338 | * @param int $stackPtr The position of the function call token. |
||
339 | * |
||
340 | * @return int |
||
341 | */ |
||
342 | public function getFunctionCallParameterCount(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
350 | |||
351 | |||
352 | /** |
||
353 | * Get information on all parameters passed to a function call. |
||
354 | * |
||
355 | * Expects to be passed the T_STRING stack pointer for the function call. |
||
356 | * If passed a T_STRING which is *not* a function call, the behaviour is unreliable. |
||
357 | * |
||
358 | * Will return an multi-dimentional array with the start token pointer, end token |
||
359 | * pointer and raw parameter value for all parameters. Index will be 1-based. |
||
360 | * If no parameters are found, will return an empty array. |
||
361 | * |
||
362 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
363 | * @param int $stackPtr The position of the function call token. |
||
364 | * |
||
365 | * @return array |
||
366 | */ |
||
367 | public function getFunctionCallParameters(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
439 | |||
440 | |||
441 | /** |
||
442 | * Get information on a specific parameter passed to a function call. |
||
443 | * |
||
444 | * Expects to be passed the T_STRING stack pointer for the function call. |
||
445 | * If passed a T_STRING which is *not* a function call, the behaviour is unreliable. |
||
446 | * |
||
447 | * Will return a array with the start token pointer, end token pointer and the raw value |
||
448 | * of the parameter at a specific offset. |
||
449 | * If the specified parameter is not found, will return false. |
||
450 | * |
||
451 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
452 | * @param int $stackPtr The position of the function call token. |
||
453 | * @param int $paramOffset The 1-based index position of the parameter to retrieve. |
||
454 | * |
||
455 | * @return array|false |
||
456 | */ |
||
457 | public function getFunctionCallParameter(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $paramOffset) |
||
468 | |||
469 | |||
470 | /** |
||
471 | * Verify whether a token is within a scoped condition. |
||
472 | * |
||
473 | * If the optional $validScopes parameter has been passed, the function |
||
474 | * will check that the token has at least one condition which is of a |
||
475 | * type defined in $validScopes. |
||
476 | * |
||
477 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
478 | * @param int $stackPtr The position of the token. |
||
479 | * @param array|int $validScopes Optional. Array of valid scopes |
||
480 | * or int value of a valid scope. |
||
481 | * Pass the T_.. constant(s) for the |
||
482 | * desired scope to this parameter. |
||
483 | * |
||
484 | * @return bool Without the optional $scopeTypes: True if within a scope, false otherwise. |
||
485 | * If the $scopeTypes are set: True if *one* of the conditions is a |
||
486 | * valid scope, false otherwise. |
||
487 | */ |
||
488 | public function tokenHasScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $validScopes = null) |
||
509 | |||
510 | |||
511 | /** |
||
512 | * Verify whether a token is within a class scope. |
||
513 | * |
||
514 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
515 | * @param int $stackPtr The position of the token. |
||
516 | * @param bool $strict Whether to strictly check for the T_CLASS |
||
517 | * scope or also accept interfaces and traits |
||
518 | * as scope. |
||
519 | * |
||
520 | * @return bool True if within class scope, false otherwise. |
||
521 | */ |
||
522 | public function inClassScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $strict = true) |
||
536 | |||
537 | |||
538 | /** |
||
539 | * Verify whether a token is within a scoped use statement. |
||
540 | * |
||
541 | * PHPCS cross-version compatibility method. |
||
542 | * |
||
543 | * In PHPCS 1.x no conditions are set for a scoped use statement. |
||
544 | * This method works around that limitation. |
||
545 | * |
||
546 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
547 | * @param int $stackPtr The position of the token. |
||
548 | * |
||
549 | * @return bool True if within use scope, false otherwise. |
||
550 | */ |
||
551 | public function inUseScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
583 | |||
584 | |||
585 | /** |
||
586 | * Returns the fully qualified class name for a new class instantiation. |
||
587 | * |
||
588 | * Returns an empty string if the class name could not be reliably inferred. |
||
589 | * |
||
590 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
591 | * @param int $stackPtr The position of a T_NEW token. |
||
592 | * |
||
593 | * @return string |
||
594 | */ |
||
595 | public function getFQClassNameFromNewToken(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
627 | |||
628 | |||
629 | /** |
||
630 | * Returns the fully qualified name of the class that the specified class extends. |
||
631 | * |
||
632 | * Returns an empty string if the class does not extend another class or if |
||
633 | * the class name could not be reliably inferred. |
||
634 | * |
||
635 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
636 | * @param int $stackPtr The position of a T_CLASS token. |
||
637 | * |
||
638 | * @return string |
||
639 | */ |
||
640 | public function getFQExtendedClassName(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
660 | |||
661 | |||
662 | /** |
||
663 | * Returns the class name for the static usage of a class. |
||
664 | * This can be a call to a method, the use of a property or constant. |
||
665 | * |
||
666 | * Returns an empty string if the class name could not be reliably inferred. |
||
667 | * |
||
668 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
669 | * @param int $stackPtr The position of a T_NEW token. |
||
670 | * |
||
671 | * @return string |
||
672 | */ |
||
673 | public function getFQClassNameFromDoubleColonToken(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
719 | |||
720 | |||
721 | /** |
||
722 | * Get the Fully Qualified name for a class/function/constant etc. |
||
723 | * |
||
724 | * Checks if a class/function/constant name is already fully qualified and |
||
725 | * if not, enrich it with the relevant namespace information. |
||
726 | * |
||
727 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
728 | * @param int $stackPtr The position of the token. |
||
729 | * @param string $name The class / function / constant name. |
||
730 | * |
||
731 | * @return string |
||
732 | */ |
||
733 | public function getFQName(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $name) |
||
754 | |||
755 | |||
756 | /** |
||
757 | * Is the class/function/constant name namespaced or global ? |
||
758 | * |
||
759 | * @param string $FQName Fully Qualified name of a class, function etc. |
||
760 | * I.e. should always start with a `\` ! |
||
761 | * |
||
762 | * @return bool True if namespaced, false if global. |
||
763 | */ |
||
764 | public function isNamespaced($FQName) { |
||
771 | |||
772 | |||
773 | /** |
||
774 | * Determine the namespace name an arbitrary token lives in. |
||
775 | * |
||
776 | * @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
||
777 | * @param int $stackPtr The token position for which to determine the namespace. |
||
778 | * |
||
779 | * @return string Namespace name or empty string if it couldn't be determined or no namespace applies. |
||
780 | */ |
||
781 | public function determineNamespace(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
831 | |||
832 | /** |
||
833 | * Get the complete namespace name for a namespace declaration. |
||
834 | * |
||
835 | * For hierarchical namespaces, the name will be composed of several tokens, |
||
836 | * i.e. MyProject\Sub\Level which will be returned together as one string. |
||
837 | * |
||
838 | * @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
||
839 | * @param int|bool $stackPtr The position of a T_NAMESPACE token. |
||
840 | * |
||
841 | * @return string|false Namespace name or false if not a namespace declaration. |
||
842 | * Namespace name can be an empty string for global namespace declaration. |
||
843 | */ |
||
844 | public function getDeclaredNamespaceName(PHP_CodeSniffer_File $phpcsFile, $stackPtr ) |
||
884 | |||
885 | |||
886 | /** |
||
887 | * Returns the method parameters for the specified T_FUNCTION token. |
||
888 | * |
||
889 | * Each parameter is in the following format: |
||
890 | * |
||
891 | * <code> |
||
892 | * 0 => array( |
||
893 | * 'name' => '$var', // The variable name. |
||
894 | * 'pass_by_reference' => false, // Passed by reference. |
||
895 | * 'type_hint' => string, // Type hint for array or custom type |
||
896 | * 'nullable_type' => bool, // Whether the type given in the type hint is nullable |
||
897 | * 'type_hint' => string, // Type hint for array or custom type |
||
898 | * 'raw' => string, // Raw content of the tokens for the parameter |
||
899 | * ) |
||
900 | * </code> |
||
901 | * |
||
902 | * Parameters with default values have an additional array index of |
||
903 | * 'default' with the value of the default as a string. |
||
904 | * |
||
905 | * {@internal Duplicate of same method as contained in the `PHP_CodeSniffer_File` |
||
906 | * class, but with some improvements which will probably be introduced in |
||
907 | * PHPCS 2.7.1/2.8. {@see https://github.com/squizlabs/PHP_CodeSniffer/pull/1117} |
||
908 | * and {@see https://github.com/squizlabs/PHP_CodeSniffer/pull/1193} |
||
909 | * |
||
910 | * Once the minimum supported PHPCS version for this sniff library goes beyond |
||
911 | * that, this method can be removed and calls to it replaced with |
||
912 | * `$phpcsFile->getMethodParameters($stackPtr)` calls. |
||
913 | * |
||
914 | * Last synced with PHPCS version: PHPCS 2.7.}} |
||
915 | * |
||
916 | * @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
||
917 | * @param int $stackPtr The position in the stack of the |
||
918 | * T_FUNCTION token to acquire the |
||
919 | * parameters for. |
||
920 | * |
||
921 | * @return array|false |
||
922 | * @throws PHP_CodeSniffer_Exception If the specified $stackPtr is not of |
||
923 | * type T_FUNCTION. |
||
924 | */ |
||
925 | public function getMethodParameters(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
1079 | |||
1080 | |||
1081 | /** |
||
1082 | * Get the hash algorithm name from the parameter in a hash function call. |
||
1083 | * |
||
1084 | * @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
||
1085 | * @param int $stackPtr The position of the T_STRING function token. |
||
1086 | * |
||
1087 | * @return string|false The algorithm name without quotes if this was a relevant hash |
||
1088 | * function call or false if it was not. |
||
1089 | */ |
||
1090 | public function getHashAlgorithmParameter(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
1125 | |||
1126 | }//end class |
||
1127 |
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.