FixerFactoryTest.php 37 KB

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