FixerFactoryTest.php 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067
  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. 'fully_qualified_strict_types',
  787. 'no_superfluous_phpdoc_tags',
  788. ],
  789. 'phpdoc_to_return_type' => [
  790. 'fully_qualified_strict_types',
  791. 'no_superfluous_phpdoc_tags',
  792. 'return_to_yield_from',
  793. 'return_type_declaration',
  794. ],
  795. 'phpdoc_types' => [
  796. 'phpdoc_to_return_type',
  797. ],
  798. 'pow_to_exponentiation' => [
  799. 'binary_operator_spaces',
  800. 'method_argument_space',
  801. 'native_function_casing',
  802. 'no_spaces_after_function_name',
  803. 'no_spaces_inside_parenthesis',
  804. 'spaces_inside_parentheses',
  805. ],
  806. 'protected_to_private' => [
  807. 'ordered_class_elements',
  808. ],
  809. 'psr_autoloading' => [
  810. 'self_accessor',
  811. ],
  812. 'regular_callable_call' => [
  813. 'native_function_invocation',
  814. ],
  815. 'return_assignment' => [
  816. 'blank_line_before_statement',
  817. ],
  818. 'return_to_yield_from' => [
  819. 'yield_from_array_to_yields',
  820. ],
  821. 'semicolon_after_instruction' => [
  822. 'simplified_if_return',
  823. ],
  824. 'simplified_if_return' => [
  825. 'multiline_whitespace_before_semicolons',
  826. 'no_singleline_whitespace_before_semicolons',
  827. ],
  828. 'simplified_null_return' => [
  829. 'no_useless_return',
  830. 'void_return',
  831. ],
  832. 'single_class_element_per_statement' => [
  833. 'class_attributes_separation',
  834. ],
  835. 'single_import_per_statement' => [
  836. 'multiline_whitespace_before_semicolons',
  837. 'no_leading_import_slash',
  838. 'no_singleline_whitespace_before_semicolons',
  839. 'space_after_semicolon',
  840. ],
  841. 'single_line_throw' => [
  842. 'braces',
  843. 'concat_space',
  844. ],
  845. 'single_quote' => [
  846. 'no_useless_concat_operator',
  847. ],
  848. 'single_space_after_construct' => [
  849. 'braces',
  850. 'function_declaration',
  851. ],
  852. 'single_space_around_construct' => [
  853. 'braces',
  854. 'function_declaration',
  855. ],
  856. 'single_trait_insert_per_statement' => [
  857. 'braces',
  858. 'space_after_semicolon',
  859. ],
  860. 'spaces_inside_parentheses' => [
  861. 'function_to_constant',
  862. 'get_class_to_class_keyword',
  863. 'string_length_to_empty',
  864. ],
  865. 'standardize_increment' => [
  866. 'increment_style',
  867. ],
  868. 'standardize_not_equals' => [
  869. 'binary_operator_spaces',
  870. ],
  871. 'statement_indentation' => [
  872. 'heredoc_indentation',
  873. ],
  874. 'strict_comparison' => [
  875. 'binary_operator_spaces',
  876. 'modernize_strpos',
  877. ],
  878. 'strict_param' => [
  879. 'method_argument_space',
  880. 'native_function_invocation',
  881. ],
  882. 'string_implicit_backslashes' => [
  883. 'heredoc_to_nowdoc',
  884. 'single_quote',
  885. ],
  886. 'string_length_to_empty' => [
  887. 'no_extra_blank_lines',
  888. 'no_trailing_whitespace',
  889. ],
  890. 'ternary_to_elvis_operator' => [
  891. 'no_trailing_whitespace',
  892. 'ternary_operator_spaces',
  893. ],
  894. 'ternary_to_null_coalescing' => [
  895. 'assign_null_coalescing_to_coalesce_equal',
  896. ],
  897. 'unary_operator_spaces' => [
  898. 'not_operator_with_space',
  899. 'not_operator_with_successor_space',
  900. ],
  901. 'use_arrow_functions' => [
  902. 'function_declaration',
  903. ],
  904. 'visibility_required' => [
  905. 'class_attributes_separation',
  906. ],
  907. 'void_return' => [
  908. 'phpdoc_no_empty_return',
  909. 'return_type_declaration',
  910. ],
  911. 'yield_from_array_to_yields' => [
  912. 'blank_line_before_statement',
  913. 'no_extra_blank_lines',
  914. 'no_multiple_statements_per_line',
  915. 'no_whitespace_in_blank_line',
  916. 'statement_indentation',
  917. ],
  918. ];
  919. }
  920. /**
  921. * @return array<string, list<string>>
  922. */
  923. private static function getPhpDocFixersPriorityGraph(): array
  924. {
  925. // Prepare bulk tests for phpdoc fixers to test that:
  926. // * `align_multiline_comment` is first
  927. // * `comment_to_phpdoc` is second
  928. // * `phpdoc_to_comment` is third
  929. // * `phpdoc_indent` is fourth
  930. // * `phpdoc_types` is fifth
  931. // * `phpdoc_scalar` is sixth
  932. // * `phpdoc_align` is last
  933. $cases = [
  934. 'align_multiline_comment' => ['comment_to_phpdoc'],
  935. 'comment_to_phpdoc' => ['phpdoc_to_comment'],
  936. 'phpdoc_to_comment' => ['phpdoc_indent'],
  937. 'phpdoc_indent' => ['phpdoc_types'],
  938. 'phpdoc_types' => ['phpdoc_scalar'],
  939. 'phpdoc_scalar' => [],
  940. ];
  941. $docFixerNames = array_filter(
  942. array_keys(self::getAllFixers()),
  943. static fn (string $name): bool => str_contains($name, 'phpdoc')
  944. );
  945. foreach ($docFixerNames as $docFixerName) {
  946. if (!\in_array($docFixerName, ['comment_to_phpdoc', 'phpdoc_to_comment', 'phpdoc_indent', 'phpdoc_types', 'phpdoc_scalar'], true)) {
  947. $cases['align_multiline_comment'][] = $docFixerName;
  948. $cases['comment_to_phpdoc'][] = $docFixerName;
  949. $cases['phpdoc_indent'][] = $docFixerName;
  950. $cases['phpdoc_to_comment'][] = $docFixerName;
  951. if ('phpdoc_annotation_without_dot' !== $docFixerName) {
  952. $cases['phpdoc_scalar'][] = $docFixerName;
  953. $cases['phpdoc_types'][] = $docFixerName;
  954. }
  955. }
  956. if ('phpdoc_align' !== $docFixerName) {
  957. $cases[$docFixerName][] = 'phpdoc_align';
  958. }
  959. }
  960. return $cases;
  961. }
  962. /**
  963. * @return array<string, int>
  964. */
  965. private static function getFixerWithFixedPosition(): array
  966. {
  967. return [
  968. 'encoding' => 0, // Expected "encoding" fixer to have the highest priority.
  969. 'full_opening_tag' => 1, // Expected "full_opening_tag" fixer has second-highest priority.
  970. 'single_blank_line_at_eof' => -1, // Expected "single_blank_line_at_eof" to have the lowest priority.
  971. ];
  972. }
  973. /**
  974. * @return array<string, FixerInterface>
  975. */
  976. private static function getAllFixers(): array
  977. {
  978. $factory = new FixerFactory();
  979. $factory->registerBuiltInFixers();
  980. $fixers = [];
  981. foreach ($factory->getFixers() as $fixer) {
  982. $fixers[$fixer->getName()] = $fixer;
  983. }
  984. return $fixers;
  985. }
  986. private static function getIntegrationPriorityDirectory(): string
  987. {
  988. return __DIR__.'/../Fixtures/Integration/priority/';
  989. }
  990. }