GlobalNamespaceImportFixerTest.php 18 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054
  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. * @author Gregor Harlan <gharlan@web.de>
  15. *
  16. * @internal
  17. *
  18. * @covers \PhpCsFixer\Fixer\Import\GlobalNamespaceImportFixer
  19. */
  20. final class GlobalNamespaceImportFixerTest extends AbstractFixerTestCase
  21. {
  22. /**
  23. * @param string $expected
  24. * @param null|string $input
  25. *
  26. * @dataProvider provideFixImportConstantsCases
  27. */
  28. public function testFixImportConstants($expected, $input = null)
  29. {
  30. $this->fixer->configure(['import_constants' => true]);
  31. $this->doTest($expected, $input);
  32. }
  33. public function provideFixImportConstantsCases()
  34. {
  35. return [
  36. 'non-global names' => [
  37. <<<'EXPECTED'
  38. <?php
  39. namespace Test;
  40. echo FOO, \Bar\BAZ, namespace\FOO2;
  41. EXPECTED
  42. ],
  43. 'name already used [1]' => [
  44. <<<'EXPECTED'
  45. <?php
  46. namespace Test;
  47. echo \FOO, FOO, \FOO;
  48. EXPECTED
  49. ],
  50. 'name already used [2]' => [
  51. <<<'EXPECTED'
  52. <?php
  53. namespace Test;
  54. use const Bar\FOO;
  55. echo \FOO;
  56. EXPECTED
  57. ],
  58. 'name already used [3]' => [
  59. <<<'EXPECTED'
  60. <?php
  61. namespace Test;
  62. const FOO = 1;
  63. echo \FOO;
  64. EXPECTED
  65. ],
  66. 'without namespace / do not import' => [
  67. <<<'INPUT'
  68. <?php
  69. echo \FOO, \BAR, \FOO;
  70. INPUT
  71. ],
  72. 'with namespace' => [
  73. <<<'EXPECTED'
  74. <?php
  75. namespace Test;
  76. use const FOO;
  77. use const BAR;
  78. echo FOO, BAR;
  79. EXPECTED
  80. ,
  81. <<<'INPUT'
  82. <?php
  83. namespace Test;
  84. echo \FOO, \BAR;
  85. INPUT
  86. ],
  87. 'with namespace with {} syntax' => [
  88. <<<'EXPECTED'
  89. <?php
  90. namespace Test {
  91. use const FOO;
  92. use const BAR;
  93. echo FOO, BAR;
  94. }
  95. EXPECTED
  96. ,
  97. <<<'INPUT'
  98. <?php
  99. namespace Test {
  100. echo \FOO, \BAR;
  101. }
  102. INPUT
  103. ],
  104. 'ignore other imported types' => [
  105. <<<'EXPECTED'
  106. <?php
  107. namespace Test;
  108. use BAR;
  109. use const FOO;
  110. use const BAR;
  111. echo FOO, BAR;
  112. EXPECTED
  113. ,
  114. <<<'INPUT'
  115. <?php
  116. namespace Test;
  117. use BAR;
  118. echo \FOO, \BAR;
  119. INPUT
  120. ],
  121. 'respect already imported names [1]' => [
  122. <<<'EXPECTED'
  123. <?php
  124. namespace Test;
  125. use const BAR;
  126. use const FOO;
  127. echo FOO, BAR;
  128. EXPECTED
  129. ,
  130. <<<'INPUT'
  131. <?php
  132. namespace Test;
  133. use const BAR;
  134. echo \FOO, \BAR;
  135. INPUT
  136. ],
  137. 'respect already imported names [2]' => [
  138. <<<'EXPECTED'
  139. <?php
  140. namespace Test;
  141. use const \BAR;
  142. use const FOO;
  143. echo FOO, BAR, BAR;
  144. EXPECTED
  145. ,
  146. <<<'INPUT'
  147. <?php
  148. namespace Test;
  149. use const \BAR;
  150. echo \FOO, \BAR, BAR;
  151. INPUT
  152. ],
  153. 'handle case sensitivity' => [
  154. <<<'EXPECTED'
  155. <?php
  156. namespace Test;
  157. use const fOO;
  158. use const FOO;
  159. use const Foo;
  160. const foO = 1;
  161. echo FOO, Foo;
  162. EXPECTED
  163. ,
  164. <<<'INPUT'
  165. <?php
  166. namespace Test;
  167. use const fOO;
  168. const foO = 1;
  169. echo \FOO, \Foo;
  170. INPUT
  171. ],
  172. 'handle aliased imports' => [
  173. <<<'EXPECTED'
  174. <?php
  175. namespace Test;
  176. use const BAR as BAZ;
  177. use const FOO;
  178. echo FOO, BAZ;
  179. EXPECTED
  180. ,
  181. <<<'INPUT'
  182. <?php
  183. namespace Test;
  184. use const BAR as BAZ;
  185. echo \FOO, \BAR;
  186. INPUT
  187. ],
  188. 'ignore class constants' => [
  189. <<<'EXPECTED'
  190. <?php
  191. namespace Test;
  192. use const FOO;
  193. class Bar {
  194. const FOO = 1;
  195. }
  196. echo FOO;
  197. EXPECTED
  198. ,
  199. <<<'INPUT'
  200. <?php
  201. namespace Test;
  202. class Bar {
  203. const FOO = 1;
  204. }
  205. echo \FOO;
  206. INPUT
  207. ],
  208. 'global namespace' => [
  209. <<<'INPUT'
  210. <?php
  211. echo \FOO, \BAR;
  212. INPUT
  213. ],
  214. [
  215. <<<'INPUT'
  216. <?php
  217. namespace {
  218. echo \FOO, \BAR;
  219. }
  220. INPUT
  221. ],
  222. ];
  223. }
  224. /**
  225. * @param string $expected
  226. * @param null|string $input
  227. *
  228. * @dataProvider provideFixImportFunctionsCases
  229. */
  230. public function testFixImportFunctions($expected, $input = null)
  231. {
  232. $this->fixer->configure(['import_functions' => true]);
  233. $this->doTest($expected, $input);
  234. }
  235. public function provideFixImportFunctionsCases()
  236. {
  237. return [
  238. 'non-global names' => [
  239. <<<'EXPECTED'
  240. <?php
  241. namespace Test;
  242. foo();
  243. Bar\baz();
  244. namespace\foo2();
  245. EXPECTED
  246. ],
  247. 'name already used [1]' => [
  248. <<<'EXPECTED'
  249. <?php
  250. namespace Test;
  251. \foo();
  252. Foo();
  253. \foo();
  254. EXPECTED
  255. ],
  256. 'name already used [2]' => [
  257. <<<'EXPECTED'
  258. <?php
  259. namespace Test;
  260. use function Bar\foo;
  261. \Foo();
  262. EXPECTED
  263. ],
  264. 'name already used [3]' => [
  265. <<<'EXPECTED'
  266. <?php
  267. namespace Test;
  268. function foo() {}
  269. \Foo();
  270. EXPECTED
  271. ],
  272. 'without namespace / do not import' => [
  273. <<<'INPUT'
  274. <?php
  275. \foo();
  276. \bar();
  277. \Foo();
  278. INPUT
  279. ],
  280. 'with namespace' => [
  281. <<<'EXPECTED'
  282. <?php
  283. namespace Test;
  284. use function foo;
  285. use function bar;
  286. foo();
  287. bar();
  288. EXPECTED
  289. ,
  290. <<<'INPUT'
  291. <?php
  292. namespace Test;
  293. \foo();
  294. \bar();
  295. INPUT
  296. ],
  297. 'with namespace with {} syntax' => [
  298. <<<'EXPECTED'
  299. <?php
  300. namespace Test {
  301. use function foo;
  302. use function bar;
  303. foo();
  304. bar();
  305. }
  306. EXPECTED
  307. ,
  308. <<<'INPUT'
  309. <?php
  310. namespace Test {
  311. \foo();
  312. \bar();
  313. }
  314. INPUT
  315. ],
  316. 'ignore other imported types' => [
  317. <<<'EXPECTED'
  318. <?php
  319. namespace Test;
  320. use bar;
  321. use function foo;
  322. use function bar;
  323. foo();
  324. bar();
  325. EXPECTED
  326. ,
  327. <<<'INPUT'
  328. <?php
  329. namespace Test;
  330. use bar;
  331. \foo();
  332. \bar();
  333. INPUT
  334. ],
  335. 'respect already imported names [1]' => [
  336. <<<'EXPECTED'
  337. <?php
  338. namespace Test;
  339. use function bar;
  340. use function foo;
  341. foo();
  342. Bar();
  343. EXPECTED
  344. ,
  345. <<<'INPUT'
  346. <?php
  347. namespace Test;
  348. use function bar;
  349. \foo();
  350. \Bar();
  351. INPUT
  352. ],
  353. 'respect already imported names [2]' => [
  354. <<<'EXPECTED'
  355. <?php
  356. namespace Test;
  357. use function \bar;
  358. use function foo;
  359. foo();
  360. Bar();
  361. bar();
  362. EXPECTED
  363. ,
  364. <<<'INPUT'
  365. <?php
  366. namespace Test;
  367. use function \bar;
  368. \foo();
  369. \Bar();
  370. bar();
  371. INPUT
  372. ],
  373. 'handle aliased imports' => [
  374. <<<'EXPECTED'
  375. <?php
  376. namespace Test;
  377. use function bar as baz;
  378. use function foo;
  379. foo();
  380. baz();
  381. EXPECTED
  382. ,
  383. <<<'INPUT'
  384. <?php
  385. namespace Test;
  386. use function bar as baz;
  387. \foo();
  388. \Bar();
  389. INPUT
  390. ],
  391. 'ignore class methods' => [
  392. <<<'EXPECTED'
  393. <?php
  394. namespace Test;
  395. use function foo;
  396. class Bar {
  397. function foo() {}
  398. }
  399. foo();
  400. EXPECTED
  401. ,
  402. <<<'INPUT'
  403. <?php
  404. namespace Test;
  405. class Bar {
  406. function foo() {}
  407. }
  408. \foo();
  409. INPUT
  410. ],
  411. ];
  412. }
  413. /**
  414. * @param string $expected
  415. * @param null|string $input
  416. *
  417. * @dataProvider provideFixImportFunctions70Cases
  418. * @requires PHP 7.0
  419. */
  420. public function testFixImportFunctions70($expected, $input = null)
  421. {
  422. $this->fixer->configure(['import_functions' => true]);
  423. $this->doTest($expected, $input);
  424. }
  425. public function provideFixImportFunctions70Cases()
  426. {
  427. return [
  428. 'name already used' => [
  429. <<<'EXPECTED'
  430. <?php
  431. namespace Test;
  432. class Bar {
  433. function baz() {
  434. new class() {
  435. function baz() {
  436. function foo() {}
  437. }
  438. };
  439. }
  440. }
  441. \foo();
  442. EXPECTED
  443. ],
  444. ];
  445. }
  446. /**
  447. * @param string $expected
  448. * @param null|string $input
  449. *
  450. * @dataProvider provideFixImportClassesCases
  451. */
  452. public function testFixImportClasses($expected, $input = null)
  453. {
  454. $this->fixer->configure(['import_classes' => true]);
  455. $this->doTest($expected, $input);
  456. }
  457. public function provideFixImportClassesCases()
  458. {
  459. return [
  460. 'non-global names' => [
  461. <<<'EXPECTED'
  462. <?php
  463. namespace Test;
  464. new Foo();
  465. new Bar\Baz();
  466. new namespace\Foo2();
  467. /** @var Foo|Bar\Baz $x */
  468. $x = x();
  469. EXPECTED
  470. ],
  471. 'name already used [1]' => [
  472. <<<'EXPECTED'
  473. <?php
  474. namespace Test;
  475. new \Foo();
  476. new foo();
  477. /** @var \Foo $foo */
  478. $foo = new \Foo();
  479. EXPECTED
  480. ],
  481. 'name already used [2]' => [
  482. <<<'EXPECTED'
  483. <?php
  484. namespace Test;
  485. use Bar\foo;
  486. /** @var \Foo $foo */
  487. $foo = new \Foo();
  488. EXPECTED
  489. ],
  490. 'name already used [3]' => [
  491. <<<'EXPECTED'
  492. <?php
  493. namespace Test;
  494. class foo {}
  495. /** @var \Foo $foo */
  496. $foo = new \Foo();
  497. EXPECTED
  498. ],
  499. 'name already used [4]' => [
  500. <<<'EXPECTED'
  501. <?php
  502. namespace Test;
  503. /** @return array<string, foo> */
  504. function x() {}
  505. /** @var \Foo $foo */
  506. $foo = new \Foo();
  507. EXPECTED
  508. ],
  509. 'without namespace / do not import' => [
  510. <<<'INPUT'
  511. <?php
  512. /** @var \Foo $foo */
  513. $foo = new \foo();
  514. new \Bar();
  515. \FOO::baz();
  516. INPUT
  517. ],
  518. 'with namespace' => [
  519. <<<'EXPECTED'
  520. <?php
  521. namespace Test;
  522. use Bar;
  523. use Baz;
  524. use foo;
  525. new Foo();
  526. Bar::baz();
  527. /** @return Baz<string, foo> */
  528. function x() {}
  529. EXPECTED
  530. ,
  531. <<<'INPUT'
  532. <?php
  533. namespace Test;
  534. new \Foo();
  535. \Bar::baz();
  536. /** @return \Baz<string, \foo> */
  537. function x() {}
  538. INPUT
  539. ],
  540. 'with namespace with {} syntax' => [
  541. <<<'EXPECTED'
  542. <?php
  543. namespace Test {
  544. use Foo;
  545. use Bar;
  546. new Foo();
  547. Bar::baz();
  548. }
  549. EXPECTED
  550. ,
  551. <<<'INPUT'
  552. <?php
  553. namespace Test {
  554. new \Foo();
  555. \Bar::baz();
  556. }
  557. INPUT
  558. ],
  559. 'phpdoc only' => [
  560. <<<'EXPECTED'
  561. <?php
  562. namespace Test;
  563. use Throwable;
  564. /** @throws Throwable */
  565. function x() {}
  566. EXPECTED
  567. ,
  568. <<<'INPUT'
  569. <?php
  570. namespace Test;
  571. /** @throws \Throwable */
  572. function x() {}
  573. INPUT
  574. ],
  575. 'ignore other imported types' => [
  576. <<<'EXPECTED'
  577. <?php
  578. namespace Test;
  579. use function Bar;
  580. use Foo;
  581. use Bar;
  582. new Foo();
  583. Bar::baz();
  584. EXPECTED
  585. ,
  586. <<<'INPUT'
  587. <?php
  588. namespace Test;
  589. use function Bar;
  590. new \Foo();
  591. \Bar::baz();
  592. INPUT
  593. ],
  594. 'respect already imported names [1]' => [
  595. <<<'EXPECTED'
  596. <?php
  597. namespace Test;
  598. use Bar;
  599. use Foo;
  600. new Foo();
  601. bar::baz();
  602. EXPECTED
  603. ,
  604. <<<'INPUT'
  605. <?php
  606. namespace Test;
  607. use Bar;
  608. new \Foo();
  609. \bar::baz();
  610. INPUT
  611. ],
  612. 'respect already imported names [2]' => [
  613. <<<'EXPECTED'
  614. <?php
  615. namespace Test;
  616. use \Bar;
  617. use Foo;
  618. new Foo();
  619. new bar();
  620. new Bar();
  621. EXPECTED
  622. ,
  623. <<<'INPUT'
  624. <?php
  625. namespace Test;
  626. use \Bar;
  627. new \Foo();
  628. new \bar();
  629. new Bar();
  630. INPUT
  631. ],
  632. 'respect already imported names [3]' => [
  633. <<<'EXPECTED'
  634. <?php
  635. namespace Test;
  636. use Throwable;
  637. /** @throws Throwable */
  638. function x() {}
  639. /** @throws Throwable */
  640. function y() {}
  641. EXPECTED
  642. ,
  643. <<<'INPUT'
  644. <?php
  645. namespace Test;
  646. use Throwable;
  647. /** @throws Throwable */
  648. function x() {}
  649. /** @throws \Throwable */
  650. function y() {}
  651. INPUT
  652. ],
  653. 'handle aliased imports' => [
  654. <<<'EXPECTED'
  655. <?php
  656. namespace Test;
  657. use Bar as Baz;
  658. use Foo;
  659. new Foo();
  660. /** @var Baz $bar */
  661. $bar = new Baz();
  662. EXPECTED
  663. ,
  664. <<<'INPUT'
  665. <?php
  666. namespace Test;
  667. use Bar as Baz;
  668. new \Foo();
  669. /** @var \bar $bar */
  670. $bar = new \bar();
  671. INPUT
  672. ],
  673. 'handle typehints' => [
  674. <<<'EXPECTED'
  675. <?php
  676. namespace Test;
  677. use Bar;
  678. use Foo;
  679. use Baz;
  680. class Abc {
  681. function bar(Foo $a, Bar $b, foo &$c, Baz ...$d) {}
  682. }
  683. EXPECTED
  684. ,
  685. <<<'INPUT'
  686. <?php
  687. namespace Test;
  688. class Abc {
  689. function bar(\Foo $a, \Bar $b, \foo &$c, \Baz ...$d) {}
  690. }
  691. INPUT
  692. ],
  693. ];
  694. }
  695. /**
  696. * @param string $expected
  697. * @param null|string $input
  698. *
  699. * @dataProvider provideFixImportClasses71Cases
  700. * @requires PHP 7.1
  701. */
  702. public function testFixImportClasses71($expected, $input = null)
  703. {
  704. $this->fixer->configure(['import_classes' => true]);
  705. $this->doTest($expected, $input);
  706. }
  707. public function provideFixImportClasses71Cases()
  708. {
  709. return [
  710. 'handle typehints' => [
  711. <<<'EXPECTED'
  712. <?php
  713. namespace Test;
  714. use Foo;
  715. use Bar;
  716. class Abc {
  717. function bar(?Foo $a): ?Bar {}
  718. }
  719. EXPECTED
  720. ,
  721. <<<'INPUT'
  722. <?php
  723. namespace Test;
  724. class Abc {
  725. function bar(?\Foo $a): ?\Bar {}
  726. }
  727. INPUT
  728. ],
  729. ];
  730. }
  731. /**
  732. * @param string $expected
  733. * @param null|string $input
  734. *
  735. * @dataProvider provideFixFullyQualifyConstantsCases
  736. */
  737. public function testFixFullyQualifyConstants($expected, $input = null)
  738. {
  739. $this->fixer->configure(['import_constants' => false]);
  740. $this->doTest($expected, $input);
  741. }
  742. public function provideFixFullyQualifyConstantsCases()
  743. {
  744. return [
  745. 'already fqn or sub namespace' => [
  746. <<<'EXPECTED'
  747. <?php
  748. use const FOO;
  749. use const BAR;
  750. echo \FOO, Baz\BAR;
  751. EXPECTED
  752. ],
  753. 'handle all occurrences' => [
  754. <<<'EXPECTED'
  755. <?php
  756. namespace X;
  757. use const FOO;
  758. use const BAR;
  759. echo \FOO, \BAR, \FOO;
  760. EXPECTED
  761. ,
  762. <<<'INPUT'
  763. <?php
  764. namespace X;
  765. use const FOO;
  766. use const BAR;
  767. echo FOO, BAR, FOO;
  768. INPUT
  769. ],
  770. 'ignore other imports and non-imported names' => [
  771. <<<'EXPECTED'
  772. <?php
  773. namespace Test;
  774. use FOO;
  775. use const BAR;
  776. use const Baz;
  777. echo FOO, \BAR, BAZ, QUX;
  778. EXPECTED
  779. ,
  780. <<<'INPUT'
  781. <?php
  782. namespace Test;
  783. use FOO;
  784. use const BAR;
  785. use const Baz;
  786. echo FOO, BAR, BAZ, QUX;
  787. INPUT
  788. ],
  789. ];
  790. }
  791. /**
  792. * @param string $expected
  793. * @param null|string $input
  794. *
  795. * @dataProvider provideFixFullyQualifyFunctionsCases
  796. */
  797. public function testFixFullyQualifyFunctions($expected, $input = null)
  798. {
  799. $this->fixer->configure(['import_functions' => false]);
  800. $this->doTest($expected, $input);
  801. }
  802. public function provideFixFullyQualifyFunctionsCases()
  803. {
  804. return [
  805. 'already fqn or sub namespace' => [
  806. <<<'EXPECTED'
  807. <?php
  808. use function foo;
  809. use function bar;
  810. \foo();
  811. Baz\bar();
  812. EXPECTED
  813. ],
  814. 'handle all occurrences' => [
  815. <<<'EXPECTED'
  816. <?php
  817. namespace X;
  818. use function foo;
  819. use function bar;
  820. \foo();
  821. \bar();
  822. \Foo();
  823. EXPECTED
  824. ,
  825. <<<'INPUT'
  826. <?php
  827. namespace X;
  828. use function foo;
  829. use function bar;
  830. foo();
  831. bar();
  832. Foo();
  833. INPUT
  834. ],
  835. 'ignore other imports and non-imported names' => [
  836. <<<'EXPECTED'
  837. <?php
  838. namespace Test;
  839. use foo;
  840. use function bar;
  841. foo();
  842. \bar();
  843. baz();
  844. EXPECTED
  845. ,
  846. <<<'INPUT'
  847. <?php
  848. namespace Test;
  849. use foo;
  850. use function bar;
  851. foo();
  852. bar();
  853. baz();
  854. INPUT
  855. ],
  856. ];
  857. }
  858. /**
  859. * @param string $expected
  860. * @param null|string $input
  861. *
  862. * @dataProvider provideFixFullyQualifyClassesCases
  863. */
  864. public function testFixFullyQualifyClasses($expected, $input = null)
  865. {
  866. $this->fixer->configure(['import_classes' => false]);
  867. $this->doTest($expected, $input);
  868. }
  869. public function provideFixFullyQualifyClassesCases()
  870. {
  871. return [
  872. 'already fqn or sub namespace' => [
  873. <<<'EXPECTED'
  874. <?php
  875. use Foo;
  876. use Bar;
  877. new \Foo();
  878. Baz\Bar::baz();
  879. /**
  880. * @param \Foo $foo
  881. * @param Baz\Bar $bar
  882. */
  883. function abc(\Foo $foo, Baz\Bar $bar = null) {}
  884. EXPECTED
  885. ],
  886. 'handle all occurrences' => [
  887. <<<'EXPECTED'
  888. <?php
  889. namespace X;
  890. use Foo;
  891. use Bar;
  892. new \Foo();
  893. new \Bar();
  894. \foo::baz();
  895. /**
  896. * @param \Foo|string $foo
  897. * @param null|\Bar[] $bar
  898. * @return array<string, ?\Bar<int, \foo>>|null
  899. */
  900. function abc($foo, \Bar $bar = null) {}
  901. EXPECTED
  902. ,
  903. <<<'INPUT'
  904. <?php
  905. namespace X;
  906. use Foo;
  907. use Bar;
  908. new Foo();
  909. new Bar();
  910. foo::baz();
  911. /**
  912. * @param Foo|string $foo
  913. * @param null|Bar[] $bar
  914. * @return array<string, ?Bar<int, foo>>|null
  915. */
  916. function abc($foo, Bar $bar = null) {}
  917. INPUT
  918. ],
  919. 'ignore other imports and non-imported names' => [
  920. <<<'EXPECTED'
  921. <?php
  922. namespace Test;
  923. use function Foo;
  924. use Bar;
  925. new Foo();
  926. new \Bar();
  927. new Baz();
  928. EXPECTED
  929. ,
  930. <<<'INPUT'
  931. <?php
  932. namespace Test;
  933. use function Foo;
  934. use Bar;
  935. new Foo();
  936. new Bar();
  937. new Baz();
  938. INPUT
  939. ],
  940. ];
  941. }
  942. /**
  943. * @param string $expected
  944. *
  945. * @dataProvider provideMultipleNamespacesCases
  946. */
  947. public function testMultipleNamespaces($expected)
  948. {
  949. $this->fixer->configure(['import_constants' => true]);
  950. $this->doTest($expected);
  951. }
  952. public function provideMultipleNamespacesCases()
  953. {
  954. yield [
  955. <<<'INPUT'
  956. <?php
  957. namespace Test;
  958. echo \FOO, \BAR;
  959. namespace OtherTest;
  960. echo \FOO, \BAR;
  961. INPUT
  962. ];
  963. yield [
  964. <<<'INPUT'
  965. <?php
  966. namespace Test {
  967. echo \FOO, \BAR;
  968. }
  969. namespace OtherTest {
  970. echo \FOO, \BAR;
  971. }
  972. INPUT
  973. ];
  974. yield [
  975. <<<'INPUT'
  976. <?php
  977. namespace {
  978. echo \FOO, \BAR;
  979. }
  980. namespace OtherTest {
  981. echo \FOO, \BAR;
  982. }
  983. INPUT
  984. ];
  985. yield [
  986. <<<'INPUT'
  987. <?php
  988. namespace Test {
  989. echo \FOO, \BAR;
  990. }
  991. namespace {
  992. echo \FOO, \BAR;
  993. }
  994. INPUT
  995. ];
  996. }
  997. }