FixerFactoryTest.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. <?php
  2. /*
  3. * This file is part of PHP CS Fixer.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. namespace PhpCsFixer\Tests\AutoReview;
  12. use PhpCsFixer\Fixer\FixerInterface;
  13. use PhpCsFixer\FixerFactory;
  14. use PhpCsFixer\Tests\Test\IntegrationCaseFactory;
  15. use PhpCsFixer\Tests\TestCase;
  16. use Symfony\Component\Finder\SplFileInfo;
  17. /**
  18. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  19. *
  20. * @internal
  21. *
  22. * @coversNothing
  23. * @group auto-review
  24. * @group covers-nothing
  25. */
  26. final class FixerFactoryTest extends TestCase
  27. {
  28. public function testFixersPriorityEdgeFixers()
  29. {
  30. $factory = new FixerFactory();
  31. $factory->registerBuiltInFixers();
  32. $fixers = $factory->getFixers();
  33. static::assertSame('encoding', $fixers[0]->getName(), 'Expected "encoding" fixer to have the highest priority.');
  34. static::assertSame('full_opening_tag', $fixers[1]->getName(), 'Expected "full_opening_tag" fixer has second highest priority.');
  35. static::assertSame('single_blank_line_at_eof', $fixers[\count($fixers) - 1]->getName(), 'Expected "single_blank_line_at_eof" to have the lowest priority.');
  36. }
  37. /**
  38. * @dataProvider provideFixersPriorityCases
  39. * @dataProvider provideFixersPrioritySpecialPhpdocCases
  40. */
  41. public function testFixersPriority(FixerInterface $first, FixerInterface $second)
  42. {
  43. static::assertLessThan($first->getPriority(), $second->getPriority(), sprintf('"%s" should have less priority than "%s"', \get_class($second), \get_class($first)));
  44. }
  45. public function provideFixersPriorityCases()
  46. {
  47. $factory = new FixerFactory();
  48. $factory->registerBuiltInFixers();
  49. $fixers = [];
  50. foreach ($factory->getFixers() as $fixer) {
  51. $fixers[$fixer->getName()] = $fixer;
  52. }
  53. return [
  54. [$fixers['align_multiline_comment'], $fixers['phpdoc_trim_consecutive_blank_line_separation']],
  55. [$fixers['array_indentation'], $fixers['align_multiline_comment']],
  56. [$fixers['array_indentation'], $fixers['binary_operator_spaces']],
  57. [$fixers['array_syntax'], $fixers['binary_operator_spaces']],
  58. [$fixers['array_syntax'], $fixers['ternary_operator_spaces']],
  59. [$fixers['backtick_to_shell_exec'], $fixers['escape_implicit_backslashes']],
  60. [$fixers['backtick_to_shell_exec'], $fixers['explicit_string_variable']],
  61. [$fixers['backtick_to_shell_exec'], $fixers['native_function_invocation']],
  62. [$fixers['backtick_to_shell_exec'], $fixers['single_quote']],
  63. [$fixers['blank_line_after_opening_tag'], $fixers['no_blank_lines_before_namespace']],
  64. [$fixers['braces'], $fixers['array_indentation']],
  65. [$fixers['braces'], $fixers['method_argument_space']],
  66. [$fixers['braces'], $fixers['method_chaining_indentation']],
  67. [$fixers['class_attributes_separation'], $fixers['braces']],
  68. [$fixers['class_attributes_separation'], $fixers['indentation_type']],
  69. [$fixers['class_definition'], $fixers['braces']],
  70. [$fixers['class_keyword_remove'], $fixers['no_unused_imports']],
  71. [$fixers['combine_consecutive_issets'], $fixers['multiline_whitespace_before_semicolons']],
  72. [$fixers['combine_consecutive_issets'], $fixers['no_singleline_whitespace_before_semicolons']],
  73. [$fixers['combine_consecutive_issets'], $fixers['no_spaces_inside_parenthesis']],
  74. [$fixers['combine_consecutive_issets'], $fixers['no_trailing_whitespace']],
  75. [$fixers['combine_consecutive_issets'], $fixers['no_whitespace_in_blank_line']],
  76. [$fixers['combine_consecutive_unsets'], $fixers['no_extra_blank_lines']],
  77. [$fixers['combine_consecutive_unsets'], $fixers['no_trailing_whitespace']],
  78. [$fixers['combine_consecutive_unsets'], $fixers['no_whitespace_in_blank_line']],
  79. [$fixers['combine_consecutive_unsets'], $fixers['space_after_semicolon']],
  80. [$fixers['combine_nested_dirname'], $fixers['method_argument_space']],
  81. [$fixers['combine_nested_dirname'], $fixers['no_spaces_inside_parenthesis']],
  82. [$fixers['declare_strict_types'], $fixers['blank_line_after_opening_tag']],
  83. [$fixers['declare_strict_types'], $fixers['declare_equal_normalize']],
  84. [$fixers['declare_strict_types'], $fixers['header_comment']],
  85. [$fixers['dir_constant'], $fixers['combine_nested_dirname']],
  86. [$fixers['doctrine_annotation_array_assignment'], $fixers['doctrine_annotation_spaces']],
  87. [$fixers['echo_tag_syntax'], $fixers['no_mixed_echo_print']],
  88. [$fixers['elseif'], $fixers['braces']],
  89. [$fixers['escape_implicit_backslashes'], $fixers['heredoc_to_nowdoc']],
  90. [$fixers['escape_implicit_backslashes'], $fixers['single_quote']],
  91. [$fixers['explicit_string_variable'], $fixers['simple_to_complex_string_variable']],
  92. [$fixers['final_internal_class'], $fixers['final_static_access']],
  93. [$fixers['final_internal_class'], $fixers['protected_to_private']],
  94. [$fixers['final_internal_class'], $fixers['self_static_accessor']],
  95. [$fixers['fully_qualified_strict_types'], $fixers['no_superfluous_phpdoc_tags']],
  96. [$fixers['function_declaration'], $fixers['method_argument_space']],
  97. [$fixers['function_to_constant'], $fixers['native_function_casing']],
  98. [$fixers['function_to_constant'], $fixers['no_extra_blank_lines']],
  99. [$fixers['function_to_constant'], $fixers['no_singleline_whitespace_before_semicolons']],
  100. [$fixers['function_to_constant'], $fixers['no_trailing_whitespace']],
  101. [$fixers['function_to_constant'], $fixers['no_whitespace_in_blank_line']],
  102. [$fixers['function_to_constant'], $fixers['self_static_accessor']],
  103. [$fixers['general_phpdoc_annotation_remove'], $fixers['no_empty_phpdoc']],
  104. [$fixers['general_phpdoc_annotation_remove'], $fixers['phpdoc_line_span']],
  105. [$fixers['general_phpdoc_annotation_remove'], $fixers['phpdoc_separation']],
  106. [$fixers['general_phpdoc_annotation_remove'], $fixers['phpdoc_trim']],
  107. [$fixers['general_phpdoc_tag_rename'], $fixers['phpdoc_add_missing_param_annotation']],
  108. [$fixers['global_namespace_import'], $fixers['no_unused_imports']],
  109. [$fixers['global_namespace_import'], $fixers['ordered_imports']],
  110. [$fixers['header_comment'], $fixers['single_line_comment_style']],
  111. [$fixers['implode_call'], $fixers['method_argument_space']],
  112. [$fixers['indentation_type'], $fixers['phpdoc_indent']],
  113. [$fixers['is_null'], $fixers['yoda_style']],
  114. [$fixers['lambda_not_used_import'], $fixers['no_spaces_inside_parenthesis']],
  115. [$fixers['line_ending'], $fixers['braces']],
  116. [$fixers['list_syntax'], $fixers['binary_operator_spaces']],
  117. [$fixers['list_syntax'], $fixers['ternary_operator_spaces']],
  118. [$fixers['method_argument_space'], $fixers['array_indentation']],
  119. [$fixers['method_chaining_indentation'], $fixers['array_indentation']],
  120. [$fixers['method_chaining_indentation'], $fixers['method_argument_space']],
  121. [$fixers['method_separation'], $fixers['braces']],
  122. [$fixers['method_separation'], $fixers['indentation_type']],
  123. [$fixers['multiline_whitespace_before_semicolons'], $fixers['space_after_semicolon']],
  124. [$fixers['native_constant_invocation'], $fixers['global_namespace_import']],
  125. [$fixers['native_function_invocation'], $fixers['global_namespace_import']],
  126. [$fixers['no_alias_functions'], $fixers['implode_call']],
  127. [$fixers['no_alias_functions'], $fixers['php_unit_dedicate_assert']],
  128. [$fixers['no_alternative_syntax'], $fixers['braces']],
  129. [$fixers['no_alternative_syntax'], $fixers['elseif']],
  130. [$fixers['no_alternative_syntax'], $fixers['no_superfluous_elseif']],
  131. [$fixers['no_alternative_syntax'], $fixers['no_useless_else']],
  132. [$fixers['no_alternative_syntax'], $fixers['switch_continue_to_break']],
  133. [$fixers['no_blank_lines_after_phpdoc'], $fixers['header_comment']],
  134. [$fixers['no_empty_comment'], $fixers['no_extra_blank_lines']],
  135. [$fixers['no_empty_comment'], $fixers['no_trailing_whitespace']],
  136. [$fixers['no_empty_comment'], $fixers['no_whitespace_in_blank_line']],
  137. [$fixers['no_empty_phpdoc'], $fixers['no_extra_blank_lines']],
  138. [$fixers['no_empty_phpdoc'], $fixers['no_trailing_whitespace']],
  139. [$fixers['no_empty_phpdoc'], $fixers['no_whitespace_in_blank_line']],
  140. [$fixers['no_empty_statement'], $fixers['braces']],
  141. [$fixers['no_empty_statement'], $fixers['combine_consecutive_unsets']],
  142. [$fixers['no_empty_statement'], $fixers['multiline_whitespace_before_semicolons']],
  143. [$fixers['no_empty_statement'], $fixers['no_extra_blank_lines']],
  144. [$fixers['no_empty_statement'], $fixers['no_singleline_whitespace_before_semicolons']],
  145. [$fixers['no_empty_statement'], $fixers['no_trailing_whitespace']],
  146. [$fixers['no_empty_statement'], $fixers['no_useless_else']],
  147. [$fixers['no_empty_statement'], $fixers['no_useless_return']],
  148. [$fixers['no_empty_statement'], $fixers['no_whitespace_in_blank_line']],
  149. [$fixers['no_empty_statement'], $fixers['return_assignment']],
  150. [$fixers['no_empty_statement'], $fixers['space_after_semicolon']],
  151. [$fixers['no_empty_statement'], $fixers['switch_case_semicolon_to_colon']],
  152. [$fixers['no_extra_blank_lines'], $fixers['blank_line_before_statement']],
  153. [$fixers['no_leading_import_slash'], $fixers['ordered_imports']],
  154. [$fixers['no_multiline_whitespace_around_double_arrow'], $fixers['binary_operator_spaces']],
  155. [$fixers['no_multiline_whitespace_around_double_arrow'], $fixers['trailing_comma_in_multiline']],
  156. [$fixers['no_php4_constructor'], $fixers['ordered_class_elements']],
  157. [$fixers['no_short_bool_cast'], $fixers['cast_spaces']],
  158. [$fixers['no_short_echo_tag'], $fixers['no_mixed_echo_print']],
  159. [$fixers['no_spaces_after_function_name'], $fixers['function_to_constant']],
  160. [$fixers['no_spaces_inside_parenthesis'], $fixers['function_to_constant']],
  161. [$fixers['no_superfluous_elseif'], $fixers['simplified_if_return']],
  162. [$fixers['no_superfluous_phpdoc_tags'], $fixers['no_empty_phpdoc']],
  163. [$fixers['no_superfluous_phpdoc_tags'], $fixers['void_return']],
  164. [$fixers['no_unneeded_control_parentheses'], $fixers['no_trailing_whitespace']],
  165. [$fixers['no_unneeded_curly_braces'], $fixers['no_useless_else']],
  166. [$fixers['no_unneeded_curly_braces'], $fixers['no_useless_return']],
  167. [$fixers['no_unneeded_curly_braces'], $fixers['return_assignment']],
  168. [$fixers['no_unneeded_curly_braces'], $fixers['simplified_if_return']],
  169. [$fixers['no_unset_cast'], $fixers['binary_operator_spaces']],
  170. [$fixers['no_unset_on_property'], $fixers['combine_consecutive_unsets']],
  171. [$fixers['no_unused_imports'], $fixers['blank_line_after_namespace']],
  172. [$fixers['no_unused_imports'], $fixers['no_extra_blank_lines']],
  173. [$fixers['no_unused_imports'], $fixers['no_leading_import_slash']],
  174. [$fixers['no_unused_imports'], $fixers['single_line_after_imports']],
  175. [$fixers['no_useless_else'], $fixers['braces']],
  176. [$fixers['no_useless_else'], $fixers['combine_consecutive_unsets']],
  177. [$fixers['no_useless_else'], $fixers['no_break_comment']],
  178. [$fixers['no_useless_else'], $fixers['no_extra_blank_lines']],
  179. [$fixers['no_useless_else'], $fixers['no_trailing_whitespace']],
  180. [$fixers['no_useless_else'], $fixers['no_useless_return']],
  181. [$fixers['no_useless_else'], $fixers['no_whitespace_in_blank_line']],
  182. [$fixers['no_useless_else'], $fixers['simplified_if_return']],
  183. [$fixers['no_useless_return'], $fixers['blank_line_before_return']],
  184. [$fixers['no_useless_return'], $fixers['blank_line_before_statement']],
  185. [$fixers['no_useless_return'], $fixers['no_extra_blank_lines']],
  186. [$fixers['no_useless_return'], $fixers['no_whitespace_in_blank_line']],
  187. [$fixers['no_useless_return'], $fixers['single_line_comment_style']],
  188. [$fixers['no_useless_sprintf'], $fixers['method_argument_space']],
  189. [$fixers['no_useless_sprintf'], $fixers['native_function_casing']],
  190. [$fixers['no_useless_sprintf'], $fixers['no_empty_statement']],
  191. [$fixers['no_useless_sprintf'], $fixers['no_extra_blank_lines']],
  192. [$fixers['no_useless_sprintf'], $fixers['no_spaces_inside_parenthesis']],
  193. [$fixers['nullable_type_declaration_for_default_null_value'], $fixers['no_unreachable_default_argument_value']],
  194. [$fixers['ordered_class_elements'], $fixers['class_attributes_separation']],
  195. [$fixers['ordered_class_elements'], $fixers['method_separation']],
  196. [$fixers['ordered_class_elements'], $fixers['no_blank_lines_after_class_opening']],
  197. [$fixers['ordered_class_elements'], $fixers['space_after_semicolon']],
  198. [$fixers['php_unit_construct'], $fixers['php_unit_dedicate_assert']],
  199. [$fixers['php_unit_dedicate_assert'], $fixers['php_unit_dedicate_assert_internal_type']],
  200. [$fixers['php_unit_fqcn_annotation'], $fixers['no_unused_imports']],
  201. [$fixers['php_unit_fqcn_annotation'], $fixers['phpdoc_order_by_value']],
  202. [$fixers['php_unit_internal_class'], $fixers['final_internal_class']],
  203. [$fixers['php_unit_no_expectation_annotation'], $fixers['no_empty_phpdoc']],
  204. [$fixers['php_unit_no_expectation_annotation'], $fixers['php_unit_expectation']],
  205. [$fixers['php_unit_test_annotation'], $fixers['no_empty_phpdoc']],
  206. [$fixers['php_unit_test_annotation'], $fixers['php_unit_method_casing']],
  207. [$fixers['php_unit_test_annotation'], $fixers['phpdoc_trim']],
  208. [$fixers['php_unit_test_case_static_method_calls'], $fixers['final_static_access']],
  209. [$fixers['php_unit_test_case_static_method_calls'], $fixers['self_static_accessor']],
  210. [$fixers['phpdoc_add_missing_param_annotation'], $fixers['no_empty_phpdoc']],
  211. [$fixers['phpdoc_add_missing_param_annotation'], $fixers['no_superfluous_phpdoc_tags']],
  212. [$fixers['phpdoc_add_missing_param_annotation'], $fixers['phpdoc_align']],
  213. [$fixers['phpdoc_add_missing_param_annotation'], $fixers['phpdoc_order']],
  214. [$fixers['phpdoc_annotation_without_dot'], $fixers['phpdoc_types']],
  215. [$fixers['phpdoc_annotation_without_dot'], $fixers['phpdoc_types_order']],
  216. [$fixers['phpdoc_no_access'], $fixers['no_empty_phpdoc']],
  217. [$fixers['phpdoc_no_access'], $fixers['phpdoc_separation']],
  218. [$fixers['phpdoc_no_access'], $fixers['phpdoc_trim']],
  219. [$fixers['phpdoc_no_alias_tag'], $fixers['phpdoc_add_missing_param_annotation']],
  220. [$fixers['phpdoc_no_alias_tag'], $fixers['phpdoc_single_line_var_spacing']],
  221. [$fixers['phpdoc_no_empty_return'], $fixers['no_empty_phpdoc']],
  222. [$fixers['phpdoc_no_empty_return'], $fixers['phpdoc_order']],
  223. [$fixers['phpdoc_no_empty_return'], $fixers['phpdoc_separation']],
  224. [$fixers['phpdoc_no_empty_return'], $fixers['phpdoc_trim']],
  225. [$fixers['phpdoc_no_package'], $fixers['no_empty_phpdoc']],
  226. [$fixers['phpdoc_no_package'], $fixers['phpdoc_separation']],
  227. [$fixers['phpdoc_no_package'], $fixers['phpdoc_trim']],
  228. [$fixers['phpdoc_no_useless_inheritdoc'], $fixers['no_empty_phpdoc']],
  229. [$fixers['phpdoc_no_useless_inheritdoc'], $fixers['no_trailing_whitespace_in_comment']],
  230. [$fixers['phpdoc_order'], $fixers['phpdoc_separation']],
  231. [$fixers['phpdoc_order'], $fixers['phpdoc_trim']],
  232. [$fixers['phpdoc_return_self_reference'], $fixers['no_superfluous_phpdoc_tags']],
  233. [$fixers['phpdoc_scalar'], $fixers['phpdoc_to_return_type']],
  234. [$fixers['phpdoc_to_comment'], $fixers['no_empty_comment']],
  235. [$fixers['phpdoc_to_comment'], $fixers['phpdoc_no_useless_inheritdoc']],
  236. [$fixers['phpdoc_to_param_type'], $fixers['no_superfluous_phpdoc_tags']],
  237. [$fixers['phpdoc_to_property_type'], $fixers['no_superfluous_phpdoc_tags']],
  238. [$fixers['phpdoc_to_return_type'], $fixers['fully_qualified_strict_types']],
  239. [$fixers['phpdoc_to_return_type'], $fixers['no_superfluous_phpdoc_tags']],
  240. [$fixers['phpdoc_to_return_type'], $fixers['return_type_declaration']],
  241. [$fixers['phpdoc_types'], $fixers['phpdoc_to_return_type']],
  242. [$fixers['pow_to_exponentiation'], $fixers['binary_operator_spaces']],
  243. [$fixers['pow_to_exponentiation'], $fixers['method_argument_space']],
  244. [$fixers['pow_to_exponentiation'], $fixers['native_function_casing']],
  245. [$fixers['pow_to_exponentiation'], $fixers['no_spaces_after_function_name']],
  246. [$fixers['pow_to_exponentiation'], $fixers['no_spaces_inside_parenthesis']],
  247. [$fixers['protected_to_private'], $fixers['ordered_class_elements']],
  248. [$fixers['return_assignment'], $fixers['blank_line_before_statement']],
  249. [$fixers['semicolon_after_instruction'], $fixers['simplified_if_return']],
  250. [$fixers['simplified_if_return'], $fixers['no_multiline_whitespace_before_semicolons']],
  251. [$fixers['simplified_if_return'], $fixers['no_singleline_whitespace_before_semicolons']],
  252. [$fixers['simplified_null_return'], $fixers['no_useless_return']],
  253. [$fixers['simplified_null_return'], $fixers['void_return']],
  254. [$fixers['single_class_element_per_statement'], $fixers['class_attributes_separation']],
  255. [$fixers['single_import_per_statement'], $fixers['multiline_whitespace_before_semicolons']],
  256. [$fixers['single_import_per_statement'], $fixers['no_leading_import_slash']],
  257. [$fixers['single_import_per_statement'], $fixers['no_singleline_whitespace_before_semicolons']],
  258. [$fixers['single_import_per_statement'], $fixers['no_unused_imports']],
  259. [$fixers['single_import_per_statement'], $fixers['space_after_semicolon']],
  260. [$fixers['single_line_throw'], $fixers['braces']],
  261. [$fixers['single_line_throw'], $fixers['concat_space']],
  262. [$fixers['single_space_after_construct'], $fixers['braces']],
  263. [$fixers['single_space_after_construct'], $fixers['function_declaration']],
  264. [$fixers['single_trait_insert_per_statement'], $fixers['braces']],
  265. [$fixers['single_trait_insert_per_statement'], $fixers['space_after_semicolon']],
  266. [$fixers['standardize_increment'], $fixers['increment_style']],
  267. [$fixers['standardize_not_equals'], $fixers['binary_operator_spaces']],
  268. [$fixers['strict_comparison'], $fixers['binary_operator_spaces']],
  269. [$fixers['strict_param'], $fixers['native_function_invocation']],
  270. [$fixers['ternary_to_elvis_operator'], $fixers['no_trailing_whitespace']],
  271. [$fixers['ternary_to_elvis_operator'], $fixers['ternary_operator_spaces']],
  272. [$fixers['unary_operator_spaces'], $fixers['not_operator_with_space']],
  273. [$fixers['unary_operator_spaces'], $fixers['not_operator_with_successor_space']],
  274. [$fixers['void_return'], $fixers['phpdoc_no_empty_return']],
  275. [$fixers['void_return'], $fixers['return_type_declaration']],
  276. ];
  277. }
  278. public function provideFixersPrioritySpecialPhpdocCases()
  279. {
  280. $factory = new FixerFactory();
  281. $factory->registerBuiltInFixers();
  282. $fixers = [];
  283. foreach ($factory->getFixers() as $fixer) {
  284. $fixers[$fixer->getName()] = $fixer;
  285. }
  286. $cases = [];
  287. // Prepare bulk tests for phpdoc fixers to test that:
  288. // * `align_multiline_comment` is first
  289. // * `comment_to_phpdoc` is second
  290. // * `phpdoc_to_comment` is third
  291. // * `phpdoc_indent` is fourth
  292. // * `phpdoc_types` is fifth
  293. // * `phpdoc_scalar` is sixth
  294. // * `phpdoc_align` is last
  295. // Add these cases in test-order instead of alphabetical
  296. $cases[] = [$fixers['align_multiline_comment'], $fixers['comment_to_phpdoc']];
  297. $cases[] = [$fixers['comment_to_phpdoc'], $fixers['phpdoc_to_comment']];
  298. $cases[] = [$fixers['phpdoc_to_comment'], $fixers['phpdoc_indent']];
  299. $cases[] = [$fixers['phpdoc_indent'], $fixers['phpdoc_types']];
  300. $cases[] = [$fixers['phpdoc_types'], $fixers['phpdoc_scalar']];
  301. $docFixerNames = array_filter(
  302. array_keys($fixers),
  303. static function ($name) {
  304. return false !== strpos($name, 'phpdoc');
  305. }
  306. );
  307. foreach ($docFixerNames as $docFixerName) {
  308. if (!\in_array($docFixerName, ['comment_to_phpdoc', 'phpdoc_to_comment', 'phpdoc_indent', 'phpdoc_types', 'phpdoc_scalar'], true)) {
  309. $cases[] = [$fixers['align_multiline_comment'], $fixers[$docFixerName]];
  310. $cases[] = [$fixers['comment_to_phpdoc'], $fixers[$docFixerName]];
  311. $cases[] = [$fixers['phpdoc_indent'], $fixers[$docFixerName]];
  312. $cases[] = [$fixers['phpdoc_to_comment'], $fixers[$docFixerName]];
  313. if ('phpdoc_annotation_without_dot' !== $docFixerName) {
  314. $cases[] = [$fixers['phpdoc_scalar'], $fixers[$docFixerName]];
  315. $cases[] = [$fixers['phpdoc_types'], $fixers[$docFixerName]];
  316. }
  317. }
  318. if ('phpdoc_align' !== $docFixerName) {
  319. $cases[] = [$fixers[$docFixerName], $fixers['phpdoc_align']];
  320. }
  321. }
  322. return $cases;
  323. }
  324. /**
  325. * @dataProvider provideFixersPriorityPairsHaveIntegrationTestCases
  326. */
  327. public function testFixersPriorityPairsHaveIntegrationTest(FixerInterface $first, FixerInterface $second)
  328. {
  329. $integrationTestName = $this->generateIntegrationTestName($first, $second);
  330. $file = $this->getIntegrationPriorityDirectory().$integrationTestName;
  331. if (is_file($file)) {
  332. $description = sprintf('Integration of fixers: %s,%s.', $first->getName(), $second->getName());
  333. $integrationTestExists = true;
  334. } else {
  335. $file = $this->getIntegrationPriorityDirectory().$this->generateIntegrationTestName($second, $first);
  336. $description = sprintf('Integration of fixers: %s,%s.', $second->getName(), $first->getName());
  337. $integrationTestExists = is_file($file);
  338. }
  339. static::assertTrue($integrationTestExists, sprintf('There shall be an integration test "%s". How do you know that priority set up is good, if there is no integration test to check it?', $integrationTestName));
  340. $file = realpath($file);
  341. $factory = new IntegrationCaseFactory();
  342. $test = $factory->create(new SplFileInfo($file, './', __DIR__));
  343. $rules = $test->getRuleset()->getRules();
  344. $expected = [$first->getName(), $second->getName()];
  345. $actual = array_keys($rules);
  346. sort($expected);
  347. sort($actual);
  348. static::assertSame($description, $test->getTitle(), sprintf('Please fix the title in "%s".', $file));
  349. static::assertCount(2, $rules, sprintf('Only the two rules that are tested for priority should be in the ruleset of "%s".', $file));
  350. foreach ($rules as $name => $config) {
  351. static::assertNotFalse($config, sprintf('The rule "%s" in "%s" may not be disabled for the test.', $name, $file));
  352. }
  353. static::assertSame($expected, $actual, sprintf('The ruleset of "%s" must contain the rules for the priority test.', $file));
  354. }
  355. public function provideFixersPriorityPairsHaveIntegrationTestCases()
  356. {
  357. return array_filter(
  358. $this->provideFixersPriorityCases(),
  359. // ignore speed-up only priorities set up
  360. function (array $case) {
  361. return !\in_array(
  362. $this->generateIntegrationTestName($case[0], $case[1]),
  363. [
  364. 'function_to_constant,native_function_casing.test',
  365. 'no_unused_imports,no_leading_import_slash.test',
  366. 'pow_to_exponentiation,method_argument_space.test',
  367. 'pow_to_exponentiation,native_function_casing.test',
  368. 'pow_to_exponentiation,no_spaces_after_function_name.test',
  369. 'pow_to_exponentiation,no_spaces_inside_parenthesis.test',
  370. 'no_useless_sprintf,native_function_casing.test',
  371. ],
  372. true
  373. );
  374. }
  375. );
  376. }
  377. public function testPriorityIntegrationDirectoryOnlyContainsFiles()
  378. {
  379. foreach (new \DirectoryIterator($this->getIntegrationPriorityDirectory()) as $candidate) {
  380. if ($candidate->isDot()) {
  381. continue;
  382. }
  383. $fileName = $candidate->getFilename();
  384. static::assertTrue($candidate->isFile(), sprintf('Expected only files in the priority integration test directory, got "%s".', $fileName));
  385. static::assertFalse($candidate->isLink(), sprintf('No (sym)links expected the priority integration test directory, got "%s".', $fileName));
  386. }
  387. }
  388. /**
  389. * @dataProvider provideIntegrationTestFilesCases
  390. *
  391. * @param string $fileName
  392. */
  393. public function testPriorityIntegrationTestFilesAreListedPriorityCases($fileName)
  394. {
  395. static $priorityCases;
  396. if (null === $priorityCases) {
  397. $priorityCases = [];
  398. foreach ($this->provideFixersPriorityCases() as $priorityCase) {
  399. $fixerName = $priorityCase[0]->getName();
  400. if (!isset($priorityCases[$fixerName])) {
  401. $priorityCases[$fixerName] = [];
  402. }
  403. $priorityCases[$fixerName][$priorityCase[1]->getName()] = true;
  404. }
  405. ksort($priorityCases);
  406. }
  407. static::assertSame(
  408. 1,
  409. preg_match('#^([a-z][a-z0-9_]*),([a-z][a-z_]*)(?:_\d{1,3})?\.test(-(in|out)\.php)?$#', $fileName, $matches),
  410. sprintf('File with unexpected name "%s" in the priority integration test directory.', $fileName)
  411. );
  412. $fixerName1 = $matches[1];
  413. $fixerName2 = $matches[2];
  414. static::assertTrue(
  415. isset($priorityCases[$fixerName1][$fixerName2]),
  416. sprintf('Missing priority test entry for file "%s".', $fileName)
  417. );
  418. }
  419. public function provideIntegrationTestFilesCases()
  420. {
  421. $fileNames = [];
  422. foreach (new \DirectoryIterator($this->getIntegrationPriorityDirectory()) as $candidate) {
  423. if ($candidate->isDot()) {
  424. continue;
  425. }
  426. $fileNames[] = [$candidate->getFilename()];
  427. }
  428. sort($fileNames);
  429. return $fileNames;
  430. }
  431. public function testProvideFixersPriorityCasesAreSorted()
  432. {
  433. $cases = $this->provideFixersPriorityCases();
  434. $sorted = $cases;
  435. usort(
  436. $sorted,
  437. /**
  438. * @param array<FixerInterface> $priorityPair1
  439. * @param array<FixerInterface> $priorityPair2
  440. */
  441. static function (array $priorityPair1, array $priorityPair2) {
  442. $fixer1 = $priorityPair1[0];
  443. $fixer2 = $priorityPair2[0];
  444. if ($fixer1->getName() === $fixer2->getName()) {
  445. $fixer1 = $priorityPair1[1];
  446. $fixer2 = $priorityPair2[1];
  447. }
  448. return strcmp($fixer1->getName(), $fixer2->getName());
  449. }
  450. );
  451. if ($sorted !== $cases) { // PHPUnit takes a very long time creating a diff view on the arrays
  452. $casesDescription = '';
  453. foreach ($cases as $pair) {
  454. $casesDescription .= sprintf("\n%s/%s", $pair[0]->getName(), $pair[1]->getName());
  455. }
  456. $sortedDescription = '';
  457. foreach ($sorted as $pair) {
  458. $sortedDescription .= sprintf("\n%s/%s", $pair[0]->getName(), $pair[1]->getName());
  459. }
  460. static::assertSame($sortedDescription, $casesDescription);
  461. } else {
  462. $this->addToAssertionCount(1);
  463. }
  464. }
  465. public function testFixerPriorityComment()
  466. {
  467. $cases = array_merge(
  468. $this->provideFixersPriorityCases(),
  469. $this->provideFixersPrioritySpecialPhpdocCases()
  470. );
  471. $map = [];
  472. foreach ($cases as $beforeAfter) {
  473. list($before, $after) = $beforeAfter;
  474. $beforeClass = \get_class($before);
  475. $afterClass = \get_class($after);
  476. $beforeName = substr($beforeClass, strrpos($beforeClass, '\\') + 1);
  477. $afterName = substr($afterClass, strrpos($afterClass, '\\') + 1);
  478. if (!isset($map[$beforeName])) {
  479. $map[$beforeName] = [
  480. 'before' => [],
  481. 'after' => [],
  482. 'class' => $beforeClass,
  483. ];
  484. }
  485. $map[$beforeName]['before'][] = $afterName;
  486. if (!isset($map[$afterName])) {
  487. $map[$afterName] = [
  488. 'before' => [],
  489. 'after' => [],
  490. 'class' => $afterClass,
  491. ];
  492. }
  493. $map[$afterName]['after'][] = $beforeName;
  494. }
  495. $fixersPhpDocIssues = [];
  496. foreach ($map as $fixerName => $priorityMap) {
  497. $expectedMessage = "/**\n * {@inheritdoc}\n *";
  498. if (\count($priorityMap['before']) > 0) {
  499. sort($priorityMap['before']);
  500. $expectedMessage .= sprintf("\n * Must run before %s.", implode(', ', $priorityMap['before']));
  501. }
  502. // @phpstan-ignore-next-line to avoid `Comparison operation ">" between int<1, max> and 0 is always true.`
  503. if (\count($priorityMap['after']) > 0) {
  504. sort($priorityMap['after']);
  505. $expectedMessage .= sprintf("\n * Must run after %s.", implode(', ', $priorityMap['after']));
  506. }
  507. $expectedMessage .= "\n */";
  508. $reflection = new \ReflectionClass($priorityMap['class']);
  509. $method = $reflection->getMethod('getPriority');
  510. $phpDoc = $method->getDocComment();
  511. if (false === $phpDoc) {
  512. $fixersPhpDocIssues[$fixerName] = sprintf("PHPDoc for %s::getPriority is missing.\nExpected:\n%s", $fixerName, $expectedMessage);
  513. continue;
  514. }
  515. if ($expectedMessage !== $phpDoc) {
  516. $fixersPhpDocIssues[$fixerName] = sprintf("PHPDoc for %s::getPriority is not as expected.\nExpected:\n%s", $fixerName, $expectedMessage);
  517. continue;
  518. }
  519. }
  520. if (0 === \count($fixersPhpDocIssues)) {
  521. $this->addToAssertionCount(1);
  522. } else {
  523. $message = sprintf("There are %d priority PHPDoc issues found.\n", \count($fixersPhpDocIssues));
  524. ksort($fixersPhpDocIssues);
  525. foreach ($fixersPhpDocIssues as $fixerName => $issue) {
  526. $message .= sprintf("\n--------------------------------------------------\n%s\n%s", $fixerName, $issue);
  527. }
  528. static::fail($message);
  529. }
  530. }
  531. /**
  532. * @return string
  533. */
  534. private function generateIntegrationTestName(FixerInterface $first, FixerInterface $second)
  535. {
  536. return "{$first->getName()},{$second->getName()}.test";
  537. }
  538. /**
  539. * @return string
  540. */
  541. private function getIntegrationPriorityDirectory()
  542. {
  543. return __DIR__.'/../Fixtures/Integration/priority/';
  544. }
  545. }