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 |
||
| 16 | class DeprecatedIniDirectivesSniffTest extends BaseSniffTest |
||
| 17 | { |
||
| 18 | |||
| 19 | const TEST_FILE = 'sniff-examples/deprecated_ini_directives.php'; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Test valid directive |
||
| 23 | * |
||
| 24 | * @group IniDirectives |
||
| 25 | * |
||
| 26 | * @return void |
||
| 27 | */ |
||
| 28 | public function testValidDirective() |
||
| 29 | { |
||
| 30 | $file = $this->sniffFile(self::TEST_FILE); |
||
| 31 | $this->assertNoViolation($file, 57); |
||
| 32 | $this->assertNoViolation($file, 58); |
||
| 33 | $this->assertNoViolation($file, 133); |
||
| 34 | } |
||
| 35 | |||
| 36 | /** |
||
| 37 | * testDeprecatedForbiddenDirectives |
||
| 38 | * |
||
| 39 | * @group IniDirectives |
||
| 40 | * |
||
| 41 | * @dataProvider dataDeprecatedForbiddenDirectives |
||
| 42 | * |
||
| 43 | * @param string $iniName Name of the ini directive. |
||
| 44 | * @param string $deprecatedIn The PHP version in which the ini directive was deprecated. |
||
| 45 | * @param string $forbiddenIn The PHP version in which the ini directive was forbidden. |
||
| 46 | * @param array $lines The line numbers in the test file which apply to this ini directive. |
||
| 47 | * @param string $okVersion A PHP version in which the ini directive was still valid. |
||
| 48 | * @param string $deprecatedVersion Optional PHP version to test deprecation message with - |
||
| 49 | * if different from the $deprecatedIn version. |
||
| 50 | * @param string $forbiddenVersion Optional PHP version to test forbidden message with - |
||
| 51 | * if different from the $forbiddenIn version. |
||
| 52 | * |
||
| 53 | * @return void |
||
| 54 | */ |
||
| 55 | public function testDeprecatedForbiddenDirectives($iniName, $deprecatedIn, $forbiddenIn, $lines, $okVersion, $deprecatedVersion = null, $forbiddenVersion = null) |
||
| 56 | { |
||
| 57 | $file = $this->sniffFile(self::TEST_FILE, $okVersion); |
||
| 58 | foreach($lines as $line) { |
||
| 59 | $this->assertNoViolation($file, $line); |
||
| 60 | } |
||
| 61 | |||
| 62 | if (isset($deprecatedVersion)){ |
||
| 63 | $file = $this->sniffFile(self::TEST_FILE, $deprecatedVersion); |
||
| 64 | } |
||
| 65 | else { |
||
| 66 | $file = $this->sniffFile(self::TEST_FILE, $deprecatedIn); |
||
| 67 | } |
||
| 68 | foreach($lines as $line) { |
||
| 69 | $this->assertWarning($file, $line, "INI directive '{$iniName}' is deprecated from PHP {$deprecatedIn}."); |
||
| 70 | } |
||
| 71 | |||
| 72 | if (isset($forbiddenVersion)){ |
||
| 73 | $file = $this->sniffFile(self::TEST_FILE, $forbiddenVersion); |
||
| 74 | } |
||
| 75 | else { |
||
| 76 | $file = $this->sniffFile(self::TEST_FILE, $forbiddenIn); |
||
| 77 | } |
||
| 78 | $this->assertError($file, $lines[0], "INI directive '{$iniName}' is deprecated from PHP {$deprecatedIn} and forbidden from PHP {$forbiddenIn}"); |
||
| 79 | $this->assertWarning($file, $lines[1], "INI directive '{$iniName}' is deprecated from PHP {$deprecatedIn} and forbidden from PHP {$forbiddenIn}"); |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Data provider. |
||
| 84 | * |
||
| 85 | * @see testDeprecatedForbiddenDirectives() |
||
| 86 | * |
||
| 87 | * @return array |
||
| 88 | */ |
||
| 89 | public function dataDeprecatedForbiddenDirectives() |
||
| 90 | { |
||
| 91 | return array( |
||
| 92 | array('define_syslog_variables', '5.3', '5.4', array(3, 4), '5.2'), |
||
| 93 | array('register_globals', '5.3', '5.4', array(6, 7), '5.2'), |
||
| 94 | array('register_long_arrays', '5.3', '5.4', array(9, 10), '5.2'), |
||
| 95 | array('magic_quotes_gpc', '5.3', '5.4', array(12, 13), '5.2'), |
||
| 96 | array('magic_quotes_runtime', '5.3', '5.4', array(15, 16), '5.2'), |
||
| 97 | array('magic_quotes_sybase', '5.3', '5.4', array(18, 19), '5.2'), |
||
| 98 | array('allow_call_time_pass_reference', '5.3', '5.4', array(21, 22), '5.2'), |
||
| 99 | array('highlight.bg', '5.3', '5.4', array(24, 25), '5.2'), |
||
| 100 | array('session.bug_compat_42', '5.3', '5.4', array(27, 28), '5.2'), |
||
| 101 | array('session.bug_compat_warn', '5.3', '5.4', array(30, 31), '5.2'), |
||
| 102 | array('y2k_compliance', '5.3', '5.4', array(33, 34), '5.2'), |
||
| 103 | array('safe_mode', '5.3', '5.4', array(39, 40), '5.2'), |
||
| 104 | array('safe_mode_gid', '5.3', '5.4', array(42, 43), '5.2'), |
||
| 105 | array('safe_mode_include_dir', '5.3', '5.4', array(45, 46), '5.2'), |
||
| 106 | array('safe_mode_exec_dir', '5.3', '5.4', array(48, 49), '5.2'), |
||
| 107 | array('safe_mode_allowed_env_vars', '5.3', '5.4', array(51, 52), '5.2'), |
||
| 108 | array('safe_mode_protected_env_vars', '5.3', '5.4', array(54, 55), '5.2'), |
||
| 109 | |||
| 110 | array('always_populate_raw_post_data', '5.6', '7.0', array(80, 81), '5.5'), |
||
| 111 | ); |
||
| 112 | } |
||
| 113 | |||
| 114 | |||
| 115 | /** |
||
| 116 | * testDeprecatedDirectives |
||
| 117 | * |
||
| 118 | * @group IniDirectives |
||
| 119 | * |
||
| 120 | * @dataProvider dataDeprecatedDirectives |
||
| 121 | * |
||
| 122 | * @param string $iniName Name of the ini directive. |
||
| 123 | * @param string $deprecatedIn The PHP version in which the ini directive was deprecated. |
||
| 124 | * @param array $lines The line numbers in the test file which apply to this ini directive. |
||
| 125 | * @param string $okVersion A PHP version in which the ini directive was still valid. |
||
| 126 | * @param string $deprecatedVersion Optional PHP version to test deprecation message with - |
||
| 127 | * if different from the $deprecatedIn version. |
||
| 128 | * |
||
| 129 | * @return void |
||
| 130 | */ |
||
| 131 | public function testDeprecatedDirectives($iniName, $deprecatedIn, $lines, $okVersion, $deprecatedVersion = null) |
||
| 132 | { |
||
| 133 | $file = $this->sniffFile(self::TEST_FILE, $okVersion); |
||
| 134 | foreach($lines as $line) { |
||
| 135 | $this->assertNoViolation($file, $line); |
||
| 136 | } |
||
| 137 | |||
| 138 | if (isset($deprecatedVersion)){ |
||
| 139 | $file = $this->sniffFile(self::TEST_FILE, $deprecatedVersion); |
||
| 140 | } |
||
| 141 | else { |
||
| 142 | $file = $this->sniffFile(self::TEST_FILE, $deprecatedIn); |
||
| 143 | } |
||
| 144 | foreach($lines as $line) { |
||
| 145 | $this->assertWarning($file, $line, "INI directive '{$iniName}' is deprecated from PHP {$deprecatedIn}."); |
||
| 146 | } |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Data provider. |
||
| 151 | * |
||
| 152 | * @see testDeprecatedDirectives() |
||
| 153 | * |
||
| 154 | * @return array |
||
| 155 | */ |
||
| 156 | public function dataDeprecatedDirectives() |
||
| 157 | { |
||
| 158 | return array( |
||
| 159 | array('safe_mode_protected_env_vars', '5.3', array(54, 55), '5.2'), |
||
| 160 | |||
| 161 | array('iconv.input_encoding', '5.6', array(62, 63), '5.5'), |
||
| 162 | array('iconv.output_encoding', '5.6', array(65, 66), '5.5'), |
||
| 163 | array('iconv.internal_encoding', '5.6', array(68, 69), '5.5'), |
||
| 164 | array('mbstring.http_input', '5.6', array(71, 72), '5.5'), |
||
| 165 | array('mbstring.http_output', '5.6', array(74, 75), '5.5'), |
||
| 166 | array('mbstring.internal_encoding', '5.6', array(77, 78), '5.5'), |
||
| 167 | ); |
||
| 168 | } |
||
| 169 | |||
| 170 | |||
| 171 | |||
| 172 | /** |
||
| 173 | * testForbiddenWithAlternative |
||
| 174 | * |
||
| 175 | * @group IniDirectives |
||
| 176 | * |
||
| 177 | * @dataProvider dataForbiddenWithAlternative |
||
| 178 | * |
||
| 179 | * @param string $iniName Name of the ini directive. |
||
| 180 | * @param string $forbiddenIn The PHP version in which the ini directive was forbidden. |
||
| 181 | * @param string $alternative An alternative ini directive for the forbidden directive. |
||
| 182 | * @param array $lines The line numbers in the test file which apply to this ini directive. |
||
| 183 | * @param string $okVersion A PHP version in which the ini directive was still valid. |
||
| 184 | * @param string $forbiddenVersion Optional PHP version to test forbidden message with - |
||
| 185 | * if different from the $forbiddenIn version. |
||
| 186 | * |
||
| 187 | * @return void |
||
| 188 | */ |
||
| 189 | public function testForbiddenWithAlternative($iniName, $forbiddenIn, $alternative, $lines, $okVersion, $forbiddenVersion = null) |
||
| 190 | { |
||
| 191 | $file = $this->sniffFile(self::TEST_FILE, $okVersion); |
||
| 192 | foreach($lines as $line) { |
||
| 193 | $this->assertNoViolation($file, $line); |
||
| 194 | } |
||
| 195 | |||
| 196 | if (isset($forbiddenVersion)){ |
||
| 197 | $file = $this->sniffFile(self::TEST_FILE, $forbiddenVersion); |
||
| 198 | } |
||
| 199 | else { |
||
| 200 | $file = $this->sniffFile(self::TEST_FILE, $forbiddenIn); |
||
| 201 | } |
||
| 202 | $this->assertError($file, $lines[0], "INI directive '{$iniName}' is forbidden from PHP {$forbiddenIn}. Use '{$alternative}' instead."); |
||
| 203 | $this->assertWarning($file, $lines[1], "INI directive '{$iniName}' is forbidden from PHP {$forbiddenIn}. Use '{$alternative}' instead."); |
||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Data provider. |
||
| 208 | * |
||
| 209 | * @see testForbiddenWithAlternative() |
||
| 210 | * |
||
| 211 | * @return array |
||
| 212 | */ |
||
| 213 | public function dataForbiddenWithAlternative() |
||
| 214 | { |
||
| 215 | return array( |
||
| 216 | array('fbsql.batchSize', '5.1', 'fbsql.batchsize', array(89, 90), '5.0'), |
||
| 217 | array('detect_unicode', '5.4', 'zend.detect_unicode', array(125, 126), '5.3'), |
||
| 218 | array('mbstring.script_encoding', '5.4', 'zend.script_encoding', array(128, 129), '5.3'), |
||
| 219 | ); |
||
| 220 | } |
||
| 221 | |||
| 222 | /** |
||
| 223 | * testForbiddenDirectives |
||
| 224 | * |
||
| 225 | * @group IniDirectives |
||
| 226 | * |
||
| 227 | * @dataProvider dataForbiddenDirectives |
||
| 228 | * |
||
| 229 | * @param string $iniName Name of the ini directive. |
||
| 230 | * @param string $forbiddenIn The PHP version in which the ini directive was forbidden. |
||
| 231 | * @param array $lines The line numbers in the test file which apply to this ini directive. |
||
| 232 | * @param string $okVersion A PHP version in which the ini directive was still valid. |
||
| 233 | * @param string $forbiddenVersion Optional PHP version to test forbidden message with - |
||
| 234 | * if different from the $forbiddenIn version. |
||
| 235 | * |
||
| 236 | * @return void |
||
| 237 | */ |
||
| 238 | public function testForbiddenDirectives($iniName, $forbiddenIn, $lines, $okVersion, $forbiddenVersion = null) |
||
| 239 | { |
||
| 240 | $file = $this->sniffFile(self::TEST_FILE, $okVersion); |
||
| 241 | foreach($lines as $line) { |
||
| 242 | $this->assertNoViolation($file, $line); |
||
| 243 | } |
||
| 244 | |||
| 245 | if (isset($forbiddenVersion)){ |
||
| 246 | $file = $this->sniffFile(self::TEST_FILE, $forbiddenVersion); |
||
| 247 | } |
||
| 248 | else { |
||
| 249 | $file = $this->sniffFile(self::TEST_FILE, $forbiddenIn); |
||
| 250 | } |
||
| 251 | $this->assertError($file, $lines[0], "INI directive '{$iniName}' is forbidden from PHP {$forbiddenIn}."); |
||
| 252 | $this->assertWarning($file, $lines[1], "INI directive '{$iniName}' is forbidden from PHP {$forbiddenIn}."); |
||
| 253 | } |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Data provider. |
||
| 257 | * |
||
| 258 | * @see testForbiddenDirectives() |
||
| 259 | * |
||
| 260 | * @return array |
||
| 261 | */ |
||
| 262 | public function dataForbiddenDirectives() |
||
| 263 | { |
||
| 264 | return array( |
||
| 265 | array('ifx.allow_persistent', '5.2.1', array(92, 93), '5.1', '5.3'), |
||
| 266 | array('ifx.blobinfile', '5.2.1', array(95, 96), '5.1', '5.3'), |
||
| 267 | array('ifx.byteasvarchar', '5.2.1', array(98, 99), '5.1', '5.3'), |
||
| 268 | array('ifx.charasvarchar', '5.2.1', array(101, 102), '5.1', '5.3'), |
||
| 269 | array('ifx.default_host', '5.2.1', array(104, 105), '5.1', '5.3'), |
||
| 270 | array('ifx.default_password', '5.2.1', array(107, 108), '5.1', '5.3'), |
||
| 271 | array('ifx.default_user', '5.2.1', array(110, 111), '5.1', '5.3'), |
||
| 272 | array('ifx.max_links', '5.2.1', array(113, 114), '5.1', '5.3'), |
||
| 273 | array('ifx.max_persistent', '5.2.1', array(116, 117), '5.1', '5.3'), |
||
| 274 | array('ifx.nullformat', '5.2.1', array(119, 120), '5.1', '5.3'), |
||
| 275 | array('ifx.textasvarchar', '5.2.1', array(122, 123), '5.1', '5.3'), |
||
| 276 | |||
| 277 | array('zend.ze1_compatibility_mode', '5.3', array(36, 37), '5.2'), |
||
| 278 | |||
| 279 | array('asp_tags', '7.0', array(83, 84), '5.6'), |
||
| 280 | array('xsl.security_prefs', '7.0', array(86, 87), '5.6'), |
||
| 281 | ); |
||
| 282 | } |
||
| 283 | |||
| 284 | } |
||
| 285 |