NoUnusedImportsFixerTest.php 17 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153
  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\Import;
  12. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  13. /**
  14. * @internal
  15. *
  16. * @covers \PhpCsFixer\Fixer\Import\NoUnusedImportsFixer
  17. */
  18. final class NoUnusedImportsFixerTest extends AbstractFixerTestCase
  19. {
  20. /**
  21. * @param string $expected
  22. * @param null|string $input
  23. *
  24. * @dataProvider provideFixCases
  25. */
  26. public function testFix($expected, $input = null)
  27. {
  28. $this->doTest($expected, $input);
  29. }
  30. public function provideFixCases()
  31. {
  32. return [
  33. 'simple' => [
  34. <<<'EOF'
  35. <?php
  36. use Foo\Bar;
  37. use Foo\Bar\FooBar as FooBaz;
  38. use SomeClass;
  39. $a = new Bar();
  40. $a = new FooBaz();
  41. $a = new SomeClass();
  42. use Symfony\Annotation\Template;
  43. use Symfony\Doctrine\Entities\Entity;
  44. use Symfony\Array123\ArrayInterface;
  45. class AnnotatedClass
  46. {
  47. /**
  48. * @Template(foobar=21)
  49. * @param Entity $foo
  50. */
  51. public function doSomething($foo)
  52. {
  53. $bar = $foo->toArray();
  54. /** @var ArrayInterface $bar */
  55. }
  56. }
  57. EOF
  58. ,
  59. <<<'EOF'
  60. <?php
  61. use Foo\Bar;
  62. use Foo\Bar\Baz;
  63. use Foo\Bar\FooBar as FooBaz;
  64. use Foo\Bar\Foo as Fooo;
  65. use Foo\Bar\Baar\Baar;
  66. use SomeClass;
  67. $a = new Bar();
  68. $a = new FooBaz();
  69. $a = new SomeClass();
  70. use Symfony\Annotation\Template;
  71. use Symfony\Doctrine\Entities\Entity;
  72. use Symfony\Array123\ArrayInterface;
  73. class AnnotatedClass
  74. {
  75. /**
  76. * @Template(foobar=21)
  77. * @param Entity $foo
  78. */
  79. public function doSomething($foo)
  80. {
  81. $bar = $foo->toArray();
  82. /** @var ArrayInterface $bar */
  83. }
  84. }
  85. EOF
  86. ,
  87. ],
  88. 'with_indents' => [
  89. <<<'EOF'
  90. <?php
  91. use Foo\Bar;
  92. $foo = 1;
  93. use Foo\Bar\FooBar as FooBaz;
  94. use SomeClassIndented;
  95. $a = new Bar();
  96. $a = new FooBaz();
  97. $a = new SomeClassIndented();
  98. EOF
  99. ,
  100. <<<'EOF'
  101. <?php
  102. use Foo\Bar;
  103. use Foo\Bar\Baz;
  104. $foo = 1;
  105. use Foo\Bar\FooBar as FooBaz;
  106. use Foo\Bar\Foo as Fooo;
  107. use Foo\Bar\Baar\Baar;
  108. use SomeClassIndented;
  109. $a = new Bar();
  110. $a = new FooBaz();
  111. $a = new SomeClassIndented();
  112. EOF
  113. ,
  114. ],
  115. 'in_same_namespace_1' => [
  116. <<<'EOF'
  117. <?php
  118. namespace Foo\Bar\FooBar;
  119. use Foo\Bar\FooBar\Foo as Fooz;
  120. use Foo\Bar\FooBar\Aaa\Bbb;
  121. use XYZ\FQCN_XYZ;
  122. $a = new Baz();
  123. $b = new Fooz();
  124. $c = new Bar\Fooz();
  125. $d = new Bbb();
  126. $e = new FQCN_Babo();
  127. $f = new FQCN_XYZ();
  128. EOF
  129. ,
  130. <<<'EOF'
  131. <?php
  132. namespace Foo\Bar\FooBar;
  133. use Foo\Bar\FooBar\Baz;
  134. use Foo\Bar\FooBar\Foo as Fooz;
  135. use Foo\Bar\FooBar\Bar;
  136. use Foo\Bar\FooBar\Aaa\Bbb;
  137. use \Foo\Bar\FooBar\FQCN_Babo;
  138. use XYZ\FQCN_XYZ;
  139. $a = new Baz();
  140. $b = new Fooz();
  141. $c = new Bar\Fooz();
  142. $d = new Bbb();
  143. $e = new FQCN_Babo();
  144. $f = new FQCN_XYZ();
  145. EOF
  146. ,
  147. ],
  148. 'in_same_namespace_2' => [
  149. <<<'EOF'
  150. <?php namespace App\Http\Controllers;
  151. EOF
  152. ,
  153. <<<'EOF'
  154. <?php namespace App\Http\Controllers;
  155. use Illuminate\Http\Request;
  156. use App\Http\Controllers\Controller;
  157. EOF
  158. ,
  159. ],
  160. 'in_same_namespace_multiple_1' => [
  161. <<<'EOF'
  162. <?php
  163. namespace Foooooooo;
  164. namespace Foo;
  165. use Foooooooo\Baaaaz;
  166. $a = new Bar();
  167. $b = new Baz();
  168. $c = new Baaaaz();
  169. EOF
  170. ,
  171. <<<'EOF'
  172. <?php
  173. namespace Foooooooo;
  174. namespace Foo;
  175. use Foo\Bar;
  176. use Foo\Baz;
  177. use Foooooooo\Baaaar;
  178. use Foooooooo\Baaaaz;
  179. $a = new Bar();
  180. $b = new Baz();
  181. $c = new Baaaaz();
  182. EOF
  183. ,
  184. ],
  185. 'in_same_namespace_multiple_2' => [
  186. <<<'EOF'
  187. <?php
  188. namespace Foooooooo;
  189. use Foo\Bar;
  190. $a = new Baaaar();
  191. $b = new Baaaaz();
  192. $c = new Bar();
  193. namespace Foo;
  194. use Foooooooo\Baaaaz;
  195. $a = new Bar();
  196. $b = new Baz();
  197. $c = new Baaaaz();
  198. EOF
  199. ,
  200. <<<'EOF'
  201. <?php
  202. namespace Foooooooo;
  203. use Foo\Bar;
  204. use Foo\Baz;
  205. use Foooooooo\Baaaar;
  206. use Foooooooo\Baaaaz;
  207. $a = new Baaaar();
  208. $b = new Baaaaz();
  209. $c = new Bar();
  210. namespace Foo;
  211. use Foo\Bar;
  212. use Foo\Baz;
  213. use Foooooooo\Baaaar;
  214. use Foooooooo\Baaaaz;
  215. $a = new Bar();
  216. $b = new Baz();
  217. $c = new Baaaaz();
  218. EOF
  219. ,
  220. ],
  221. 'in_same_namespace_multiple_braces' => [
  222. <<<'EOF'
  223. <?php
  224. namespace Foooooooo
  225. {
  226. use Foo\Bar;
  227. $a = new Baaaar();
  228. $b = new Baaaaz();
  229. $c = new Bar();
  230. }
  231. namespace Foo
  232. {
  233. use Foooooooo\Baaaaz;
  234. $a = new Bar();
  235. $b = new Baz();
  236. $c = new Baaaaz();
  237. }
  238. EOF
  239. ,
  240. <<<'EOF'
  241. <?php
  242. namespace Foooooooo
  243. {
  244. use Foo\Bar;
  245. use Foo\Baz;
  246. use Foooooooo\Baaaar;
  247. use Foooooooo\Baaaaz;
  248. $a = new Baaaar();
  249. $b = new Baaaaz();
  250. $c = new Bar();
  251. }
  252. namespace Foo
  253. {
  254. use Foo\Bar;
  255. use Foo\Baz;
  256. use Foooooooo\Baaaar;
  257. use Foooooooo\Baaaaz;
  258. $a = new Bar();
  259. $b = new Baz();
  260. $c = new Baaaaz();
  261. }
  262. EOF
  263. ,
  264. ],
  265. 'multiple_use' => [
  266. <<<'EOF'
  267. <?php
  268. namespace Foo;
  269. use BarB, BarC as C, BarD;
  270. use BarE;
  271. $c = new D();
  272. $e = new BarE();
  273. EOF
  274. ,
  275. <<<'EOF'
  276. <?php
  277. namespace Foo;
  278. use Bar;
  279. use BarA;
  280. use BarB, BarC as C, BarD;
  281. use BarB2;
  282. use BarB\B2;
  283. use BarE;
  284. $c = new D();
  285. $e = new BarE();
  286. EOF
  287. ,
  288. ],
  289. 'with_braces' => [
  290. <<<'EOF'
  291. <?php
  292. namespace Foo\Bar\FooBar {
  293. use Foo\Bar\FooBar\Foo as Fooz;
  294. use Foo\Bar\FooBar\Aaa\Bbb;
  295. $a = new Baz();
  296. $b = new Fooz();
  297. $c = new Bar\Fooz();
  298. $d = new Bbb();
  299. }
  300. EOF
  301. ,
  302. <<<'EOF'
  303. <?php
  304. namespace Foo\Bar\FooBar {
  305. use Foo\Bar\FooBar\Baz;
  306. use Foo\Bar\FooBar\Foo as Fooz;
  307. use Foo\Bar\FooBar\Bar;
  308. use Foo\Bar\FooBar\Aaa\Bbb;
  309. $a = new Baz();
  310. $b = new Fooz();
  311. $c = new Bar\Fooz();
  312. $d = new Bbb();
  313. }
  314. EOF
  315. ,
  316. ],
  317. 'trailing_spaces' => [
  318. <<<'EOF'
  319. <?php
  320. use Foo\Bar ;
  321. use Foo\Bar\FooBar as FooBaz ;
  322. $a = new Bar();
  323. $a = new FooBaz();
  324. EOF
  325. ,
  326. <<<'EOF'
  327. <?php
  328. use Foo\Bar ;
  329. use Foo\Bar\FooBar as FooBaz ;
  330. use Foo\Bar\Foo as Fooo ;
  331. use SomeClass ;
  332. $a = new Bar();
  333. $a = new FooBaz();
  334. EOF
  335. ,
  336. ],
  337. 'traits' => [
  338. <<<'EOF'
  339. <?php
  340. use Foo as Bar;
  341. class MyParent
  342. {
  343. use MyTrait1;
  344. use MyTrait2;
  345. use Bar;
  346. }
  347. EOF
  348. ,
  349. <<<'EOF'
  350. <?php
  351. use Foo;
  352. use Foo as Bar;
  353. class MyParent
  354. {
  355. use MyTrait1;
  356. use MyTrait2;
  357. use Bar;
  358. }
  359. EOF
  360. ,
  361. ],
  362. 'function_use' => [
  363. <<<'EOF'
  364. <?php
  365. use Foo;
  366. $f = new Foo();
  367. $a = function ($item) use ($f) {
  368. return !in_array($item, $f);
  369. };
  370. EOF
  371. ,
  372. <<<'EOF'
  373. <?php
  374. use Foo;
  375. use Bar;
  376. $f = new Foo();
  377. $a = function ($item) use ($f) {
  378. return !in_array($item, $f);
  379. };
  380. EOF
  381. ,
  382. ],
  383. 'similar_names' => [
  384. <<<'EOF'
  385. <?php
  386. use SomeEntityRepository;
  387. class SomeService
  388. {
  389. public function __construct(SomeEntityRepository $repo)
  390. {
  391. $this->repo = $repo;
  392. }
  393. }
  394. EOF
  395. ,
  396. <<<'EOF'
  397. <?php
  398. use SomeEntityRepository;
  399. use SomeEntity;
  400. class SomeService
  401. {
  402. public function __construct(SomeEntityRepository $repo)
  403. {
  404. $this->repo = $repo;
  405. }
  406. }
  407. EOF
  408. ,
  409. ],
  410. 'variable_name' => [
  411. <<<'EOF'
  412. <?php
  413. $bar = null;
  414. EOF
  415. ,
  416. <<<'EOF'
  417. <?php
  418. use Foo\Bar;
  419. $bar = null;
  420. EOF
  421. ,
  422. ],
  423. 'property name, method name, static method call, static property' => [
  424. <<<'EOF'
  425. <?php
  426. $foo->bar = null;
  427. $foo->bar();
  428. $foo::bar();
  429. $foo::bar;
  430. EOF
  431. ,
  432. <<<'EOF'
  433. <?php
  434. use Foo\Bar;
  435. $foo->bar = null;
  436. $foo->bar();
  437. $foo::bar();
  438. $foo::bar;
  439. EOF
  440. ,
  441. ],
  442. 'constant_name' => [
  443. <<<'EOF'
  444. <?php
  445. class Baz
  446. {
  447. const BAR = 0;
  448. }
  449. EOF
  450. ,
  451. <<<'EOF'
  452. <?php
  453. use Foo\Bar;
  454. class Baz
  455. {
  456. const BAR = 0;
  457. }
  458. EOF
  459. ,
  460. ],
  461. 'namespace_part' => [
  462. <<<'EOF'
  463. <?php
  464. new \Baz\Bar();
  465. EOF
  466. ,
  467. <<<'EOF'
  468. <?php
  469. use Foo\Bar;
  470. new \Baz\Bar();
  471. EOF
  472. ,
  473. ],
  474. 'use_in_string_1' => [
  475. <<<'EOF'
  476. <?php
  477. $x=<<<'EOA'
  478. use a;
  479. use b;
  480. EOA;
  481. EOF
  482. ,
  483. ],
  484. 'use_in_string_2' => [
  485. <<<'EOF'
  486. <?php
  487. $x='
  488. use a;
  489. use b;
  490. ';
  491. EOF
  492. ,
  493. ],
  494. 'use_in_string_3' => [
  495. <<<'EOF'
  496. <?php
  497. $x="
  498. use a;
  499. use b;
  500. ";
  501. EOF
  502. ,
  503. ],
  504. 'import_in_global_namespace' => [
  505. <<<'EOF'
  506. <?php
  507. namespace A;
  508. use \SplFileInfo;
  509. new SplFileInfo(__FILE__);
  510. EOF
  511. ,
  512. ],
  513. 'use_as_last_statement' => [
  514. <<<'EOF'
  515. <?php
  516. EOF
  517. ,
  518. <<<'EOF'
  519. <?php
  520. use Bar\Finder;
  521. EOF
  522. ,
  523. ],
  524. 'use_with_same_last_part_that_is_in_namespace' => [
  525. <<<'EOF'
  526. <?php
  527. namespace Foo\Finder;
  528. EOF
  529. ,
  530. <<<'EOF'
  531. <?php
  532. namespace Foo\Finder;
  533. use Bar\Finder;
  534. EOF
  535. ,
  536. ],
  537. 'used_use_with_same_last_part_that_is_in_namespace' => [
  538. <<<'EOF'
  539. <?php
  540. namespace Foo\Finder;
  541. use Bar\Finder;
  542. class Baz extends Finder
  543. {
  544. }
  545. EOF
  546. ,
  547. ],
  548. 'foo' => [
  549. <<<'EOF'
  550. <?php
  551. namespace Aaa;
  552. class Ddd
  553. {
  554. }
  555. EOF
  556. ,
  557. <<<'EOF'
  558. <?php
  559. namespace Aaa;
  560. use Aaa\Bbb;
  561. use Ccc;
  562. class Ddd
  563. {
  564. }
  565. EOF
  566. ,
  567. ],
  568. 'close_tag_1' => [
  569. '<?php
  570. ?>inline content<?php ?>',
  571. '<?php
  572. use A\AA;
  573. use B\C?>inline content<?php use A\D; use E\F ?>',
  574. ],
  575. 'close_tag_2' => [
  576. '<?php ?>',
  577. '<?php use A\B;?>',
  578. ],
  579. 'close_tag_3' => [
  580. '<?php ?>',
  581. '<?php use A\B?>',
  582. ],
  583. 'with_comments' => [
  584. '<?php
  585. # 1
  586. # 2
  587. # 3
  588. # 4
  589. use /**/A\B/**/;
  590. echo 1;
  591. new B();
  592. ',
  593. '<?php
  594. use# 1
  595. \# 2
  596. Exception# 3
  597. # 4
  598. ;
  599. use /**/A\B/**/;
  600. echo 1;
  601. new B();
  602. ',
  603. ],
  604. 'with_matches_in_comments' => [
  605. '<?php
  606. use Foo;
  607. use Bar;
  608. use Baz;
  609. //Foo
  610. #Bar
  611. /*Baz*/',
  612. ],
  613. 'with_case_insensitive_matches_in_comments' => [
  614. '<?php
  615. use Foo;
  616. use Bar;
  617. use Baz;
  618. //foo
  619. #bar
  620. /*baz*/',
  621. ],
  622. 'with_same_namespace_import_and_unused_import' => [
  623. <<<'EOF'
  624. <?php
  625. namespace Foo;
  626. use Bar\C;
  627. /* test */
  628. abstract class D extends A implements C
  629. {
  630. }
  631. EOF
  632. ,
  633. <<<'EOF'
  634. <?php
  635. namespace Foo;
  636. use Bar\C;
  637. use Foo\A;
  638. use Foo\Bar\B /* test */ ;
  639. abstract class D extends A implements C
  640. {
  641. }
  642. EOF
  643. ,
  644. ],
  645. 'with_same_namespace_import_and_unused_import_after_namespace_statement' => [
  646. <<<'EOF'
  647. <?php
  648. namespace Foo;
  649. use Foo\Bar\C;
  650. abstract class D extends A implements C
  651. {
  652. }
  653. EOF
  654. ,
  655. <<<'EOF'
  656. <?php
  657. namespace Foo;
  658. use Foo\A;
  659. use Foo\Bar\B;
  660. use Foo\Bar\C;
  661. abstract class D extends A implements C
  662. {
  663. }
  664. EOF
  665. ,
  666. ],
  667. 'wrong_casing' => [
  668. <<<'EOF'
  669. <?php
  670. use Foo\Foo;
  671. use Bar\Bar;
  672. $a = new FOO();
  673. $b = new bar();
  674. EOF
  675. ,
  676. ],
  677. 'phpdoc_unused' => [
  678. <<<'EOF'
  679. <?php
  680. class Foo extends \PHPUnit_Framework_TestCase
  681. {
  682. /**
  683. * @expectedException \Exception
  684. */
  685. public function testBar()
  686. { }
  687. }
  688. EOF
  689. ,
  690. <<<'EOF'
  691. <?php
  692. use Some\Exception;
  693. class Foo extends \PHPUnit_Framework_TestCase
  694. {
  695. /**
  696. * @expectedException \Exception
  697. */
  698. public function testBar()
  699. { }
  700. }
  701. EOF
  702. ,
  703. ],
  704. 'imported_class_is_used_for_constants_1' => [
  705. '<?php
  706. use A\ABC;
  707. $a = 5-ABC::Test;
  708. $a = 5-ABC::Test-5;
  709. $a = ABC::Test-5;
  710. ',
  711. ],
  712. 'imported_class_is_used_for_constants_2' => [
  713. '<?php
  714. use A\ABC;
  715. $a = 5-ABC::Test;
  716. $a = 5-ABC::Test-5;
  717. ',
  718. ],
  719. 'imported_class_is_used_for_constants_3' => [
  720. '<?php
  721. use A\ABC;
  722. $a = 5-ABC::Test;
  723. ',
  724. ],
  725. 'imported_class_is_used_for_constants_4' => ['<?php
  726. use A\ABC;
  727. $a = ABC::Test-5;
  728. ',
  729. ],
  730. 'imported_class_is_used_for_constants_5' => ['<?php
  731. use A\ABC;
  732. $a = 5-ABC::Test-5;
  733. ',
  734. ],
  735. 'imported_class_is_used_for_constants_6' => ['<?php
  736. use A\ABC;
  737. $b = $a-->ABC::Test;
  738. ',
  739. ],
  740. 'imported_class_name_is_prefix_with_dash_of_constant' => [
  741. <<<'EOF'
  742. <?php
  743. class Dummy
  744. {
  745. const C = 'bar-bados';
  746. }
  747. EOF
  748. ,
  749. <<<'EOF'
  750. <?php
  751. use Foo\Bar;
  752. class Dummy
  753. {
  754. const C = 'bar-bados';
  755. }
  756. EOF
  757. ,
  758. ],
  759. 'imported_class_name_is_suffix_with_dash_of_constant' => [
  760. <<<'EOF'
  761. <?php
  762. class Dummy
  763. {
  764. const C = 'tool-bar';
  765. }
  766. EOF
  767. ,
  768. <<<'EOF'
  769. <?php
  770. use Foo\Bar;
  771. class Dummy
  772. {
  773. const C = 'tool-bar';
  774. }
  775. EOF
  776. ,
  777. ],
  778. 'imported_class_name_is_inside_with_dash_of_constant' => [
  779. <<<'EOF'
  780. <?php
  781. class Dummy
  782. {
  783. const C = 'tool-bar-bados';
  784. }
  785. EOF
  786. ,
  787. <<<'EOF'
  788. <?php
  789. use Foo\Bar;
  790. class Dummy
  791. {
  792. const C = 'tool-bar-bados';
  793. }
  794. EOF
  795. ,
  796. ],
  797. 'functions_in_the_global_namespace_should_not_be_removed_even_when_declaration_has_new_lines_and_is_uppercase' => [
  798. <<<'EOF'
  799. <?php
  800. namespace Foo;
  801. use function is_int;
  802. is_int(1);
  803. EOF
  804. ,
  805. <<<'EOF'
  806. <?php
  807. namespace Foo;
  808. use function is_int;
  809. use function is_float;
  810. is_int(1);
  811. EOF
  812. ,
  813. ],
  814. 'constants_in_the_global_namespace_should_not_be_removed' => [
  815. $expected = <<<'EOF'
  816. <?php
  817. namespace Foo;
  818. use const PHP_INT_MAX;
  819. echo PHP_INT_MAX;
  820. EOF
  821. ,
  822. <<<'EOF'
  823. <?php
  824. namespace Foo;
  825. use const PHP_INT_MAX;
  826. use const PHP_INT_MIN;
  827. echo PHP_INT_MAX;
  828. EOF
  829. ,
  830. ],
  831. 'functions_in_the_global_namespace_should_not_be_removed_even_when_declaration_has_ne_lines_and_is_uppercase' => [
  832. <<<'EOF'
  833. <?php
  834. namespace Foo;use/**/FUNCTION#1
  835. is_int;#2
  836. is_int(1);
  837. EOF
  838. ,
  839. <<<'EOF'
  840. <?php
  841. namespace Foo;use/**/FUNCTION#1
  842. is_int;#2
  843. use function
  844. is_float;
  845. use
  846. const
  847. PHP_INT_MIN;
  848. is_int(1);
  849. EOF
  850. ,
  851. ],
  852. 'use_trait should never be removed' => [
  853. <<<'EOF'
  854. <?php
  855. class UsesTraits
  856. {
  857. /**
  858. * @see #4086
  859. */
  860. private function withComplexStringVariable()
  861. {
  862. $name = 'World';
  863. return "Hello, {$name}!";
  864. }
  865. use MyTrait;
  866. }
  867. EOF
  868. ],
  869. 'imported_name_is_part_of_namespace' => [
  870. <<<'EOF'
  871. <?php
  872. namespace App\Foo;
  873. class Baz
  874. {
  875. }
  876. EOF
  877. ,
  878. <<<'EOF'
  879. <?php
  880. namespace App\Foo;
  881. use Foo\Bar\App;
  882. class Baz
  883. {
  884. }
  885. EOF
  886. ],
  887. 'imported_name_is_part_of_namespace with closing tag' => [
  888. <<<'EOF'
  889. <?php
  890. namespace A\B {?>
  891. <?php
  892. require_once __DIR__.'/test2.php' ?>
  893. <?php
  894. use X\Z\Y
  895. ?>
  896. <?php
  897. $y = new Y() ?>
  898. <?php
  899. var_dump($y);}
  900. EOF
  901. ],
  902. ];
  903. }
  904. /**
  905. * @requires PHP 7.0
  906. */
  907. public function testPHP70()
  908. {
  909. $expected = <<<'EOF'
  910. <?php
  911. use some\a\{ClassD};
  912. use some\b\{ClassA, ClassB, ClassC as C};
  913. use function some\c\{fn_a, fn_b, fn_c};
  914. use const some\d\{ConstA, ConstB, ConstC};
  915. new CLassD();
  916. echo fn_a();
  917. EOF;
  918. $this->doTest($expected);
  919. }
  920. /**
  921. * @param string $expected
  922. * @param null|string $input
  923. *
  924. * @dataProvider provideFix72Cases
  925. * @requires PHP 7.2
  926. */
  927. public function testFix72($expected, $input = null)
  928. {
  929. $this->doTest($expected, $input);
  930. }
  931. public function provideFix72Cases()
  932. {
  933. return [
  934. [ // TODO test shows lot of cases where imports are not removed while could be
  935. '<?php use A\{B,};
  936. use some\y\{ClassA, ClassB, ClassC as C,};
  937. use function some\a\{fn_a, fn_b, fn_c,};
  938. use const some\Z\{ConstAA,ConstBB,ConstCC,};
  939. use const some\X\{ConstA,ConstB,ConstC,ConstF};
  940. use C\{D,E,};
  941. echo ConstA.ConstB.ConstC,ConstF;
  942. echo ConstBB.ConstCC;
  943. fn_a(ClassA::test, new C());
  944. ',
  945. '<?php use A\{B,};
  946. use some\y\{ClassA, ClassB, ClassC as C,};
  947. use function some\a\{fn_a, fn_b, fn_c,};
  948. use const some\Z\{ConstAA,ConstBB,ConstCC,};
  949. use const some\X\{ConstA,ConstB,ConstC,ConstF};
  950. use C\{D,E,};
  951. use Z;
  952. echo ConstA.ConstB.ConstC,ConstF;
  953. echo ConstBB.ConstCC;
  954. fn_a(ClassA::test, new C());
  955. ',
  956. ],
  957. ];
  958. }
  959. }