FixerFactoryTest.php 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078
  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 list<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. /**
  143. * @return iterable<array{\DirectoryIterator}>
  144. */
  145. public static function providePriorityIntegrationTestFilesAreListedInPriorityGraphCases(): iterable
  146. {
  147. foreach (new \DirectoryIterator(self::getIntegrationPriorityDirectory()) as $candidate) {
  148. if (!$candidate->isDot()) {
  149. yield [clone $candidate];
  150. }
  151. }
  152. }
  153. public function testFixersPriorityGraphIsSorted(): void
  154. {
  155. $previous = '';
  156. foreach (self::getFixersPriorityGraph() as $fixerName => $edges) {
  157. self::assertLessThan(0, $previous <=> $fixerName, \sprintf('Not sorted "%s" "%s".', $previous, $fixerName));
  158. $edgesSorted = $edges;
  159. sort($edgesSorted);
  160. self::assertSame($edgesSorted, $edges, \sprintf('Fixer "%s" edges are not sorted', $fixerName));
  161. $previous = $fixerName;
  162. }
  163. }
  164. public function testFixersPriorityComment(): void
  165. {
  166. $fixersPhpDocIssues = [];
  167. $fixers = self::getAllFixers();
  168. foreach ($fixers as $name => $fixer) {
  169. $reflection = new \ReflectionObject($fixer);
  170. $fixers[$name] = ['reflection' => $reflection, 'short_classname' => $reflection->getShortName()];
  171. }
  172. $mergedGraph = array_merge_recursive(
  173. self::getFixersPriorityGraph(),
  174. self::getPhpDocFixersPriorityGraph()
  175. );
  176. // expend $graph
  177. $graph = [];
  178. foreach ($mergedGraph as $fixerName => $edges) {
  179. if (!isset($graph[$fixerName]['before'])) {
  180. $graph[$fixerName] = ['before' => []];
  181. }
  182. foreach ($mergedGraph as $candidateFixer => $candidateEdges) {
  183. if (\in_array($fixerName, $candidateEdges, true)) {
  184. $graph[$fixerName]['after'][$candidateFixer] = true;
  185. }
  186. }
  187. foreach ($edges as $edge) {
  188. if (!isset($graph[$edge]['after'])) {
  189. $graph[$edge] = ['after' => []];
  190. }
  191. $graph[$edge]['after'][$fixerName] = true;
  192. $graph[$fixerName]['before'][$edge] = true;
  193. }
  194. }
  195. foreach ($graph as $fixerName => $edges) {
  196. $expectedMessage = "/**\n * {@inheritdoc}\n *";
  197. foreach ($edges as $label => $others) {
  198. if (\count($others) > 0) {
  199. $shortClassNames = [];
  200. foreach ($others as $other => $foo) {
  201. $shortClassNames[$other] = $fixers[$other]['short_classname'];
  202. }
  203. sort($shortClassNames);
  204. $expectedMessage .= \sprintf("\n * Must run %s %s.", $label, implode(', ', $shortClassNames));
  205. }
  206. }
  207. $expectedMessage .= "\n */";
  208. $method = $fixers[$fixerName]['reflection']->getMethod('getPriority');
  209. $phpDoc = $method->getDocComment();
  210. if (false === $phpDoc) {
  211. $fixersPhpDocIssues[$fixerName] = \sprintf("PHPDoc for %s::getPriority is missing.\nExpected:\n%s", $fixers[$fixerName]['short_classname'], $expectedMessage);
  212. } elseif ($expectedMessage !== $phpDoc) {
  213. $fixersPhpDocIssues[$fixerName] = \sprintf("PHPDoc for %s::getPriority is not as expected.\nExpected:\n%s", $fixers[$fixerName]['short_classname'], $expectedMessage);
  214. }
  215. }
  216. if (0 === \count($fixersPhpDocIssues)) {
  217. $this->addToAssertionCount(1);
  218. } else {
  219. $message = \sprintf("There are %d priority PHPDoc issues found.\n", \count($fixersPhpDocIssues));
  220. ksort($fixersPhpDocIssues);
  221. foreach ($fixersPhpDocIssues as $fixerName => $issue) {
  222. $message .= \sprintf("\n--------------------------------------------------\n[%s] %s", $fixerName, $issue);
  223. }
  224. self::fail($message);
  225. }
  226. }
  227. public function testFixerWithNoneDefaultPriorityIsTested(): void
  228. {
  229. $knownIssues = [ // should only shrink
  230. '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
  231. 'simple_to_complex_string_variable' => true, // had prio case but no longer, left prio the same for BC reasons
  232. ];
  233. $factory = new FixerFactory();
  234. $factory->registerBuiltInFixers();
  235. $fixers = $factory->getFixers();
  236. $fixerNamesWithTests = [];
  237. foreach (self::getFixerWithFixedPosition() as $fixerName => $priority) {
  238. $fixerNamesWithTests[$fixerName] = true;
  239. }
  240. foreach ([
  241. self::getFixersPriorityGraph(),
  242. self::getPhpDocFixersPriorityGraph(),
  243. ] as $set) {
  244. foreach ($set as $fixerName => $edges) {
  245. $fixerNamesWithTests[$fixerName] = true;
  246. foreach ($edges as $edge) {
  247. $fixerNamesWithTests[$edge] = true;
  248. }
  249. }
  250. }
  251. $missing = [];
  252. foreach ($fixers as $fixer) {
  253. $fixerName = $fixer->getName();
  254. if (0 !== $fixer->getPriority() && !isset($fixerNamesWithTests[$fixerName])) {
  255. $missing[$fixerName] = true;
  256. }
  257. }
  258. foreach ($knownIssues as $knownIssue => $true) {
  259. if (isset($missing[$knownIssue])) {
  260. unset($missing[$knownIssue]);
  261. } else {
  262. self::fail(\sprintf('No longer found known issue "%s", please update the set.', $knownIssue));
  263. }
  264. }
  265. self::assertEmpty($missing, 'Fixers with non-default priority and yet without priority unit tests [vide "getFixersPriorityGraph()" and "getPhpDocFixersPriorityGraph()"]: "'.implode('", "', array_keys($missing)).'."');
  266. }
  267. /**
  268. * @return array<string, list<string>>
  269. */
  270. private static function getFixersPriorityGraph(): array
  271. {
  272. return [
  273. 'align_multiline_comment' => [
  274. 'phpdoc_trim_consecutive_blank_line_separation',
  275. ],
  276. 'array_indentation' => [
  277. 'align_multiline_comment',
  278. 'binary_operator_spaces',
  279. ],
  280. 'array_syntax' => [
  281. 'binary_operator_spaces',
  282. 'single_space_after_construct',
  283. 'single_space_around_construct',
  284. 'ternary_operator_spaces',
  285. ],
  286. 'assign_null_coalescing_to_coalesce_equal' => [
  287. 'binary_operator_spaces',
  288. 'no_whitespace_in_blank_line',
  289. ],
  290. 'backtick_to_shell_exec' => [
  291. 'explicit_string_variable',
  292. 'native_function_invocation',
  293. 'single_quote',
  294. ],
  295. 'blank_line_after_opening_tag' => [
  296. 'blank_lines_before_namespace',
  297. 'no_blank_lines_before_namespace',
  298. ],
  299. 'braces' => [
  300. 'heredoc_indentation',
  301. ],
  302. 'braces_position' => [
  303. 'single_line_empty_body',
  304. 'statement_indentation',
  305. ],
  306. 'class_attributes_separation' => [
  307. 'braces',
  308. 'indentation_type',
  309. 'no_extra_blank_lines',
  310. 'statement_indentation',
  311. ],
  312. 'class_definition' => [
  313. 'braces',
  314. 'single_line_empty_body',
  315. ],
  316. 'class_keyword' => [
  317. 'fully_qualified_strict_types',
  318. ],
  319. 'class_keyword_remove' => [
  320. 'no_unused_imports',
  321. ],
  322. 'clean_namespace' => [
  323. 'php_unit_data_provider_return_type',
  324. ],
  325. 'combine_consecutive_issets' => [
  326. 'multiline_whitespace_before_semicolons',
  327. 'no_singleline_whitespace_before_semicolons',
  328. 'no_spaces_inside_parenthesis',
  329. 'no_trailing_whitespace',
  330. 'no_whitespace_in_blank_line',
  331. 'spaces_inside_parentheses',
  332. ],
  333. 'combine_consecutive_unsets' => [
  334. 'no_extra_blank_lines',
  335. 'no_trailing_whitespace',
  336. 'no_whitespace_in_blank_line',
  337. 'space_after_semicolon',
  338. ],
  339. 'combine_nested_dirname' => [
  340. 'method_argument_space',
  341. 'no_spaces_inside_parenthesis',
  342. 'spaces_inside_parentheses',
  343. ],
  344. 'control_structure_braces' => [
  345. 'braces_position',
  346. 'control_structure_continuation_position',
  347. 'curly_braces_position',
  348. 'no_multiple_statements_per_line',
  349. ],
  350. 'curly_braces_position' => [
  351. 'single_line_empty_body',
  352. 'statement_indentation',
  353. ],
  354. 'declare_strict_types' => [
  355. 'blank_line_after_opening_tag',
  356. 'declare_equal_normalize',
  357. 'header_comment',
  358. ],
  359. 'dir_constant' => [
  360. 'combine_nested_dirname',
  361. ],
  362. 'doctrine_annotation_array_assignment' => [
  363. 'doctrine_annotation_spaces',
  364. ],
  365. 'echo_tag_syntax' => [
  366. 'no_mixed_echo_print',
  367. ],
  368. 'empty_loop_body' => [
  369. 'braces',
  370. 'no_extra_blank_lines',
  371. 'no_trailing_whitespace',
  372. ],
  373. 'empty_loop_condition' => [
  374. 'no_extra_blank_lines',
  375. 'no_trailing_whitespace',
  376. ],
  377. 'escape_implicit_backslashes' => [
  378. 'heredoc_to_nowdoc',
  379. 'single_quote',
  380. ],
  381. 'explicit_string_variable' => [
  382. 'no_useless_concat_operator',
  383. ],
  384. 'final_class' => [
  385. 'protected_to_private',
  386. 'self_static_accessor',
  387. ],
  388. 'final_internal_class' => [
  389. 'protected_to_private',
  390. 'self_static_accessor',
  391. ],
  392. 'fully_qualified_strict_types' => [
  393. 'no_superfluous_phpdoc_tags',
  394. 'ordered_attributes',
  395. 'ordered_imports',
  396. 'ordered_interfaces',
  397. 'statement_indentation',
  398. ],
  399. 'function_declaration' => [
  400. 'method_argument_space',
  401. ],
  402. 'function_to_constant' => [
  403. 'native_constant_invocation',
  404. 'native_function_casing',
  405. 'no_extra_blank_lines',
  406. 'no_singleline_whitespace_before_semicolons',
  407. 'no_trailing_whitespace',
  408. 'no_whitespace_in_blank_line',
  409. 'self_static_accessor',
  410. ],
  411. 'general_phpdoc_annotation_remove' => [
  412. 'no_empty_phpdoc',
  413. 'phpdoc_line_span',
  414. 'phpdoc_separation',
  415. 'phpdoc_trim',
  416. ],
  417. 'general_phpdoc_tag_rename' => [
  418. 'phpdoc_add_missing_param_annotation',
  419. ],
  420. 'get_class_to_class_keyword' => [
  421. 'multiline_whitespace_before_semicolons',
  422. ],
  423. 'global_namespace_import' => [
  424. 'no_unused_imports',
  425. 'ordered_imports',
  426. 'statement_indentation',
  427. ],
  428. 'header_comment' => [
  429. 'blank_lines_before_namespace',
  430. 'single_blank_line_before_namespace',
  431. 'single_line_comment_style',
  432. ],
  433. 'implode_call' => [
  434. 'method_argument_space',
  435. ],
  436. 'increment_style' => [
  437. 'no_spaces_inside_parenthesis',
  438. 'spaces_inside_parentheses',
  439. ],
  440. 'indentation_type' => [
  441. 'phpdoc_indent',
  442. ],
  443. 'is_null' => [
  444. 'yoda_style',
  445. ],
  446. 'lambda_not_used_import' => [
  447. 'method_argument_space',
  448. 'no_spaces_inside_parenthesis',
  449. 'spaces_inside_parentheses',
  450. ],
  451. 'list_syntax' => [
  452. 'binary_operator_spaces',
  453. 'ternary_operator_spaces',
  454. ],
  455. 'long_to_shorthand_operator' => [
  456. 'binary_operator_spaces',
  457. 'no_extra_blank_lines',
  458. 'no_singleline_whitespace_before_semicolons',
  459. 'standardize_increment',
  460. ],
  461. 'mb_str_functions' => [
  462. 'native_function_invocation',
  463. ],
  464. 'method_argument_space' => [
  465. 'array_indentation',
  466. 'statement_indentation',
  467. ],
  468. 'modernize_strpos' => [
  469. 'binary_operator_spaces',
  470. 'no_extra_blank_lines',
  471. 'no_spaces_inside_parenthesis',
  472. 'no_trailing_whitespace',
  473. 'not_operator_with_space',
  474. 'not_operator_with_successor_space',
  475. 'php_unit_dedicate_assert',
  476. 'single_space_after_construct',
  477. 'single_space_around_construct',
  478. 'spaces_inside_parentheses',
  479. ],
  480. 'modernize_types_casting' => [
  481. 'no_unneeded_control_parentheses',
  482. ],
  483. 'multiline_string_to_heredoc' => [
  484. 'escape_implicit_backslashes',
  485. 'heredoc_indentation',
  486. 'string_implicit_backslashes',
  487. ],
  488. 'multiline_whitespace_before_semicolons' => [
  489. 'space_after_semicolon',
  490. ],
  491. 'native_constant_invocation' => [
  492. 'global_namespace_import',
  493. ],
  494. 'native_function_invocation' => [
  495. 'global_namespace_import',
  496. ],
  497. 'new_with_braces' => [
  498. 'class_definition',
  499. ],
  500. 'new_with_parentheses' => [
  501. 'class_definition',
  502. ],
  503. 'no_alias_functions' => [
  504. 'implode_call',
  505. 'php_unit_dedicate_assert',
  506. ],
  507. 'no_alternative_syntax' => [
  508. 'braces',
  509. 'elseif',
  510. 'no_superfluous_elseif',
  511. 'no_unneeded_control_parentheses',
  512. 'no_useless_else',
  513. 'switch_continue_to_break',
  514. ],
  515. 'no_binary_string' => [
  516. 'no_useless_concat_operator',
  517. 'php_unit_dedicate_assert_internal_type',
  518. 'regular_callable_call',
  519. 'set_type_to_cast',
  520. ],
  521. 'no_blank_lines_after_phpdoc' => [
  522. 'header_comment',
  523. ],
  524. 'no_empty_comment' => [
  525. 'no_extra_blank_lines',
  526. 'no_trailing_whitespace',
  527. 'no_whitespace_in_blank_line',
  528. ],
  529. 'no_empty_phpdoc' => [
  530. 'no_extra_blank_lines',
  531. 'no_trailing_whitespace',
  532. ],
  533. 'no_empty_statement' => [
  534. 'braces',
  535. 'combine_consecutive_unsets',
  536. 'empty_loop_body',
  537. 'multiline_whitespace_before_semicolons',
  538. 'no_extra_blank_lines',
  539. 'no_multiple_statements_per_line',
  540. 'no_singleline_whitespace_before_semicolons',
  541. 'no_trailing_whitespace',
  542. 'no_useless_else',
  543. 'no_useless_return',
  544. 'no_whitespace_in_blank_line',
  545. 'return_assignment',
  546. 'space_after_semicolon',
  547. 'switch_case_semicolon_to_colon',
  548. ],
  549. 'no_extra_blank_lines' => [
  550. 'blank_line_before_statement',
  551. ],
  552. 'no_leading_import_slash' => [
  553. 'ordered_imports',
  554. ],
  555. 'no_multiline_whitespace_around_double_arrow' => [
  556. 'binary_operator_spaces',
  557. 'method_argument_space',
  558. ],
  559. 'no_multiple_statements_per_line' => [
  560. 'braces_position',
  561. 'curly_braces_position',
  562. ],
  563. 'no_php4_constructor' => [
  564. 'ordered_class_elements',
  565. ],
  566. 'no_short_bool_cast' => [
  567. 'cast_spaces',
  568. ],
  569. 'no_space_around_double_colon' => [
  570. 'method_chaining_indentation',
  571. ],
  572. 'no_spaces_after_function_name' => [
  573. 'function_to_constant',
  574. 'get_class_to_class_keyword',
  575. ],
  576. 'no_spaces_inside_parenthesis' => [
  577. 'function_to_constant',
  578. 'get_class_to_class_keyword',
  579. 'string_length_to_empty',
  580. ],
  581. 'no_superfluous_elseif' => [
  582. 'simplified_if_return',
  583. ],
  584. 'no_superfluous_phpdoc_tags' => [
  585. 'no_empty_phpdoc',
  586. 'void_return',
  587. ],
  588. 'no_unneeded_braces' => [
  589. 'no_useless_else',
  590. 'no_useless_return',
  591. 'return_assignment',
  592. 'simplified_if_return',
  593. ],
  594. 'no_unneeded_control_parentheses' => [
  595. 'concat_space',
  596. 'no_trailing_whitespace',
  597. ],
  598. 'no_unneeded_curly_braces' => [
  599. 'no_useless_else',
  600. 'no_useless_return',
  601. 'return_assignment',
  602. 'simplified_if_return',
  603. ],
  604. 'no_unneeded_import_alias' => [
  605. 'no_singleline_whitespace_before_semicolons',
  606. ],
  607. 'no_unset_cast' => [
  608. 'binary_operator_spaces',
  609. ],
  610. 'no_unset_on_property' => [
  611. 'combine_consecutive_unsets',
  612. ],
  613. 'no_unused_imports' => [
  614. 'blank_line_after_namespace',
  615. 'no_extra_blank_lines',
  616. 'no_leading_import_slash',
  617. 'single_line_after_imports',
  618. ],
  619. 'no_useless_concat_operator' => [
  620. 'date_time_create_from_format_call',
  621. 'ereg_to_preg',
  622. 'php_unit_dedicate_assert_internal_type',
  623. 'regular_callable_call',
  624. 'set_type_to_cast',
  625. ],
  626. 'no_useless_else' => [
  627. 'blank_line_before_statement',
  628. 'braces',
  629. 'combine_consecutive_unsets',
  630. 'no_break_comment',
  631. 'no_extra_blank_lines',
  632. 'no_trailing_whitespace',
  633. 'no_useless_return',
  634. 'no_whitespace_in_blank_line',
  635. 'simplified_if_return',
  636. 'statement_indentation',
  637. ],
  638. 'no_useless_return' => [
  639. 'blank_line_before_statement',
  640. 'no_extra_blank_lines',
  641. 'no_whitespace_in_blank_line',
  642. 'single_line_comment_style',
  643. 'single_line_empty_body',
  644. ],
  645. 'no_useless_sprintf' => [
  646. 'method_argument_space',
  647. 'native_function_casing',
  648. 'no_empty_statement',
  649. 'no_extra_blank_lines',
  650. 'no_spaces_inside_parenthesis',
  651. 'spaces_inside_parentheses',
  652. ],
  653. 'nullable_type_declaration' => [
  654. 'ordered_types',
  655. 'types_spaces',
  656. ],
  657. 'nullable_type_declaration_for_default_null_value' => [
  658. 'no_unreachable_default_argument_value',
  659. 'nullable_type_declaration',
  660. 'ordered_types',
  661. ],
  662. 'ordered_class_elements' => [
  663. 'class_attributes_separation',
  664. 'no_blank_lines_after_class_opening',
  665. 'php_unit_data_provider_method_order',
  666. 'space_after_semicolon',
  667. ],
  668. 'ordered_imports' => [
  669. 'blank_line_between_import_groups',
  670. ],
  671. 'ordered_types' => [
  672. 'types_spaces',
  673. ],
  674. 'php_unit_attributes' => [
  675. 'fully_qualified_strict_types',
  676. 'no_empty_phpdoc',
  677. 'phpdoc_separation',
  678. 'phpdoc_trim',
  679. 'phpdoc_trim_consecutive_blank_line_separation',
  680. ],
  681. 'php_unit_construct' => [
  682. 'php_unit_dedicate_assert',
  683. ],
  684. 'php_unit_data_provider_method_order' => [
  685. 'class_attributes_separation',
  686. 'no_blank_lines_after_class_opening',
  687. ],
  688. 'php_unit_data_provider_name' => [
  689. 'php_unit_attributes',
  690. ],
  691. 'php_unit_data_provider_return_type' => [
  692. 'php_unit_attributes',
  693. 'return_to_yield_from',
  694. 'return_type_declaration',
  695. ],
  696. 'php_unit_data_provider_static' => [
  697. 'php_unit_attributes',
  698. ],
  699. 'php_unit_dedicate_assert' => [
  700. 'no_unused_imports',
  701. 'php_unit_assert_new_names',
  702. 'php_unit_dedicate_assert_internal_type',
  703. ],
  704. 'php_unit_fqcn_annotation' => [
  705. 'no_unused_imports',
  706. 'phpdoc_order_by_value',
  707. ],
  708. 'php_unit_internal_class' => [
  709. 'final_internal_class',
  710. 'phpdoc_separation',
  711. ],
  712. 'php_unit_no_expectation_annotation' => [
  713. 'no_empty_phpdoc',
  714. 'php_unit_expectation',
  715. ],
  716. 'php_unit_size_class' => [
  717. 'php_unit_attributes',
  718. 'phpdoc_separation',
  719. ],
  720. 'php_unit_test_annotation' => [
  721. 'no_empty_phpdoc',
  722. 'php_unit_method_casing',
  723. 'phpdoc_trim',
  724. ],
  725. 'php_unit_test_case_static_method_calls' => [
  726. 'self_static_accessor',
  727. ],
  728. 'php_unit_test_class_requires_covers' => [
  729. 'php_unit_attributes',
  730. 'phpdoc_separation',
  731. ],
  732. 'phpdoc_add_missing_param_annotation' => [
  733. 'no_empty_phpdoc',
  734. 'no_superfluous_phpdoc_tags',
  735. 'phpdoc_align',
  736. 'phpdoc_order',
  737. ],
  738. 'phpdoc_array_type' => [
  739. 'phpdoc_list_type',
  740. 'phpdoc_types_order',
  741. ],
  742. 'phpdoc_line_span' => [
  743. 'no_superfluous_phpdoc_tags',
  744. ],
  745. 'phpdoc_list_type' => [
  746. 'phpdoc_types_order',
  747. ],
  748. 'phpdoc_no_access' => [
  749. 'no_empty_phpdoc',
  750. 'phpdoc_separation',
  751. 'phpdoc_trim',
  752. ],
  753. 'phpdoc_no_alias_tag' => [
  754. 'phpdoc_add_missing_param_annotation',
  755. 'phpdoc_single_line_var_spacing',
  756. ],
  757. 'phpdoc_no_empty_return' => [
  758. 'no_empty_phpdoc',
  759. 'phpdoc_order',
  760. 'phpdoc_separation',
  761. 'phpdoc_trim',
  762. ],
  763. 'phpdoc_no_package' => [
  764. 'no_empty_phpdoc',
  765. 'phpdoc_separation',
  766. 'phpdoc_trim',
  767. ],
  768. 'phpdoc_no_useless_inheritdoc' => [
  769. 'no_empty_phpdoc',
  770. 'no_trailing_whitespace_in_comment',
  771. ],
  772. 'phpdoc_order' => [
  773. 'phpdoc_separation',
  774. 'phpdoc_trim',
  775. ],
  776. 'phpdoc_readonly_class_comment_to_keyword' => [
  777. 'no_empty_phpdoc',
  778. 'no_extra_blank_lines',
  779. 'phpdoc_align',
  780. ],
  781. 'phpdoc_return_self_reference' => [
  782. 'no_superfluous_phpdoc_tags',
  783. ],
  784. 'phpdoc_scalar' => [
  785. 'phpdoc_to_return_type',
  786. ],
  787. 'phpdoc_to_comment' => [
  788. 'no_empty_comment',
  789. 'phpdoc_no_useless_inheritdoc',
  790. 'single_line_comment_spacing',
  791. 'single_line_comment_style',
  792. ],
  793. 'phpdoc_to_param_type' => [
  794. 'no_superfluous_phpdoc_tags',
  795. ],
  796. 'phpdoc_to_property_type' => [
  797. 'fully_qualified_strict_types',
  798. 'no_superfluous_phpdoc_tags',
  799. ],
  800. 'phpdoc_to_return_type' => [
  801. 'fully_qualified_strict_types',
  802. 'no_superfluous_phpdoc_tags',
  803. 'return_to_yield_from',
  804. 'return_type_declaration',
  805. ],
  806. 'phpdoc_types' => [
  807. 'phpdoc_to_return_type',
  808. ],
  809. 'pow_to_exponentiation' => [
  810. 'binary_operator_spaces',
  811. 'method_argument_space',
  812. 'native_function_casing',
  813. 'no_spaces_after_function_name',
  814. 'no_spaces_inside_parenthesis',
  815. 'spaces_inside_parentheses',
  816. ],
  817. 'protected_to_private' => [
  818. 'ordered_class_elements',
  819. ],
  820. 'psr_autoloading' => [
  821. 'self_accessor',
  822. ],
  823. 'regular_callable_call' => [
  824. 'native_function_invocation',
  825. ],
  826. 'return_assignment' => [
  827. 'blank_line_before_statement',
  828. ],
  829. 'return_to_yield_from' => [
  830. 'yield_from_array_to_yields',
  831. ],
  832. 'semicolon_after_instruction' => [
  833. 'simplified_if_return',
  834. ],
  835. 'simplified_if_return' => [
  836. 'multiline_whitespace_before_semicolons',
  837. 'no_singleline_whitespace_before_semicolons',
  838. ],
  839. 'simplified_null_return' => [
  840. 'no_useless_return',
  841. 'void_return',
  842. ],
  843. 'single_class_element_per_statement' => [
  844. 'class_attributes_separation',
  845. ],
  846. 'single_import_per_statement' => [
  847. 'multiline_whitespace_before_semicolons',
  848. 'no_leading_import_slash',
  849. 'no_singleline_whitespace_before_semicolons',
  850. 'space_after_semicolon',
  851. ],
  852. 'single_line_throw' => [
  853. 'braces',
  854. 'concat_space',
  855. ],
  856. 'single_quote' => [
  857. 'no_useless_concat_operator',
  858. ],
  859. 'single_space_after_construct' => [
  860. 'braces',
  861. 'function_declaration',
  862. ],
  863. 'single_space_around_construct' => [
  864. 'braces',
  865. 'function_declaration',
  866. ],
  867. 'single_trait_insert_per_statement' => [
  868. 'braces',
  869. 'space_after_semicolon',
  870. ],
  871. 'spaces_inside_parentheses' => [
  872. 'function_to_constant',
  873. 'get_class_to_class_keyword',
  874. 'string_length_to_empty',
  875. ],
  876. 'standardize_increment' => [
  877. 'increment_style',
  878. ],
  879. 'standardize_not_equals' => [
  880. 'binary_operator_spaces',
  881. ],
  882. 'statement_indentation' => [
  883. 'heredoc_indentation',
  884. ],
  885. 'strict_comparison' => [
  886. 'binary_operator_spaces',
  887. 'modernize_strpos',
  888. ],
  889. 'strict_param' => [
  890. 'method_argument_space',
  891. 'native_function_invocation',
  892. ],
  893. 'string_implicit_backslashes' => [
  894. 'heredoc_to_nowdoc',
  895. 'single_quote',
  896. ],
  897. 'string_length_to_empty' => [
  898. 'no_extra_blank_lines',
  899. 'no_trailing_whitespace',
  900. ],
  901. 'ternary_to_elvis_operator' => [
  902. 'no_trailing_whitespace',
  903. 'ternary_operator_spaces',
  904. ],
  905. 'ternary_to_null_coalescing' => [
  906. 'assign_null_coalescing_to_coalesce_equal',
  907. ],
  908. 'unary_operator_spaces' => [
  909. 'not_operator_with_space',
  910. 'not_operator_with_successor_space',
  911. ],
  912. 'use_arrow_functions' => [
  913. 'function_declaration',
  914. ],
  915. 'visibility_required' => [
  916. 'class_attributes_separation',
  917. ],
  918. 'void_return' => [
  919. 'phpdoc_no_empty_return',
  920. 'return_type_declaration',
  921. ],
  922. 'yield_from_array_to_yields' => [
  923. 'blank_line_before_statement',
  924. 'no_extra_blank_lines',
  925. 'no_multiple_statements_per_line',
  926. 'no_whitespace_in_blank_line',
  927. 'statement_indentation',
  928. ],
  929. ];
  930. }
  931. /**
  932. * @return array<string, list<string>>
  933. */
  934. private static function getPhpDocFixersPriorityGraph(): array
  935. {
  936. // Prepare bulk tests for phpdoc fixers to test that:
  937. // * `align_multiline_comment` is first
  938. // * `comment_to_phpdoc` is second
  939. // * `phpdoc_to_comment` is third
  940. // * `phpdoc_indent` is fourth
  941. // * `phpdoc_types` is fifth
  942. // * `phpdoc_scalar` is sixth
  943. // * `phpdoc_align` is last
  944. $cases = [
  945. 'align_multiline_comment' => ['comment_to_phpdoc'],
  946. 'comment_to_phpdoc' => ['phpdoc_to_comment'],
  947. 'phpdoc_to_comment' => ['phpdoc_indent'],
  948. 'phpdoc_indent' => ['phpdoc_types'],
  949. 'phpdoc_types' => ['phpdoc_scalar'],
  950. 'phpdoc_scalar' => [],
  951. ];
  952. $docFixerNames = array_filter(
  953. array_keys(self::getAllFixers()),
  954. static fn (string $name): bool => str_contains($name, 'phpdoc')
  955. );
  956. foreach ($docFixerNames as $docFixerName) {
  957. if (!\in_array($docFixerName, ['comment_to_phpdoc', 'phpdoc_to_comment', 'phpdoc_indent', 'phpdoc_types', 'phpdoc_scalar'], true)) {
  958. $cases['align_multiline_comment'][] = $docFixerName;
  959. $cases['comment_to_phpdoc'][] = $docFixerName;
  960. $cases['phpdoc_indent'][] = $docFixerName;
  961. $cases['phpdoc_to_comment'][] = $docFixerName;
  962. if ('phpdoc_annotation_without_dot' !== $docFixerName) {
  963. $cases['phpdoc_scalar'][] = $docFixerName;
  964. $cases['phpdoc_types'][] = $docFixerName;
  965. }
  966. }
  967. if ('phpdoc_align' !== $docFixerName) {
  968. $cases[$docFixerName][] = 'phpdoc_align';
  969. }
  970. }
  971. return $cases;
  972. }
  973. /**
  974. * @return array<string, int>
  975. */
  976. private static function getFixerWithFixedPosition(): array
  977. {
  978. return [
  979. 'encoding' => 0, // Expected "encoding" fixer to have the highest priority.
  980. 'full_opening_tag' => 1, // Expected "full_opening_tag" fixer has second-highest priority.
  981. 'single_blank_line_at_eof' => -1, // Expected "single_blank_line_at_eof" to have the lowest priority.
  982. ];
  983. }
  984. /**
  985. * @return array<string, FixerInterface>
  986. */
  987. private static function getAllFixers(): array
  988. {
  989. $factory = new FixerFactory();
  990. $factory->registerBuiltInFixers();
  991. $fixers = [];
  992. foreach ($factory->getFixers() as $fixer) {
  993. $fixers[$fixer->getName()] = $fixer;
  994. }
  995. return $fixers;
  996. }
  997. private static function getIntegrationPriorityDirectory(): string
  998. {
  999. return __DIR__.'/../Fixtures/Integration/priority/';
  1000. }
  1001. }