PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/PHPCompatibility/Tests/FunctionUse/NewFunctionParametersUnitTest.php

http://github.com/wimg/PHPCompatibility
PHP | 253 lines | 182 code | 11 blank | 60 comment | 0 complexity | 146633d446421e16f641338d397947d9 MD5 | raw file
Possible License(s): LGPL-3.0
  1. <?php
  2. /**
  3. * PHPCompatibility, an external standard for PHP_CodeSniffer.
  4. *
  5. * @package PHPCompatibility
  6. * @copyright 2012-2020 PHPCompatibility Contributors
  7. * @license https://opensource.org/licenses/LGPL-3.0 LGPL3
  8. * @link https://github.com/PHPCompatibility/PHPCompatibility
  9. */
  10. namespace PHPCompatibility\Tests\FunctionUse;
  11. use PHPCompatibility\Tests\BaseSniffTest;
  12. /**
  13. * Test the NewFunctionParameters sniff.
  14. *
  15. * @group newFunctionParameters
  16. * @group functionUse
  17. *
  18. * @covers \PHPCompatibility\Sniffs\FunctionUse\NewFunctionParametersSniff
  19. *
  20. * @since 7.0.0
  21. */
  22. class NewFunctionParametersUnitTest extends BaseSniffTest
  23. {
  24. /**
  25. * testInvalidParameter
  26. *
  27. * @dataProvider dataInvalidParameter
  28. *
  29. * @param string $functionName Function name.
  30. * @param string $parameterName Parameter name.
  31. * @param string $lastVersionBefore The PHP version just *before* the parameter was introduced.
  32. * @param array $lines The line numbers in the test file which apply to this function parameter.
  33. * @param string $okVersion A PHP version in which the parameter was ok to be used.
  34. * @param string $testVersion Optional PHP version to use for testing the flagged case.
  35. *
  36. * @return void
  37. */
  38. public function testInvalidParameter($functionName, $parameterName, $lastVersionBefore, $lines, $okVersion, $testVersion = null)
  39. {
  40. $errorVersion = (isset($testVersion)) ? $testVersion : $lastVersionBefore;
  41. $file = $this->sniffFile(__FILE__, $errorVersion);
  42. $error = "The function {$functionName}() does not have a parameter \"{$parameterName}\" in PHP version {$lastVersionBefore} or earlier";
  43. foreach ($lines as $line) {
  44. $this->assertError($file, $line, $error);
  45. }
  46. $file = $this->sniffFile(__FILE__, $okVersion);
  47. foreach ($lines as $line) {
  48. $this->assertNoViolation($file, $line);
  49. }
  50. }
  51. /**
  52. * Data provider.
  53. *
  54. * @see testInvalidParameter()
  55. *
  56. * @return array
  57. */
  58. public function dataInvalidParameter()
  59. {
  60. return [
  61. ['array_filter', 'flag', '5.5', [11], '5.6'],
  62. ['array_slice', 'preserve_keys', '5.0.1', [12], '5.1', '5.0'],
  63. ['array_unique', 'sort_flags', '5.2.8', [13], '5.3', '5.2'],
  64. ['assert', 'description', '5.4.7', [14], '5.5', '5.4'],
  65. ['base64_decode', 'strict', '5.1', [15], '5.2'],
  66. ['bcmod', 'scale', '7.1', [96], '7.2'],
  67. ['class_implements', 'autoload', '5.0', [16], '5.1'],
  68. ['class_parents', 'autoload', '5.0', [17], '5.1'],
  69. ['clearstatcache', 'clear_realpath_cache', '5.2', [18], '5.3'],
  70. ['clearstatcache', 'filename', '5.2', [18], '5.3'],
  71. ['copy', 'context', '5.2', [19], '5.3'],
  72. ['curl_multi_info_read', 'msgs_in_queue', '5.1', [20], '5.2'],
  73. ['date_time_set', 'microseconds', '7.0', [119], '7.1'],
  74. ['debug_backtrace', 'options', '5.2.4', [21], '5.4', '5.2'],
  75. ['debug_backtrace', 'limit', '5.3', [21], '5.4'],
  76. ['debug_print_backtrace', 'options', '5.3.5', [22], '5.4', '5.3'],
  77. ['debug_print_backtrace', 'limit', '5.3', [22], '5.4'],
  78. ['dirname', 'levels', '5.6', [23], '7.0'],
  79. ['dns_get_record', 'raw', '5.3', [24], '5.4'],
  80. ['fgetcsv', 'escape', '5.2', [25], '5.3'],
  81. ['fputcsv', 'escape_char', '5.5.3', [26], '5.6', '5.5'],
  82. ['file_get_contents', 'offset', '5.0', [27], '5.1'],
  83. ['file_get_contents', 'maxlen', '5.0', [27], '5.1'],
  84. ['filter_input_array', 'add_empty', '5.3', [28], '5.4'],
  85. ['filter_var_array', 'add_empty', '5.3', [29], '5.4'],
  86. ['getenv', 'local_only', '5.5.37', [105], '5.6', '5.5'],
  87. ['getopt', 'optind', '7.0', [98], '7.1'],
  88. ['gettimeofday', 'return_float', '5.0', [30], '5.1'],
  89. ['get_defined_functions', 'exclude_disabled', '7.0.14', [95], '7.1', '7.0'],
  90. ['get_headers', 'context', '7.0', [97], '7.1'],
  91. ['get_html_translation_table', 'encoding', '5.3.3', [31], '5.4', '5.3'],
  92. ['get_loaded_extensions', 'zend_extensions', '5.2.3', [32], '5.3', '5.2'],
  93. ['gzcompress', 'encoding', '5.3', [33], '5.4'],
  94. ['gzdeflate', 'encoding', '5.3', [34], '5.4'],
  95. ['htmlentities', 'double_encode', '5.2.2', [35], '5.3', '5.2'],
  96. ['htmlspecialchars', 'double_encode', '5.2.2', [36], '5.3', '5.2'],
  97. ['http_build_query', 'arg_separator', '5.1.1', [37], '5.4', '5.1'],
  98. ['http_build_query', 'enc_type', '5.3', [37], '5.4'],
  99. ['idn_to_ascii', 'variant', '5.3', [38], '5.4'],
  100. ['idn_to_ascii', 'idna_info', '5.3', [38], '5.4'],
  101. ['idn_to_utf8', 'variant', '5.3', [39], '5.4'],
  102. ['idn_to_utf8', 'idna_info', '5.3', [39], '5.4'],
  103. ['imagecolorset', 'alpha', '5.3', [40], '5.4'],
  104. ['imagepng', 'quality', '5.1.1', [41], '5.2', '5.1'],
  105. ['imagepng', 'filters', '5.1.2', [41], '5.2', '5.1'],
  106. ['imagerotate', 'ignore_transparent', '5.0', [42], '5.1'],
  107. ['imap_open', 'n_retries', '5.1', [43], '5.4'],
  108. ['imap_open', 'params', '5.3.1', [43], '5.4', '5.3'],
  109. ['imap_reopen', 'n_retries', '5.1', [44], '5.2'],
  110. ['ini_get_all', 'details', '5.2', [45], '5.3'],
  111. ['is_a', 'allow_string', '5.3.8', [46], '5.4', '5.3'],
  112. ['is_subclass_of', 'allow_string', '5.3.8', [47], '5.4', '5.3'],
  113. ['iterator_to_array', 'use_keys', '5.2.0', [48], '5.3', '5.2'], // Function introduced in 5.2.1.
  114. ['json_decode', 'depth', '5.2', [49], '5.4'], // OK version > version in which last parameter was added to the function.
  115. ['json_decode', 'options', '5.3', [49], '5.4'],
  116. ['json_encode', 'options', '5.2', [50], '5.5'], // OK version > version in which last parameter was added to the function.
  117. ['json_encode', 'depth', '5.4', [50], '5.5'],
  118. ['ldap_add', 'serverctrls', '7.2', [106], '7.3'],
  119. ['ldap_compare', 'serverctrls', '7.2', [107], '7.3'],
  120. ['ldap_delete', 'serverctrls', '7.2', [108], '7.3'],
  121. ['ldap_exop', 'serverctrls', '7.2', [121], '7.3'],
  122. ['ldap_exop_passwd', 'serverctrls', '7.2', [122], '7.3'],
  123. ['ldap_list', 'serverctrls', '7.2', [109], '7.3'],
  124. ['ldap_mod_add', 'serverctrls', '7.2', [110], '7.3'],
  125. ['ldap_mod_del', 'serverctrls', '7.2', [111], '7.3'],
  126. ['ldap_mod_replace', 'serverctrls', '7.2', [112], '7.3'],
  127. ['ldap_modify_batch', 'serverctrls', '7.2', [113], '7.3'],
  128. ['ldap_parse_result', 'serverctrls', '7.2', [114], '7.3'],
  129. ['ldap_read', 'serverctrls', '7.2', [115], '7.3'],
  130. ['ldap_rename', 'serverctrls', '7.2', [116], '7.3'],
  131. ['ldap_search', 'serverctrls', '7.2', [117], '7.3'],
  132. ['memory_get_peak_usage', 'real_usage', '5.1', [51], '5.2'],
  133. ['memory_get_usage', 'real_usage', '5.1', [52], '5.2'],
  134. ['mb_decode_numericentity', 'is_hex', '5.3', [120], '5.4'],
  135. ['mb_encode_numericentity', 'is_hex', '5.3', [53], '5.4'],
  136. ['mb_strrpos', 'offset', '5.1', [54], '5.2'],
  137. ['mssql_connect', 'new_link', '5.0', [55], '5.1'],
  138. ['mysqli_commit', 'flags', '5.4', [56], '5.5'],
  139. ['mysqli_commit', 'name', '5.4', [56], '5.5'],
  140. ['mysqli_rollback', 'flags', '5.4', [57], '5.5'],
  141. ['mysqli_rollback', 'name', '5.4', [57], '5.5'],
  142. ['nl2br', 'is_xhtml', '5.2', [58], '5.3'],
  143. ['openssl_decrypt', 'iv', '5.3.2', [59], '7.1', '5.3'], // OK version > version in which last parameter was added to the function.
  144. ['openssl_decrypt', 'tag', '7.0', [59], '7.1'],
  145. ['openssl_decrypt', 'aad', '7.0', [59], '7.1'],
  146. ['openssl_encrypt', 'iv', '5.3.2', [60], '7.1', '5.3'], // OK version > version in which last parameter was added to the function.
  147. ['openssl_encrypt', 'tag', '7.0', [60], '7.1'],
  148. ['openssl_encrypt', 'aad', '7.0', [60], '7.1'],
  149. ['openssl_encrypt', 'tag_length', '7.0', [60], '7.1'],
  150. ['openssl_open', 'method', '5.2', [103], '7.0'], // OK version > version in which last parameter was added to the function.
  151. ['openssl_open', 'iv', '5.6', [103], '7.0'],
  152. ['openssl_pkcs7_verify', 'content', '5.0', [61], '7.2'], // OK version > version in which last parameter was added to the function.
  153. ['openssl_pkcs7_verify', 'p7bfilename', '7.1', [61], '7.2'],
  154. ['openssl_seal', 'method', '5.2', [62], '7.0'], // OK version > version in which last parameter was added to the function.
  155. ['openssl_seal', 'iv', '5.6', [62], '7.0'],
  156. ['openssl_verify', 'signature_alg', '5.1', [63], '5.2'],
  157. ['parse_ini_file', 'scanner_mode', '5.2', [64], '5.3'],
  158. ['parse_url', 'component', '5.1.1', [65], '5.2', '5.1'],
  159. ['pg_escape_bytea', 'connection', '5.1', [123], '5.2'],
  160. ['pg_escape_string', 'connection', '5.1', [124], '5.2'],
  161. ['pg_fetch_all', 'result_type', '7.0', [99], '7.1'],
  162. ['pg_last_notice', 'option', '7.0', [100], '7.1'],
  163. ['pg_lo_create', 'object_id', '5.2', [66], '5.3'],
  164. ['pg_lo_import', 'object_id', '5.2', [67], '5.3'],
  165. ['pg_meta_data', 'extended', '5.5', [125], '5.6'],
  166. ['pg_select', 'result_type', '7.0', [101], '7.1'],
  167. ['php_uname', 'mode', '4.2', [104], '4.3'],
  168. ['preg_replace', 'count', '5.0', [68], '5.1'],
  169. ['preg_replace_callback', 'count', '5.0', [69], '7.4'], // OK version > version in which last parameter was added to the function.
  170. ['preg_replace_callback', 'flags', '7.3', [69], '7.4'],
  171. ['preg_replace_callback_array', 'flags', '7.3', [118], '7.4'],
  172. ['round', 'mode', '5.2', [70], '5.3'],
  173. ['sem_acquire', 'nowait', '5.6.0', [71], '7.0', '5.6'],
  174. ['session_regenerate_id', 'delete_old_session', '5.0', [72], '5.1'],
  175. ['session_set_cookie_params', 'httponly', '5.1', [73], '5.2'],
  176. ['session_set_save_handler', 'create_sid', '5.5.0', [74], '7.0', '5.5'], // OK version > version in which last parameter was added to the function.
  177. ['session_set_save_handler', 'validate_sid', '5.6', [74], '7.0'],
  178. ['session_set_save_handler', 'update_timestamp', '5.6', [74], '7.0'],
  179. ['session_start', 'options', '5.6', [75], '7.0'],
  180. ['setcookie', 'httponly', '5.1', [76], '5.2'],
  181. ['setrawcookie', 'httponly', '5.1', [77], '5.2'],
  182. ['simplexml_load_file', 'is_prefix', '5.1', [78], '5.2'],
  183. ['simplexml_load_string', 'is_prefix', '5.1', [79], '5.2'],
  184. ['spl_autoload_register', 'prepend', '5.2', [80], '5.3'],
  185. ['stream_context_create', 'params', '5.2', [81], '5.3'],
  186. ['stream_copy_to_stream', 'offset', '5.0', [82], '5.1'],
  187. ['stream_get_contents', 'offset', '5.0', [83], '5.1'],
  188. ['stream_wrapper_register', 'flags', '5.2.3', [84], '5.3', '5.2'],
  189. ['stristr', 'before_needle', '5.2', [85], '5.3'],
  190. ['strstr', 'before_needle', '5.2', [86], '5.3'],
  191. ['str_word_count', 'charlist', '5.0', [87], '5.1'],
  192. ['substr_count', 'offset', '5.0', [88], '5.1'],
  193. ['substr_count', 'length', '5.0', [88], '5.1'],
  194. ['sybase_connect', 'new', '5.2', [89], '5.3'],
  195. ['timezone_transitions_get', 'timestamp_begin', '5.2', [90], '5.3'],
  196. ['timezone_transitions_get', 'timestamp_end', '5.2', [90], '5.3'],
  197. ['timezone_identifiers_list', 'what', '5.2', [91], '5.3'],
  198. ['timezone_identifiers_list', 'country', '5.2', [91], '5.3'],
  199. ['token_get_all', 'flags', '5.6', [92], '7.0'],
  200. ['ucwords', 'delimiters', '5.4.31', [93], '5.6', '5.4'], // Function introduced in 5.4.31 and 5.5.15.
  201. ['unpack', 'offset', '7.0', [102], '7.1'],
  202. ['unserialize', 'options', '5.6', [94], '7.0'],
  203. ];
  204. }
  205. /**
  206. * testNoFalsePositives
  207. *
  208. * @dataProvider dataNoFalsePositives
  209. *
  210. * @param int $line The line number.
  211. *
  212. * @return void
  213. */
  214. public function testNoFalsePositives($line)
  215. {
  216. $file = $this->sniffFile(__FILE__, '5.0'); // Low version below the first addition.
  217. $this->assertNoViolation($file, $line);
  218. }
  219. /**
  220. * Data provider.
  221. *
  222. * @see testNoFalsePositives()
  223. *
  224. * @return array
  225. */
  226. public function dataNoFalsePositives()
  227. {
  228. return [
  229. [4],
  230. ];
  231. }
  232. /**
  233. * Verify no notices are thrown at all.
  234. *
  235. * @return void
  236. */
  237. public function testNoViolationsInFileOnValidVersion()
  238. {
  239. $file = $this->sniffFile(__FILE__, '99.0'); // High version beyond newest addition.
  240. $this->assertNoViolation($file);
  241. }
  242. }