NoExtraBlankLinesFixerTest.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344
  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\Fixer\Whitespace;
  13. use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException;
  14. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  15. use PhpCsFixer\WhitespacesFixerConfig;
  16. /**
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\Fixer\Whitespace\NoExtraBlankLinesFixer
  20. *
  21. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Whitespace\NoExtraBlankLinesFixer>
  22. *
  23. * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Whitespace\NoExtraBlankLinesFixer
  24. */
  25. final class NoExtraBlankLinesFixerTest extends AbstractFixerTestCase
  26. {
  27. private string $template = <<<'EOF'
  28. <?php
  29. use \DateTime;
  30. use \stdClass;
  31. use \InvalidArgumentException;
  32. class Test {
  33. public function testThrow($a)
  34. {
  35. if ($a) {
  36. throw new InvalidArgumentException('test.'); // test
  37. }
  38. $date = new DateTime();
  39. $class = new stdClass();
  40. $class = (string) $class;
  41. $e = new InvalidArgumentException($class.$date->format('Y'));
  42. throw $e;
  43. }
  44. public function testBreak($a)
  45. {
  46. switch($a) {
  47. case 1:
  48. echo $a;
  49. break;
  50. case 2:
  51. echo 'test';
  52. break;
  53. }
  54. }
  55. protected static function testContinueAndReturn($a, $b)
  56. {
  57. while($a < 100) {
  58. if ($b < time()) {
  59. continue;
  60. }
  61. return $b;
  62. }
  63. return $a;
  64. }
  65. private function test(){
  66. // comment
  67. }
  68. private function test123(){
  69. // comment
  70. }
  71. }
  72. EOF;
  73. /**
  74. * @param list<int> $lineNumberRemoved Line numbers expected to be removed after fixing
  75. * @param _AutogeneratedInputConfiguration['tokens'] $configTokens
  76. *
  77. * @dataProvider provideWithConfigCases
  78. */
  79. public function testWithConfig(array $lineNumberRemoved, array $configTokens): void
  80. {
  81. $this->fixer->configure(['tokens' => $configTokens]);
  82. $this->doTest($this->removeLinesFromString($this->template, $lineNumberRemoved), $this->template);
  83. }
  84. public static function provideWithConfigCases(): iterable
  85. {
  86. $tests = [
  87. [
  88. [9, 14, 21, 43, 45, 49, 53, 57],
  89. ['curly_brace_block'],
  90. ],
  91. [
  92. [3, 5],
  93. ['use'],
  94. ],
  95. [
  96. [23, 24],
  97. ['extra'],
  98. ],
  99. [
  100. [49, 53],
  101. ['return'],
  102. ],
  103. [
  104. [45],
  105. ['continue'],
  106. ],
  107. [
  108. [32],
  109. ['break'],
  110. ],
  111. [
  112. [14, 21],
  113. ['throw'],
  114. ],
  115. ];
  116. yield from $tests;
  117. $all = [[], []];
  118. foreach ($tests as $test) {
  119. $all[0] = array_merge($test[0], $all[0]);
  120. $all[1] = array_merge($test[1], $all[1]);
  121. }
  122. yield $all;
  123. }
  124. public function testFix(): void
  125. {
  126. $expected = <<<'EOF'
  127. <?php
  128. $a = new Bar();
  129. $a = new FooBaz();
  130. EOF;
  131. $input = <<<'EOF'
  132. <?php
  133. $a = new Bar();
  134. $a = new FooBaz();
  135. EOF;
  136. $this->doTest($expected, $input);
  137. }
  138. public function testFixWithManyEmptyLines(): void
  139. {
  140. $expected = <<<'EOF'
  141. <?php
  142. $a = new Bar();
  143. $a = new FooBaz();
  144. EOF;
  145. $input = <<<'EOF'
  146. <?php
  147. $a = new Bar();
  148. $a = new FooBaz();
  149. EOF;
  150. $this->doTest($expected, $input);
  151. }
  152. public function testFixWithHeredoc(): void
  153. {
  154. $expected = '
  155. <?php
  156. $b = <<<TEXT
  157. Foo TEXT
  158. Bar
  159. FooFoo
  160. TEXT;
  161. ';
  162. $this->doTest($expected);
  163. }
  164. public function testFixWithNowdoc(): void
  165. {
  166. $expected = '
  167. <?php
  168. $b = <<<\'TEXT\'
  169. Foo TEXT;
  170. Bar1}
  171. FooFoo
  172. TEXT;
  173. ';
  174. $this->doTest($expected);
  175. }
  176. public function testFixWithEncapsulatedNowdoc(): void
  177. {
  178. $expected = '
  179. <?php
  180. $b = <<<\'TEXT\'
  181. Foo TEXT
  182. Bar
  183. <<<\'TEMPLATE\'
  184. BarFooBar TEMPLATE
  185. TEMPLATE;
  186. FooFoo
  187. TEXT;
  188. ';
  189. $this->doTest($expected);
  190. }
  191. public function testFixWithMultilineString(): void
  192. {
  193. $expected = <<<'EOF'
  194. <?php
  195. $a = 'Foo
  196. Bar';
  197. EOF;
  198. $this->doTest($expected);
  199. }
  200. public function testFixWithTrickyMultilineStrings(): void
  201. {
  202. $expected = <<<'EOF'
  203. <?php
  204. $a = 'Foo';
  205. $b = 'Bar
  206. Here\'s an escaped quote '
  207. .
  208. '
  209. FooFoo';
  210. EOF;
  211. $input = <<<'EOF'
  212. <?php
  213. $a = 'Foo';
  214. $b = 'Bar
  215. Here\'s an escaped quote '
  216. .
  217. '
  218. FooFoo';
  219. EOF;
  220. $this->doTest($expected, $input);
  221. }
  222. public function testFixWithCommentWithQuote(): void
  223. {
  224. $expected = <<<'EOF'
  225. <?php
  226. $a = 'foo';
  227. // my comment's must have a quote
  228. $b = 'foobar';
  229. $c = 'bar';
  230. EOF;
  231. $input = <<<'EOF'
  232. <?php
  233. $a = 'foo';
  234. // my comment's must have a quote
  235. $b = 'foobar';
  236. $c = 'bar';
  237. EOF;
  238. $this->doTest($expected, $input);
  239. }
  240. public function testFixWithTrailingInlineBlock(): void
  241. {
  242. $expected = "
  243. <?php
  244. echo 'hello';
  245. ?>
  246. \$a = 0;
  247. //a
  248. <?php
  249. \$a = 0;
  250. \$b = 1;
  251. //a
  252. ?>
  253. ";
  254. $this->doTest($expected);
  255. }
  256. /**
  257. * @dataProvider provideFixWithCommentsCases
  258. */
  259. public function testFixWithComments(string $expected, string $input): void
  260. {
  261. $this->doTest($expected, $input);
  262. }
  263. /**
  264. * @return iterable<array{string, string}>
  265. */
  266. public static function provideFixWithCommentsCases(): iterable
  267. {
  268. yield [
  269. <<<'EOF'
  270. <?php
  271. //class Test
  272. $a; //
  273. $b;
  274. /***/
  275. $c;
  276. //
  277. $d;
  278. EOF,
  279. <<<'EOF'
  280. <?php
  281. //class Test
  282. $a; //
  283. $b;
  284. /***/
  285. $c;
  286. //
  287. $d;
  288. EOF,
  289. ];
  290. yield [
  291. "<?php\n//a\n\n\$a =1;",
  292. "<?php\n//a\n\n\n\n\$a =1;",
  293. ];
  294. }
  295. /**
  296. * @dataProvider provideFixWithLineBreaksCases
  297. */
  298. public function testFixWithLineBreaks(string $expected, ?string $input = null): void
  299. {
  300. $this->doTest($expected, $input);
  301. }
  302. /**
  303. * @return iterable<array{0: string, 1?: string}>
  304. */
  305. public static function provideFixWithLineBreaksCases(): iterable
  306. {
  307. $input = '<?php //
  308. $a = 1;
  309. $b = 1;
  310. ';
  311. $expected = '<?php //
  312. $a = 1;
  313. $b = 1;
  314. ';
  315. yield [
  316. "<?php\r\n//a\r\n\r\n\$a =1;",
  317. "<?php\r\n//a\r\n\r\n\r\n\r\n\$a =1;",
  318. ];
  319. yield [
  320. $expected,
  321. $input,
  322. ];
  323. yield [
  324. str_replace("\n", "\r\n", $expected),
  325. str_replace("\n", "\r\n", $input),
  326. ];
  327. yield [
  328. str_replace("\n", "\r", $input),
  329. ];
  330. yield [
  331. str_replace("\n", "\r", $expected),
  332. ];
  333. }
  334. public function testWrongConfig(): void
  335. {
  336. $this->expectException(InvalidFixerConfigurationException::class);
  337. $this->expectExceptionMessageMatches('/^\[no_extra_blank_lines\] Invalid configuration: The option "tokens" .*\.$/');
  338. $this->fixer->configure(['tokens' => ['__TEST__']]); // @phpstan-ignore-line
  339. }
  340. /**
  341. * @dataProvider provideBetweenUseCases
  342. */
  343. public function testBetweenUse(string $expected, ?string $input = null): void
  344. {
  345. $this->fixer->configure(['tokens' => ['use']]);
  346. $this->doTest($expected, $input);
  347. }
  348. /**
  349. * @return iterable<array{0: string, 1?: string}>
  350. */
  351. public static function provideBetweenUseCases(): iterable
  352. {
  353. yield ['<?php use A\B;'];
  354. yield ['<?php use A\B?>'];
  355. yield ['<?php use A\B;use A\D; return 1;'];
  356. yield ["<?php use A\\B?>\n\n<?php use D\\E\\F?>"];
  357. yield ['<?php use Y\B;use A\D; return 1;'];
  358. yield [
  359. '<?php
  360. use A\B;
  361. use A\C;',
  362. '<?php
  363. use A\B;
  364. use A\C;',
  365. ];
  366. yield [
  367. '<?php use A\E;use A\Z;
  368. use C;
  369. return 1;
  370. ',
  371. '<?php use A\E;use A\Z;
  372. use C;
  373. return 1;
  374. ',
  375. ];
  376. yield [
  377. '<?php
  378. class Test {
  379. use A;
  380. use B;
  381. }',
  382. ];
  383. yield [
  384. '<?php
  385. $example = function () use ($message) { var_dump($message); };
  386. $example = function () use ($message) { var_dump($message); };
  387. ',
  388. ];
  389. yield [
  390. '<?php
  391. use function A; use function B;
  392. echo 1;',
  393. ];
  394. yield [
  395. '<?php
  396. use some\a\{ClassA, ClassB, ClassC as C,};
  397. use function some\a\{fn_a, fn_b, fn_c,};
  398. use const some\a\{ConstA,ConstB,ConstC
  399. ,
  400. };
  401. use const some\Z\{ConstX,ConstY,ConstZ,};
  402. ',
  403. '<?php
  404. use some\a\{ClassA, ClassB, ClassC as C,};
  405. use function some\a\{fn_a, fn_b, fn_c,};
  406. use const some\a\{ConstA,ConstB,ConstC
  407. ,
  408. };
  409. '.'
  410. use const some\Z\{ConstX,ConstY,ConstZ,};
  411. ',
  412. ];
  413. }
  414. /**
  415. * @dataProvider provideRemoveLinesBetweenUseStatementsCases
  416. */
  417. public function testRemoveLinesBetweenUseStatements(string $expected, ?string $input = null): void
  418. {
  419. $this->fixer->configure(['tokens' => ['use']]);
  420. $this->doTest($expected, $input);
  421. }
  422. /**
  423. * @return iterable<array{string, string}>
  424. */
  425. public static function provideRemoveLinesBetweenUseStatementsCases(): iterable
  426. {
  427. yield [
  428. <<<'EOF'
  429. <?php
  430. use Zxy\Qux;
  431. use Zoo\Bar as Bar2;
  432. use Foo\Bar as Bar1;
  433. use Foo\Zar\Baz;
  434. $c = 1;
  435. use Foo\Quxx as Quxx1;
  436. use Foo\Zar\Quxx;
  437. $a = new Bar1();
  438. $a = new Bar2();
  439. $a = new Baz();
  440. $a = new Qux();
  441. EOF,
  442. <<<'EOF'
  443. <?php
  444. use Zxy\Qux;
  445. use Zoo\Bar as Bar2;
  446. use Foo\Bar as Bar1;
  447. use Foo\Zar\Baz;
  448. $c = 1;
  449. use Foo\Quxx as Quxx1;
  450. use Foo\Zar\Quxx;
  451. $a = new Bar1();
  452. $a = new Bar2();
  453. $a = new Baz();
  454. $a = new Qux();
  455. EOF,
  456. ];
  457. yield [
  458. '<?php
  459. use some\a\{ClassA, ClassB, ClassC as C};
  460. use function some\a\{fn_a, fn_b, fn_c};
  461. use const some\a\{ConstA, ConstB, ConstC};
  462. ',
  463. '<?php
  464. use some\a\{ClassA, ClassB, ClassC as C};
  465. use function some\a\{fn_a, fn_b, fn_c};
  466. use const some\a\{ConstA, ConstB, ConstC};
  467. ',
  468. ];
  469. }
  470. /**
  471. * @dataProvider provideWithoutUsesCases
  472. */
  473. public function testWithoutUses(string $expected): void
  474. {
  475. $this->fixer->configure(['tokens' => ['use']]);
  476. $this->doTest($expected);
  477. }
  478. /**
  479. * @return iterable<array{string}>
  480. */
  481. public static function provideWithoutUsesCases(): iterable
  482. {
  483. yield [
  484. '<?php
  485. $c = 1;
  486. $a = new Baz();
  487. $a = new Qux();',
  488. ];
  489. yield [
  490. '<?php use A\B;',
  491. ];
  492. yield [
  493. '<?php use A\B?>',
  494. ];
  495. yield [
  496. '<?php use A\B;?>',
  497. ];
  498. }
  499. /**
  500. * @dataProvider provideRemoveBetweenUseTraitsCases
  501. *
  502. * @group legacy
  503. */
  504. public function testRemoveBetweenUseTraits(string $expected, string $input): void
  505. {
  506. $this->expectDeprecation('Option "tokens: use_trait" used in `no_extra_blank_lines` rule is deprecated, use the rule `class_attributes_separation` with `elements: trait_import` instead.');
  507. $this->fixer->configure(['tokens' => ['use_trait']]);
  508. $this->doTest($expected, $input);
  509. }
  510. /**
  511. * @return iterable<array{string, string}>
  512. */
  513. public static function provideRemoveBetweenUseTraitsCases(): iterable
  514. {
  515. yield [
  516. '<?php
  517. class Foo
  518. {
  519. use Z; // 123
  520. use Bar;/* */use Baz;
  521. public function baz() {}
  522. use Bar1; use Baz1;
  523. public function baz1() {}
  524. }
  525. ',
  526. '<?php
  527. class Foo
  528. {
  529. use Z; // 123
  530. use Bar;/* */use Baz;
  531. public function baz() {}
  532. use Bar1; use Baz1;
  533. public function baz1() {}
  534. }
  535. ',
  536. ];
  537. yield [
  538. '<?php
  539. class Foo
  540. {
  541. use Bar;use Baz;
  542. use Bar1;use Baz1;
  543. public function baz() {}
  544. }
  545. ',
  546. '<?php
  547. class Foo
  548. {
  549. use Bar;use Baz;
  550. use Bar1;use Baz1;
  551. public function baz() {}
  552. }
  553. ',
  554. ];
  555. yield [
  556. '<?php
  557. namespace T\A;
  558. use V;
  559. use W;
  560. class Test {
  561. use A;
  562. use B;
  563. private function test($b) {
  564. $a = function() use ($b) { echo $b;};
  565. $b = function() use ($b) { echo $b;};
  566. }
  567. }',
  568. '<?php
  569. namespace T\A;
  570. use V;
  571. use W;
  572. class Test {
  573. use A;
  574. use B;
  575. private function test($b) {
  576. $a = function() use ($b) { echo $b;};
  577. $b = function() use ($b) { echo $b;};
  578. }
  579. }',
  580. ];
  581. }
  582. /**
  583. * @dataProvider provideOneOrInLineCases
  584. */
  585. public function testOneOrInLine(string $expected, ?string $input = null): void
  586. {
  587. $this->fixer->configure(['tokens' => [
  588. 'break',
  589. 'continue',
  590. 'return',
  591. 'throw',
  592. 'curly_brace_block',
  593. 'square_brace_block',
  594. 'parenthesis_brace_block',
  595. ]]);
  596. $this->doTest($expected, $input);
  597. }
  598. /**
  599. * @return iterable<array{0: string, 1?: string}>
  600. */
  601. public static function provideOneOrInLineCases(): iterable
  602. {
  603. yield [
  604. "<?php\n\n\$a = function() use (\$b) { while(3<1)break; \$c = \$b[1]; while(\$b<1)continue; if (true) throw \$e; return 1; };\n\n",
  605. ];
  606. yield [
  607. "<?php throw new \\Exception('do not import.');\n",
  608. "<?php throw new \\Exception('do not import.');\n\n",
  609. ];
  610. yield [
  611. "<?php\n\n\$a = \$b[0];\n\n",
  612. ];
  613. yield [
  614. "<?php\n\n\$a->{'Test'};\nfunction test(){}\n",
  615. ];
  616. yield [
  617. "<?php\n\n\$a = new class { public function a () { while(4<1)break; while(3<1)continue; if (true) throw \$e; return 1; }};\n\n",
  618. ];
  619. }
  620. /**
  621. * @param _AutogeneratedInputConfiguration $config
  622. *
  623. * @dataProvider provideBracesCases
  624. */
  625. public function testBraces(array $config, string $expected, ?string $input = null): void
  626. {
  627. $this->fixer->configure($config);
  628. $this->doTest($expected, $input);
  629. }
  630. public static function provideBracesCases(): iterable
  631. {
  632. yield [
  633. ['tokens' => ['curly_brace_block']],
  634. "<?php function test()\n\n{}\n\necho 789;",
  635. ];
  636. yield [
  637. ['tokens' => ['curly_brace_block']],
  638. "<?php switch(\$a){\ncase 1:echo 789;}",
  639. "<?php switch(\$a){\n \ncase 1:echo 789;}",
  640. ];
  641. yield [
  642. ['tokens' => ['parenthesis_brace_block']],
  643. '<?php
  644. is_int(
  645. 1);
  646. function test(
  647. $a,
  648. $b,
  649. $c
  650. )
  651. {
  652. }',
  653. '<?php
  654. is_int(
  655. 1);
  656. function test(
  657. $a,
  658. $b,
  659. $c
  660. )
  661. {
  662. }',
  663. ];
  664. yield [
  665. ['tokens' => ['parenthesis_brace_block']],
  666. "<?php array(\n1,\n2,\n3,\n);",
  667. "<?php array(\n \n1,\n2,\n3,\n\n\n);",
  668. ];
  669. yield [
  670. ['tokens' => ['parenthesis_brace_block']],
  671. '<?php
  672. function a()
  673. {
  674. $b->d(e(
  675. ));
  676. foreach ($a as $x) {
  677. }
  678. }',
  679. ];
  680. yield [
  681. ['tokens' => ['return']],
  682. '<?php
  683. class Foo
  684. {
  685. public function bar() {return 1;}
  686. public function baz() {return 2;
  687. }
  688. }',
  689. '<?php
  690. class Foo
  691. {
  692. public function bar() {return 1;}
  693. public function baz() {return 2;
  694. }
  695. }',
  696. ];
  697. yield [
  698. ['tokens' => ['square_brace_block']],
  699. "<?php \$c = \$b[0];\n\n\n\$a = [\n 1,\n2];\necho 1;\n\$b = [];\n\n\n//a\n",
  700. "<?php \$c = \$b[0];\n\n\n\$a = [\n\n 1,\n2];\necho 1;\n\$b = [];\n\n\n//a\n",
  701. ];
  702. }
  703. /**
  704. * @dataProvider provideFixPre80Cases
  705. *
  706. * @requires PHP <8.0
  707. */
  708. public function testFixPre80(string $expected, ?string $input = null): void
  709. {
  710. $this->doTest($expected, $input);
  711. }
  712. /**
  713. * @return iterable<array{string, 1?: string}>
  714. */
  715. public static function provideFixPre80Cases(): iterable
  716. {
  717. yield [
  718. "<?php\n\n\$a = \$b{0};\n\n",
  719. ];
  720. }
  721. /**
  722. * @param _AutogeneratedInputConfiguration $config
  723. *
  724. * @dataProvider provideMessyWhitespacesCases
  725. */
  726. public function testMessyWhitespaces(array $config, string $expected, ?string $input = null): void
  727. {
  728. $this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t", "\r\n"));
  729. $this->fixer->configure($config);
  730. $this->doTest($expected, $input);
  731. }
  732. public static function provideMessyWhitespacesCases(): iterable
  733. {
  734. yield [
  735. [],
  736. "<?php\r\nuse AAA;\r\n\r\nuse BBB;\r\n\r\n",
  737. "<?php\r\nuse AAA;\r\n\r\n\r\n\r\nuse BBB;\r\n\r\n",
  738. ];
  739. yield [
  740. ['tokens' => ['parenthesis_brace_block']],
  741. "<?php is_int(\r\n1);",
  742. "<?php is_int(\r\n\r\n\r\n\r\n1);",
  743. ];
  744. yield [
  745. ['tokens' => ['square_brace_block']],
  746. "<?php \$c = \$b[0];\r\n\r\n\r\n\$a = [\r\n 1,\r\n2];\r\necho 1;\r\n\$b = [];\r\n\r\n\r\n//a\r\n",
  747. "<?php \$c = \$b[0];\r\n\r\n\r\n\$a = [\r\n\r\n 1,\r\n2];\r\necho 1;\r\n\$b = [];\r\n\r\n\r\n//a\r\n",
  748. ];
  749. yield [
  750. ['tokens' => ['square_brace_block']],
  751. "<?php \$c = \$b[0];\r\n\r\n\r\n\$a = [\r\n\t1,\r\n2];",
  752. "<?php \$c = \$b[0];\r\n\r\n\r\n\$a = [\r\n\r\n\t1,\r\n2];",
  753. ];
  754. }
  755. /**
  756. * @param _AutogeneratedInputConfiguration['tokens'] $configTokens
  757. *
  758. * @dataProvider provideInSwitchStatementCases
  759. */
  760. public function testInSwitchStatement(array $configTokens, string $expected, ?string $input = null): void
  761. {
  762. $this->fixer->configure(['tokens' => $configTokens]);
  763. $this->doTest($expected, $input);
  764. }
  765. public static function provideInSwitchStatementCases(): iterable
  766. {
  767. yield [
  768. [
  769. 'break',
  770. 'continue',
  771. 'extra',
  772. 'return',
  773. 'throw',
  774. ],
  775. '<?php
  776. /** a */
  777. switch ($a) {
  778. case 1:
  779. break;
  780. case 2:
  781. continue;
  782. case 3:
  783. return 1;
  784. case 4:
  785. throw $e;
  786. case 5:
  787. throw new \Exception();
  788. case Token::STRING_TYPE:
  789. echo 123;
  790. return new ConstantNode($token->getValue());
  791. case 7:
  792. return new ConstantNode($token->getValue());
  793. case 8:
  794. return 8;
  795. default:
  796. echo 1;
  797. }',
  798. '<?php
  799. /** a */
  800. switch ($a) {
  801. case 1:
  802. break;
  803. case 2:
  804. continue;
  805. case 3:
  806. return 1;
  807. case 4:
  808. throw $e;
  809. case 5:
  810. throw new \Exception();
  811. case Token::STRING_TYPE:
  812. echo 123;
  813. return new ConstantNode($token->getValue());
  814. case 7:
  815. return new ConstantNode($token->getValue());
  816. '.'
  817. case 8:
  818. return 8;
  819. '.'
  820. default:
  821. echo 1;
  822. }',
  823. ];
  824. yield [
  825. [
  826. 'switch',
  827. 'case',
  828. 'default',
  829. ],
  830. '<?php
  831. switch($a) {
  832. case 0:
  833. case 1:
  834. default:
  835. return 1;
  836. }',
  837. '<?php
  838. switch($a) {
  839. case 0:
  840. case 1:
  841. default:
  842. return 1;
  843. }',
  844. ];
  845. yield [
  846. [
  847. 'switch',
  848. 'case',
  849. 'default',
  850. ],
  851. '<?php
  852. switch($a) { case 2: echo 3;
  853. default: return 1;}
  854. // above stays empty',
  855. '<?php
  856. switch($a) { case 2: echo 3;
  857. default: return 1;}
  858. // above stays empty',
  859. ];
  860. }
  861. public function testRemovingEmptyLinesAfterOpenTag(): void
  862. {
  863. $this->doTest(
  864. '<?php
  865. class Foo {}',
  866. '<?php
  867. class Foo {}'
  868. );
  869. }
  870. /**
  871. * @param _AutogeneratedInputConfiguration $config
  872. *
  873. * @dataProvider provideFix80Cases
  874. *
  875. * @requires PHP 8.0
  876. */
  877. public function testFix80(array $config, string $expected, ?string $input = null): void
  878. {
  879. $this->fixer->configure($config);
  880. $this->doTest($expected, $input);
  881. }
  882. public static function provideFix80Cases(): iterable
  883. {
  884. yield [
  885. ['tokens' => ['throw']],
  886. '<?php
  887. $nullCoalescingOperator1 = $bar ?? throw new \Exception();
  888. $nullCoalescingOperator2 = $bar ?? throw new \Exception();
  889. $nullCoalescingOperator3 = $bar ?? throw new \Exception();
  890. $ternaryOperator1 = $bar ? 42 : throw new \Exception();
  891. $ternaryOperator2 = $bar ? 42 : throw new \Exception();
  892. $ternaryOperator3 = $bar ? 42 : throw new \Exception();
  893. $orOperator1 = $bar || throw new \Exception();
  894. $orOperator2 = $bar || throw new \Exception();
  895. $orOperator3 = $bar || throw new \Exception();
  896. ',
  897. ];
  898. yield [
  899. ['tokens' => ['throw']],
  900. '<?php
  901. $a = $bar ?? throw new \Exception();
  902. // Now, we are going to use it!
  903. var_dump($a);
  904. ',
  905. ];
  906. yield [
  907. ['tokens' => ['attribute']],
  908. '<?php
  909. #[Attr]
  910. #[AttrFoo1]
  911. #[AttrFoo2]
  912. function foo(){}
  913. ',
  914. '<?php
  915. #[Attr]
  916. #[AttrFoo1]
  917. #[AttrFoo2]
  918. function foo(){}
  919. ',
  920. ];
  921. yield [
  922. ['tokens' => ['attribute']],
  923. '<?php class Foo
  924. {
  925. protected function f1(string $x): string { return "r1"; }
  926. protected function f2(string $x): string { return "r1"; }
  927. protected function f3(#[Attr] string $x): string { return "r1"; }
  928. protected function f4(string $x): string { return "r1"; }
  929. }',
  930. ];
  931. yield [
  932. ['tokens' => ['attribute']],
  933. '<?php abstract class Foo
  934. {
  935. abstract protected function f1(string $x): string;
  936. abstract protected function f2(string $x): string;
  937. abstract protected function f3(#[Attr] string $x): string;
  938. abstract protected function f4(string $x): string;
  939. }',
  940. ];
  941. }
  942. /**
  943. * @dataProvider provideFix81Cases
  944. *
  945. * @requires PHP 8.1
  946. */
  947. public function testFix81(string $expected, ?string $input = null): void
  948. {
  949. $this->fixer->configure(['tokens' => ['case']]);
  950. $this->doTest($expected, $input);
  951. }
  952. /**
  953. * @return iterable<array{0: string, 1?: string}>
  954. */
  955. public static function provideFix81Cases(): iterable
  956. {
  957. yield [
  958. '<?php
  959. enum test
  960. {
  961. case Baz;
  962. public function foo() {
  963. switch (bar()) {
  964. case 1: echo 1; break;
  965. case 2: echo 2; break;
  966. }
  967. }
  968. case Bad;
  969. }
  970. ',
  971. '<?php
  972. enum test
  973. {
  974. case Baz;
  975. public function foo() {
  976. switch (bar()) {
  977. case 1: echo 1; break;
  978. case 2: echo 2; break;
  979. }
  980. }
  981. case Bad;
  982. }
  983. ',
  984. ];
  985. $expectedTemplate = '<?php
  986. enum Foo
  987. {
  988. case CASE_1;
  989. %s
  990. }';
  991. $enumAttributes = [
  992. 'case CASE_2;',
  993. 'const CONST_1 = self::CASE_1;',
  994. 'private const CONST_1 = self::CASE_1;',
  995. 'public function bar(): void {}',
  996. 'protected function bar(): void {}',
  997. 'private function bar(): void {}',
  998. 'static function bar(): void {}',
  999. 'final function bar(): void {}',
  1000. ];
  1001. foreach ($enumAttributes as $enumAttribute) {
  1002. yield [
  1003. \sprintf($expectedTemplate, $enumAttribute),
  1004. ];
  1005. }
  1006. }
  1007. /**
  1008. * @param list<int> $lineNumbers
  1009. */
  1010. private function removeLinesFromString(string $input, array $lineNumbers): string
  1011. {
  1012. sort($lineNumbers);
  1013. $lines = explode("\n", $input);
  1014. foreach ($lineNumbers as $lineNumber) {
  1015. --$lineNumber;
  1016. unset($lines[$lineNumber]);
  1017. }
  1018. return implode("\n", $lines);
  1019. }
  1020. }