FixerFactoryTest.php 37 KB

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