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 NewIniDirectivesSniffTest extends BaseSniffTest |
||
|
|||
17 | { |
||
18 | /** |
||
19 | * Test functions that shouldnt be flagged by this sniff |
||
20 | * |
||
21 | * @return void |
||
22 | */ |
||
23 | public function testFunctionThatShouldntBeFlagged() |
||
24 | { |
||
25 | $file = $this->sniffFile('sniff-examples/new_ini_directives.php', '5.1'); |
||
26 | |||
27 | $this->assertNoViolation($file, 3); |
||
28 | $this->assertNoViolation($file, 4); |
||
29 | $this->assertNoViolation($file, 5); |
||
30 | } |
||
31 | |||
32 | /** |
||
33 | * Test allow_url_include |
||
34 | * |
||
35 | * @return void |
||
36 | */ |
||
37 | public function testAllowUrlInclude() |
||
38 | { |
||
39 | $file = $this->sniffFile('sniff-examples/new_ini_directives.php', '5.1'); |
||
40 | |||
41 | $this->assertWarning($file, 7, "INI directive 'allow_url_include' is not available before version 5.2"); |
||
42 | $this->assertWarning($file, 8, "INI directive 'allow_url_include' is not available before version 5.2"); |
||
43 | |||
44 | // Line 9 tests using double quotes |
||
45 | $this->assertWarning($file, 9, "INI directive 'allow_url_include' is not available before version 5.2"); |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * testPcreBacktracLimit |
||
50 | * |
||
51 | * @return void |
||
52 | */ |
||
53 | public function testPcreBacktrackLimit() |
||
54 | { |
||
55 | $file = $this->sniffFile('sniff-examples/new_ini_directives.php', '5.1'); |
||
56 | |||
57 | $this->assertWarning($file, 11, "INI directive 'pcre.backtrack_limit' is not available before version 5.2"); |
||
58 | $this->assertWarning($file, 12, "INI directive 'pcre.backtrack_limit' is not available before version 5.2"); |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * testPcreRecursionLimit |
||
63 | * |
||
64 | * @return void |
||
65 | */ |
||
66 | public function testPcreRecursionLimit() |
||
67 | { |
||
68 | $file = $this->sniffFile('sniff-examples/new_ini_directives.php', '5.1'); |
||
69 | |||
70 | $this->assertWarning($file, 14, "INI directive 'pcre.recursion_limit' is not available before version 5.2"); |
||
71 | $this->assertWarning($file, 15, "INI directive 'pcre.recursion_limit' is not available before version 5.2"); |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * testSessionCookieHttpOnly |
||
76 | * |
||
77 | * @return void |
||
78 | */ |
||
79 | public function testSessionCookieHttpOnly() |
||
80 | { |
||
81 | $file = $this->sniffFile('sniff-examples/new_ini_directives.php', '5.1'); |
||
82 | |||
83 | $this->assertWarning($file, 17, "INI directive 'session.cookie_httponly' is not available before version 5.2"); |
||
84 | $this->assertWarning($file, 18, "INI directive 'session.cookie_httponly' is not available before version 5.2"); |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * testMaxInputNestingLevel |
||
89 | * |
||
90 | * @return void |
||
91 | */ |
||
92 | View Code Duplication | public function testMaxInputNestingLevel() |
|
93 | { |
||
94 | $file = $this->sniffFile('sniff-examples/new_ini_directives.php', '5.1'); |
||
95 | |||
96 | $this->assertWarning($file, 20, "INI directive 'max_input_nesting_level' is not available before version 5.2.3"); |
||
97 | $this->assertWarning($file, 21, "INI directive 'max_input_nesting_level' is not available before version 5.2.3"); |
||
98 | |||
99 | $file = $this->sniffFile('sniff-examples/new_ini_directives.php', '5.2'); |
||
100 | |||
101 | $this->assertWarning($file, 20, "INI directive 'max_input_nesting_level' is not available before version 5.2.3"); |
||
102 | $this->assertWarning($file, 21, "INI directive 'max_input_nesting_level' is not available before version 5.2.3"); |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * testUserIniFilename |
||
107 | * |
||
108 | * @return void |
||
109 | */ |
||
110 | public function testUserIniFilename() |
||
111 | { |
||
112 | $file = $this->sniffFile('sniff-examples/new_ini_directives.php', '5.2'); |
||
113 | |||
114 | $this->assertWarning($file, 23, "INI directive 'user_ini.filename' is not available before version 5.3"); |
||
115 | $this->assertWarning($file, 24, "INI directive 'user_ini.filename' is not available before version 5.3"); |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * testUserInitCacheTtl |
||
120 | * |
||
121 | * @return void |
||
122 | */ |
||
123 | public function testUserInitCacheTtl() |
||
124 | { |
||
125 | $file = $this->sniffFile('sniff-examples/new_ini_directives.php', '5.2'); |
||
126 | |||
127 | $this->assertWarning($file, 26, "INI directive 'user_ini.cache_ttl' is not available before version 5.3"); |
||
128 | $this->assertWarning($file, 27, "INI directive 'user_ini.cache_ttl' is not available before version 5.3"); |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * testExitOnTimeout |
||
133 | * |
||
134 | * @return void |
||
135 | */ |
||
136 | public function testExitOnTimeout() |
||
137 | { |
||
138 | $file = $this->sniffFile('sniff-examples/new_ini_directives.php', '5.2'); |
||
139 | |||
140 | $this->assertWarning($file, 29, "INI directive 'exit_on_timeout' is not available before version 5.3"); |
||
141 | $this->assertWarning($file, 30, "INI directive 'exit_on_timeout' is not available before version 5.3"); |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * testMbstringHttpOutputConvMimetype |
||
146 | * |
||
147 | * @return void |
||
148 | */ |
||
149 | public function testMbstringHttpOutputConvMimetype() |
||
150 | { |
||
151 | $file = $this->sniffFile('sniff-examples/new_ini_directives.php', '5.2'); |
||
152 | |||
153 | $this->assertWarning($file, 32, "INI directive 'mbstring.http_output_conv_mimetype' is not available before version 5.3"); |
||
154 | $this->assertWarning($file, 33, "INI directive 'mbstring.http_output_conv_mimetype' is not available before version 5.3"); |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * testRequestOrder |
||
159 | * |
||
160 | * @return void |
||
161 | */ |
||
162 | public function testRequestOrder() |
||
163 | { |
||
164 | $file = $this->sniffFile('sniff-examples/new_ini_directives.php', '5.2'); |
||
165 | |||
166 | $this->assertWarning($file, 35, "INI directive 'request_order' is not available before version 5.3"); |
||
167 | $this->assertWarning($file, 36, "INI directive 'request_order' is not available before version 5.3"); |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * testCliPager |
||
172 | * |
||
173 | * @return void |
||
174 | */ |
||
175 | public function testCliPager() |
||
176 | { |
||
177 | $file = $this->sniffFile('sniff-examples/new_ini_directives.php', '5.3'); |
||
178 | |||
179 | $this->assertWarning($file, 38, "INI directive 'cli.pager' is not available before version 5.4"); |
||
180 | $this->assertWarning($file, 39, "INI directive 'cli.pager' is not available before version 5.4"); |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * testCliPrompt |
||
185 | * |
||
186 | * @return void |
||
187 | */ |
||
188 | public function testCliPrompt() |
||
189 | { |
||
190 | $file = $this->sniffFile('sniff-examples/new_ini_directives.php', '5.3'); |
||
191 | |||
192 | $this->assertWarning($file, 41, "INI directive 'cli.prompt' is not available before version 5.4"); |
||
193 | $this->assertWarning($file, 42, "INI directive 'cli.prompt' is not available before version 5.4"); |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * testCliServerColor |
||
198 | * |
||
199 | * @return void |
||
200 | */ |
||
201 | public function testCliServerColor() |
||
202 | { |
||
203 | $file = $this->sniffFile('sniff-examples/new_ini_directives.php', '5.3'); |
||
204 | |||
205 | $this->assertWarning($file, 44, "INI directive 'cli_server.color' is not available before version 5.4"); |
||
206 | $this->assertWarning($file, 45, "INI directive 'cli_server.color' is not available before version 5.4"); |
||
207 | } |
||
208 | |||
209 | /** |
||
210 | * testMaxInputVars |
||
211 | * |
||
212 | * @return void |
||
213 | */ |
||
214 | public function testMaxInputVars() |
||
221 | |||
222 | /** |
||
223 | * testZendMultibyte |
||
224 | * |
||
225 | * @return void |
||
226 | */ |
||
227 | public function testZendMultibyte() |
||
228 | { |
||
229 | $file = $this->sniffFile('sniff-examples/new_ini_directives.php', '5.3'); |
||
230 | |||
231 | $this->assertWarning($file, 50, "INI directive 'zend.multibyte' is not available before version 5.4"); |
||
232 | $this->assertWarning($file, 51, "INI directive 'zend.multibyte' is not available before version 5.4"); |
||
233 | } |
||
234 | |||
235 | /** |
||
236 | * testZendScriptEncoding |
||
237 | * |
||
238 | * @return void |
||
239 | */ |
||
240 | public function testZendScriptEncoding() |
||
247 | |||
248 | /** |
||
249 | * testZendSignalCheck |
||
250 | * |
||
251 | * @return void |
||
252 | */ |
||
253 | public function testZendSignalCheck() |
||
260 | |||
261 | /** |
||
262 | * testSessionUploadProgressEnabled |
||
263 | * |
||
264 | * @return void |
||
265 | */ |
||
266 | public function testSessionUploadProgressEnabled() |
||
273 | |||
274 | /** |
||
275 | * testSessionUploadProgressCleanup |
||
276 | * |
||
277 | * @return void |
||
278 | */ |
||
279 | public function testSessionUploadProgressCleanup() |
||
286 | |||
287 | /** |
||
288 | * testSessionUploadProgressName |
||
289 | * |
||
290 | * @return void |
||
291 | */ |
||
292 | public function testSessionUploadProgressName() |
||
299 | |||
300 | /** |
||
301 | * testSessionUploadProgressFreq |
||
302 | * |
||
303 | * @return void |
||
304 | */ |
||
305 | public function testSessionUploadProgressFreq() |
||
312 | |||
313 | /** |
||
314 | * testEnablePostDataReading |
||
315 | * |
||
316 | * @return void |
||
317 | */ |
||
318 | public function testEnablePostDataReading() |
||
325 | |||
326 | /** |
||
327 | * testWindowsShowCrtWarning |
||
328 | * |
||
329 | * @return void |
||
330 | */ |
||
331 | public function testWindowsShowCrtWarning() |
||
338 | |||
339 | /** |
||
340 | * testIntlUseExceptions |
||
341 | * |
||
342 | * @return void |
||
343 | */ |
||
344 | public function testIntlUseExceptions() |
||
351 | |||
352 | /** |
||
353 | * testMysqlndSha256ServerPublicKey |
||
354 | * |
||
355 | * @return void |
||
356 | */ |
||
357 | public function testMysqlndSha256ServerPublicKey() |
||
364 | |||
365 | |||
366 | /** |
||
367 | * testNewIniDirectives |
||
368 | * |
||
369 | * @dataProvider dataNewIniDirectives |
||
370 | * |
||
371 | * @return void |
||
372 | */ |
||
373 | View Code Duplication | public function testNewIniDirectives($iniName, $fromVersion, $lines, $warningVersion, $okVersion) |
|
374 | { |
||
375 | $file = $this->sniffFile('sniff-examples/new_ini_directives.php', $warningVersion); |
||
376 | foreach($lines as $line) { |
||
377 | $this->assertWarning($file, $line, "INI directive '{$iniName}' is not available before version $fromVersion"); |
||
378 | } |
||
379 | |||
380 | $file = $this->sniffFile('sniff-examples/new_ini_directives.php', $okVersion); |
||
381 | foreach( $lines as $line ) { |
||
382 | $this->assertNoViolation($file, $line); |
||
383 | } |
||
384 | } |
||
385 | |||
386 | public function dataNewIniDirectives() { |
||
387 | return array( |
||
388 | array('auto_globals_jit', '5.0', array(83, 84), '4.4', '5.1'), |
||
389 | array('com.code_page', '5.0', array(86, 87), '4.4', '5.1'), |
||
390 | array('date.default_latitude', '5.0', array(89, 90), '4.4', '5.1'), |
||
391 | array('date.default_longitude', '5.0', array(92, 93), '4.4', '5.1'), |
||
392 | array('date.sunrise_zenith', '5.0', array(95, 96), '4.4', '5.1'), |
||
393 | array('date.sunset_zenith', '5.0', array(98, 99), '4.4', '5.1'), |
||
394 | array('ibase.default_charset', '5.0', array(101, 102), '4.4', '5.1'), |
||
395 | array('ibase.default_db', '5.0', array(104, 105), '4.4', '5.1'), |
||
396 | array('mail.force_extra_parameters', '5.0', array(107, 108), '4.4', '5.1'), |
||
397 | array('mime_magic.debug', '5.0', array(110, 111), '4.4', '5.1'), |
||
398 | array('mysqli.max_links', '5.0', array(113, 114), '4.4', '5.1'), |
||
399 | array('mysqli.default_port', '5.0', array(116, 117), '4.4', '5.1'), |
||
400 | array('mysqli.default_socket', '5.0', array(119, 120), '4.4', '5.1'), |
||
401 | array('mysqli.default_host', '5.0', array(122, 123), '4.4', '5.1'), |
||
402 | array('mysqli.default_user', '5.0', array(125, 126), '4.4', '5.1'), |
||
403 | array('mysqli.default_pw', '5.0', array(128, 129), '4.4', '5.1'), |
||
404 | array('report_zend_debug', '5.0', array(131, 132), '4.4', '5.1'), |
||
405 | array('session.hash_bits_per_character', '5.0', array(134, 135), '4.4', '5.1'), |
||
406 | array('session.hash_function', '5.0', array(137, 138), '4.4', '5.1'), |
||
407 | array('soap.wsdl_cache_dir', '5.0', array(140, 141), '4.4', '5.1'), |
||
408 | array('soap.wsdl_cache_enabled', '5.0', array(143, 144), '4.4', '5.1'), |
||
409 | array('soap.wsdl_cache_ttl', '5.0', array(146, 147), '4.4', '5.1'), |
||
410 | array('sqlite.assoc_case', '5.0', array(149, 150), '4.4', '5.1'), |
||
411 | array('tidy.clean_output', '5.0', array(152, 153), '4.4', '5.1'), |
||
412 | array('tidy.default_config', '5.0', array(155, 156), '4.4', '5.1'), |
||
413 | array('zend.ze1_compatibility_mode', '5.0', array(158, 159), '4.4', '5.1'), |
||
414 | |||
415 | array('date.timezone', '5.1', array(161, 162), '5.0', '5.2'), |
||
416 | array('detect_unicode', '5.1', array(164, 165), '5.0', '5.2'), |
||
417 | array('realpath_cache_size', '5.1', array(170, 171), '5.0', '5.2'), |
||
418 | array('realpath_cache_ttl', '5.1', array(173, 174), '5.0', '5.2'), |
||
419 | |||
420 | array('mbstring.strict_detection', '5.1.2', array(176, 177), '5.1', '5.2'), |
||
421 | array('mssql.charset', '5.1.2', array(179, 180), '5.1', '5.2'), |
||
422 | |||
423 | array('gd.jpeg_ignore_warning', '5.1.3', array(182, 183), '5.1', '5.2'), |
||
424 | |||
425 | array('fbsql.show_timestamp_decimals', '5.1.5', array(185, 186), '5.1', '5.2'), |
||
426 | array('soap.wsdl_cache', '5.1.5', array(188, 189), '5.1', '5.2'), |
||
427 | array('soap.wsdl_cache_limit', '5.1.5', array(191, 192), '5.1', '5.2'), |
||
428 | |||
429 | array('filter.default', '5.2', array(194, 195), '5.1', '5.3'), |
||
430 | array('filter.default_flags', '5.2', array(197, 198), '5.1', '5.3'), |
||
431 | |||
432 | array('cgi.check_shebang_line', '5.2.1', array(200, 201), '5.2', '5.3'), |
||
433 | |||
434 | array('mysqli.allow_local_infile', '5.2.4', array(203, 204), '5.2', '5.3'), |
||
435 | |||
436 | array('max_file_uploads', '5.2.12', array(206, 207), '5.2', '5.3'), |
||
437 | |||
438 | array('cgi.discard_path', '5.3', array(209, 210), '5.2', '5.4'), |
||
439 | array('intl.default_locale', '5.3', array(212, 213), '5.2', '5.4'), |
||
440 | array('intl.error_level', '5.3', array(215, 216), '5.2', '5.4'), |
||
441 | array('mail.add_x_header', '5.3', array(218, 219), '5.2', '5.4'), |
||
442 | array('mail.log', '5.3', array(221, 222), '5.2', '5.4'), |
||
443 | array('mysqli.allow_persistent', '5.3', array(224, 225), '5.2', '5.4'), |
||
444 | array('mysqli.max_persistent', '5.3', array(227, 228), '5.2', '5.4'), |
||
445 | array('mysqli.cache_size', '5.3', array(230, 231), '5.2', '5.4'), |
||
446 | array('mysqlnd.collect_memory_statistics', '5.3', array(233, 234), '5.2', '5.4'), |
||
447 | array('mysqlnd.collect_statistics', '5.3', array(236, 237), '5.2', '5.4'), |
||
448 | array('mysqlnd.debug', '5.3', array(239, 240), '5.2', '5.4'), |
||
449 | array('mysqlnd.net_read_buffer_size', '5.3', array(242, 243), '5.2', '5.4'), |
||
450 | array('odbc.default_cursortype', '5.3', array(245, 246), '5.2', '5.4'), |
||
451 | array('zend.enable_gc', '5.3', array(248, 249), '5.2', '5.4'), |
||
452 | |||
453 | array('curl.cainfo', '5.3.7', array(251, 252), '5.3', '5.4'), |
||
454 | |||
455 | array('sqlite3.extension_dir', '5.3.11', array(254, 255), '5.3', '5.4'), |
||
456 | |||
457 | array('session.upload_progress.prefix', '5.4', array(257, 258), '5.3', '5.5'), |
||
458 | array('mysqlnd.log_mask', '5.4', array(263, 264), '5.3', '5.5'), |
||
459 | array('mysqlnd.mempool_default_size', '5.4', array(266, 267), '5.3', '5.5'), |
||
460 | array('mysqlnd.net_cmd_buffer_size', '5.4', array(269, 270), '5.3', '5.5'), |
||
461 | array('mysqlnd.net_read_timeout', '5.4', array(272, 273), '5.3', '5.5'), |
||
462 | array('phar.cache_list', '5.4', array(275, 276), '5.3', '5.5'), |
||
463 | |||
464 | array('mysqlnd.trace_alloc', '5.5', array(278, 279), '5.4', '5.6'), |
||
465 | array('sys_temp_dir', '5.5', array(281, 282), '5.4', '5.6'), |
||
466 | array('xsl.security_prefs', '5.5', array(284, 285), '5.4', '5.6'), |
||
467 | |||
468 | array('session.use_strict_mode', '5.5.2', array(287, 288), '5.5', '5.6'), |
||
469 | |||
470 | array('mysqli.rollback_on_cached_plink', '5.6', array(290, 291), '5.5', '7.0'), |
||
471 | |||
472 | array('assert.exception', '7.0', array(293, 294), '5.6', '7.1'), |
||
473 | array('pcre.jit', '7.0', array(296, 297), '5.6', '7.1'), |
||
474 | array('session.lazy_write', '7.0', array(299, 300), '5.6', '7.1'), |
||
475 | array('zend.assertions', '7.0', array(302, 303), '5.6', '7.1'), |
||
476 | |||
477 | ); |
||
478 | } |
||
479 | |||
480 | |||
481 | /** |
||
482 | * testNewIniDirectivesWithAlternative |
||
483 | * |
||
484 | * @dataProvider dataNewIniDirectivesWithAlternative |
||
485 | * |
||
486 | * @return void |
||
487 | */ |
||
488 | View Code Duplication | public function testNewIniDirectivesWithAlternative($iniName, $fromVersion, $alternative, $lines, $warningVersion, $okVersion) |
|
489 | { |
||
490 | $file = $this->sniffFile('sniff-examples/new_ini_directives.php', $warningVersion); |
||
491 | foreach($lines as $line) { |
||
492 | $this->assertWarning($file, $line, "INI directive '{$iniName}' is not available before version $fromVersion"); |
||
493 | } |
||
494 | |||
495 | $file = $this->sniffFile('sniff-examples/new_ini_directives.php', $okVersion); |
||
496 | foreach($lines as $line) { |
||
497 | $this->assertNoViolation($file, $line); |
||
498 | } |
||
499 | } |
||
500 | |||
501 | public function dataNewIniDirectivesWithAlternative() { |
||
507 | |||
508 | } |
||
509 |
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.