Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,24 @@ final class ImportFullyQualifiedNamesRector extends AbstractRector
*/
private $shouldImportDocBlocks = true;

/**
* @var bool
*/
private $shouldImportRootNamespaceClasses = true;

/**
* @var NameImporter
*/
private $nameImporter;

public function __construct(NameImporter $nameImporter, bool $shouldImportDocBlocks = true)
{
public function __construct(
NameImporter $nameImporter,
bool $shouldImportDocBlocks = true,
bool $shouldImportRootNamespaceClasses = true
) {
$this->nameImporter = $nameImporter;
$this->shouldImportDocBlocks = $shouldImportDocBlocks;
$this->shouldImportRootNamespaceClasses = $shouldImportRootNamespaceClasses;
}

public function getDefinition(): RectorDefinition
Expand Down Expand Up @@ -75,12 +84,20 @@ public function refactor(Node $node): ?Node
$this->useAddingCommander->analyseFileInfoUseStatements($node);

if ($node instanceof Name) {
Comment thread
gnutix marked this conversation as resolved.
// Importing root namespace classes (like \DateTime) is optional
if (! $this->shouldImportRootNamespaceClasses) {
$name = $this->getName($node);
if ($name !== null && substr_count($name, '\\') === 0) {
return null;
}
}

return $this->nameImporter->importName($node);
}

// process doc blocks
if ($this->shouldImportDocBlocks) {
$this->docBlockManipulator->importNames($node);
$this->docBlockManipulator->importNames($node, $this->shouldImportRootNamespaceClasses);
return $node;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

namespace Rector\CodingStyle\Tests\Rector\Namespace_\ImportFullyQualifiedNamesRector\Fixture;

final class ImportRootNamespaceClassesDisabled
{
/**
* @var \DateTime
*/
private $date;

/**
* @var \Rector\CodingStyle\Tests\Rector\Namespace_\ImportFullyQualifiedNamesRector\Source\Response
*/
private $response;

public function __construct()
{
/** @var \DateTime $currentDate */
$currentDate = new \DateTime();

/** @var \Rector\CodingStyle\Tests\Rector\Namespace_\ImportFullyQualifiedNamesRector\Source\Response $response */
$response = new \Rector\CodingStyle\Tests\Rector\Namespace_\ImportFullyQualifiedNamesRector\Source\Response();

$this->date = $currentDate;
$this->response = $response;
}

public function setDate(?\DateTime $date): void
{
$this->date = $date;
}

public function getDate(): ?\DateTime
{
return $this->date;
}

public function setResponse(?\Rector\CodingStyle\Tests\Rector\Namespace_\ImportFullyQualifiedNamesRector\Source\Response $response): void
{
$this->response = $response;
}

public function getResponse(): ?\Rector\CodingStyle\Tests\Rector\Namespace_\ImportFullyQualifiedNamesRector\Source\Response
{
return $this->response;
}
}
?>
-----
<?php

namespace Rector\CodingStyle\Tests\Rector\Namespace_\ImportFullyQualifiedNamesRector\Fixture;

use Rector\CodingStyle\Tests\Rector\Namespace_\ImportFullyQualifiedNamesRector\Source\Response;
final class ImportRootNamespaceClassesDisabled
{
/**
* @var \DateTime
*/
private $date;

/**
* @var Response
*/
private $response;

public function __construct()
{
/** @var \DateTime $currentDate */
$currentDate = new \DateTime();

/** @var Response $response */
$response = new Response();

$this->date = $currentDate;
$this->response = $response;
}

public function setDate(?\DateTime $date): void
{
$this->date = $date;
}

public function getDate(): ?\DateTime
{
return $this->date;
}

public function setResponse(?Response $response): void
{
$this->response = $response;
}

public function getResponse(): ?Response
{
return $this->response;
}
}
?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Rector\CodingStyle\Tests\Rector\Namespace_\ImportFullyQualifiedNamesRector\Fixture;

final class ImportRootNamespaceClassesEnabled
{
/**
* @var \DateTime
*/
private $date;

public function __construct()
{
/** @var \DateTime $currentDate */
$currentDate = new \DateTime();

$this->date = $currentDate;
}

public function setDate(?\DateTime $date): void
{
$this->date = $date;
}

public function getDate(): ?\DateTime
{
return $this->date;
}
}
?>
-----
<?php

namespace Rector\CodingStyle\Tests\Rector\Namespace_\ImportFullyQualifiedNamesRector\Fixture;

use DateTime;
final class ImportRootNamespaceClassesEnabled
{
/**
* @var DateTime
*/
private $date;

public function __construct()
{
/** @var DateTime $currentDate */
$currentDate = new DateTime();

$this->date = $currentDate;
}

public function setDate(?DateTime $date): void
{
$this->date = $date;
}

public function getDate(): ?DateTime
{
return $this->date;
}
}
?>
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ public function provideNamespacedClasses(): Iterator
yield [__DIR__ . '/Fixture/keep_static_method.php.inc'];
yield [__DIR__ . '/Fixture/keep_various_request.php.inc'];
yield [__DIR__ . '/Fixture/instance_of.php.inc'];

yield [__DIR__ . '/Fixture/import_root_namespace_classes_enabled.php.inc'];
}

public function provideFunctions(): Iterator
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Rector\CodingStyle\Tests\Rector\Namespace_\ImportFullyQualifiedNamesRector;

use Rector\CodingStyle\Rector\Namespace_\ImportFullyQualifiedNamesRector;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class ImportRootNamespaceClassesDisabledTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideDataForTest()
*/
public function test(string $file): void
{
$this->doTestFile($file);
}

public function provideDataForTest(): iterable
{
yield [__DIR__ . '/Fixture/import_root_namespace_classes_disabled.php.inc'];
}

protected function getRectorsWithConfiguration(): array
{
return [
ImportFullyQualifiedNamesRector::class => [
'$shouldImportRootNamespaceClasses' => false,
],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -345,14 +345,18 @@ public function replaceTagByAnother(PhpDocNode $phpDocNode, string $oldTag, stri
}
}

public function importNames(Node $node): void
public function importNames(Node $node, bool $shouldImportRootNamespaceClasses = true): void
{
if ($node->getDocComment() === null) {
return;
}

$phpDocInfo = $this->createPhpDocInfoFromNode($node);
$hasNodeChanged = $this->docBlockNameImporter->importNames($phpDocInfo, $node);
$hasNodeChanged = $this->docBlockNameImporter->importNames(
$phpDocInfo,
$node,
$shouldImportRootNamespaceClasses
);

if ($hasNodeChanged) {
$this->updateNodeWithPhpDocInfo($node, $phpDocInfo);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,16 @@ public function __construct(
$this->importSkipper = $importSkipper;
}

public function importNames(PhpDocInfo $phpDocInfo, Node $phpParserNode): bool
{
public function importNames(
PhpDocInfo $phpDocInfo,
Node $phpParserNode,
bool $shouldImportRootNamespaceClasses = true
): bool {
$phpDocNode = $phpDocInfo->getPhpDocNode();

$this->phpDocNodeTraverser->traverseWithCallable($phpDocNode, function (PhpDocParserNode $docNode) use (
$phpParserNode
$phpParserNode,
$shouldImportRootNamespaceClasses
): PhpDocParserNode {
if (! $docNode instanceof IdentifierTypeNode) {
return $docNode;
Expand All @@ -93,6 +97,11 @@ public function importNames(PhpDocInfo $phpDocInfo, Node $phpParserNode): bool
return $docNode;
}

// Importing root namespace classes (like \DateTime) is optional
if (! $shouldImportRootNamespaceClasses && substr_count($staticType->getClassName(), '\\') === 0) {
return $docNode;
}

return $this->processFqnNameImport($phpParserNode, $docNode, $staticType);
});

Expand Down