NoUnusedImportsFixerTest.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  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\Test\AbstractFixerTestCase;
  13. /**
  14. * @internal
  15. *
  16. * @covers \PhpCsFixer\Fixer\Import\NoUnusedImportsFixer
  17. */
  18. final class NoUnusedImportsFixerTest extends AbstractFixerTestCase
  19. {
  20. public function testFix()
  21. {
  22. $expected = <<<'EOF'
  23. <?php
  24. use Foo\Bar;
  25. use Foo\Bar\FooBar as FooBaz;
  26. use SomeClass;
  27. $a = new Bar();
  28. $a = new FooBaz();
  29. $a = new SomeClass();
  30. use Symfony\Annotation\Template;
  31. use Symfony\Doctrine\Entities\Entity;
  32. use Symfony\Array123\ArrayInterface;
  33. class AnnotatedClass
  34. {
  35. /**
  36. * @Template(foobar=21)
  37. * @param Entity $foo
  38. */
  39. public function doSomething($foo)
  40. {
  41. $bar = $foo->toArray();
  42. /** @var ArrayInterface $bar */
  43. }
  44. }
  45. EOF;
  46. $input = <<<'EOF'
  47. <?php
  48. use Foo\Bar;
  49. use Foo\Bar\Baz;
  50. use Foo\Bar\FooBar as FooBaz;
  51. use Foo\Bar\Foo as Fooo;
  52. use Foo\Bar\Baar\Baar;
  53. use SomeClass;
  54. $a = new Bar();
  55. $a = new FooBaz();
  56. $a = new SomeClass();
  57. use Symfony\Annotation\Template;
  58. use Symfony\Doctrine\Entities\Entity;
  59. use Symfony\Array123\ArrayInterface;
  60. class AnnotatedClass
  61. {
  62. /**
  63. * @Template(foobar=21)
  64. * @param Entity $foo
  65. */
  66. public function doSomething($foo)
  67. {
  68. $bar = $foo->toArray();
  69. /** @var ArrayInterface $bar */
  70. }
  71. }
  72. EOF;
  73. $this->doTest($expected, $input);
  74. }
  75. public function testFixFunWithIndent()
  76. {
  77. $expected = <<<'EOF'
  78. <?php
  79. use Foo\Bar;
  80. $foo = 1;
  81. use Foo\Bar\FooBar as FooBaz;
  82. use SomeClassIndented;
  83. $a = new Bar();
  84. $a = new FooBaz();
  85. $a = new SomeClassIndented();
  86. EOF;
  87. $input = <<<'EOF'
  88. <?php
  89. use Foo\Bar;
  90. use Foo\Bar\Baz;
  91. $foo = 1;
  92. use Foo\Bar\FooBar as FooBaz;
  93. use Foo\Bar\Foo as Fooo;
  94. use Foo\Bar\Baar\Baar;
  95. use SomeClassIndented;
  96. $a = new Bar();
  97. $a = new FooBaz();
  98. $a = new SomeClassIndented();
  99. EOF;
  100. $this->doTest($expected, $input);
  101. }
  102. public function testFixUseInTheSameNamespace()
  103. {
  104. $expected = <<<'EOF'
  105. <?php
  106. namespace Foo\Bar\FooBar;
  107. use Foo\Bar\FooBar\Foo as Fooz;
  108. use Foo\Bar\FooBar\Aaa\Bbb;
  109. use XYZ\FQCN_XYZ;
  110. $a = new Baz();
  111. $b = new Fooz();
  112. $c = new Bar\Fooz();
  113. $d = new Bbb();
  114. $e = new FQCN_Babo();
  115. $f = new FQCN_XYZ();
  116. EOF;
  117. $input = <<<'EOF'
  118. <?php
  119. namespace Foo\Bar\FooBar;
  120. use Foo\Bar\FooBar\Baz;
  121. use Foo\Bar\FooBar\Foo as Fooz;
  122. use Foo\Bar\FooBar\Bar;
  123. use Foo\Bar\FooBar\Aaa\Bbb;
  124. use \Foo\Bar\FooBar\FQCN_Babo;
  125. use XYZ\FQCN_XYZ;
  126. $a = new Baz();
  127. $b = new Fooz();
  128. $c = new Bar\Fooz();
  129. $d = new Bbb();
  130. $e = new FQCN_Babo();
  131. $f = new FQCN_XYZ();
  132. EOF;
  133. $this->doTest($expected, $input);
  134. // the fixer doesn't support file with multiple namespace - test if we don't remove imports in that case
  135. $expected = <<<'EOF'
  136. <?php
  137. namespace Foooooooo;
  138. namespace Foo;
  139. use Foo\Bar;
  140. use Foo\Baz;
  141. $a = new Bar();
  142. $b = new Baz();
  143. EOF;
  144. $this->doTest($expected);
  145. }
  146. public function testMultipleUseStatements()
  147. {
  148. $expected = <<<'EOF'
  149. <?php
  150. namespace Foo;
  151. use BarB, BarC as C, BarD;
  152. use BarE;
  153. $c = new D();
  154. $e = new BarE();
  155. EOF;
  156. $input = <<<'EOF'
  157. <?php
  158. namespace Foo;
  159. use Bar;
  160. use BarA;
  161. use BarB, BarC as C, BarD;
  162. use BarB2;
  163. use BarB\B2;
  164. use BarE;
  165. $c = new D();
  166. $e = new BarE();
  167. EOF;
  168. $this->doTest($expected, $input);
  169. }
  170. public function testNamespaceWithBraces()
  171. {
  172. $expected = <<<'EOF'
  173. <?php
  174. namespace Foo\Bar\FooBar {
  175. use Foo\Bar\FooBar\Foo as Fooz;
  176. use Foo\Bar\FooBar\Aaa\Bbb;
  177. $a = new Baz();
  178. $b = new Fooz();
  179. $c = new Bar\Fooz();
  180. $d = new Bbb();
  181. }
  182. EOF;
  183. $input = <<<'EOF'
  184. <?php
  185. namespace Foo\Bar\FooBar {
  186. use Foo\Bar\FooBar\Baz;
  187. use Foo\Bar\FooBar\Foo as Fooz;
  188. use Foo\Bar\FooBar\Bar;
  189. use Foo\Bar\FooBar\Aaa\Bbb;
  190. $a = new Baz();
  191. $b = new Fooz();
  192. $c = new Bar\Fooz();
  193. $d = new Bbb();
  194. }
  195. EOF;
  196. $this->doTest($expected, $input);
  197. }
  198. public function testTrailingSpaces()
  199. {
  200. $expected = <<<'EOF'
  201. <?php
  202. use Foo\Bar ;
  203. use Foo\Bar\FooBar as FooBaz ;
  204. $a = new Bar();
  205. $a = new FooBaz();
  206. EOF;
  207. $input = <<<'EOF'
  208. <?php
  209. use Foo\Bar ;
  210. use Foo\Bar\FooBar as FooBaz ;
  211. use Foo\Bar\Foo as Fooo ;
  212. use SomeClass ;
  213. $a = new Bar();
  214. $a = new FooBaz();
  215. EOF;
  216. $this->doTest($expected, $input);
  217. }
  218. public function testTraits()
  219. {
  220. $expected = <<<'EOF'
  221. <?php
  222. use Foo as Bar;
  223. class MyParent
  224. {
  225. use MyTrait1;
  226. use MyTrait2;
  227. use Bar;
  228. }
  229. EOF;
  230. $input = <<<'EOF'
  231. <?php
  232. use Foo;
  233. use Foo as Bar;
  234. class MyParent
  235. {
  236. use MyTrait1;
  237. use MyTrait2;
  238. use Bar;
  239. }
  240. EOF;
  241. $this->doTest($expected, $input);
  242. }
  243. public function testFunctionUse()
  244. {
  245. $expected = <<<'EOF'
  246. <?php
  247. use Foo;
  248. $f = new Foo();
  249. $a = function ($item) use ($f) {
  250. return !in_array($item, $f);
  251. };
  252. EOF;
  253. $input = <<<'EOF'
  254. <?php
  255. use Foo;
  256. use Bar;
  257. $f = new Foo();
  258. $a = function ($item) use ($f) {
  259. return !in_array($item, $f);
  260. };
  261. EOF;
  262. $this->doTest($expected, $input);
  263. }
  264. public function testSimilarNames()
  265. {
  266. $expected = <<<'EOF'
  267. <?php
  268. use SomeEntityRepository;
  269. class SomeService
  270. {
  271. public function __construct(SomeEntityRepository $repo)
  272. {
  273. $this->repo = $repo;
  274. }
  275. }
  276. EOF;
  277. $input = <<<'EOF'
  278. <?php
  279. use SomeEntityRepository;
  280. use SomeEntity;
  281. class SomeService
  282. {
  283. public function __construct(SomeEntityRepository $repo)
  284. {
  285. $this->repo = $repo;
  286. }
  287. }
  288. EOF;
  289. $this->doTest($expected, $input);
  290. }
  291. public function testVariableName()
  292. {
  293. $expected = <<<'EOF'
  294. <?php
  295. $bar = null;
  296. EOF;
  297. $input = <<<'EOF'
  298. <?php
  299. use Foo\Bar;
  300. $bar = null;
  301. EOF;
  302. $this->doTest($expected, $input);
  303. }
  304. public function testPropertyName()
  305. {
  306. $expected = <<<'EOF'
  307. <?php
  308. $foo->bar = null;
  309. EOF;
  310. $input = <<<'EOF'
  311. <?php
  312. use Foo\Bar;
  313. $foo->bar = null;
  314. EOF;
  315. $this->doTest($expected, $input);
  316. }
  317. public function testNamespacePart()
  318. {
  319. $expected = <<<'EOF'
  320. <?php
  321. new \Baz\Bar();
  322. EOF;
  323. $input = <<<'EOF'
  324. <?php
  325. use Foo\Bar;
  326. new \Baz\Bar();
  327. EOF;
  328. $this->doTest($expected, $input);
  329. }
  330. /**
  331. * @param string $expected
  332. * @param null|string $input
  333. *
  334. * @dataProvider providerUseInString
  335. */
  336. public function testUseInString($expected, $input = null)
  337. {
  338. $this->doTest($expected, $input);
  339. }
  340. public function providerUseInString()
  341. {
  342. $expected1 = <<<'EOF'
  343. $x=<<<'EOA'
  344. use a;
  345. use b;
  346. EOA;
  347. EOF;
  348. $expected2 = <<<'EOF'
  349. $x='
  350. use a;
  351. use b;
  352. ';
  353. EOF;
  354. $expected3 = <<<'EOF'
  355. $x="
  356. use a;
  357. use b;
  358. ";
  359. EOF;
  360. return array(
  361. array($expected1),
  362. array($expected2),
  363. array($expected3),
  364. );
  365. }
  366. public function testUseAsLastStatement()
  367. {
  368. $expected = <<<'EOF'
  369. <?php
  370. EOF;
  371. $input = <<<'EOF'
  372. <?php
  373. use Bar\Finder;
  374. EOF;
  375. $this->doTest($expected, $input);
  376. }
  377. public function testUseWithSameLastPartThatIsInNamespace()
  378. {
  379. $expected = <<<'EOF'
  380. <?php
  381. namespace Foo\Finder;
  382. EOF;
  383. $input = <<<'EOF'
  384. <?php
  385. namespace Foo\Finder;
  386. use Bar\Finder;
  387. EOF;
  388. $this->doTest($expected, $input);
  389. }
  390. public function testFoo()
  391. {
  392. $expected = <<<'EOF'
  393. <?php
  394. namespace Aaa;
  395. class Ddd
  396. {
  397. }
  398. EOF;
  399. $input = <<<'EOF'
  400. <?php
  401. namespace Aaa;
  402. use Aaa\Bbb;
  403. use Ccc;
  404. class Ddd
  405. {
  406. }
  407. EOF;
  408. $this->doTest($expected, $input);
  409. }
  410. /**
  411. * @param string $expected
  412. * @param null|string $input
  413. *
  414. * @dataProvider provideCloseTagCases
  415. */
  416. public function testFixABC($expected, $input = null)
  417. {
  418. $this->doTest($expected, $input);
  419. }
  420. public function provideCloseTagCases()
  421. {
  422. return array(
  423. array(
  424. '<?php
  425. ?>inline content<?php ?>',
  426. '<?php
  427. use A\AA;
  428. use B\C?>inline content<?php use A\D; use E\F ?>',
  429. ),
  430. array(
  431. '<?php ?>',
  432. '<?php use A\B;?>',
  433. ),
  434. array(
  435. '<?php ?>',
  436. '<?php use A\B?>',
  437. ),
  438. );
  439. }
  440. /**
  441. * @requires PHP 7.0
  442. */
  443. public function testPHP70()
  444. {
  445. $expected = <<<'EOF'
  446. <?php
  447. use some\a\{ClassD};
  448. use some\b\{ClassA, ClassB, ClassC as C};
  449. use function some\c\{fn_a, fn_b, fn_c};
  450. use const some\d\{ConstA, ConstB, ConstC};
  451. new CLassD();
  452. echo fn_a();
  453. EOF;
  454. $this->doTest($expected);
  455. }
  456. public function testFixWithComments()
  457. {
  458. $input = '<?php
  459. use#
  460. \#
  461. Exception#
  462. #
  463. ;
  464. echo 1;';
  465. $expected = '<?php
  466. echo 1;';
  467. $this->doTest($expected, $input);
  468. }
  469. }