FixerFactoryTest.php 36 KB

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