FixerFactoryTest.php 33 KB

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