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 PHPCompatibility_Sniffs_PHP_NewKeywordsSniff extends PHPCompatibility_Sniff |
||
| 23 | { |
||
| 24 | |||
| 25 | /** |
||
| 26 | * A list of new keywords, not present in older versions. |
||
| 27 | * |
||
| 28 | * The array lists : version number with false (not present) or true (present). |
||
| 29 | * If's sufficient to list the first version where the keyword appears. |
||
| 30 | * |
||
| 31 | * @var array(string => array(string => int|string|null)) |
||
| 32 | */ |
||
| 33 | protected $newKeywords = array( |
||
| 34 | 'T_HALT_COMPILER' => array( |
||
| 35 | '5.0' => false, |
||
| 36 | '5.1' => true, |
||
| 37 | 'description' => '"__halt_compiler" keyword' |
||
| 38 | ), |
||
| 39 | 'T_CONST' => array( |
||
| 40 | '5.2' => false, |
||
| 41 | '5.3' => true, |
||
| 42 | 'description' => '"const" keyword' |
||
| 43 | ), |
||
| 44 | 'T_CALLABLE' => array( |
||
| 45 | '5.3' => false, |
||
| 46 | '5.4' => true, |
||
| 47 | 'description' => '"callable" keyword' |
||
| 48 | ), |
||
| 49 | 'T_DIR' => array( |
||
| 50 | '5.2' => false, |
||
| 51 | '5.3' => true, |
||
| 52 | 'description' => '__DIR__ magic constant' |
||
| 53 | ), |
||
| 54 | 'T_GOTO' => array( |
||
| 55 | '5.2' => false, |
||
| 56 | '5.3' => true, |
||
| 57 | 'description' => '"goto" keyword' |
||
| 58 | ), |
||
| 59 | 'T_INSTEADOF' => array( |
||
| 60 | '5.3' => false, |
||
| 61 | '5.4' => true, |
||
| 62 | 'description' => '"insteadof" keyword (for traits)' |
||
| 63 | ), |
||
| 64 | 'T_NAMESPACE' => array( |
||
| 65 | '5.2' => false, |
||
| 66 | '5.3' => true, |
||
| 67 | 'description' => '"namespace" keyword' |
||
| 68 | ), |
||
| 69 | 'T_NS_C' => array( |
||
| 70 | '5.2' => false, |
||
| 71 | '5.3' => true, |
||
| 72 | 'description' => '__NAMESPACE__ magic constant' |
||
| 73 | ), |
||
| 74 | 'T_USE' => array( |
||
| 75 | '5.2' => false, |
||
| 76 | '5.3' => true, |
||
| 77 | 'description' => '"use" keyword (for traits/namespaces/anonymous functions)' |
||
| 78 | ), |
||
| 79 | 'T_TRAIT' => array( |
||
| 80 | '5.3' => false, |
||
| 81 | '5.4' => true, |
||
| 82 | 'description' => '"trait" keyword' |
||
| 83 | ), |
||
| 84 | 'T_TRAIT_C' => array( |
||
| 85 | '5.3' => false, |
||
| 86 | '5.4' => true, |
||
| 87 | 'description' => '__TRAIT__ magic constant' |
||
| 88 | ), |
||
| 89 | 'T_YIELD' => array( |
||
| 90 | '5.4' => false, |
||
| 91 | '5.5' => true, |
||
| 92 | 'description' => '"yield" keyword (for generators)' |
||
| 93 | ), |
||
| 94 | 'T_FINALLY' => array( |
||
| 95 | '5.4' => false, |
||
| 96 | '5.5' => true, |
||
| 97 | 'description' => '"finally" keyword (in exception handling)' |
||
| 98 | ), |
||
| 99 | 'T_START_NOWDOC' => array( |
||
| 100 | '5.2' => false, |
||
| 101 | '5.3' => true, |
||
| 102 | 'description' => 'nowdoc functionality' |
||
| 103 | ), |
||
| 104 | 'T_END_NOWDOC' => array( |
||
| 105 | '5.2' => false, |
||
| 106 | '5.3' => true, |
||
| 107 | 'description' => 'nowdoc functionality' |
||
| 108 | ), |
||
| 109 | ); |
||
| 110 | |||
| 111 | |||
| 112 | /** |
||
| 113 | * Returns an array of tokens this test wants to listen for. |
||
| 114 | * |
||
| 115 | * @return array |
||
| 116 | */ |
||
| 117 | View Code Duplication | public function register() |
|
| 128 | |||
| 129 | |||
| 130 | /** |
||
| 131 | * Processes this test, when one of its tokens is encountered. |
||
| 132 | * |
||
| 133 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 134 | * @param int $stackPtr The position of the current token in |
||
| 135 | * the stack passed in $tokens. |
||
| 136 | * |
||
| 137 | * @return void |
||
| 138 | */ |
||
| 139 | public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 160 | |||
| 161 | |||
| 162 | /** |
||
| 163 | * Generates the error or wanrning for this sniff. |
||
| 164 | * |
||
| 165 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 166 | * @param int $stackPtr The position of the function |
||
| 167 | * in the token array. |
||
| 168 | * @param string $function The name of the function. |
||
| 169 | * @param string $pattern The pattern used for the match. |
||
| 170 | * |
||
| 171 | * @return void |
||
| 172 | */ |
||
| 173 | View Code Duplication | protected function addError($phpcsFile, $stackPtr, $keywordName, $pattern=null) |
|
| 200 | |||
| 201 | }//end class |
||
| 202 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.