[CodingStyle] Import short classes as well [ref #1877]

This commit is contained in:
Tomas Votruba 2019-08-21 18:57:05 +02:00
parent 824b089cea
commit 49e36355c4
4 changed files with 38 additions and 11 deletions

View File

@ -150,12 +150,7 @@ CODE_SAMPLE
{
$originalName = $name->getAttribute('originalName');
if ($originalName instanceof Name) {
// already short
if (! Strings::contains($originalName->toString(), '\\')) {
return null;
}
} else {
if (! $originalName instanceof Name) {
// not sure what to do
return null;
}
@ -163,6 +158,7 @@ CODE_SAMPLE
// the short name is already used, skip it
// @todo this is duplicated check of - $this->useAddingCommander->isShortImported?
$shortName = $this->classNaming->getShortName($name->toString());
if ($this->isShortNameAlreadyUsedForDifferentFqn($name, $shortName)) {
return null;
}
@ -203,16 +199,25 @@ CODE_SAMPLE
// 1. name is fully qualified → import it
private function shouldSkipName(Name $name, string $fullyQualifiedName): bool
{
// not namespaced class
if (! Strings::contains($fullyQualifiedName, '\\')) {
$shortName = $this->classNaming->getShortName($fullyQualifiedName);
$parentNode = $name->getAttribute(AttributeKey::PARENT_NODE);
if ($parentNode instanceof Node\Expr\ConstFetch) { // is true, false, null etc.
return true;
}
$shortName = $this->classNaming->getShortName($fullyQualifiedName);
if ($this->isNames($name, ['self', 'parent', 'static'])) {
return true;
}
// skip native function calls
if ($parentNode instanceof FuncCall && ! Strings::contains($fullyQualifiedName, '\\')) {
return true;
}
// nothing to change
if ($shortName === $fullyQualifiedName) {
return true;
return false;
}
return $this->useAddingCommander->canImportBeAdded($name, $fullyQualifiedName);

View File

@ -35,6 +35,7 @@ final class StockRepository
namespace Rector\CodingStyle\Tests\Rector\Namespace_\ImportFullyQualifiedNamesRector\Source;
use Rector\CodingStyle\Tests\Rector\Namespace_\ImportFullyQualifiedNamesRector\Source\Stock\Query;
use InvalidArgumentException;
use Doctrine\DBAL\Connection;
final class StockRepository
{
@ -44,7 +45,7 @@ final class StockRepository
$query = 5;
if (!($query instanceof Stock\Querying\Query)) {
throw new \InvalidArgumentException('Wrong query provided');
throw new InvalidArgumentException('Wrong query provided');
}
/** @var Querying\Query $query */

View File

@ -0,0 +1,20 @@
<?php
namespace Rector\CodingStyle\Tests\Rector\Namespace_\ImportFullyQualifiedNamesRector\Fixture;
class SomeException extends \Exception
{
}
?>
-----
<?php
namespace Rector\CodingStyle\Tests\Rector\Namespace_\ImportFullyQualifiedNamesRector\Fixture;
use Exception;
class SomeException extends Exception
{
}
?>

View File

@ -32,6 +32,7 @@ final class ImportFullyQualifiedNamesRectorTest extends AbstractRectorTestCase
yield [__DIR__ . '/Fixture/already_with_use.php.inc'];
yield [__DIR__ . '/Fixture/already_class_name.php.inc'];
yield [__DIR__ . '/Fixture/no_class.php.inc'];
yield [__DIR__ . '/Fixture/short.php.inc'];
// keep
yield [__DIR__ . '/Fixture/keep.php.inc'];