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 NewFunctionParameterSniffTest extends BaseSniffTest  | 
            ||
| 17 | { | 
            ||
| 18 | |||
| 19 | const TEST_FILE = 'sniff-examples/new_function_parameter.php';  | 
            ||
| 20 | |||
| 21 | /**  | 
            ||
| 22 | * testInvalidParameter  | 
            ||
| 23 | *  | 
            ||
| 24 | * @dataProvider dataInvalidParameter  | 
            ||
| 25 | *  | 
            ||
| 26 | * @param string $functionName Function name.  | 
            ||
| 27 | * @param string $parameterName Parameter name.  | 
            ||
| 28 | * @param string $lastVersionBefore The PHP version just *before* the parameter was introduced.  | 
            ||
| 29 | * @param array $lines The line numbers in the test file which apply to this class.  | 
            ||
| 30 | * @param string $okVersion A PHP version in which the parameter was ok to be used.  | 
            ||
| 31 | * @param string $testVersion Optional PHP version to use for testing the flagged case.  | 
            ||
| 32 | *  | 
            ||
| 33 | * @return void  | 
            ||
| 34 | */  | 
            ||
| 35 | public function testInvalidParameter($functionName, $parameterName, $lastVersionBefore, $lines, $okVersion, $testVersion = null)  | 
            ||
| 36 |     { | 
            ||
| 37 |         if (isset($testVersion)){ | 
            ||
| 38 | $file = $this->sniffFile(self::TEST_FILE, $testVersion);  | 
            ||
| 39 | }  | 
            ||
| 40 |         else { | 
            ||
| 41 | $file = $this->sniffFile(self::TEST_FILE, $lastVersionBefore);  | 
            ||
| 42 | }  | 
            ||
| 43 |         foreach ($lines as $line) { | 
            ||
| 44 |             $this->assertError($file, $line, "The function {$functionName} does not have a parameter \"{$parameterName}\" in PHP version {$lastVersionBefore} or earlier"); | 
            ||
| 45 | }  | 
            ||
| 46 | |||
| 47 | $file = $this->sniffFile(self::TEST_FILE, $okVersion);  | 
            ||
| 48 |         foreach ($lines as $line) { | 
            ||
| 49 | $this->assertNoViolation($file, $line);  | 
            ||
| 50 | }  | 
            ||
| 51 | }  | 
            ||
| 52 | |||
| 53 | /**  | 
            ||
| 54 | * Data provider.  | 
            ||
| 55 | *  | 
            ||
| 56 | * @see testInvalidParameter()  | 
            ||
| 57 | *  | 
            ||
| 58 | * @return array  | 
            ||
| 59 | */  | 
            ||
| 60 | public function dataInvalidParameter()  | 
            ||
| 61 |     { | 
            ||
| 62 | return array(  | 
            ||
| 63 |             array('array_filter', 'flag', '5.5', array(11), '5.6'), | 
            ||
| 64 |             array('array_slice', 'preserve_keys', '5.0.1', array(12), '5.1', '5.0'), | 
            ||
| 65 |             array('array_unique', 'sort_flags', '5.2.8', array(13), '5.3', '5.2'), | 
            ||
| 66 |             array('assert', 'description', '5.4.7', array(14), '5.5', '5.4'), | 
            ||
| 67 |             array('base64_decode', 'strict', '5.1', array(15), '5.2'), | 
            ||
| 68 |             array('class_implements', 'autoload', '5.0', array(16), '5.1'), | 
            ||
| 69 |             array('class_parents', 'autoload', '5.0', array(17), '5.1'), | 
            ||
| 70 |             array('clearstatcache', 'clear_realpath_cache', '5.2', array(18), '5.3'), | 
            ||
| 71 |             array('clearstatcache', 'filename', '5.2', array(18), '5.3'), | 
            ||
| 72 |             array('copy', 'context', '5.2', array(19), '5.3'), | 
            ||
| 73 |             array('curl_multi_info_read', 'msgs_in_queue', '5.1', array(20), '5.2'), | 
            ||
| 74 |             array('debug_backtrace', 'options', '5.2.4', array(21), '5.4', '5.2'), | 
            ||
| 75 |             array('debug_backtrace', 'limit', '5.3', array(21), '5.4'), | 
            ||
| 76 |             array('debug_print_backtrace', 'options', '5.3.5', array(22), '5.4', '5.3'), | 
            ||
| 77 |             array('debug_print_backtrace', 'limit', '5.3', array(22), '5.4'), | 
            ||
| 78 |             array('dirname', 'levels', '5.6', array(23), '7.0'), | 
            ||
| 79 |             array('dns_get_record', 'raw', '5.3', array(24), '5.4'), | 
            ||
| 80 |             array('fgetcsv', 'escape', '5.2', array(25), '5.3'), | 
            ||
| 81 |             array('fputcsv', 'escape_char', '5.5.3', array(26), '5.6', '5.5'), | 
            ||
| 82 |             array('file_get_contents', 'offset', '5.0', array(27), '5.1'), | 
            ||
| 83 |             array('file_get_contents', 'maxlen', '5.0', array(27), '5.1'), | 
            ||
| 84 |             array('filter_input_array', 'add_empty', '5.3', array(28), '5.4'), | 
            ||
| 85 |             array('filter_var_array', 'add_empty', '5.3', array(29), '5.4'), | 
            ||
| 86 |             array('gettimeofday', 'return_float', '5.0', array(30), '5.1'), | 
            ||
| 87 |             array('get_html_translation_table', 'encoding', '5.3.3', array(31), '5.4', '5.3'), | 
            ||
| 88 |             array('get_loaded_extensions', 'zend_extensions', '5.2.3', array(32), '5.3', '5.2'), | 
            ||
| 89 |             array('gzcompress', 'encoding', '5.3', array(33), '5.4'), | 
            ||
| 90 |             array('gzdeflate', 'encoding', '5.3', array(34), '5.4'), | 
            ||
| 91 |             array('htmlentities', 'double_encode', '5.2.2', array(35), '5.3', '5.2'), | 
            ||
| 92 |             array('htmlspecialchars', 'double_encode', '5.2.2', array(36), '5.3', '5.2'), | 
            ||
| 93 |             array('http_build_query', 'arg_separator', '5.1.1', array(37), '5.4', '5.1'), | 
            ||
| 94 |             array('http_build_query', 'enc_type', '5.3', array(37), '5.4'), | 
            ||
| 95 |             array('idn_to_ascii', 'variant', '5.3', array(38), '5.4'), | 
            ||
| 96 |             array('idn_to_ascii', 'idna_info', '5.3', array(38), '5.4'), | 
            ||
| 97 |             array('idn_to_utf8', 'variant', '5.3', array(39), '5.4'), | 
            ||
| 98 |             array('idn_to_utf8', 'idna_info', '5.3', array(39), '5.4'), | 
            ||
| 99 |             array('imagecolorset', 'alpha', '5.3', array(40), '5.4'), | 
            ||
| 100 |             array('imagepng', 'quality', '5.1.1', array(41), '5.2', '5.1'), | 
            ||
| 101 |             array('imagepng', 'filters', '5.1.2', array(41), '5.2', '5.1'), | 
            ||
| 102 |             array('imagerotate', 'ignore_transparent', '5.0', array(42), '5.1'), | 
            ||
| 103 |             array('imap_open', 'n_retries', '5.1', array(43), '5.4'), | 
            ||
| 104 |             array('imap_open', 'params', '5.3.1', array(43), '5.4', '5.3'), | 
            ||
| 105 |             array('imap_reopen', 'n_retries', '5.1', array(44), '5.2'), | 
            ||
| 106 |             array('ini_get_all', 'details', '5.2', array(45), '5.3'), | 
            ||
| 107 |             array('is_a', 'allow_string', '5.3.8', array(46), '5.4', '5.3'), | 
            ||
| 108 |             array('is_subclass_of', 'allow_string', '5.3.8', array(47), '5.4', '5.3'), | 
            ||
| 109 |             array('iterator_to_array', 'use_keys', '5.2', array(48), '5.3', '5.2'), | 
            ||
| 110 |             array('json_decode', 'depth', '5.2', array(49), '5.4'), | 
            ||
| 111 |             array('json_decode', 'options', '5.3', array(49), '5.4'), | 
            ||
| 112 |             array('json_encode', 'options', '5.2', array(50), '5.5'), | 
            ||
| 113 |             array('json_encode', 'depth', '5.4', array(50), '5.5'), | 
            ||
| 114 |             array('memory_get_peak_usage', 'real_usage', '5.1', array(51), '5.2'), | 
            ||
| 115 |             array('memory_get_usage', 'real_usage', '5.1', array(52), '5.2'), | 
            ||
| 116 |             array('mb_encode_numericentity', 'is_hex', '5.3', array(53), '5.4'), | 
            ||
| 117 |             array('mb_strrpos', 'offset', '5.1', array(54), '5.2'), | 
            ||
| 118 |             array('mssql_connect', 'new_link', '5.0', array(55), '5.1'), | 
            ||
| 119 |             array('mysqli_commit', 'flags', '5.4', array(56), '5.5'), | 
            ||
| 120 |             array('mysqli_commit', 'name', '5.4', array(56), '5.5'), | 
            ||
| 121 |             array('mysqli_rollback', 'flags', '5.4', array(57), '5.5'), | 
            ||
| 122 |             array('mysqli_rollback', 'name', '5.4', array(57), '5.5'), | 
            ||
| 123 |             array('nl2br', 'is_xhtml', '5.2', array(58), '5.3'), | 
            ||
| 124 |             array('openssl_decrypt', 'iv', '5.3.2', array(59), '5.4', '5.3'), | 
            ||
| 125 |             array('openssl_encrypt', 'iv', '5.3.2', array(60), '5.4', '5.3'), | 
            ||
| 126 |             array('openssl_pkcs7_verify', 'content', '5.0', array(61), '5.1'), | 
            ||
| 127 |             array('openssl_seal', 'method', '5.2', array(62), '5.3'), | 
            ||
| 128 |             array('openssl_verify', 'signature_alg', '5.1', array(63), '5.2'), | 
            ||
| 129 |             array('parse_ini_file', 'scanner_mode', '5.2', array(64), '5.3'), | 
            ||
| 130 |             array('parse_url', 'component', '5.1.1', array(65), '5.2', '5.1'), | 
            ||
| 131 |             array('pg_lo_create', 'object_id', '5.2', array(66), '5.3'), | 
            ||
| 132 |             array('pg_lo_import', 'object_id', '5.2', array(67), '5.3'), | 
            ||
| 133 |             array('preg_replace', 'count', '5.0', array(68), '5.1'), | 
            ||
| 134 |             array('preg_replace_callback', 'count', '5.0', array(69), '5.1'), | 
            ||
| 135 |             array('round', 'mode', '5.2', array(70), '5.3'), | 
            ||
| 136 |             array('sem_acquire', 'nowait', '5.6', array(71), '7.0'), | 
            ||
| 137 |             array('session_regenerate_id', 'delete_old_session', '5.0', array(72), '5.1'), | 
            ||
| 138 |             array('session_set_cookie_params', 'httponly', '5.1', array(73), '5.2'), | 
            ||
| 139 |             array('session_set_save_handler', 'create_sid', '5.5', array(74), '5.6'), | 
            ||
| 140 |             array('session_start', 'options', '5.6', array(75), '7.0'), | 
            ||
| 141 |             array('setcookie', 'httponly', '5.1', array(76), '5.2'), | 
            ||
| 142 |             array('setrawcookie', 'httponly', '5.1', array(77), '5.2'), | 
            ||
| 143 |             array('simplexml_load_file', 'is_prefix', '5.1', array(78), '5.2'), | 
            ||
| 144 |             array('simplexml_load_string', 'is_prefix', '5.1', array(79), '5.2'), | 
            ||
| 145 |             array('spl_autoload_register', 'prepend', '5.2', array(80), '5.3'), | 
            ||
| 146 |             array('stream_context_create', 'params', '5.2', array(81), '5.3'), | 
            ||
| 147 |             array('stream_copy_to_stream', 'offset', '5.0', array(82), '5.1'), | 
            ||
| 148 |             array('stream_get_contents', 'offset', '5.0', array(83), '5.1'), | 
            ||
| 149 |             array('stream_wrapper_register', 'flags', '5.2.3', array(84), '5.3', '5.2'), | 
            ||
| 150 |             array('stristr', 'before_needle', '5.2', array(85), '5.3'), | 
            ||
| 151 |             array('strstr', 'before_needle', '5.2', array(86), '5.3'), | 
            ||
| 152 |             array('str_word_count', 'charlist', '5.0', array(87), '5.1'), | 
            ||
| 153 |             array('substr_count', 'offset', '5.0', array(88), '5.1'), | 
            ||
| 154 |             array('substr_count', 'length', '5.0', array(88), '5.1'), | 
            ||
| 155 |             array('sybase_connect', 'new', '5.2', array(89), '5.2.17'), | 
            ||
| 156 |             array('timezone_transitions_get', 'timestamp_begin', '5.2', array(90), '5.3'), | 
            ||
| 157 |             array('timezone_transitions_get', 'timestamp_end', '5.2', array(90), '5.3'), | 
            ||
| 158 |             array('timezone_identifiers_list', 'what', '5.2', array(91), '5.3'), | 
            ||
| 159 |             array('timezone_identifiers_list', 'country', '5.2', array(91), '5.3'), | 
            ||
| 160 |             array('token_get_all', 'flags', '5.6', array(92), '7.0'), | 
            ||
| 161 |             array('ucwords', 'delimiters', '5.4.31', array(93), '5.6', '5.4'), | 
            ||
| 162 |             array('unserialize', 'options', '5.6', array(94), '7.0'), | 
            ||
| 163 | );  | 
            ||
| 164 | }  | 
            ||
| 165 | |||
| 166 | |||
| 167 | /**  | 
            ||
| 168 | * testValidParameter  | 
            ||
| 169 | *  | 
            ||
| 170 | * @dataProvider dataValidParameter  | 
            ||
| 171 | *  | 
            ||
| 172 | * @param int $line The line number.  | 
            ||
| 173 | *  | 
            ||
| 174 | * @return void  | 
            ||
| 175 | */  | 
            ||
| 176 | public function testValidParameter($line)  | 
            ||
| 177 |     { | 
            ||
| 178 | $file = $this->sniffFile(self::TEST_FILE);  | 
            ||
| 179 | $this->assertNoViolation($file, $line);  | 
            ||
| 180 | }  | 
            ||
| 181 | |||
| 182 | /**  | 
            ||
| 183 | * Data provider.  | 
            ||
| 184 | *  | 
            ||
| 185 | * @see testValidParameter()  | 
            ||
| 186 | *  | 
            ||
| 187 | * @return array  | 
            ||
| 188 | */  | 
            ||
| 189 | public function dataValidParameter()  | 
            ||
| 190 |     { | 
            ||
| 191 | return array(  | 
            ||
| 192 | array(4),  | 
            ||
| 193 | );  | 
            ||
| 194 | }  | 
            ||
| 195 | |||
| 196 | }  | 
            ||
| 197 |