FixerFactoryTest.php 36 KB

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