Skip to content

Commit

Permalink
Merge pull request #3077 from rectorphp/fix-auto-import
Browse files Browse the repository at this point in the history
Fix auto import
  • Loading branch information
TomasVotruba authored Mar 27, 2020
2 parents 8220e5c + 6521583 commit 0899c25
Show file tree
Hide file tree
Showing 15 changed files with 200 additions and 62 deletions.
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,11 @@
"rules/renaming/tests/Rector/Class_/RenameClassRector/Source/TwigFilter.php",
"rules/renaming/tests/Rector/Class_/RenameClassRector/Source/Manual_Twig_Filter.php",
"rules/solid/tests/Rector/ClassMethod/UseInterfaceOverImplementationInConstructorRector/Source/Apple.php",
"tests/Rector/Namespace_/PseudoNamespaceToNamespaceRector/Source/ChangeMeAnotherNamespace.php"
"tests/Rector/Namespace_/PseudoNamespaceToNamespaceRector/Source/ChangeMeAnotherNamespace.php",
"rules/coding-style/tests/Rector/Namespace_/ImportFullyQualifiedNamesRector/Source/Foo.php",
"rules/coding-style/tests/Rector/Namespace_/ImportFullyQualifiedNamesRector/Source/safe_count.php",
"rules/coding-style/tests/Rector/Namespace_/ImportFullyQualifiedNamesRector/Source/AnotherClass.php",
"rules/coding-style/tests/Rector/Namespace_/ImportFullyQualifiedNamesRector/Source/YetAnotherClass.php"
]
},
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion docs/AllRectorsOverview.md
Original file line number Diff line number Diff line change
Expand Up @@ -7464,7 +7464,7 @@ Changes property `@var` annotations from annotation to type.
- class: [`Rector\Php80\Rector\NotIdentical\StrContainsRector`](/../master/rules/php80/src/Rector/NotIdentical/StrContainsRector.php)
- [test fixtures](/../master/rules/php80/tests/Rector/NotIdentical/StrContainsRector/Fixture)

Replace strpos() !== false, strstr and preg_match() with str_contains()
Replace strpos() !== false and strstr() with str_contains()

```diff
class SomeClass
Expand Down
23 changes: 21 additions & 2 deletions rules/coding-style/src/Node/NameImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt\Namespace_;
use PhpParser\Node\Stmt\UseUse;
use Rector\CodingStyle\Application\UseAddingCommander;
use Rector\CodingStyle\Imports\AliasUsesResolver;
use Rector\CodingStyle\Imports\ImportSkipper;
use Rector\Core\Configuration\Option;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\ClassExistenceStaticHelper;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PHPStan\Type\FullyQualifiedObjectType;
use Rector\StaticTypeMapper\StaticTypeMapper;
Expand Down Expand Up @@ -95,7 +97,7 @@ private function shouldSkipName(Name $name): bool
}

// is scalar name?
if (in_array($name->toString(), ['true', 'false', 'bool'], true)) {
if (in_array($name->toLowerString(), ['true', 'false', 'bool'], true)) {
return true;
}

Expand All @@ -107,9 +109,12 @@ private function shouldSkipName(Name $name): bool
return true;
}

if ($this->isNonExistingClassLikeAndFunctionFullyQualifiedName($name)) {
return true;
}

// Importing root namespace classes (like \DateTime) is optional
$importShortClasses = $this->parameterProvider->provideParameter(Option::IMPORT_SHORT_CLASSES_PARAMETER);

if (! $importShortClasses) {
$name = $this->nodeNameResolver->getName($name);
if ($name !== null && substr_count($name, '\\') === 0) {
Expand Down Expand Up @@ -187,4 +192,18 @@ private function addUseImport(Name $name, FullyQualifiedObjectType $fullyQualifi
$this->useAddingCommander->addUseImport($name, $fullyQualifiedObjectType);
}
}

private function isNonExistingClassLikeAndFunctionFullyQualifiedName(Name $name): bool
{
if (! $name instanceof FullyQualified) {
return false;
}

// skip-non existing class-likes and functions
if (ClassExistenceStaticHelper::doesClassLikeExist($name->toString())) {
return false;
}

return ! function_exists($name->toString());
}
}
Original file line number Diff line number Diff line change
@@ -1,30 +1,42 @@
<?php

declare(strict_types=1);
namespace {
function run_me_never()
{
// silent deprecations, since we test them
error_reporting(E_ALL ^ E_DEPRECATED);

// performance boost
\SomeNamespace\gc_disable();
}
}

function run_me_never()
namespace SomeNamespace
{
// silent deprecations, since we test them
error_reporting(E_ALL ^ E_DEPRECATED);

// performance boost
\SomeNamespace\gc_disable();
function gc_disable()
{}
}

?>
-----
<?php

declare(strict_types=1);
use function SomeNamespace\gc_disable;
namespace {
use function SomeNamespace\gc_disable;
function run_me_never()
{
// silent deprecations, since we test them
error_reporting(E_ALL ^ E_DEPRECATED);

function run_me_never()
{
// silent deprecations, since we test them
error_reporting(E_ALL ^ E_DEPRECATED);
// performance boost
gc_disable();
}
}

// performance boost
gc_disable();
namespace SomeNamespace
{
function gc_disable()
{}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace SomeAnother;

final class AnotherClass
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Foo;

final class Bar
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace SomeAnother;

final class YetAnotherClass
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Safe;

function count()
{

}

function substr()
{

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace Rector\Renaming\Tests\Rector\Class_\RenameClassRector\Fixture\AutoImpo

use Rector\Renaming\Tests\Rector\Class_\RenameClassRector\Source\SecondNamespace\SecondOriginalClass;
use Rector\Renaming\Tests\Rector\Class_\RenameClassRector\Source\FirstNamespace\FirstOriginalClass as AliasedClass;
use Rector\Renaming\Tests\Rector\Class_\RenameClassRector\Source\SecondNamespace;

$aliasedClass = new AliasedClass();
$secondClass = new SecondOriginalClass();
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Rector\Renaming\Tests\Rector\Class_\RenameClassRector\Fixture\FixtureAutoImportNamesParameter;

class DoubleFunctionImport
{
public function create()
{
return \Rector\Renaming\Tests\Rector\Class_\RenameClassRector\Source\some_another_function(1, 2);
}

public function emulate()
{
return \Rector\Renaming\Tests\Rector\Class_\RenameClassRector\Source\some_another_function(4, 5);
}
}

?>
-----
<?php

namespace Rector\Renaming\Tests\Rector\Class_\RenameClassRector\Fixture\FixtureAutoImportNamesParameter;

use function Rector\Renaming\Tests\Rector\Class_\RenameClassRector\Source\some_another_function;
class DoubleFunctionImport
{
public function create()
{
return some_another_function(1, 2);
}

public function emulate()
{
return some_another_function(4, 5);
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace Rector\Renaming\Tests\Rector\Class_\RenameClassRector;

use Iterator;
use Rector\Core\Testing\PHPUnit\AbstractRectorTestCase;
use Rector\Renaming\Rector\Class_\RenameClassRector;
use Rector\Renaming\Tests\Rector\Class_\RenameClassRector\Source\NewClass;
use Rector\Renaming\Tests\Rector\Class_\RenameClassRector\Source\OldClass;

/**
* @see \Rector\CodingStyle\Application\NameImportingCommander
*/
final class FunctionAutoImportNamesParameterTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/FixtureAutoImportNamesFunction');
}

protected function getAutoImportNames(): ?bool
{
return true;
}

/**
* @return mixed[]
*/
protected function getRectorsWithConfiguration(): array
{
return [
RenameClassRector::class => [
'$oldToNewClasses' => [
OldClass::class => NewClass::class,
],
],
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Rector\Renaming\Tests\Rector\Class_\RenameClassRector\Source;

if (! function_exists('Rector\Renaming\Tests\Rector\Class_\RenameClassRector\Source\some_another_function')) {
function some_another_function()
{
}
}


0 comments on commit 0899c25

Please sign in to comment.