1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153 |
- <?php
- /*
- * This file is part of PHP CS Fixer.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- * Dariusz Rumiński <dariusz.ruminski@gmail.com>
- *
- * This source file is subject to the MIT license that is bundled
- * with this source code in the file LICENSE.
- */
- namespace PhpCsFixer\Tests\Fixer\Import;
- use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
- /**
- * @internal
- *
- * @covers \PhpCsFixer\Fixer\Import\NoUnusedImportsFixer
- */
- final class NoUnusedImportsFixerTest extends AbstractFixerTestCase
- {
- /**
- * @param string $expected
- * @param null|string $input
- *
- * @dataProvider provideFixCases
- */
- public function testFix($expected, $input = null)
- {
- $this->doTest($expected, $input);
- }
- public function provideFixCases()
- {
- return [
- 'simple' => [
- <<<'EOF'
- <?php
- use Foo\Bar;
- use Foo\Bar\FooBar as FooBaz;
- use SomeClass;
- $a = new Bar();
- $a = new FooBaz();
- $a = new SomeClass();
- use Symfony\Annotation\Template;
- use Symfony\Doctrine\Entities\Entity;
- use Symfony\Array123\ArrayInterface;
- class AnnotatedClass
- {
- /**
- * @Template(foobar=21)
- * @param Entity $foo
- */
- public function doSomething($foo)
- {
- $bar = $foo->toArray();
- /** @var ArrayInterface $bar */
- }
- }
- EOF
- ,
- <<<'EOF'
- <?php
- use Foo\Bar;
- use Foo\Bar\Baz;
- use Foo\Bar\FooBar as FooBaz;
- use Foo\Bar\Foo as Fooo;
- use Foo\Bar\Baar\Baar;
- use SomeClass;
- $a = new Bar();
- $a = new FooBaz();
- $a = new SomeClass();
- use Symfony\Annotation\Template;
- use Symfony\Doctrine\Entities\Entity;
- use Symfony\Array123\ArrayInterface;
- class AnnotatedClass
- {
- /**
- * @Template(foobar=21)
- * @param Entity $foo
- */
- public function doSomething($foo)
- {
- $bar = $foo->toArray();
- /** @var ArrayInterface $bar */
- }
- }
- EOF
- ,
- ],
- 'with_indents' => [
- <<<'EOF'
- <?php
- use Foo\Bar;
- $foo = 1;
- use Foo\Bar\FooBar as FooBaz;
- use SomeClassIndented;
- $a = new Bar();
- $a = new FooBaz();
- $a = new SomeClassIndented();
- EOF
- ,
- <<<'EOF'
- <?php
- use Foo\Bar;
- use Foo\Bar\Baz;
- $foo = 1;
- use Foo\Bar\FooBar as FooBaz;
- use Foo\Bar\Foo as Fooo;
- use Foo\Bar\Baar\Baar;
- use SomeClassIndented;
- $a = new Bar();
- $a = new FooBaz();
- $a = new SomeClassIndented();
- EOF
- ,
- ],
- 'in_same_namespace_1' => [
- <<<'EOF'
- <?php
- namespace Foo\Bar\FooBar;
- use Foo\Bar\FooBar\Foo as Fooz;
- use Foo\Bar\FooBar\Aaa\Bbb;
- use XYZ\FQCN_XYZ;
- $a = new Baz();
- $b = new Fooz();
- $c = new Bar\Fooz();
- $d = new Bbb();
- $e = new FQCN_Babo();
- $f = new FQCN_XYZ();
- EOF
- ,
- <<<'EOF'
- <?php
- namespace Foo\Bar\FooBar;
- use Foo\Bar\FooBar\Baz;
- use Foo\Bar\FooBar\Foo as Fooz;
- use Foo\Bar\FooBar\Bar;
- use Foo\Bar\FooBar\Aaa\Bbb;
- use \Foo\Bar\FooBar\FQCN_Babo;
- use XYZ\FQCN_XYZ;
- $a = new Baz();
- $b = new Fooz();
- $c = new Bar\Fooz();
- $d = new Bbb();
- $e = new FQCN_Babo();
- $f = new FQCN_XYZ();
- EOF
- ,
- ],
- 'in_same_namespace_2' => [
- <<<'EOF'
- <?php namespace App\Http\Controllers;
- EOF
- ,
- <<<'EOF'
- <?php namespace App\Http\Controllers;
- use Illuminate\Http\Request;
- use App\Http\Controllers\Controller;
- EOF
- ,
- ],
- 'in_same_namespace_multiple_1' => [
- <<<'EOF'
- <?php
- namespace Foooooooo;
- namespace Foo;
- use Foooooooo\Baaaaz;
- $a = new Bar();
- $b = new Baz();
- $c = new Baaaaz();
- EOF
- ,
- <<<'EOF'
- <?php
- namespace Foooooooo;
- namespace Foo;
- use Foo\Bar;
- use Foo\Baz;
- use Foooooooo\Baaaar;
- use Foooooooo\Baaaaz;
- $a = new Bar();
- $b = new Baz();
- $c = new Baaaaz();
- EOF
- ,
- ],
- 'in_same_namespace_multiple_2' => [
- <<<'EOF'
- <?php
- namespace Foooooooo;
- use Foo\Bar;
- $a = new Baaaar();
- $b = new Baaaaz();
- $c = new Bar();
- namespace Foo;
- use Foooooooo\Baaaaz;
- $a = new Bar();
- $b = new Baz();
- $c = new Baaaaz();
- EOF
- ,
- <<<'EOF'
- <?php
- namespace Foooooooo;
- use Foo\Bar;
- use Foo\Baz;
- use Foooooooo\Baaaar;
- use Foooooooo\Baaaaz;
- $a = new Baaaar();
- $b = new Baaaaz();
- $c = new Bar();
- namespace Foo;
- use Foo\Bar;
- use Foo\Baz;
- use Foooooooo\Baaaar;
- use Foooooooo\Baaaaz;
- $a = new Bar();
- $b = new Baz();
- $c = new Baaaaz();
- EOF
- ,
- ],
- 'in_same_namespace_multiple_braces' => [
- <<<'EOF'
- <?php
- namespace Foooooooo
- {
- use Foo\Bar;
- $a = new Baaaar();
- $b = new Baaaaz();
- $c = new Bar();
- }
- namespace Foo
- {
- use Foooooooo\Baaaaz;
- $a = new Bar();
- $b = new Baz();
- $c = new Baaaaz();
- }
- EOF
- ,
- <<<'EOF'
- <?php
- namespace Foooooooo
- {
- use Foo\Bar;
- use Foo\Baz;
- use Foooooooo\Baaaar;
- use Foooooooo\Baaaaz;
- $a = new Baaaar();
- $b = new Baaaaz();
- $c = new Bar();
- }
- namespace Foo
- {
- use Foo\Bar;
- use Foo\Baz;
- use Foooooooo\Baaaar;
- use Foooooooo\Baaaaz;
- $a = new Bar();
- $b = new Baz();
- $c = new Baaaaz();
- }
- EOF
- ,
- ],
- 'multiple_use' => [
- <<<'EOF'
- <?php
- namespace Foo;
- use BarB, BarC as C, BarD;
- use BarE;
- $c = new D();
- $e = new BarE();
- EOF
- ,
- <<<'EOF'
- <?php
- namespace Foo;
- use Bar;
- use BarA;
- use BarB, BarC as C, BarD;
- use BarB2;
- use BarB\B2;
- use BarE;
- $c = new D();
- $e = new BarE();
- EOF
- ,
- ],
- 'with_braces' => [
- <<<'EOF'
- <?php
- namespace Foo\Bar\FooBar {
- use Foo\Bar\FooBar\Foo as Fooz;
- use Foo\Bar\FooBar\Aaa\Bbb;
- $a = new Baz();
- $b = new Fooz();
- $c = new Bar\Fooz();
- $d = new Bbb();
- }
- EOF
- ,
- <<<'EOF'
- <?php
- namespace Foo\Bar\FooBar {
- use Foo\Bar\FooBar\Baz;
- use Foo\Bar\FooBar\Foo as Fooz;
- use Foo\Bar\FooBar\Bar;
- use Foo\Bar\FooBar\Aaa\Bbb;
- $a = new Baz();
- $b = new Fooz();
- $c = new Bar\Fooz();
- $d = new Bbb();
- }
- EOF
- ,
- ],
- 'trailing_spaces' => [
- <<<'EOF'
- <?php
- use Foo\Bar ;
- use Foo\Bar\FooBar as FooBaz ;
- $a = new Bar();
- $a = new FooBaz();
- EOF
- ,
- <<<'EOF'
- <?php
- use Foo\Bar ;
- use Foo\Bar\FooBar as FooBaz ;
- use Foo\Bar\Foo as Fooo ;
- use SomeClass ;
- $a = new Bar();
- $a = new FooBaz();
- EOF
- ,
- ],
- 'traits' => [
- <<<'EOF'
- <?php
- use Foo as Bar;
- class MyParent
- {
- use MyTrait1;
- use MyTrait2;
- use Bar;
- }
- EOF
- ,
- <<<'EOF'
- <?php
- use Foo;
- use Foo as Bar;
- class MyParent
- {
- use MyTrait1;
- use MyTrait2;
- use Bar;
- }
- EOF
- ,
- ],
- 'function_use' => [
- <<<'EOF'
- <?php
- use Foo;
- $f = new Foo();
- $a = function ($item) use ($f) {
- return !in_array($item, $f);
- };
- EOF
- ,
- <<<'EOF'
- <?php
- use Foo;
- use Bar;
- $f = new Foo();
- $a = function ($item) use ($f) {
- return !in_array($item, $f);
- };
- EOF
- ,
- ],
- 'similar_names' => [
- <<<'EOF'
- <?php
- use SomeEntityRepository;
- class SomeService
- {
- public function __construct(SomeEntityRepository $repo)
- {
- $this->repo = $repo;
- }
- }
- EOF
- ,
- <<<'EOF'
- <?php
- use SomeEntityRepository;
- use SomeEntity;
- class SomeService
- {
- public function __construct(SomeEntityRepository $repo)
- {
- $this->repo = $repo;
- }
- }
- EOF
- ,
- ],
- 'variable_name' => [
- <<<'EOF'
- <?php
- $bar = null;
- EOF
- ,
- <<<'EOF'
- <?php
- use Foo\Bar;
- $bar = null;
- EOF
- ,
- ],
- 'property name, method name, static method call, static property' => [
- <<<'EOF'
- <?php
- $foo->bar = null;
- $foo->bar();
- $foo::bar();
- $foo::bar;
- EOF
- ,
- <<<'EOF'
- <?php
- use Foo\Bar;
- $foo->bar = null;
- $foo->bar();
- $foo::bar();
- $foo::bar;
- EOF
- ,
- ],
- 'constant_name' => [
- <<<'EOF'
- <?php
- class Baz
- {
- const BAR = 0;
- }
- EOF
- ,
- <<<'EOF'
- <?php
- use Foo\Bar;
- class Baz
- {
- const BAR = 0;
- }
- EOF
- ,
- ],
- 'namespace_part' => [
- <<<'EOF'
- <?php
- new \Baz\Bar();
- EOF
- ,
- <<<'EOF'
- <?php
- use Foo\Bar;
- new \Baz\Bar();
- EOF
- ,
- ],
- 'use_in_string_1' => [
- <<<'EOF'
- <?php
- $x=<<<'EOA'
- use a;
- use b;
- EOA;
- EOF
- ,
- ],
- 'use_in_string_2' => [
- <<<'EOF'
- <?php
- $x='
- use a;
- use b;
- ';
- EOF
- ,
- ],
- 'use_in_string_3' => [
- <<<'EOF'
- <?php
- $x="
- use a;
- use b;
- ";
- EOF
- ,
- ],
- 'import_in_global_namespace' => [
- <<<'EOF'
- <?php
- namespace A;
- use \SplFileInfo;
- new SplFileInfo(__FILE__);
- EOF
- ,
- ],
- 'use_as_last_statement' => [
- <<<'EOF'
- <?php
- EOF
- ,
- <<<'EOF'
- <?php
- use Bar\Finder;
- EOF
- ,
- ],
- 'use_with_same_last_part_that_is_in_namespace' => [
- <<<'EOF'
- <?php
- namespace Foo\Finder;
- EOF
- ,
- <<<'EOF'
- <?php
- namespace Foo\Finder;
- use Bar\Finder;
- EOF
- ,
- ],
- 'used_use_with_same_last_part_that_is_in_namespace' => [
- <<<'EOF'
- <?php
- namespace Foo\Finder;
- use Bar\Finder;
- class Baz extends Finder
- {
- }
- EOF
- ,
- ],
- 'foo' => [
- <<<'EOF'
- <?php
- namespace Aaa;
- class Ddd
- {
- }
- EOF
- ,
- <<<'EOF'
- <?php
- namespace Aaa;
- use Aaa\Bbb;
- use Ccc;
- class Ddd
- {
- }
- EOF
- ,
- ],
- 'close_tag_1' => [
- '<?php
- ?>inline content<?php ?>',
- '<?php
- use A\AA;
- use B\C?>inline content<?php use A\D; use E\F ?>',
- ],
- 'close_tag_2' => [
- '<?php ?>',
- '<?php use A\B;?>',
- ],
- 'close_tag_3' => [
- '<?php ?>',
- '<?php use A\B?>',
- ],
- 'with_comments' => [
- '<?php
- # 1
- # 2
- # 3
- # 4
- use /**/A\B/**/;
- echo 1;
- new B();
- ',
- '<?php
- use# 1
- \# 2
- Exception# 3
- # 4
- ;
- use /**/A\B/**/;
- echo 1;
- new B();
- ',
- ],
- 'with_matches_in_comments' => [
- '<?php
- use Foo;
- use Bar;
- use Baz;
- //Foo
- #Bar
- /*Baz*/',
- ],
- 'with_case_insensitive_matches_in_comments' => [
- '<?php
- use Foo;
- use Bar;
- use Baz;
- //foo
- #bar
- /*baz*/',
- ],
- 'with_same_namespace_import_and_unused_import' => [
- <<<'EOF'
- <?php
- namespace Foo;
- use Bar\C;
- /* test */
- abstract class D extends A implements C
- {
- }
- EOF
- ,
- <<<'EOF'
- <?php
- namespace Foo;
- use Bar\C;
- use Foo\A;
- use Foo\Bar\B /* test */ ;
- abstract class D extends A implements C
- {
- }
- EOF
- ,
- ],
- 'with_same_namespace_import_and_unused_import_after_namespace_statement' => [
- <<<'EOF'
- <?php
- namespace Foo;
- use Foo\Bar\C;
- abstract class D extends A implements C
- {
- }
- EOF
- ,
- <<<'EOF'
- <?php
- namespace Foo;
- use Foo\A;
- use Foo\Bar\B;
- use Foo\Bar\C;
- abstract class D extends A implements C
- {
- }
- EOF
- ,
- ],
- 'wrong_casing' => [
- <<<'EOF'
- <?php
- use Foo\Foo;
- use Bar\Bar;
- $a = new FOO();
- $b = new bar();
- EOF
- ,
- ],
- 'phpdoc_unused' => [
- <<<'EOF'
- <?php
- class Foo extends \PHPUnit_Framework_TestCase
- {
- /**
- * @expectedException \Exception
- */
- public function testBar()
- { }
- }
- EOF
- ,
- <<<'EOF'
- <?php
- use Some\Exception;
- class Foo extends \PHPUnit_Framework_TestCase
- {
- /**
- * @expectedException \Exception
- */
- public function testBar()
- { }
- }
- EOF
- ,
- ],
- 'imported_class_is_used_for_constants_1' => [
- '<?php
- use A\ABC;
- $a = 5-ABC::Test;
- $a = 5-ABC::Test-5;
- $a = ABC::Test-5;
- ',
- ],
- 'imported_class_is_used_for_constants_2' => [
- '<?php
- use A\ABC;
- $a = 5-ABC::Test;
- $a = 5-ABC::Test-5;
- ',
- ],
- 'imported_class_is_used_for_constants_3' => [
- '<?php
- use A\ABC;
- $a = 5-ABC::Test;
- ',
- ],
- 'imported_class_is_used_for_constants_4' => ['<?php
- use A\ABC;
- $a = ABC::Test-5;
- ',
- ],
- 'imported_class_is_used_for_constants_5' => ['<?php
- use A\ABC;
- $a = 5-ABC::Test-5;
- ',
- ],
- 'imported_class_is_used_for_constants_6' => ['<?php
- use A\ABC;
- $b = $a-->ABC::Test;
- ',
- ],
- 'imported_class_name_is_prefix_with_dash_of_constant' => [
- <<<'EOF'
- <?php
- class Dummy
- {
- const C = 'bar-bados';
- }
- EOF
- ,
- <<<'EOF'
- <?php
- use Foo\Bar;
- class Dummy
- {
- const C = 'bar-bados';
- }
- EOF
- ,
- ],
- 'imported_class_name_is_suffix_with_dash_of_constant' => [
- <<<'EOF'
- <?php
- class Dummy
- {
- const C = 'tool-bar';
- }
- EOF
- ,
- <<<'EOF'
- <?php
- use Foo\Bar;
- class Dummy
- {
- const C = 'tool-bar';
- }
- EOF
- ,
- ],
- 'imported_class_name_is_inside_with_dash_of_constant' => [
- <<<'EOF'
- <?php
- class Dummy
- {
- const C = 'tool-bar-bados';
- }
- EOF
- ,
- <<<'EOF'
- <?php
- use Foo\Bar;
- class Dummy
- {
- const C = 'tool-bar-bados';
- }
- EOF
- ,
- ],
- 'functions_in_the_global_namespace_should_not_be_removed_even_when_declaration_has_new_lines_and_is_uppercase' => [
- <<<'EOF'
- <?php
- namespace Foo;
- use function is_int;
- is_int(1);
- EOF
- ,
- <<<'EOF'
- <?php
- namespace Foo;
- use function is_int;
- use function is_float;
- is_int(1);
- EOF
- ,
- ],
- 'constants_in_the_global_namespace_should_not_be_removed' => [
- $expected = <<<'EOF'
- <?php
- namespace Foo;
- use const PHP_INT_MAX;
- echo PHP_INT_MAX;
- EOF
- ,
- <<<'EOF'
- <?php
- namespace Foo;
- use const PHP_INT_MAX;
- use const PHP_INT_MIN;
- echo PHP_INT_MAX;
- EOF
- ,
- ],
- 'functions_in_the_global_namespace_should_not_be_removed_even_when_declaration_has_ne_lines_and_is_uppercase' => [
- <<<'EOF'
- <?php
- namespace Foo;use/**/FUNCTION#1
- is_int;#2
- is_int(1);
- EOF
- ,
- <<<'EOF'
- <?php
- namespace Foo;use/**/FUNCTION#1
- is_int;#2
- use function
- is_float;
- use
- const
- PHP_INT_MIN;
- is_int(1);
- EOF
- ,
- ],
- 'use_trait should never be removed' => [
- <<<'EOF'
- <?php
- class UsesTraits
- {
- /**
- * @see #4086
- */
- private function withComplexStringVariable()
- {
- $name = 'World';
- return "Hello, {$name}!";
- }
- use MyTrait;
- }
- EOF
- ],
- 'imported_name_is_part_of_namespace' => [
- <<<'EOF'
- <?php
- namespace App\Foo;
- class Baz
- {
- }
- EOF
- ,
- <<<'EOF'
- <?php
- namespace App\Foo;
- use Foo\Bar\App;
- class Baz
- {
- }
- EOF
- ],
- 'imported_name_is_part_of_namespace with closing tag' => [
- <<<'EOF'
- <?php
- namespace A\B {?>
- <?php
- require_once __DIR__.'/test2.php' ?>
- <?php
- use X\Z\Y
- ?>
- <?php
- $y = new Y() ?>
- <?php
- var_dump($y);}
- EOF
- ],
- ];
- }
- /**
- * @requires PHP 7.0
- */
- public function testPHP70()
- {
- $expected = <<<'EOF'
- <?php
- use some\a\{ClassD};
- use some\b\{ClassA, ClassB, ClassC as C};
- use function some\c\{fn_a, fn_b, fn_c};
- use const some\d\{ConstA, ConstB, ConstC};
- new CLassD();
- echo fn_a();
- EOF;
- $this->doTest($expected);
- }
- /**
- * @param string $expected
- * @param null|string $input
- *
- * @dataProvider provideFix72Cases
- * @requires PHP 7.2
- */
- public function testFix72($expected, $input = null)
- {
- $this->doTest($expected, $input);
- }
- public function provideFix72Cases()
- {
- return [
- [ // TODO test shows lot of cases where imports are not removed while could be
- '<?php use A\{B,};
- use some\y\{ClassA, ClassB, ClassC as C,};
- use function some\a\{fn_a, fn_b, fn_c,};
- use const some\Z\{ConstAA,ConstBB,ConstCC,};
- use const some\X\{ConstA,ConstB,ConstC,ConstF};
- use C\{D,E,};
- echo ConstA.ConstB.ConstC,ConstF;
- echo ConstBB.ConstCC;
- fn_a(ClassA::test, new C());
- ',
- '<?php use A\{B,};
- use some\y\{ClassA, ClassB, ClassC as C,};
- use function some\a\{fn_a, fn_b, fn_c,};
- use const some\Z\{ConstAA,ConstBB,ConstCC,};
- use const some\X\{ConstA,ConstB,ConstC,ConstF};
- use C\{D,E,};
- use Z;
- echo ConstA.ConstB.ConstC,ConstF;
- echo ConstBB.ConstCC;
- fn_a(ClassA::test, new C());
- ',
- ],
- ];
- }
- }
|