FixerFactoryTest.php 35 KB

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