NoEmptyStatementFixerTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  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\Semicolon;
  12. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  13. /**
  14. * @author SpacePossum
  15. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  16. *
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\Fixer\Semicolon\NoEmptyStatementFixer
  20. */
  21. final class NoEmptyStatementFixerTest extends AbstractFixerTestCase
  22. {
  23. /**
  24. * @param string $expected
  25. * @param null|string $input
  26. *
  27. * @dataProvider provideNoEmptyStatementsCases
  28. */
  29. public function testNoEmptyStatements($expected, $input = null)
  30. {
  31. $this->doTest($expected, $input);
  32. }
  33. public function provideNoEmptyStatementsCases()
  34. {
  35. return [
  36. [
  37. '<?php
  38. abstract class TestClass0 extends Test IMPLEMENTS TestInterface, TestInterface2
  39. {
  40. }
  41. ',
  42. '<?php
  43. abstract class TestClass0 extends Test IMPLEMENTS TestInterface, TestInterface2
  44. {
  45. };
  46. ',
  47. ],
  48. [
  49. '<?php
  50. abstract class TestClass1 EXTENDS Test implements TestInterface
  51. {
  52. }
  53. ',
  54. '<?php
  55. abstract class TestClass1 EXTENDS Test implements TestInterface
  56. {
  57. };
  58. ',
  59. ],
  60. [
  61. '<?php
  62. CLASS TestClass2 extends Test
  63. {
  64. }
  65. ',
  66. '<?php
  67. CLASS TestClass2 extends Test
  68. {
  69. };
  70. ',
  71. ],
  72. [
  73. '<?php
  74. class TestClass3 implements TestInterface1
  75. {
  76. }
  77. ',
  78. '<?php
  79. class TestClass3 implements TestInterface1
  80. {
  81. };
  82. ',
  83. ],
  84. [
  85. '<?php
  86. class TestClass4
  87. {
  88. }
  89. ',
  90. '<?php
  91. class TestClass4
  92. {
  93. };
  94. ',
  95. ],
  96. [
  97. '<?php
  98. interface TestInterface1
  99. {
  100. }
  101. ',
  102. '<?php
  103. interface TestInterface1
  104. {
  105. };
  106. ',
  107. ],
  108. [
  109. '<?php
  110. interface TestExtendingInterface extends TestInterface2, TestInterface3 {
  111. }
  112. ',
  113. '<?php
  114. interface TestExtendingInterface extends TestInterface2, TestInterface3 {
  115. };
  116. ',
  117. ],
  118. [
  119. '<?php
  120. namespace Two {
  121. $a = 1; {
  122. }
  123. }
  124. ',
  125. '<?php
  126. namespace Two {;;
  127. $a = 1; {
  128. };
  129. }
  130. ',
  131. ],
  132. [
  133. '<?php
  134. {
  135. '.'
  136. }
  137. echo 1;
  138. ',
  139. '<?php
  140. {
  141. ;
  142. };
  143. echo 1;
  144. ',
  145. ],
  146. [
  147. '<?php
  148. while($time < $a)
  149. ;
  150. echo "done waiting.";
  151. $b = \Test;
  152. ',
  153. ],
  154. [
  155. '<?php
  156. if($a>1){
  157. }
  158. ',
  159. '<?php
  160. if($a>1){
  161. };
  162. ',
  163. ],
  164. [
  165. '<?php
  166. if($a>1) {
  167. } else {
  168. }
  169. ',
  170. '<?php
  171. if($a>1) {
  172. } else {
  173. };
  174. ',
  175. ],
  176. [
  177. '<?php
  178. try{
  179. }catch (\Exception $e) {
  180. }
  181. ',
  182. '<?php
  183. try{
  184. }catch (\Exception $e) {
  185. };
  186. ',
  187. ],
  188. [
  189. '<?php ',
  190. '<?php ;',
  191. ],
  192. [
  193. '<?php
  194. function foo()
  195. {
  196. // a
  197. }
  198. ',
  199. '<?php
  200. function foo()
  201. {
  202. ; // a
  203. }
  204. ',
  205. ],
  206. [
  207. '<?php function foo(){}',
  208. '<?php function foo(){;;}',
  209. ],
  210. [
  211. '<?php class Test{}',
  212. '<?php class Test{};',
  213. ],
  214. [
  215. '<?php
  216. for(;;) {
  217. }
  218. ',
  219. '<?php
  220. for(;;) {
  221. };
  222. ',
  223. ],
  224. [
  225. '<?php
  226. foreach($a as $b) {
  227. }
  228. foreach($a as $b => $c) {
  229. }
  230. ',
  231. '<?php
  232. foreach($a as $b) {
  233. };
  234. foreach($a as $b => $c) {
  235. };
  236. ',
  237. ],
  238. [
  239. '<?php
  240. while($a > 1){
  241. }
  242. do {
  243. } while($a>1);
  244. ',
  245. '<?php
  246. while($a > 1){
  247. };
  248. do {
  249. } while($a>1);
  250. ',
  251. ],
  252. [
  253. '<?php
  254. switch($a) {
  255. default : {echo 1;}
  256. }
  257. ',
  258. '<?php
  259. switch($a) {
  260. default : {echo 1;}
  261. };
  262. ',
  263. ],
  264. [
  265. '<?php
  266. function test($a, $b) {
  267. }
  268. ',
  269. '<?php
  270. function test($a, $b) {
  271. };
  272. ',
  273. ],
  274. [
  275. '<?php
  276. function foo($n)
  277. {
  278. '.'
  279. $a = function(){};
  280. $b = function() use ($a) {};
  281. ++${"a"};
  282. switch(fooBar()) {
  283. case 5;{
  284. }
  285. }
  286. return $n->{$o};
  287. }
  288. ',
  289. '<?php
  290. function foo($n)
  291. {
  292. ;
  293. $a = function(){};
  294. $b = function() use ($a) {};
  295. ++${"a"};
  296. switch(fooBar()) {
  297. case 5;{
  298. }
  299. };
  300. return $n->{$o};
  301. };
  302. ',
  303. ],
  304. [
  305. '<?php
  306. declare(ticks=1) {
  307. // entire script here
  308. }
  309. declare(ticks=1);
  310. ',
  311. '<?php
  312. declare(ticks=1) {
  313. // entire script here
  314. };
  315. declare(ticks=1);
  316. ',
  317. ],
  318. [
  319. '<?php
  320. namespace A\B\C;
  321. use D;
  322. ',
  323. '<?php
  324. namespace A\B\C;;;;
  325. use D;;;;
  326. ',
  327. ],
  328. [
  329. '<?php
  330. namespace A\B\C;
  331. use D;
  332. ',
  333. '<?php
  334. namespace A\B\C;
  335. use D;;;;
  336. ',
  337. ],
  338. [
  339. '<?php
  340. namespace A\B\C;use D;
  341. ',
  342. '<?php
  343. namespace A\B\C;;use D;
  344. ',
  345. ],
  346. [
  347. '<?php
  348. trait TestTrait
  349. {
  350. }
  351. ',
  352. '<?php
  353. trait TestTrait
  354. {
  355. };
  356. ',
  357. ],
  358. [
  359. '<?php
  360. try {
  361. throw new \Exception("a");
  362. } catch (\Exception $e){
  363. //
  364. } finally {
  365. } '.'
  366. ',
  367. '<?php
  368. try {
  369. throw new \Exception("a");
  370. } catch (\Exception $e){
  371. //
  372. } finally {
  373. } ;
  374. ',
  375. ],
  376. ];
  377. }
  378. /**
  379. * @param string $expected
  380. * @param null|string $input
  381. *
  382. * @dataProvider providePHP7Cases
  383. * @requires PHP 7.0
  384. */
  385. public function testFixPHP7($expected, $input = null)
  386. {
  387. $this->doTest($expected, $input);
  388. }
  389. public function providePHP7Cases()
  390. {
  391. return [
  392. [
  393. '<?php
  394. use function Functional\map;
  395. $a = new class {
  396. public function log($msg)
  397. {
  398. }
  399. };
  400. ',
  401. ],
  402. [
  403. '<?php
  404. use function Functional\map;
  405. $a = new class extends A {
  406. };
  407. ',
  408. ],
  409. [
  410. '<?php
  411. use function Functional\map;
  412. $a = new class implements B {
  413. };
  414. ',
  415. ],
  416. [
  417. '<?php
  418. use function Functional\map;
  419. $a = new class extends A implements B {
  420. };
  421. ',
  422. ],
  423. [
  424. '<?php
  425. $a = new class extends \A implements B\C {
  426. };
  427. ',
  428. ],
  429. [
  430. '<?php {{}}',
  431. '<?php {{}};',
  432. ],
  433. [
  434. '<?php
  435. namespace A\B\C {
  436. }
  437. ',
  438. '<?php
  439. namespace A\B\C {
  440. };
  441. ',
  442. ],
  443. [
  444. '<?php
  445. namespace A{
  446. }
  447. ',
  448. '<?php
  449. namespace A{
  450. };
  451. ',
  452. ],
  453. [
  454. '<?php
  455. namespace{
  456. }
  457. ',
  458. '<?php
  459. namespace{
  460. };
  461. ',
  462. ],
  463. ];
  464. }
  465. /**
  466. * @param string $expected
  467. * @param null|string $input
  468. *
  469. * @dataProvider provideCasesWithShortOpenTagCases
  470. */
  471. public function testCasesWithShortOpenTag($expected, $input = null)
  472. {
  473. if (!ini_get('short_open_tag')) {
  474. static::markTestSkipped('No short tag tests possible.');
  475. }
  476. $this->doTest($expected, $input);
  477. }
  478. public function provideCasesWithShortOpenTagCases()
  479. {
  480. return [
  481. [
  482. '<? ',
  483. '<? ;',
  484. ],
  485. ];
  486. }
  487. /**
  488. * @param string $expected
  489. * @param null|string $input
  490. *
  491. * @dataProvider provideFixMultipleSemicolonsCases
  492. */
  493. public function testFixMultipleSemicolons($expected, $input = null)
  494. {
  495. $this->doTest($expected, $input);
  496. }
  497. public function provideFixMultipleSemicolonsCases()
  498. {
  499. return [
  500. [
  501. '<?php $foo = 2 ; //
  502. '.'
  503. ',
  504. '<?php $foo = 2 ; //
  505. ;
  506. ',
  507. ],
  508. [
  509. '<?php $foo = 3; /**/ ',
  510. '<?php $foo = 3; /**/; ;',
  511. ],
  512. [
  513. '<?php $foo = 1;',
  514. '<?php $foo = 1;;;',
  515. ],
  516. [
  517. '<?php $foo = 4; ',
  518. '<?php $foo = 4;; ;;',
  519. ],
  520. [
  521. '<?php $foo = 5;
  522. ',
  523. '<?php $foo = 5;;
  524. ;
  525. ;',
  526. ],
  527. [
  528. '<?php $foo = 6; ',
  529. '<?php $foo = 6;; ',
  530. ],
  531. [
  532. '<?php for ($i = 7; ; ++$i) {}',
  533. ],
  534. [
  535. '<?php
  536. switch($a){
  537. case 8;
  538. echo 9;
  539. }
  540. ',
  541. '<?php
  542. switch($a){
  543. case 8;;
  544. echo 9;
  545. }
  546. ',
  547. ],
  548. ];
  549. }
  550. }