FixerFactoryTest.php 30 KB

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