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 |
||
| 21 | class PHPCompatibility_Sniffs_PHP_DeprecatedIniDirectivesSniff extends PHPCompatibility_AbstractRemovedFeatureSniff |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * A list of deprecated INI directives. |
||
| 25 | * |
||
| 26 | * The array lists : version number with false (deprecated) and true (removed). |
||
| 27 | * If's sufficient to list the first version where the ini directive was deprecated/removed. |
||
| 28 | * |
||
| 29 | * @var array(string) |
||
| 30 | */ |
||
| 31 | protected $deprecatedIniDirectives = array( |
||
| 32 | 'fbsql.batchSize' => array( |
||
| 33 | '5.1' => true, |
||
| 34 | 'alternative' => 'fbsql.batchsize', |
||
| 35 | ), |
||
| 36 | |||
| 37 | 'ifx.allow_persistent' => array( |
||
| 38 | '5.2.1' => true, |
||
| 39 | ), |
||
| 40 | 'ifx.blobinfile' => array( |
||
| 41 | '5.2.1' => true, |
||
| 42 | ), |
||
| 43 | 'ifx.byteasvarchar' => array( |
||
| 44 | '5.2.1' => true, |
||
| 45 | ), |
||
| 46 | 'ifx.charasvarchar' => array( |
||
| 47 | '5.2.1' => true, |
||
| 48 | ), |
||
| 49 | 'ifx.default_host' => array( |
||
| 50 | '5.2.1' => true, |
||
| 51 | ), |
||
| 52 | 'ifx.default_password' => array( |
||
| 53 | '5.2.1' => true, |
||
| 54 | ), |
||
| 55 | 'ifx.default_user' => array( |
||
| 56 | '5.2.1' => true, |
||
| 57 | ), |
||
| 58 | 'ifx.max_links' => array( |
||
| 59 | '5.2.1' => true, |
||
| 60 | ), |
||
| 61 | 'ifx.max_persistent' => array( |
||
| 62 | '5.2.1' => true, |
||
| 63 | ), |
||
| 64 | 'ifx.nullformat' => array( |
||
| 65 | '5.2.1' => true, |
||
| 66 | ), |
||
| 67 | 'ifx.textasvarchar' => array( |
||
| 68 | '5.2.1' => true, |
||
| 69 | ), |
||
| 70 | |||
| 71 | 'zend.ze1_compatibility_mode' => array( |
||
| 72 | '5.3' => true, |
||
| 73 | ), |
||
| 74 | |||
| 75 | 'allow_call_time_pass_reference' => array( |
||
| 76 | '5.3' => false, |
||
| 77 | '5.4' => true, |
||
| 78 | ), |
||
| 79 | 'define_syslog_variables' => array( |
||
| 80 | '5.3' => false, |
||
| 81 | '5.4' => true, |
||
| 82 | ), |
||
| 83 | 'detect_unicode' => array( |
||
| 84 | '5.4' => true, |
||
| 85 | 'alternative' => 'zend.detect_unicode', |
||
| 86 | ), |
||
| 87 | 'highlight.bg' => array( |
||
| 88 | '5.3' => false, |
||
| 89 | '5.4' => true, |
||
| 90 | ), |
||
| 91 | 'magic_quotes_gpc' => array( |
||
| 92 | '5.3' => false, |
||
| 93 | '5.4' => true, |
||
| 94 | ), |
||
| 95 | 'magic_quotes_runtime' => array( |
||
| 96 | '5.3' => false, |
||
| 97 | '5.4' => true, |
||
| 98 | ), |
||
| 99 | 'magic_quotes_sybase' => array( |
||
| 100 | '5.3' => false, |
||
| 101 | '5.4' => true, |
||
| 102 | ), |
||
| 103 | 'mbstring.script_encoding' => array( |
||
| 104 | '5.4' => true, |
||
| 105 | 'alternative' => 'zend.script_encoding', |
||
| 106 | ), |
||
| 107 | 'register_globals' => array( |
||
| 108 | '5.3' => false, |
||
| 109 | '5.4' => true, |
||
| 110 | ), |
||
| 111 | 'register_long_arrays' => array( |
||
| 112 | '5.3' => false, |
||
| 113 | '5.4' => true, |
||
| 114 | ), |
||
| 115 | 'safe_mode' => array( |
||
| 116 | '5.3' => false, |
||
| 117 | '5.4' => true, |
||
| 118 | ), |
||
| 119 | 'safe_mode_allowed_env_vars' => array( |
||
| 120 | '5.3' => false, |
||
| 121 | '5.4' => true, |
||
| 122 | ), |
||
| 123 | 'safe_mode_exec_dir' => array( |
||
| 124 | '5.3' => false, |
||
| 125 | '5.4' => true, |
||
| 126 | ), |
||
| 127 | 'safe_mode_gid' => array( |
||
| 128 | '5.3' => false, |
||
| 129 | '5.4' => true, |
||
| 130 | ), |
||
| 131 | 'safe_mode_include_dir' => array( |
||
| 132 | '5.3' => false, |
||
| 133 | '5.4' => true, |
||
| 134 | ), |
||
| 135 | 'safe_mode_protected_env_vars' => array( |
||
| 136 | '5.3' => false, |
||
| 137 | '5.4' => true, |
||
| 138 | ), |
||
| 139 | 'session.bug_compat_42' => array( |
||
| 140 | '5.3' => false, |
||
| 141 | '5.4' => true, |
||
| 142 | ), |
||
| 143 | 'session.bug_compat_warn' => array( |
||
| 144 | '5.3' => false, |
||
| 145 | '5.4' => true, |
||
| 146 | ), |
||
| 147 | 'y2k_compliance' => array( |
||
| 148 | '5.3' => false, |
||
| 149 | '5.4' => true, |
||
| 150 | ), |
||
| 151 | |||
| 152 | 'always_populate_raw_post_data' => array( |
||
| 153 | '5.6' => false, |
||
| 154 | '7.0' => true, |
||
| 155 | ), |
||
| 156 | 'iconv.input_encoding' => array( |
||
| 157 | '5.6' => false, |
||
| 158 | ), |
||
| 159 | 'iconv.output_encoding' => array( |
||
| 160 | '5.6' => false, |
||
| 161 | ), |
||
| 162 | 'iconv.internal_encoding' => array( |
||
| 163 | '5.6' => false, |
||
| 164 | ), |
||
| 165 | 'mbstring.http_input' => array( |
||
| 166 | '5.6' => false, |
||
| 167 | ), |
||
| 168 | 'mbstring.http_output' => array( |
||
| 169 | '5.6' => false, |
||
| 170 | ), |
||
| 171 | 'mbstring.internal_encoding' => array( |
||
| 172 | '5.6' => false, |
||
| 173 | ), |
||
| 174 | |||
| 175 | 'asp_tags' => array( |
||
| 176 | '7.0' => true, |
||
| 177 | ), |
||
| 178 | 'xsl.security_prefs' => array( |
||
| 179 | '7.0' => true, |
||
| 180 | ), |
||
| 181 | |||
| 182 | 'mcrypt.algorithms_dir' => array( |
||
| 183 | '7.1' => false, |
||
| 184 | ), |
||
| 185 | 'mcrypt.modes_dir' => array( |
||
| 186 | '7.1' => false, |
||
| 187 | ), |
||
| 188 | 'session.entropy_file' => array( |
||
| 189 | '7.1' => true, |
||
| 190 | ), |
||
| 191 | 'session.entropy_length' => array( |
||
| 192 | '7.1' => true, |
||
| 193 | ), |
||
| 194 | 'session.hash_function' => array( |
||
| 195 | '7.1' => true, |
||
| 196 | ), |
||
| 197 | 'session.hash_bits_per_character' => array( |
||
| 198 | '7.1' => true, |
||
| 199 | ), |
||
| 200 | |||
| 201 | 'mbstring.func_overload' => array( |
||
| 202 | '7.2' => false, |
||
| 203 | ), |
||
| 204 | ); |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Returns an array of tokens this test wants to listen for. |
||
| 208 | * |
||
| 209 | * @return array |
||
| 210 | */ |
||
| 211 | public function register() |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Processes this test, when one of its tokens is encountered. |
||
| 219 | * |
||
| 220 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 221 | * @param int $stackPtr The position of the current token in the |
||
| 222 | * stack passed in $tokens. |
||
| 223 | * |
||
| 224 | * @return void |
||
| 225 | */ |
||
| 226 | View Code Duplication | public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
| 265 | |||
| 266 | |||
| 267 | /** |
||
| 268 | * Get the relevant sub-array for a specific item from a multi-dimensional array. |
||
| 269 | * |
||
| 270 | * @param array $itemInfo Base information about the item. |
||
| 271 | * |
||
| 272 | * @return array Version and other information about the item. |
||
| 273 | */ |
||
| 274 | public function getItemArray(array $itemInfo) |
||
| 278 | |||
| 279 | |||
| 280 | /** |
||
| 281 | * Retrieve the relevant detail (version) information for use in an error message. |
||
| 282 | * |
||
| 283 | * @param array $itemArray Version and other information about the item. |
||
| 284 | * @param array $itemInfo Base information about the item. |
||
| 285 | * |
||
| 286 | * @return array |
||
| 287 | */ |
||
| 288 | public function getErrorInfo(array $itemArray, array $itemInfo) |
||
| 299 | |||
| 300 | |||
| 301 | /** |
||
| 302 | * Get the error message template for this sniff. |
||
| 303 | * |
||
| 304 | * @return string |
||
| 305 | */ |
||
| 306 | protected function getErrorMsgTemplate() |
||
| 310 | |||
| 311 | |||
| 312 | /** |
||
| 313 | * Get the error message template for suggesting an alternative for a specific sniff. |
||
| 314 | * |
||
| 315 | * @return string |
||
| 316 | */ |
||
| 317 | protected function getAlternativeOptionTemplate() |
||
| 321 | |||
| 322 | |||
| 323 | }//end class |
||
| 324 |
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.