Implement DocBlock support, simplify some code, make the fixture more bulletproof (one example of root namespace class, one example of non root namespace class).

This commit is contained in:
Dorian Villet 2019-10-04 12:55:50 +02:00
parent 239ad3b99f
commit 205754067c
4 changed files with 97 additions and 14 deletions

View File

@ -81,23 +81,23 @@ PHP
*/
public function refactor(Node $node): ?Node
{
// Importing root namespace classes (like \DateTime) is optional
if (! $this->shouldImportRootNamespaceClasses && $node instanceof Name) {
$name = $this->getName($node);
if ($name !== null && substr_count($name, '\\') === 0) {
return null;
}
}
$this->useAddingCommander->analyseFileInfoUseStatements($node);
if ($node instanceof Name) {
// 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;
}

View File

@ -9,12 +9,21 @@ final class ImportRootNamespaceClassesDisabled
*/
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
@ -26,4 +35,66 @@ final class ImportRootNamespaceClassesDisabled
{
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;
}
}
?>

View File

@ -345,14 +345,18 @@ final class DocBlockManipulator
}
}
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);

View File

@ -77,12 +77,16 @@ final class DocBlockNameImporter
$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;
@ -93,6 +97,10 @@ final class DocBlockNameImporter
return $docNode;
}
if (! $shouldImportRootNamespaceClasses && substr_count($staticType->getClassName(), '\\') === 0) {
return $docNode;
}
return $this->processFqnNameImport($phpParserNode, $docNode, $staticType);
});