NoExtraBlankLinesFixerTest.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083
  1. <?php
  2. /*
  3. * This file is part of PHP CS Fixer.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. namespace PhpCsFixer\Tests\Fixer\Whitespace;
  12. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  13. use PhpCsFixer\WhitespacesFixerConfig;
  14. /**
  15. * @internal
  16. *
  17. * @covers \PhpCsFixer\Fixer\Whitespace\NoExtraBlankLinesFixer
  18. */
  19. final class NoExtraBlankLinesFixerTest extends AbstractFixerTestCase
  20. {
  21. private $template = <<<'EOF'
  22. <?php
  23. use \DateTime;
  24. use \stdClass;
  25. use \InvalidArgumentException;
  26. class Test {
  27. public function testThrow($a)
  28. {
  29. if ($a) {
  30. throw new InvalidArgumentException('test'); // test
  31. }
  32. $date = new DateTime();
  33. $class = new stdClass();
  34. $class = (string) $class;
  35. $e = new InvalidArgumentException($class.$date->format('Y'));
  36. throw $e;
  37. }
  38. public function testBreak($a)
  39. {
  40. switch($a) {
  41. case 1:
  42. echo $a;
  43. break;
  44. case 2:
  45. echo 'test';
  46. break;
  47. }
  48. }
  49. protected static function testContinueAndReturn($a, $b)
  50. {
  51. while($a < 100) {
  52. if ($b < time()) {
  53. continue;
  54. }
  55. return $b;
  56. }
  57. return $a;
  58. }
  59. private function test(){
  60. // comment
  61. }
  62. private function test123(){
  63. // comment
  64. }
  65. }
  66. EOF;
  67. /**
  68. * @param int[] $lineNumberRemoved Line numbers expected to be removed after fixing
  69. * @param null|string[] $config
  70. *
  71. * @dataProvider provideWithConfigCases
  72. */
  73. public function testWithConfig(array $lineNumberRemoved, array $config)
  74. {
  75. $this->fixer->configure(['tokens' => $config]);
  76. $this->doTest($this->removeLinesFromString($this->template, $lineNumberRemoved), $this->template);
  77. }
  78. public function provideWithConfigCases()
  79. {
  80. $tests = [
  81. [
  82. [9, 14, 21, 43, 45, 49, 53, 57],
  83. ['curly_brace_block'],
  84. ],
  85. [
  86. [3, 5],
  87. ['use'],
  88. ],
  89. [
  90. [23, 24],
  91. ['extra'],
  92. ],
  93. [
  94. [49, 53],
  95. ['return'],
  96. ],
  97. [
  98. [45],
  99. ['continue'],
  100. ],
  101. [
  102. [32],
  103. ['break'],
  104. ],
  105. [
  106. [14, 21],
  107. ['throw'],
  108. ],
  109. ];
  110. $all = [[], []];
  111. foreach ($tests as $test) {
  112. $all[0] = array_merge($test[0], $all[0]);
  113. $all[1] = array_merge($test[1], $all[1]);
  114. }
  115. $tests[] = $all;
  116. return $tests;
  117. }
  118. public function testFix()
  119. {
  120. $expected = <<<'EOF'
  121. <?php
  122. $a = new Bar();
  123. $a = new FooBaz();
  124. EOF;
  125. $input = <<<'EOF'
  126. <?php
  127. $a = new Bar();
  128. $a = new FooBaz();
  129. EOF;
  130. $this->doTest($expected, $input);
  131. }
  132. public function testFixWithManyEmptyLines()
  133. {
  134. $expected = <<<'EOF'
  135. <?php
  136. $a = new Bar();
  137. $a = new FooBaz();
  138. EOF;
  139. $input = <<<'EOF'
  140. <?php
  141. $a = new Bar();
  142. $a = new FooBaz();
  143. EOF;
  144. $this->doTest($expected, $input);
  145. }
  146. public function testFixWithHeredoc()
  147. {
  148. $expected = '
  149. <?php
  150. $b = <<<TEXT
  151. Foo TEXT
  152. Bar
  153. FooFoo
  154. TEXT;
  155. ';
  156. $this->doTest($expected);
  157. }
  158. public function testFixWithNowdoc()
  159. {
  160. $expected = '
  161. <?php
  162. $b = <<<\'TEXT\'
  163. Foo TEXT;
  164. Bar1}
  165. FooFoo
  166. TEXT;
  167. ';
  168. $this->doTest($expected);
  169. }
  170. public function testFixWithEncapsulatedNowdoc()
  171. {
  172. $expected = '
  173. <?php
  174. $b = <<<\'TEXT\'
  175. Foo TEXT
  176. Bar
  177. <<<\'TEMPLATE\'
  178. BarFooBar TEMPLATE
  179. TEMPLATE;
  180. FooFoo
  181. TEXT;
  182. ';
  183. $this->doTest($expected);
  184. }
  185. public function testFixWithMultilineString()
  186. {
  187. $expected = <<<'EOF'
  188. <?php
  189. $a = 'Foo
  190. Bar';
  191. EOF;
  192. $this->doTest($expected);
  193. }
  194. public function testFixWithTrickyMultilineStrings()
  195. {
  196. $expected = <<<'EOF'
  197. <?php
  198. $a = 'Foo';
  199. $b = 'Bar
  200. Here\'s an escaped quote '
  201. .
  202. '
  203. FooFoo';
  204. EOF;
  205. $input = <<<'EOF'
  206. <?php
  207. $a = 'Foo';
  208. $b = 'Bar
  209. Here\'s an escaped quote '
  210. .
  211. '
  212. FooFoo';
  213. EOF;
  214. $this->doTest($expected, $input);
  215. }
  216. public function testFixWithCommentWithQuote()
  217. {
  218. $expected = <<<'EOF'
  219. <?php
  220. $a = 'foo';
  221. // my comment's must have a quote
  222. $b = 'foobar';
  223. $c = 'bar';
  224. EOF;
  225. $input = <<<'EOF'
  226. <?php
  227. $a = 'foo';
  228. // my comment's must have a quote
  229. $b = 'foobar';
  230. $c = 'bar';
  231. EOF;
  232. $this->doTest($expected, $input);
  233. }
  234. public function testFixWithTrailingInlineBlock()
  235. {
  236. $expected = "
  237. <?php
  238. echo 'hello';
  239. ?>
  240. \$a = 0;
  241. //a
  242. <?php
  243. \$a = 0;
  244. \$b = 1;
  245. //a
  246. ?>
  247. ";
  248. $this->doTest($expected);
  249. }
  250. /**
  251. * @param string $expected
  252. * @param string $input
  253. *
  254. * @dataProvider provideCommentCases
  255. */
  256. public function testFixWithComments($expected, $input)
  257. {
  258. $this->doTest($expected, $input);
  259. }
  260. public function provideCommentCases()
  261. {
  262. return [
  263. [
  264. <<<'EOF'
  265. <?php
  266. //class Test
  267. $a; //
  268. $b;
  269. /***/
  270. $c;
  271. //
  272. $d;
  273. EOF
  274. ,
  275. <<<'EOF'
  276. <?php
  277. //class Test
  278. $a; //
  279. $b;
  280. /***/
  281. $c;
  282. //
  283. $d;
  284. EOF
  285. ],
  286. [
  287. "<?php\n//a\n\n\$a =1;",
  288. "<?php\n//a\n\n\n\n\$a =1;",
  289. ],
  290. ];
  291. }
  292. /**
  293. * @param string $expected
  294. * @param null|string $input
  295. *
  296. * @dataProvider provideLineBreakCases
  297. */
  298. public function testFixWithLineBreaks($expected, $input = null)
  299. {
  300. $this->doTest($expected, $input);
  301. }
  302. public function provideLineBreakCases()
  303. {
  304. $input = '<?php //
  305. $a = 1;
  306. $b = 1;
  307. ';
  308. $expected = '<?php //
  309. $a = 1;
  310. $b = 1;
  311. ';
  312. return [
  313. [
  314. "<?php\r\n//a\r\n\r\n\$a =1;",
  315. "<?php\r\n//a\r\n\r\n\r\n\r\n\$a =1;",
  316. ],
  317. [
  318. $expected,
  319. $input,
  320. ],
  321. [
  322. str_replace("\n", "\r\n", $expected),
  323. str_replace("\n", "\r\n", $input),
  324. ],
  325. [
  326. str_replace("\n", "\r", $input),
  327. ],
  328. [
  329. str_replace("\n", "\r", $expected),
  330. ],
  331. ];
  332. }
  333. public function testWrongConfig()
  334. {
  335. $this->expectException(\PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException::class);
  336. $this->expectExceptionMessageRegExp('/^\[no_extra_blank_lines\] Invalid configuration: The option "tokens" .*\.$/');
  337. $this->fixer->configure(['tokens' => ['__TEST__']]);
  338. }
  339. /**
  340. * @param string $expected
  341. * @param null|string $input
  342. *
  343. * @dataProvider provideBetweenUseCases
  344. */
  345. public function testBetweenUse($expected, $input = null)
  346. {
  347. $this->fixer->configure(['tokens' => ['use']]);
  348. $this->doTest($expected, $input);
  349. }
  350. public function provideBetweenUseCases()
  351. {
  352. return [
  353. ['<?php use A\B;'],
  354. ['<?php use A\B?>'],
  355. ['<?php use A\B;use A\D; return 1;'],
  356. [
  357. '<?php
  358. use A\B;
  359. use A\C;',
  360. '<?php
  361. use A\B;
  362. use A\C;',
  363. ],
  364. [
  365. '<?php use A\E;use A\Z;
  366. use C;
  367. return 1;
  368. ',
  369. '<?php use A\E;use A\Z;
  370. use C;
  371. return 1;
  372. ',
  373. ],
  374. [
  375. '<?php
  376. class Test {
  377. use A;
  378. use B;
  379. }',
  380. ],
  381. [
  382. '<?php
  383. $example = function () use ($message) { var_dump($message); };
  384. $example = function () use ($message) { var_dump($message); };
  385. ',
  386. ],
  387. ];
  388. }
  389. public function testRemoveLinesBetweenUseStatements()
  390. {
  391. $expected = <<<'EOF'
  392. <?php
  393. use Zxy\Qux;
  394. use Zoo\Bar as Bar2;
  395. use Foo\Bar as Bar1;
  396. use Foo\Zar\Baz;
  397. $c = 1;
  398. use Foo\Quxx as Quxx1;
  399. use Foo\Zar\Quxx;
  400. $a = new Bar1();
  401. $a = new Bar2();
  402. $a = new Baz();
  403. $a = new Qux();
  404. EOF
  405. ;
  406. $input = <<<'EOF'
  407. <?php
  408. use Zxy\Qux;
  409. use Zoo\Bar as Bar2;
  410. use Foo\Bar as Bar1;
  411. use Foo\Zar\Baz;
  412. $c = 1;
  413. use Foo\Quxx as Quxx1;
  414. use Foo\Zar\Quxx;
  415. $a = new Bar1();
  416. $a = new Bar2();
  417. $a = new Baz();
  418. $a = new Qux();
  419. EOF
  420. ;
  421. $this->fixer->configure(['tokens' => ['use']]);
  422. $this->doTest($expected, $input);
  423. }
  424. /**
  425. * @param string $expected
  426. * @param null|string $input
  427. *
  428. * @dataProvider provideRemoveLinesBetweenUseStatements70Cases
  429. * @requires PHP 7.0
  430. */
  431. public function testRemoveLinesBetweenUseStatements70($expected, $input = null)
  432. {
  433. $this->fixer->configure(['tokens' => ['use']]);
  434. $this->doTest($expected, $input);
  435. }
  436. public function provideRemoveLinesBetweenUseStatements70Cases()
  437. {
  438. return [
  439. [
  440. '<?php
  441. use some\a\{ClassA, ClassB, ClassC as C};
  442. use function some\a\{fn_a, fn_b, fn_c};
  443. use const some\a\{ConstA, ConstB, ConstC};
  444. ',
  445. '<?php
  446. use some\a\{ClassA, ClassB, ClassC as C};
  447. use function some\a\{fn_a, fn_b, fn_c};
  448. use const some\a\{ConstA, ConstB, ConstC};
  449. ',
  450. ],
  451. ];
  452. }
  453. /**
  454. * @param string $expected
  455. *
  456. * @dataProvider provideWithoutUsesCases
  457. */
  458. public function testWithoutUses($expected)
  459. {
  460. $this->fixer->configure(['tokens' => ['use']]);
  461. $this->doTest($expected);
  462. }
  463. public function provideWithoutUsesCases()
  464. {
  465. return [
  466. [
  467. '<?php
  468. $c = 1;
  469. $a = new Baz();
  470. $a = new Qux();',
  471. ],
  472. [
  473. '<?php use A\B;',
  474. ],
  475. [
  476. '<?php use A\B?>',
  477. ],
  478. [
  479. '<?php use A\B;?>',
  480. ],
  481. ];
  482. }
  483. public function testRemoveBetweenUseTraits()
  484. {
  485. $this->fixer->configure(['tokens' => ['use_trait']]);
  486. $this->doTest(
  487. '<?php
  488. namespace T\A;
  489. use V;
  490. use W;
  491. class Test {
  492. use A;
  493. use B;
  494. private function test($b) {
  495. $a = function() use ($b) { echo $b;};
  496. $b = function() use ($b) { echo $b;};
  497. }
  498. }',
  499. '<?php
  500. namespace T\A;
  501. use V;
  502. use W;
  503. class Test {
  504. use A;
  505. use B;
  506. private function test($b) {
  507. $a = function() use ($b) { echo $b;};
  508. $b = function() use ($b) { echo $b;};
  509. }
  510. }'
  511. );
  512. }
  513. /**
  514. * @param string $expected
  515. * @param null|string $input
  516. *
  517. * @dataProvider provideOneAndInLineCases
  518. */
  519. public function testOneOrInLineCases($expected, $input = null)
  520. {
  521. $this->fixer->configure(['tokens' => [
  522. 'break',
  523. 'continue',
  524. 'return',
  525. 'throw',
  526. 'curly_brace_block',
  527. 'square_brace_block',
  528. 'parenthesis_brace_block',
  529. ]]);
  530. $this->doTest($expected, $input);
  531. }
  532. public function provideOneAndInLineCases()
  533. {
  534. return [
  535. [
  536. "<?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",
  537. ],
  538. [
  539. "<?php throw new \\Exception('do not import');\n",
  540. "<?php throw new \\Exception('do not import');\n\n",
  541. ],
  542. [
  543. "<?php\n\n\$a = \$b{0};\n\n",
  544. ],
  545. [
  546. "<?php\n\n\$a->{'Test'};\nfunction test(){}\n",
  547. ],
  548. ];
  549. }
  550. /**
  551. * @param string $expected
  552. * @param null|string $input
  553. *
  554. * @dataProvider provideOneAndInLine70Cases
  555. * @requires PHP 7.0
  556. */
  557. public function testOneOrInLine70Cases($expected, $input = null)
  558. {
  559. $this->fixer->configure(['tokens' => [
  560. 'break',
  561. 'continue',
  562. 'return',
  563. 'throw',
  564. 'curly_brace_block',
  565. 'square_brace_block',
  566. 'parenthesis_brace_block',
  567. ]]);
  568. $this->doTest($expected, $input);
  569. }
  570. public function provideOneAndInLine70Cases()
  571. {
  572. return [
  573. [
  574. "<?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",
  575. ],
  576. ];
  577. }
  578. /**
  579. * @param string $expected
  580. * @param null|string $input
  581. *
  582. * @dataProvider provideBraceCases
  583. */
  584. public function testBraces(array $config = null, $expected, $input = null)
  585. {
  586. $this->fixer->configure(['tokens' => $config]);
  587. $this->doTest($expected, $input);
  588. }
  589. public function provideBraceCases()
  590. {
  591. return [
  592. [
  593. ['curly_brace_block'],
  594. "<?php function test()\n\n{}\n\necho 789;",
  595. ],
  596. [
  597. ['curly_brace_block'],
  598. "<?php switch(\$a){\ncase 1:echo 789;}",
  599. "<?php switch(\$a){\n \ncase 1:echo 789;}",
  600. ],
  601. [
  602. ['parenthesis_brace_block'],
  603. '<?php
  604. is_int(
  605. 1);
  606. function test(
  607. $a,
  608. $b,
  609. $c
  610. )
  611. {
  612. }',
  613. '<?php
  614. is_int(
  615. 1);
  616. function test(
  617. $a,
  618. $b,
  619. $c
  620. )
  621. {
  622. }',
  623. ],
  624. [
  625. ['parenthesis_brace_block'],
  626. "<?php array(\n1,\n2,\n3,\n);",
  627. "<?php array(\n \n1,\n2,\n3,\n\n\n);",
  628. ],
  629. [
  630. ['parenthesis_brace_block'],
  631. '<?php
  632. function a()
  633. {
  634. $b->d(e(
  635. ));
  636. foreach ($a as $x) {
  637. }
  638. }',
  639. ],
  640. [
  641. ['return'],
  642. '<?php
  643. class Foo
  644. {
  645. public function bar() {return 1;}
  646. public function baz() {return 2;
  647. }
  648. }',
  649. '<?php
  650. class Foo
  651. {
  652. public function bar() {return 1;}
  653. public function baz() {return 2;
  654. }
  655. }',
  656. ],
  657. [
  658. ['square_brace_block'],
  659. "<?php \$c = \$b[0];\n\n\n\$a = [\n 1,\n2];\necho 1;\n\$b = [];\n\n\n//a\n",
  660. "<?php \$c = \$b[0];\n\n\n\$a = [\n\n 1,\n2];\necho 1;\n\$b = [];\n\n\n//a\n",
  661. ],
  662. ];
  663. }
  664. /**
  665. * @param array $config
  666. * @param string $expected
  667. * @param null|string $input
  668. *
  669. * @dataProvider provideMessyWhitespacesCases
  670. */
  671. public function testMessyWhitespaces(array $config, $expected, $input = null)
  672. {
  673. $this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t", "\r\n"));
  674. $this->fixer->configure($config);
  675. $this->doTest($expected, $input);
  676. }
  677. public function provideMessyWhitespacesCases()
  678. {
  679. return [
  680. [
  681. [],
  682. "<?php\r\nuse AAA;\r\n\r\nuse BBB;\r\n\r\n",
  683. "<?php\r\nuse AAA;\r\n\r\n\r\n\r\nuse BBB;\r\n\r\n",
  684. ],
  685. [
  686. ['tokens' => ['parenthesis_brace_block']],
  687. "<?php is_int(\r\n1);",
  688. "<?php is_int(\r\n\r\n\r\n\r\n1);",
  689. ],
  690. [
  691. ['tokens' => ['square_brace_block']],
  692. "<?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",
  693. "<?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",
  694. ],
  695. [
  696. ['tokens' => ['square_brace_block']],
  697. "<?php \$c = \$b[0];\r\n\r\n\r\n\$a = [\r\n\t1,\r\n2];",
  698. "<?php \$c = \$b[0];\r\n\r\n\r\n\$a = [\r\n\r\n\t1,\r\n2];",
  699. ],
  700. ];
  701. }
  702. /**
  703. * @param array $config
  704. * @param string $expected
  705. * @param null|string $input
  706. *
  707. * @dataProvider provideSwitchCases
  708. */
  709. public function testInSwitchStatement(array $config, $expected, $input = null)
  710. {
  711. $this->fixer->configure(['tokens' => $config]);
  712. $this->doTest($expected, $input);
  713. }
  714. public function provideSwitchCases()
  715. {
  716. return [
  717. [
  718. [
  719. 'break',
  720. 'continue',
  721. 'extra',
  722. 'return',
  723. 'throw',
  724. ],
  725. '<?php
  726. /** a */
  727. switch ($a) {
  728. case 1:
  729. break;
  730. case 2:
  731. continue;
  732. case 3:
  733. return 1;
  734. case 4:
  735. throw $e;
  736. case 5:
  737. throw new \Exception();
  738. case Token::STRING_TYPE:
  739. echo 123;
  740. return new ConstantNode($token->getValue());
  741. case 7:
  742. return new ConstantNode($token->getValue());
  743. case 8:
  744. return 8;
  745. default:
  746. echo 1;
  747. }',
  748. '<?php
  749. /** a */
  750. switch ($a) {
  751. case 1:
  752. break;
  753. case 2:
  754. continue;
  755. case 3:
  756. return 1;
  757. case 4:
  758. throw $e;
  759. case 5:
  760. throw new \Exception();
  761. case Token::STRING_TYPE:
  762. echo 123;
  763. return new ConstantNode($token->getValue());
  764. case 7:
  765. return new ConstantNode($token->getValue());
  766. '.'
  767. case 8:
  768. return 8;
  769. '.'
  770. default:
  771. echo 1;
  772. }',
  773. ],
  774. [
  775. [
  776. 'switch',
  777. 'case',
  778. 'default',
  779. ],
  780. '<?php
  781. switch($a) {
  782. case 0:
  783. case 1:
  784. default:
  785. return 1;
  786. }',
  787. '<?php
  788. switch($a) {
  789. case 0:
  790. case 1:
  791. default:
  792. return 1;
  793. }',
  794. ],
  795. [
  796. [
  797. 'switch',
  798. 'case',
  799. 'default',
  800. ],
  801. '<?php
  802. switch($a) { case 2: echo 3;
  803. default: return 1;}
  804. // above stays empty',
  805. '<?php
  806. switch($a) { case 2: echo 3;
  807. default: return 1;}
  808. // above stays empty',
  809. ],
  810. ];
  811. }
  812. /**
  813. * @param string $expected
  814. * @param string $input
  815. *
  816. * @dataProvider provideFix72Cases
  817. * @requires PHP 7.2
  818. */
  819. public function testFix72($expected, $input)
  820. {
  821. $this->fixer->configure(['tokens' => ['use']]);
  822. $this->doTest($expected, $input);
  823. }
  824. public function provideFix72Cases()
  825. {
  826. return [
  827. [
  828. '<?php
  829. use some\a\{ClassA, ClassB, ClassC as C,};
  830. use function some\a\{fn_a, fn_b, fn_c,};
  831. use const some\a\{ConstA,ConstB,ConstC
  832. ,
  833. };
  834. use const some\Z\{ConstA,ConstB,ConstC,};
  835. ',
  836. '<?php
  837. use some\a\{ClassA, ClassB, ClassC as C,};
  838. use function some\a\{fn_a, fn_b, fn_c,};
  839. use const some\a\{ConstA,ConstB,ConstC
  840. ,
  841. };
  842. '.'
  843. use const some\Z\{ConstA,ConstB,ConstC,};
  844. ',
  845. ],
  846. ];
  847. }
  848. private function removeLinesFromString($input, array $lineNumbers)
  849. {
  850. sort($lineNumbers);
  851. $lines = explode("\n", $input);
  852. foreach ($lineNumbers as $lineNumber) {
  853. --$lineNumber;
  854. unset($lines[$lineNumber]);
  855. }
  856. return implode("\n", $lines);
  857. }
  858. }