GlobalNamespaceImportFixerTest.php 17 KB

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