FixerFactoryTest.php 37 KB

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