FixerFactoryTest.php 31 KB

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