FixerFactoryTest.php 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030
  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, $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 with non-default priority and yet without priority unit tests [vide "getFixersPriorityGraph()" and "getPhpDocFixersPriorityGraph()"]: "'.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. 'single_space_after_construct',
  280. 'single_space_around_construct',
  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. 'blank_lines_before_namespace',
  295. 'no_blank_lines_before_namespace',
  296. ],
  297. 'braces' => [
  298. 'heredoc_indentation',
  299. ],
  300. 'braces_position' => [
  301. 'single_line_empty_body',
  302. 'statement_indentation',
  303. ],
  304. 'class_attributes_separation' => [
  305. 'braces',
  306. 'indentation_type',
  307. 'no_extra_blank_lines',
  308. 'statement_indentation',
  309. ],
  310. 'class_definition' => [
  311. 'braces',
  312. 'single_line_empty_body',
  313. ],
  314. 'class_keyword_remove' => [
  315. 'no_unused_imports',
  316. ],
  317. 'clean_namespace' => [
  318. 'php_unit_data_provider_return_type',
  319. ],
  320. 'combine_consecutive_issets' => [
  321. 'multiline_whitespace_before_semicolons',
  322. 'no_singleline_whitespace_before_semicolons',
  323. 'no_spaces_inside_parenthesis',
  324. 'no_trailing_whitespace',
  325. 'no_whitespace_in_blank_line',
  326. 'spaces_inside_parentheses',
  327. ],
  328. 'combine_consecutive_unsets' => [
  329. 'no_extra_blank_lines',
  330. 'no_trailing_whitespace',
  331. 'no_whitespace_in_blank_line',
  332. 'space_after_semicolon',
  333. ],
  334. 'combine_nested_dirname' => [
  335. 'method_argument_space',
  336. 'no_spaces_inside_parenthesis',
  337. 'spaces_inside_parentheses',
  338. ],
  339. 'control_structure_braces' => [
  340. 'braces_position',
  341. 'control_structure_continuation_position',
  342. 'curly_braces_position',
  343. 'no_multiple_statements_per_line',
  344. ],
  345. 'curly_braces_position' => [
  346. 'single_line_empty_body',
  347. 'statement_indentation',
  348. ],
  349. 'declare_strict_types' => [
  350. 'blank_line_after_opening_tag',
  351. 'declare_equal_normalize',
  352. 'header_comment',
  353. ],
  354. 'dir_constant' => [
  355. 'combine_nested_dirname',
  356. ],
  357. 'doctrine_annotation_array_assignment' => [
  358. 'doctrine_annotation_spaces',
  359. ],
  360. 'echo_tag_syntax' => [
  361. 'no_mixed_echo_print',
  362. ],
  363. 'empty_loop_body' => [
  364. 'braces',
  365. 'no_extra_blank_lines',
  366. 'no_trailing_whitespace',
  367. ],
  368. 'empty_loop_condition' => [
  369. 'no_extra_blank_lines',
  370. 'no_trailing_whitespace',
  371. ],
  372. 'escape_implicit_backslashes' => [
  373. 'heredoc_to_nowdoc',
  374. 'single_quote',
  375. ],
  376. 'final_class' => [
  377. 'protected_to_private',
  378. 'self_static_accessor',
  379. ],
  380. 'final_internal_class' => [
  381. 'protected_to_private',
  382. 'self_static_accessor',
  383. ],
  384. 'fully_qualified_strict_types' => [
  385. 'no_superfluous_phpdoc_tags',
  386. 'ordered_imports',
  387. 'statement_indentation',
  388. ],
  389. 'function_declaration' => [
  390. 'method_argument_space',
  391. ],
  392. 'function_to_constant' => [
  393. 'native_constant_invocation',
  394. 'native_function_casing',
  395. 'no_extra_blank_lines',
  396. 'no_singleline_whitespace_before_semicolons',
  397. 'no_trailing_whitespace',
  398. 'no_whitespace_in_blank_line',
  399. 'self_static_accessor',
  400. ],
  401. 'general_phpdoc_annotation_remove' => [
  402. 'no_empty_phpdoc',
  403. 'phpdoc_line_span',
  404. 'phpdoc_separation',
  405. 'phpdoc_trim',
  406. ],
  407. 'general_phpdoc_tag_rename' => [
  408. 'phpdoc_add_missing_param_annotation',
  409. ],
  410. 'get_class_to_class_keyword' => [
  411. 'multiline_whitespace_before_semicolons',
  412. ],
  413. 'global_namespace_import' => [
  414. 'no_unused_imports',
  415. 'ordered_imports',
  416. 'statement_indentation',
  417. ],
  418. 'header_comment' => [
  419. 'blank_lines_before_namespace',
  420. 'single_blank_line_before_namespace',
  421. 'single_line_comment_style',
  422. ],
  423. 'implode_call' => [
  424. 'method_argument_space',
  425. ],
  426. 'increment_style' => [
  427. 'no_spaces_inside_parenthesis',
  428. 'spaces_inside_parentheses',
  429. ],
  430. 'indentation_type' => [
  431. 'phpdoc_indent',
  432. ],
  433. 'is_null' => [
  434. 'yoda_style',
  435. ],
  436. 'lambda_not_used_import' => [
  437. 'method_argument_space',
  438. 'no_spaces_inside_parenthesis',
  439. 'spaces_inside_parentheses',
  440. ],
  441. 'list_syntax' => [
  442. 'binary_operator_spaces',
  443. 'ternary_operator_spaces',
  444. ],
  445. 'long_to_shorthand_operator' => [
  446. 'binary_operator_spaces',
  447. 'no_extra_blank_lines',
  448. 'no_singleline_whitespace_before_semicolons',
  449. 'standardize_increment',
  450. ],
  451. 'method_argument_space' => [
  452. 'array_indentation',
  453. 'statement_indentation',
  454. ],
  455. 'modernize_strpos' => [
  456. 'binary_operator_spaces',
  457. 'no_extra_blank_lines',
  458. 'no_spaces_inside_parenthesis',
  459. 'no_trailing_whitespace',
  460. 'not_operator_with_space',
  461. 'not_operator_with_successor_space',
  462. 'php_unit_dedicate_assert',
  463. 'single_space_after_construct',
  464. 'single_space_around_construct',
  465. 'spaces_inside_parentheses',
  466. ],
  467. 'modernize_types_casting' => [
  468. 'no_unneeded_control_parentheses',
  469. ],
  470. 'multiline_string_to_heredoc' => [
  471. 'escape_implicit_backslashes',
  472. 'heredoc_indentation',
  473. ],
  474. 'multiline_whitespace_before_semicolons' => [
  475. 'space_after_semicolon',
  476. ],
  477. 'native_constant_invocation' => [
  478. 'global_namespace_import',
  479. ],
  480. 'native_function_invocation' => [
  481. 'global_namespace_import',
  482. ],
  483. 'new_with_braces' => [
  484. 'class_definition',
  485. ],
  486. 'new_with_parentheses' => [
  487. 'class_definition',
  488. ],
  489. 'no_alias_functions' => [
  490. 'implode_call',
  491. 'php_unit_dedicate_assert',
  492. ],
  493. 'no_alternative_syntax' => [
  494. 'braces',
  495. 'elseif',
  496. 'no_superfluous_elseif',
  497. 'no_unneeded_control_parentheses',
  498. 'no_useless_else',
  499. 'switch_continue_to_break',
  500. ],
  501. 'no_binary_string' => [
  502. 'no_useless_concat_operator',
  503. 'php_unit_dedicate_assert_internal_type',
  504. 'regular_callable_call',
  505. 'set_type_to_cast',
  506. ],
  507. 'no_blank_lines_after_phpdoc' => [
  508. 'header_comment',
  509. ],
  510. 'no_empty_comment' => [
  511. 'no_extra_blank_lines',
  512. 'no_trailing_whitespace',
  513. 'no_whitespace_in_blank_line',
  514. ],
  515. 'no_empty_phpdoc' => [
  516. 'no_extra_blank_lines',
  517. 'no_trailing_whitespace',
  518. 'no_whitespace_in_blank_line',
  519. ],
  520. 'no_empty_statement' => [
  521. 'braces',
  522. 'combine_consecutive_unsets',
  523. 'empty_loop_body',
  524. 'multiline_whitespace_before_semicolons',
  525. 'no_extra_blank_lines',
  526. 'no_multiple_statements_per_line',
  527. 'no_singleline_whitespace_before_semicolons',
  528. 'no_trailing_whitespace',
  529. 'no_useless_else',
  530. 'no_useless_return',
  531. 'no_whitespace_in_blank_line',
  532. 'return_assignment',
  533. 'space_after_semicolon',
  534. 'switch_case_semicolon_to_colon',
  535. ],
  536. 'no_extra_blank_lines' => [
  537. 'blank_line_before_statement',
  538. ],
  539. 'no_leading_import_slash' => [
  540. 'ordered_imports',
  541. ],
  542. 'no_multiline_whitespace_around_double_arrow' => [
  543. 'binary_operator_spaces',
  544. 'method_argument_space',
  545. ],
  546. 'no_multiple_statements_per_line' => [
  547. 'braces_position',
  548. 'curly_braces_position',
  549. ],
  550. 'no_php4_constructor' => [
  551. 'ordered_class_elements',
  552. ],
  553. 'no_short_bool_cast' => [
  554. 'cast_spaces',
  555. ],
  556. 'no_spaces_after_function_name' => [
  557. 'function_to_constant',
  558. 'get_class_to_class_keyword',
  559. ],
  560. 'no_spaces_inside_parenthesis' => [
  561. 'function_to_constant',
  562. 'get_class_to_class_keyword',
  563. 'string_length_to_empty',
  564. ],
  565. 'no_superfluous_elseif' => [
  566. 'simplified_if_return',
  567. ],
  568. 'no_superfluous_phpdoc_tags' => [
  569. 'no_empty_phpdoc',
  570. 'void_return',
  571. ],
  572. 'no_unneeded_braces' => [
  573. 'no_useless_else',
  574. 'no_useless_return',
  575. 'return_assignment',
  576. 'simplified_if_return',
  577. ],
  578. 'no_unneeded_control_parentheses' => [
  579. 'concat_space',
  580. 'no_trailing_whitespace',
  581. ],
  582. 'no_unneeded_curly_braces' => [
  583. 'no_useless_else',
  584. 'no_useless_return',
  585. 'return_assignment',
  586. 'simplified_if_return',
  587. ],
  588. 'no_unneeded_import_alias' => [
  589. 'no_singleline_whitespace_before_semicolons',
  590. ],
  591. 'no_unset_cast' => [
  592. 'binary_operator_spaces',
  593. ],
  594. 'no_unset_on_property' => [
  595. 'combine_consecutive_unsets',
  596. ],
  597. 'no_unused_imports' => [
  598. 'blank_line_after_namespace',
  599. 'no_extra_blank_lines',
  600. 'no_leading_import_slash',
  601. 'single_line_after_imports',
  602. ],
  603. 'no_useless_concat_operator' => [
  604. 'date_time_create_from_format_call',
  605. 'ereg_to_preg',
  606. 'php_unit_dedicate_assert_internal_type',
  607. 'regular_callable_call',
  608. 'set_type_to_cast',
  609. ],
  610. 'no_useless_else' => [
  611. 'blank_line_before_statement',
  612. 'braces',
  613. 'combine_consecutive_unsets',
  614. 'no_break_comment',
  615. 'no_extra_blank_lines',
  616. 'no_trailing_whitespace',
  617. 'no_useless_return',
  618. 'no_whitespace_in_blank_line',
  619. 'simplified_if_return',
  620. 'statement_indentation',
  621. ],
  622. 'no_useless_return' => [
  623. 'blank_line_before_statement',
  624. 'no_extra_blank_lines',
  625. 'no_whitespace_in_blank_line',
  626. 'single_line_comment_style',
  627. 'single_line_empty_body',
  628. ],
  629. 'no_useless_sprintf' => [
  630. 'method_argument_space',
  631. 'native_function_casing',
  632. 'no_empty_statement',
  633. 'no_extra_blank_lines',
  634. 'no_spaces_inside_parenthesis',
  635. 'spaces_inside_parentheses',
  636. ],
  637. 'nullable_type_declaration' => [
  638. 'ordered_types',
  639. 'types_spaces',
  640. ],
  641. 'nullable_type_declaration_for_default_null_value' => [
  642. 'no_unreachable_default_argument_value',
  643. 'nullable_type_declaration',
  644. 'ordered_types',
  645. ],
  646. 'ordered_class_elements' => [
  647. 'class_attributes_separation',
  648. 'no_blank_lines_after_class_opening',
  649. 'space_after_semicolon',
  650. ],
  651. 'ordered_imports' => [
  652. 'blank_line_between_import_groups',
  653. ],
  654. 'ordered_types' => [
  655. 'types_spaces',
  656. ],
  657. 'php_unit_construct' => [
  658. 'php_unit_dedicate_assert',
  659. ],
  660. 'php_unit_data_provider_return_type' => [
  661. 'return_to_yield_from',
  662. 'return_type_declaration',
  663. ],
  664. 'php_unit_dedicate_assert' => [
  665. 'no_unused_imports',
  666. 'php_unit_dedicate_assert_internal_type',
  667. ],
  668. 'php_unit_fqcn_annotation' => [
  669. 'no_unused_imports',
  670. 'phpdoc_order_by_value',
  671. ],
  672. 'php_unit_internal_class' => [
  673. 'final_internal_class',
  674. 'phpdoc_separation',
  675. ],
  676. 'php_unit_no_expectation_annotation' => [
  677. 'no_empty_phpdoc',
  678. 'php_unit_expectation',
  679. ],
  680. 'php_unit_size_class' => [
  681. 'phpdoc_separation',
  682. ],
  683. 'php_unit_test_annotation' => [
  684. 'no_empty_phpdoc',
  685. 'php_unit_method_casing',
  686. 'phpdoc_trim',
  687. ],
  688. 'php_unit_test_case_static_method_calls' => [
  689. 'self_static_accessor',
  690. ],
  691. 'php_unit_test_class_requires_covers' => [
  692. 'phpdoc_separation',
  693. ],
  694. 'phpdoc_add_missing_param_annotation' => [
  695. 'no_empty_phpdoc',
  696. 'no_superfluous_phpdoc_tags',
  697. 'phpdoc_align',
  698. 'phpdoc_order',
  699. ],
  700. 'phpdoc_line_span' => [
  701. 'no_superfluous_phpdoc_tags',
  702. ],
  703. 'phpdoc_no_access' => [
  704. 'no_empty_phpdoc',
  705. 'phpdoc_separation',
  706. 'phpdoc_trim',
  707. ],
  708. 'phpdoc_no_alias_tag' => [
  709. 'phpdoc_add_missing_param_annotation',
  710. 'phpdoc_single_line_var_spacing',
  711. ],
  712. 'phpdoc_no_empty_return' => [
  713. 'no_empty_phpdoc',
  714. 'phpdoc_order',
  715. 'phpdoc_separation',
  716. 'phpdoc_trim',
  717. ],
  718. 'phpdoc_no_package' => [
  719. 'no_empty_phpdoc',
  720. 'phpdoc_separation',
  721. 'phpdoc_trim',
  722. ],
  723. 'phpdoc_no_useless_inheritdoc' => [
  724. 'no_empty_phpdoc',
  725. 'no_trailing_whitespace_in_comment',
  726. ],
  727. 'phpdoc_order' => [
  728. 'phpdoc_separation',
  729. 'phpdoc_trim',
  730. ],
  731. 'phpdoc_readonly_class_comment_to_keyword' => [
  732. 'no_empty_phpdoc',
  733. 'no_extra_blank_lines',
  734. 'phpdoc_align',
  735. ],
  736. 'phpdoc_return_self_reference' => [
  737. 'no_superfluous_phpdoc_tags',
  738. ],
  739. 'phpdoc_scalar' => [
  740. 'phpdoc_to_return_type',
  741. ],
  742. 'phpdoc_to_comment' => [
  743. 'no_empty_comment',
  744. 'phpdoc_no_useless_inheritdoc',
  745. 'single_line_comment_spacing',
  746. 'single_line_comment_style',
  747. ],
  748. 'phpdoc_to_param_type' => [
  749. 'no_superfluous_phpdoc_tags',
  750. ],
  751. 'phpdoc_to_property_type' => [
  752. 'no_superfluous_phpdoc_tags',
  753. ],
  754. 'phpdoc_to_return_type' => [
  755. 'fully_qualified_strict_types',
  756. 'no_superfluous_phpdoc_tags',
  757. 'return_to_yield_from',
  758. 'return_type_declaration',
  759. ],
  760. 'phpdoc_types' => [
  761. 'phpdoc_to_return_type',
  762. ],
  763. 'pow_to_exponentiation' => [
  764. 'binary_operator_spaces',
  765. 'method_argument_space',
  766. 'native_function_casing',
  767. 'no_spaces_after_function_name',
  768. 'no_spaces_inside_parenthesis',
  769. 'spaces_inside_parentheses',
  770. ],
  771. 'protected_to_private' => [
  772. 'ordered_class_elements',
  773. ],
  774. 'psr_autoloading' => [
  775. 'self_accessor',
  776. ],
  777. 'regular_callable_call' => [
  778. 'native_function_invocation',
  779. ],
  780. 'return_assignment' => [
  781. 'blank_line_before_statement',
  782. ],
  783. 'return_to_yield_from' => [
  784. 'yield_from_array_to_yields',
  785. ],
  786. 'semicolon_after_instruction' => [
  787. 'simplified_if_return',
  788. ],
  789. 'simplified_if_return' => [
  790. 'multiline_whitespace_before_semicolons',
  791. 'no_singleline_whitespace_before_semicolons',
  792. ],
  793. 'simplified_null_return' => [
  794. 'no_useless_return',
  795. 'void_return',
  796. ],
  797. 'single_class_element_per_statement' => [
  798. 'class_attributes_separation',
  799. ],
  800. 'single_import_per_statement' => [
  801. 'multiline_whitespace_before_semicolons',
  802. 'no_leading_import_slash',
  803. 'no_singleline_whitespace_before_semicolons',
  804. 'no_unused_imports',
  805. 'space_after_semicolon',
  806. ],
  807. 'single_line_throw' => [
  808. 'braces',
  809. 'concat_space',
  810. ],
  811. 'single_quote' => [
  812. 'no_useless_concat_operator',
  813. ],
  814. 'single_space_after_construct' => [
  815. 'braces',
  816. 'function_declaration',
  817. ],
  818. 'single_space_around_construct' => [
  819. 'braces',
  820. 'function_declaration',
  821. 'nullable_type_declaration',
  822. ],
  823. 'single_trait_insert_per_statement' => [
  824. 'braces',
  825. 'space_after_semicolon',
  826. ],
  827. 'spaces_inside_parentheses' => [
  828. 'function_to_constant',
  829. 'get_class_to_class_keyword',
  830. 'string_length_to_empty',
  831. ],
  832. 'standardize_increment' => [
  833. 'increment_style',
  834. ],
  835. 'standardize_not_equals' => [
  836. 'binary_operator_spaces',
  837. ],
  838. 'statement_indentation' => [
  839. 'heredoc_indentation',
  840. ],
  841. 'strict_comparison' => [
  842. 'binary_operator_spaces',
  843. 'modernize_strpos',
  844. ],
  845. 'strict_param' => [
  846. 'method_argument_space',
  847. 'native_function_invocation',
  848. ],
  849. 'string_length_to_empty' => [
  850. 'no_extra_blank_lines',
  851. 'no_trailing_whitespace',
  852. ],
  853. 'ternary_to_elvis_operator' => [
  854. 'no_trailing_whitespace',
  855. 'ternary_operator_spaces',
  856. ],
  857. 'ternary_to_null_coalescing' => [
  858. 'assign_null_coalescing_to_coalesce_equal',
  859. ],
  860. 'unary_operator_spaces' => [
  861. 'not_operator_with_space',
  862. 'not_operator_with_successor_space',
  863. ],
  864. 'use_arrow_functions' => [
  865. 'function_declaration',
  866. ],
  867. 'visibility_required' => [
  868. 'class_attributes_separation',
  869. ],
  870. 'void_return' => [
  871. 'phpdoc_no_empty_return',
  872. 'return_type_declaration',
  873. ],
  874. 'yield_from_array_to_yields' => [
  875. 'blank_line_before_statement',
  876. 'no_extra_blank_lines',
  877. 'no_multiple_statements_per_line',
  878. 'no_whitespace_in_blank_line',
  879. 'statement_indentation',
  880. ],
  881. ];
  882. }
  883. /**
  884. * @return array<string, list<string>>
  885. */
  886. private static function getPhpDocFixersPriorityGraph(): array
  887. {
  888. // Prepare bulk tests for phpdoc fixers to test that:
  889. // * `align_multiline_comment` is first
  890. // * `comment_to_phpdoc` is second
  891. // * `phpdoc_to_comment` is third
  892. // * `phpdoc_indent` is fourth
  893. // * `phpdoc_types` is fifth
  894. // * `phpdoc_scalar` is sixth
  895. // * `phpdoc_align` is last
  896. $cases = [
  897. 'align_multiline_comment' => ['comment_to_phpdoc'],
  898. 'comment_to_phpdoc' => ['phpdoc_to_comment'],
  899. 'phpdoc_to_comment' => ['phpdoc_indent'],
  900. 'phpdoc_indent' => ['phpdoc_types'],
  901. 'phpdoc_types' => ['phpdoc_scalar'],
  902. 'phpdoc_scalar' => [],
  903. ];
  904. $docFixerNames = array_filter(
  905. array_keys(self::getAllFixers()),
  906. static fn (string $name): bool => str_contains($name, 'phpdoc')
  907. );
  908. foreach ($docFixerNames as $docFixerName) {
  909. if (!\in_array($docFixerName, ['comment_to_phpdoc', 'phpdoc_to_comment', 'phpdoc_indent', 'phpdoc_types', 'phpdoc_scalar'], true)) {
  910. $cases['align_multiline_comment'][] = $docFixerName;
  911. $cases['comment_to_phpdoc'][] = $docFixerName;
  912. $cases['phpdoc_indent'][] = $docFixerName;
  913. $cases['phpdoc_to_comment'][] = $docFixerName;
  914. if ('phpdoc_annotation_without_dot' !== $docFixerName) {
  915. $cases['phpdoc_scalar'][] = $docFixerName;
  916. $cases['phpdoc_types'][] = $docFixerName;
  917. }
  918. }
  919. if ('phpdoc_align' !== $docFixerName) {
  920. $cases[$docFixerName][] = 'phpdoc_align';
  921. }
  922. }
  923. return $cases;
  924. }
  925. /**
  926. * @return array<string, int>
  927. */
  928. private static function getFixerWithFixedPosition(): array
  929. {
  930. return [
  931. 'encoding' => 0, // Expected "encoding" fixer to have the highest priority.
  932. 'full_opening_tag' => 1, // Expected "full_opening_tag" fixer has second-highest priority.
  933. 'single_blank_line_at_eof' => -1, // Expected "single_blank_line_at_eof" to have the lowest priority.
  934. ];
  935. }
  936. /**
  937. * @return array<string, FixerInterface>
  938. */
  939. private static function getAllFixers(): array
  940. {
  941. $factory = new FixerFactory();
  942. $factory->registerBuiltInFixers();
  943. $fixers = [];
  944. foreach ($factory->getFixers() as $fixer) {
  945. $fixers[$fixer->getName()] = $fixer;
  946. }
  947. return $fixers;
  948. }
  949. private static function getIntegrationPriorityDirectory(): string
  950. {
  951. return __DIR__.'/../Fixtures/Integration/priority/';
  952. }
  953. }