FixerFactoryTest.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940
  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. *
  25. * @group auto-review
  26. * @group covers-nothing
  27. */
  28. final class FixerFactoryTest extends TestCase
  29. {
  30. public function testFixersPriorityEdgeFixers(): void
  31. {
  32. $factory = new FixerFactory();
  33. $factory->registerBuiltInFixers();
  34. $fixers = $factory->getFixers();
  35. foreach (self::getFixerWithFixedPosition() as $fixerName => $offset) {
  36. if ($offset < 0) {
  37. self::assertSame($fixerName, $fixers[\count($fixers) + $offset]->getName(), $fixerName);
  38. } else {
  39. self::assertSame($fixerName, $fixers[$offset]->getName(), $fixerName);
  40. }
  41. }
  42. }
  43. public function testFixersPriority(): void
  44. {
  45. $fixers = self::getAllFixers();
  46. $graphs = [
  47. self::getFixersPriorityGraph(),
  48. self::getPhpDocFixersPriorityGraph(),
  49. ];
  50. foreach ($graphs as $graph) {
  51. foreach ($graph as $fixerName => $edges) {
  52. $first = $fixers[$fixerName];
  53. foreach ($edges as $edge) {
  54. $second = $fixers[$edge];
  55. self::assertLessThan($first->getPriority(), $second->getPriority(), sprintf('"%s" should have less priority than "%s"', $edge, $fixerName));
  56. }
  57. }
  58. }
  59. }
  60. /**
  61. * @param string[] $edges
  62. *
  63. * @dataProvider provideFixersPriorityCasesHaveIntegrationCases
  64. */
  65. public function testFixersPriorityCasesHaveIntegrationTest(string $fixerName, array $edges): void
  66. {
  67. static $forPerformanceEdgesOnly = [
  68. 'function_to_constant' => [
  69. 'native_function_casing' => true,
  70. ],
  71. 'no_unused_imports' => [
  72. 'no_leading_import_slash' => true,
  73. ],
  74. 'no_useless_sprintf' => [
  75. 'native_function_casing' => true,
  76. ],
  77. 'pow_to_exponentiation' => [
  78. 'method_argument_space' => true,
  79. 'native_function_casing' => true,
  80. 'no_spaces_after_function_name' => true,
  81. 'no_spaces_inside_parenthesis' => true,
  82. ],
  83. ];
  84. $missingIntegrationsTests = [];
  85. foreach ($edges as $edge) {
  86. if (isset($forPerformanceEdgesOnly[$fixerName][$edge])) {
  87. continue;
  88. }
  89. $file = self::getIntegrationPriorityDirectory().$fixerName.','.$edge.'.test';
  90. if (!is_file($file)) {
  91. $missingIntegrationsTests[] = $file;
  92. continue;
  93. }
  94. $file = realpath($file);
  95. $factory = new IntegrationCaseFactory();
  96. $test = $factory->create(new SplFileInfo($file, './', __DIR__));
  97. $rules = $test->getRuleset()->getRules();
  98. $expected = [$fixerName, $edge];
  99. $actual = array_keys($rules);
  100. sort($expected);
  101. sort($actual);
  102. self::assertSame(
  103. sprintf('Integration of fixers: %s,%s.', $fixerName, $edge),
  104. $test->getTitle(),
  105. sprintf('Please fix the title in "%s".', $file)
  106. );
  107. self::assertCount(2, $rules, sprintf('Only the two rules that are tested for priority should be in the ruleset of "%s".', $file));
  108. foreach ($rules as $name => $config) {
  109. self::assertNotFalse($config, sprintf('The rule "%s" in "%s" may not be disabled for the test.', $name, $file));
  110. }
  111. self::assertSame($expected, $actual, sprintf('The ruleset of "%s" must contain the rules for the priority test.', $file));
  112. }
  113. self::assertCount(0, $missingIntegrationsTests, sprintf("There shall be an integration test. How do you know that priority set up is good, if there is no integration test to check it?\nMissing:\n- %s", implode("\n- ", $missingIntegrationsTests)));
  114. }
  115. public static function provideFixersPriorityCasesHaveIntegrationCases(): iterable
  116. {
  117. foreach (self::getFixersPriorityGraph() as $fixerName => $edges) {
  118. yield $fixerName => [$fixerName, $edges];
  119. }
  120. }
  121. /**
  122. * @dataProvider provideIntegrationTestFilesCases
  123. */
  124. public function testPriorityIntegrationTestFilesAreListedAsPriorityCases(\SplFileInfo $file): void
  125. {
  126. $fileName = $file->getFilename();
  127. self::assertTrue($file->isFile(), sprintf('Expected only files in the priority integration test directory, got "%s".', $fileName));
  128. self::assertFalse($file->isLink(), sprintf('No (sym)links expected the priority integration test directory, got "%s".', $fileName));
  129. self::assertSame(
  130. 1,
  131. preg_match('#^([a-z][a-z0-9_]*),([a-z][a-z_]*)(?:_\d{1,3})?\.test(-(in|out)\.php)?$#', $fileName, $matches),
  132. sprintf('File with unexpected name "%s" in the priority integration test directory.', $fileName)
  133. );
  134. [, $fixerName1, $fixerName2] = $matches;
  135. $graph = self::getFixersPriorityGraph();
  136. self::assertTrue(
  137. isset($graph[$fixerName1]) && \in_array($fixerName2, $graph[$fixerName1], true),
  138. sprintf('Missing priority test entry for file "%s".', $fileName)
  139. );
  140. }
  141. public static function provideIntegrationTestFilesCases(): iterable
  142. {
  143. foreach (new \DirectoryIterator(self::getIntegrationPriorityDirectory()) as $candidate) {
  144. if (!$candidate->isDot()) {
  145. yield [clone $candidate];
  146. }
  147. }
  148. }
  149. public function testFixersPriorityGraphIsSorted(): void
  150. {
  151. $previous = '';
  152. foreach (self::getFixersPriorityGraph() as $fixerName => $edges) {
  153. self::assertLessThan(0, strcmp($previous, $fixerName), sprintf('Not sorted "%s" "%s".', $previous, $fixerName));
  154. $edgesSorted = $edges;
  155. sort($edgesSorted);
  156. self::assertSame($edgesSorted, $edges, sprintf('Fixer "%s" edges are not sorted', $fixerName));
  157. $previous = $fixerName;
  158. }
  159. }
  160. public function testFixersPriorityComment(): void
  161. {
  162. $fixersPhpDocIssues = [];
  163. $fixers = self::getAllFixers();
  164. foreach ($fixers as $name => $fixer) {
  165. $reflection = new \ReflectionObject($fixer);
  166. $fixers[$name] = ['reflection' => $reflection, 'short_classname' => $reflection->getShortName()];
  167. }
  168. $mergedGraph = array_merge_recursive(
  169. self::getFixersPriorityGraph(),
  170. self::getPhpDocFixersPriorityGraph()
  171. );
  172. // expend $graph
  173. $graph = [];
  174. foreach ($mergedGraph as $fixerName => $edges) {
  175. if (!isset($graph[$fixerName]['before'])) {
  176. $graph[$fixerName] = ['before' => []];
  177. }
  178. foreach ($mergedGraph as $candidateFixer => $candidateEdges) {
  179. if (\in_array($fixerName, $candidateEdges, true)) {
  180. $graph[$fixerName]['after'][$candidateFixer] = true;
  181. }
  182. }
  183. foreach ($edges as $edge) {
  184. if (!isset($graph[$edge]['after'])) {
  185. $graph[$edge] = ['after' => []];
  186. }
  187. $graph[$edge]['after'][$fixerName] = true;
  188. $graph[$fixerName]['before'][$edge] = true;
  189. }
  190. }
  191. foreach ($graph as $fixerName => $edges) {
  192. $expectedMessage = "/**\n * {@inheritdoc}\n *";
  193. foreach ($edges as $label => $others) {
  194. if (\count($others) > 0) {
  195. $shortClassNames = [];
  196. foreach ($others as $other => $foo) {
  197. $shortClassNames[$other] = $fixers[$other]['short_classname'];
  198. }
  199. sort($shortClassNames);
  200. $expectedMessage .= sprintf("\n * Must run %s %s.", $label, implode(', ', $shortClassNames));
  201. }
  202. }
  203. $expectedMessage .= "\n */";
  204. $method = $fixers[$fixerName]['reflection']->getMethod('getPriority');
  205. $phpDoc = $method->getDocComment();
  206. if (false === $phpDoc) {
  207. $fixersPhpDocIssues[$fixerName] = sprintf("PHPDoc for %s::getPriority is missing.\nExpected:\n%s", $fixerName, $expectedMessage);
  208. } elseif ($expectedMessage !== $phpDoc) {
  209. $fixersPhpDocIssues[$fixerName] = sprintf("PHPDoc for %s::getPriority is not as expected.\nExpected:\n%s", $fixerName, $expectedMessage);
  210. }
  211. }
  212. if (0 === \count($fixersPhpDocIssues)) {
  213. $this->addToAssertionCount(1);
  214. } else {
  215. $message = sprintf("There are %d priority PHPDoc issues found.\n", \count($fixersPhpDocIssues));
  216. ksort($fixersPhpDocIssues);
  217. foreach ($fixersPhpDocIssues as $fixerName => $issue) {
  218. $message .= sprintf("\n--------------------------------------------------\n%s\n%s", $fixers[$fixerName]['short_classname'], $issue);
  219. }
  220. self::fail($message);
  221. }
  222. }
  223. public function testFixerWithNoneDefaultPriorityIsTested(): void
  224. {
  225. $knownIssues = [ // should only shrink
  226. 'final_class' => true,
  227. 'no_trailing_comma_in_singleline_function_call' => true, // had prio case but no longer, left prio the same for BC reasons, rule has been deprecated
  228. 'psr_autoloading' => true,
  229. 'simple_to_complex_string_variable' => true, // had prio case but no longer, left prio the same for BC reasons
  230. 'single_blank_line_before_namespace' => true,
  231. ];
  232. $factory = new FixerFactory();
  233. $factory->registerBuiltInFixers();
  234. $fixers = $factory->getFixers();
  235. $fixerNamesWithTests = [];
  236. foreach (self::getFixerWithFixedPosition() as $fixerName => $priority) {
  237. $fixerNamesWithTests[$fixerName] = true;
  238. }
  239. foreach ([
  240. self::getFixersPriorityGraph(),
  241. self::getPhpDocFixersPriorityGraph(),
  242. ] as $set) {
  243. foreach ($set as $fixerName => $edges) {
  244. $fixerNamesWithTests[$fixerName] = true;
  245. foreach ($edges as $edge) {
  246. $fixerNamesWithTests[$edge] = true;
  247. }
  248. }
  249. }
  250. $missing = [];
  251. foreach ($fixers as $fixer) {
  252. $fixerName = $fixer->getName();
  253. if (0 !== $fixer->getPriority() && !isset($fixerNamesWithTests[$fixerName])) {
  254. $missing[$fixerName] = true;
  255. }
  256. }
  257. foreach ($knownIssues as $knownIssue => $true) {
  258. if (isset($missing[$knownIssue])) {
  259. unset($missing[$knownIssue]);
  260. } else {
  261. self::fail(sprintf('No longer found known issue "%s", please update the set.', $knownIssue));
  262. }
  263. }
  264. self::assertEmpty($missing, 'Fixers without default priority and without priority tests: "'.implode('", "', array_keys($missing)).'."');
  265. }
  266. /**
  267. * @return array<string, string[]>
  268. */
  269. private static function getFixersPriorityGraph(): array
  270. {
  271. return [
  272. 'align_multiline_comment' => [
  273. 'phpdoc_trim_consecutive_blank_line_separation',
  274. ],
  275. 'array_indentation' => [
  276. 'align_multiline_comment',
  277. 'binary_operator_spaces',
  278. ],
  279. 'array_syntax' => [
  280. 'binary_operator_spaces',
  281. 'ternary_operator_spaces',
  282. ],
  283. 'assign_null_coalescing_to_coalesce_equal' => [
  284. 'binary_operator_spaces',
  285. 'no_whitespace_in_blank_line',
  286. ],
  287. 'backtick_to_shell_exec' => [
  288. 'escape_implicit_backslashes',
  289. 'explicit_string_variable',
  290. 'native_function_invocation',
  291. 'single_quote',
  292. ],
  293. 'blank_line_after_opening_tag' => [
  294. 'no_blank_lines_before_namespace',
  295. ],
  296. 'braces' => [
  297. 'heredoc_indentation',
  298. ],
  299. 'class_attributes_separation' => [
  300. 'braces',
  301. 'indentation_type',
  302. 'no_extra_blank_lines',
  303. 'statement_indentation',
  304. ],
  305. 'class_definition' => [
  306. 'braces',
  307. 'single_line_empty_body',
  308. ],
  309. 'class_keyword_remove' => [
  310. 'no_unused_imports',
  311. ],
  312. 'combine_consecutive_issets' => [
  313. 'multiline_whitespace_before_semicolons',
  314. 'no_singleline_whitespace_before_semicolons',
  315. 'no_spaces_inside_parenthesis',
  316. 'no_trailing_whitespace',
  317. 'no_whitespace_in_blank_line',
  318. ],
  319. 'combine_consecutive_unsets' => [
  320. 'no_extra_blank_lines',
  321. 'no_trailing_whitespace',
  322. 'no_whitespace_in_blank_line',
  323. 'space_after_semicolon',
  324. ],
  325. 'combine_nested_dirname' => [
  326. 'method_argument_space',
  327. 'no_spaces_inside_parenthesis',
  328. ],
  329. 'control_structure_braces' => [
  330. 'control_structure_continuation_position',
  331. 'curly_braces_position',
  332. 'no_multiple_statements_per_line',
  333. ],
  334. 'curly_braces_position' => [
  335. 'single_line_empty_body',
  336. ],
  337. 'declare_strict_types' => [
  338. 'blank_line_after_opening_tag',
  339. 'declare_equal_normalize',
  340. 'header_comment',
  341. ],
  342. 'dir_constant' => [
  343. 'combine_nested_dirname',
  344. ],
  345. 'doctrine_annotation_array_assignment' => [
  346. 'doctrine_annotation_spaces',
  347. ],
  348. 'echo_tag_syntax' => [
  349. 'no_mixed_echo_print',
  350. ],
  351. 'empty_loop_body' => [
  352. 'braces',
  353. 'no_extra_blank_lines',
  354. 'no_trailing_whitespace',
  355. ],
  356. 'empty_loop_condition' => [
  357. 'no_extra_blank_lines',
  358. 'no_trailing_whitespace',
  359. ],
  360. 'escape_implicit_backslashes' => [
  361. 'heredoc_to_nowdoc',
  362. 'single_quote',
  363. ],
  364. 'final_internal_class' => [
  365. 'protected_to_private',
  366. 'self_static_accessor',
  367. ],
  368. 'fully_qualified_strict_types' => [
  369. 'no_superfluous_phpdoc_tags',
  370. ],
  371. 'function_declaration' => [
  372. 'method_argument_space',
  373. ],
  374. 'function_to_constant' => [
  375. 'native_function_casing',
  376. 'no_extra_blank_lines',
  377. 'no_singleline_whitespace_before_semicolons',
  378. 'no_trailing_whitespace',
  379. 'no_whitespace_in_blank_line',
  380. 'self_static_accessor',
  381. ],
  382. 'general_phpdoc_annotation_remove' => [
  383. 'no_empty_phpdoc',
  384. 'phpdoc_line_span',
  385. 'phpdoc_separation',
  386. 'phpdoc_trim',
  387. ],
  388. 'general_phpdoc_tag_rename' => [
  389. 'phpdoc_add_missing_param_annotation',
  390. ],
  391. 'get_class_to_class_keyword' => [
  392. 'multiline_whitespace_before_semicolons',
  393. ],
  394. 'global_namespace_import' => [
  395. 'no_unused_imports',
  396. 'ordered_imports',
  397. ],
  398. 'header_comment' => [
  399. 'single_line_comment_style',
  400. ],
  401. 'implode_call' => [
  402. 'method_argument_space',
  403. ],
  404. 'increment_style' => [
  405. 'no_spaces_inside_parenthesis',
  406. ],
  407. 'indentation_type' => [
  408. 'phpdoc_indent',
  409. ],
  410. 'is_null' => [
  411. 'yoda_style',
  412. ],
  413. 'lambda_not_used_import' => [
  414. 'method_argument_space',
  415. 'no_spaces_inside_parenthesis',
  416. ],
  417. 'list_syntax' => [
  418. 'binary_operator_spaces',
  419. 'ternary_operator_spaces',
  420. ],
  421. 'method_argument_space' => [
  422. 'array_indentation',
  423. ],
  424. 'modernize_strpos' => [
  425. 'binary_operator_spaces',
  426. 'no_extra_blank_lines',
  427. 'no_spaces_inside_parenthesis',
  428. 'no_trailing_whitespace',
  429. 'not_operator_with_space',
  430. 'not_operator_with_successor_space',
  431. 'php_unit_dedicate_assert',
  432. 'single_space_after_construct',
  433. 'single_space_around_construct',
  434. ],
  435. 'modernize_types_casting' => [
  436. 'no_unneeded_control_parentheses',
  437. ],
  438. 'multiline_whitespace_before_semicolons' => [
  439. 'space_after_semicolon',
  440. ],
  441. 'native_constant_invocation' => [
  442. 'global_namespace_import',
  443. ],
  444. 'native_function_invocation' => [
  445. 'global_namespace_import',
  446. ],
  447. 'new_with_braces' => [
  448. 'class_definition',
  449. ],
  450. 'no_alias_functions' => [
  451. 'implode_call',
  452. 'php_unit_dedicate_assert',
  453. ],
  454. 'no_alternative_syntax' => [
  455. 'braces',
  456. 'elseif',
  457. 'no_superfluous_elseif',
  458. 'no_unneeded_control_parentheses',
  459. 'no_useless_else',
  460. 'switch_continue_to_break',
  461. ],
  462. 'no_binary_string' => [
  463. 'no_useless_concat_operator',
  464. 'php_unit_dedicate_assert_internal_type',
  465. 'regular_callable_call',
  466. 'set_type_to_cast',
  467. ],
  468. 'no_blank_lines_after_phpdoc' => [
  469. 'header_comment',
  470. ],
  471. 'no_empty_comment' => [
  472. 'no_extra_blank_lines',
  473. 'no_trailing_whitespace',
  474. 'no_whitespace_in_blank_line',
  475. ],
  476. 'no_empty_phpdoc' => [
  477. 'no_extra_blank_lines',
  478. 'no_trailing_whitespace',
  479. 'no_whitespace_in_blank_line',
  480. ],
  481. 'no_empty_statement' => [
  482. 'braces',
  483. 'combine_consecutive_unsets',
  484. 'empty_loop_body',
  485. 'multiline_whitespace_before_semicolons',
  486. 'no_extra_blank_lines',
  487. 'no_multiple_statements_per_line',
  488. 'no_singleline_whitespace_before_semicolons',
  489. 'no_trailing_whitespace',
  490. 'no_useless_else',
  491. 'no_useless_return',
  492. 'no_whitespace_in_blank_line',
  493. 'return_assignment',
  494. 'space_after_semicolon',
  495. 'switch_case_semicolon_to_colon',
  496. ],
  497. 'no_extra_blank_lines' => [
  498. 'blank_line_before_statement',
  499. ],
  500. 'no_leading_import_slash' => [
  501. 'ordered_imports',
  502. ],
  503. 'no_multiline_whitespace_around_double_arrow' => [
  504. 'binary_operator_spaces',
  505. 'method_argument_space',
  506. ],
  507. 'no_php4_constructor' => [
  508. 'ordered_class_elements',
  509. ],
  510. 'no_short_bool_cast' => [
  511. 'cast_spaces',
  512. ],
  513. 'no_spaces_after_function_name' => [
  514. 'function_to_constant',
  515. 'get_class_to_class_keyword',
  516. ],
  517. 'no_spaces_inside_parenthesis' => [
  518. 'function_to_constant',
  519. 'get_class_to_class_keyword',
  520. 'string_length_to_empty',
  521. ],
  522. 'no_superfluous_elseif' => [
  523. 'simplified_if_return',
  524. ],
  525. 'no_superfluous_phpdoc_tags' => [
  526. 'no_empty_phpdoc',
  527. 'void_return',
  528. ],
  529. 'no_unneeded_control_parentheses' => [
  530. 'concat_space',
  531. 'no_trailing_whitespace',
  532. ],
  533. 'no_unneeded_curly_braces' => [
  534. 'no_useless_else',
  535. 'no_useless_return',
  536. 'return_assignment',
  537. 'simplified_if_return',
  538. ],
  539. 'no_unneeded_import_alias' => [
  540. 'no_singleline_whitespace_before_semicolons',
  541. ],
  542. 'no_unset_cast' => [
  543. 'binary_operator_spaces',
  544. ],
  545. 'no_unset_on_property' => [
  546. 'combine_consecutive_unsets',
  547. ],
  548. 'no_unused_imports' => [
  549. 'blank_line_after_namespace',
  550. 'no_extra_blank_lines',
  551. 'no_leading_import_slash',
  552. 'single_line_after_imports',
  553. ],
  554. 'no_useless_concat_operator' => [
  555. 'date_time_create_from_format_call',
  556. 'ereg_to_preg',
  557. 'php_unit_dedicate_assert_internal_type',
  558. 'regular_callable_call',
  559. 'set_type_to_cast',
  560. ],
  561. 'no_useless_else' => [
  562. 'braces',
  563. 'combine_consecutive_unsets',
  564. 'no_break_comment',
  565. 'no_extra_blank_lines',
  566. 'no_trailing_whitespace',
  567. 'no_useless_return',
  568. 'no_whitespace_in_blank_line',
  569. 'simplified_if_return',
  570. ],
  571. 'no_useless_return' => [
  572. 'blank_line_before_statement',
  573. 'no_extra_blank_lines',
  574. 'no_whitespace_in_blank_line',
  575. 'single_line_comment_style',
  576. ],
  577. 'no_useless_sprintf' => [
  578. 'method_argument_space',
  579. 'native_function_casing',
  580. 'no_empty_statement',
  581. 'no_extra_blank_lines',
  582. 'no_spaces_inside_parenthesis',
  583. ],
  584. 'nullable_type_declaration_for_default_null_value' => [
  585. 'no_unreachable_default_argument_value',
  586. ],
  587. 'ordered_class_elements' => [
  588. 'class_attributes_separation',
  589. 'no_blank_lines_after_class_opening',
  590. 'space_after_semicolon',
  591. ],
  592. 'ordered_imports' => [
  593. 'blank_line_between_import_groups',
  594. ],
  595. 'ordered_types' => [
  596. 'types_spaces',
  597. ],
  598. 'php_unit_construct' => [
  599. 'php_unit_dedicate_assert',
  600. ],
  601. 'php_unit_dedicate_assert' => [
  602. 'no_unused_imports',
  603. 'php_unit_dedicate_assert_internal_type',
  604. ],
  605. 'php_unit_fqcn_annotation' => [
  606. 'no_unused_imports',
  607. 'phpdoc_order_by_value',
  608. ],
  609. 'php_unit_internal_class' => [
  610. 'final_internal_class',
  611. 'phpdoc_separation',
  612. ],
  613. 'php_unit_no_expectation_annotation' => [
  614. 'no_empty_phpdoc',
  615. 'php_unit_expectation',
  616. ],
  617. 'php_unit_size_class' => [
  618. 'phpdoc_separation',
  619. ],
  620. 'php_unit_test_annotation' => [
  621. 'no_empty_phpdoc',
  622. 'php_unit_method_casing',
  623. 'phpdoc_trim',
  624. ],
  625. 'php_unit_test_case_static_method_calls' => [
  626. 'self_static_accessor',
  627. ],
  628. 'php_unit_test_class_requires_covers' => [
  629. 'phpdoc_separation',
  630. ],
  631. 'phpdoc_add_missing_param_annotation' => [
  632. 'no_empty_phpdoc',
  633. 'no_superfluous_phpdoc_tags',
  634. 'phpdoc_align',
  635. 'phpdoc_order',
  636. ],
  637. 'phpdoc_line_span' => [
  638. 'no_superfluous_phpdoc_tags',
  639. ],
  640. 'phpdoc_no_access' => [
  641. 'no_empty_phpdoc',
  642. 'phpdoc_separation',
  643. 'phpdoc_trim',
  644. ],
  645. 'phpdoc_no_alias_tag' => [
  646. 'phpdoc_add_missing_param_annotation',
  647. 'phpdoc_single_line_var_spacing',
  648. ],
  649. 'phpdoc_no_empty_return' => [
  650. 'no_empty_phpdoc',
  651. 'phpdoc_order',
  652. 'phpdoc_separation',
  653. 'phpdoc_trim',
  654. ],
  655. 'phpdoc_no_package' => [
  656. 'no_empty_phpdoc',
  657. 'phpdoc_separation',
  658. 'phpdoc_trim',
  659. ],
  660. 'phpdoc_no_useless_inheritdoc' => [
  661. 'no_empty_phpdoc',
  662. 'no_trailing_whitespace_in_comment',
  663. ],
  664. 'phpdoc_order' => [
  665. 'phpdoc_separation',
  666. 'phpdoc_trim',
  667. ],
  668. 'phpdoc_return_self_reference' => [
  669. 'no_superfluous_phpdoc_tags',
  670. ],
  671. 'phpdoc_scalar' => [
  672. 'phpdoc_to_return_type',
  673. ],
  674. 'phpdoc_to_comment' => [
  675. 'no_empty_comment',
  676. 'phpdoc_no_useless_inheritdoc',
  677. 'single_line_comment_spacing',
  678. 'single_line_comment_style',
  679. ],
  680. 'phpdoc_to_param_type' => [
  681. 'no_superfluous_phpdoc_tags',
  682. ],
  683. 'phpdoc_to_property_type' => [
  684. 'no_superfluous_phpdoc_tags',
  685. ],
  686. 'phpdoc_to_return_type' => [
  687. 'fully_qualified_strict_types',
  688. 'no_superfluous_phpdoc_tags',
  689. 'return_type_declaration',
  690. ],
  691. 'phpdoc_types' => [
  692. 'phpdoc_to_return_type',
  693. ],
  694. 'pow_to_exponentiation' => [
  695. 'binary_operator_spaces',
  696. 'method_argument_space',
  697. 'native_function_casing',
  698. 'no_spaces_after_function_name',
  699. 'no_spaces_inside_parenthesis',
  700. ],
  701. 'protected_to_private' => [
  702. 'ordered_class_elements',
  703. ],
  704. 'regular_callable_call' => [
  705. 'native_function_invocation',
  706. ],
  707. 'return_assignment' => [
  708. 'blank_line_before_statement',
  709. ],
  710. 'semicolon_after_instruction' => [
  711. 'simplified_if_return',
  712. ],
  713. 'simplified_if_return' => [
  714. 'multiline_whitespace_before_semicolons',
  715. 'no_singleline_whitespace_before_semicolons',
  716. ],
  717. 'simplified_null_return' => [
  718. 'no_useless_return',
  719. 'void_return',
  720. ],
  721. 'single_class_element_per_statement' => [
  722. 'class_attributes_separation',
  723. ],
  724. 'single_import_per_statement' => [
  725. 'multiline_whitespace_before_semicolons',
  726. 'no_leading_import_slash',
  727. 'no_singleline_whitespace_before_semicolons',
  728. 'no_unused_imports',
  729. 'space_after_semicolon',
  730. ],
  731. 'single_line_throw' => [
  732. 'braces',
  733. 'concat_space',
  734. ],
  735. 'single_quote' => [
  736. 'no_useless_concat_operator',
  737. ],
  738. 'single_space_after_construct' => [
  739. 'braces',
  740. 'function_declaration',
  741. ],
  742. 'single_space_around_construct' => [
  743. 'braces',
  744. 'function_declaration',
  745. ],
  746. 'single_trait_insert_per_statement' => [
  747. 'braces',
  748. 'space_after_semicolon',
  749. ],
  750. 'standardize_increment' => [
  751. 'increment_style',
  752. ],
  753. 'standardize_not_equals' => [
  754. 'binary_operator_spaces',
  755. ],
  756. 'statement_indentation' => [
  757. 'heredoc_indentation',
  758. ],
  759. 'strict_comparison' => [
  760. 'binary_operator_spaces',
  761. 'modernize_strpos',
  762. ],
  763. 'strict_param' => [
  764. 'method_argument_space',
  765. 'native_function_invocation',
  766. ],
  767. 'string_length_to_empty' => [
  768. 'no_extra_blank_lines',
  769. 'no_trailing_whitespace',
  770. ],
  771. 'ternary_to_elvis_operator' => [
  772. 'no_trailing_whitespace',
  773. 'ternary_operator_spaces',
  774. ],
  775. 'ternary_to_null_coalescing' => [
  776. 'assign_null_coalescing_to_coalesce_equal',
  777. ],
  778. 'unary_operator_spaces' => [
  779. 'not_operator_with_space',
  780. 'not_operator_with_successor_space',
  781. ],
  782. 'visibility_required' => [
  783. 'class_attributes_separation',
  784. ],
  785. 'void_return' => [
  786. 'phpdoc_no_empty_return',
  787. 'return_type_declaration',
  788. ],
  789. ];
  790. }
  791. /**
  792. * @return array<string, list<string>>
  793. */
  794. private static function getPhpDocFixersPriorityGraph(): array
  795. {
  796. // Prepare bulk tests for phpdoc fixers to test that:
  797. // * `align_multiline_comment` is first
  798. // * `comment_to_phpdoc` is second
  799. // * `phpdoc_to_comment` is third
  800. // * `phpdoc_indent` is fourth
  801. // * `phpdoc_types` is fifth
  802. // * `phpdoc_scalar` is sixth
  803. // * `phpdoc_align` is last
  804. $cases = [
  805. 'align_multiline_comment' => ['comment_to_phpdoc'],
  806. 'comment_to_phpdoc' => ['phpdoc_to_comment'],
  807. 'phpdoc_to_comment' => ['phpdoc_indent'],
  808. 'phpdoc_indent' => ['phpdoc_types'],
  809. 'phpdoc_types' => ['phpdoc_scalar'],
  810. 'phpdoc_scalar' => [],
  811. ];
  812. $docFixerNames = array_filter(
  813. array_keys(self::getAllFixers()),
  814. static function (string $name): bool {
  815. return str_contains($name, 'phpdoc');
  816. }
  817. );
  818. foreach ($docFixerNames as $docFixerName) {
  819. if (!\in_array($docFixerName, ['comment_to_phpdoc', 'phpdoc_to_comment', 'phpdoc_indent', 'phpdoc_types', 'phpdoc_scalar'], true)) {
  820. $cases['align_multiline_comment'][] = $docFixerName;
  821. $cases['comment_to_phpdoc'][] = $docFixerName;
  822. $cases['phpdoc_indent'][] = $docFixerName;
  823. $cases['phpdoc_to_comment'][] = $docFixerName;
  824. if ('phpdoc_annotation_without_dot' !== $docFixerName) {
  825. $cases['phpdoc_scalar'][] = $docFixerName;
  826. $cases['phpdoc_types'][] = $docFixerName;
  827. }
  828. }
  829. if ('phpdoc_align' !== $docFixerName) {
  830. $cases[$docFixerName][] = 'phpdoc_align';
  831. }
  832. }
  833. return $cases;
  834. }
  835. /**
  836. * @return array<string, int>
  837. */
  838. private static function getFixerWithFixedPosition(): array
  839. {
  840. return [
  841. 'encoding' => 0, // Expected "encoding" fixer to have the highest priority.
  842. 'full_opening_tag' => 1, // Expected "full_opening_tag" fixer has second-highest priority.
  843. 'single_blank_line_at_eof' => -1, // Expected "single_blank_line_at_eof" to have the lowest priority.
  844. ];
  845. }
  846. /**
  847. * @return array<string, FixerInterface>
  848. */
  849. private static function getAllFixers(): array
  850. {
  851. $factory = new FixerFactory();
  852. $factory->registerBuiltInFixers();
  853. $fixers = [];
  854. foreach ($factory->getFixers() as $fixer) {
  855. $fixers[$fixer->getName()] = $fixer;
  856. }
  857. return $fixers;
  858. }
  859. private static function getIntegrationPriorityDirectory(): string
  860. {
  861. return __DIR__.'/../Fixtures/Integration/priority/';
  862. }
  863. }