FixerFactoryTest.php 37 KB

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