mirror of
https://github.com/rectorphp/rector.git
synced 2025-02-13 20:36:23 +01:00
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:
parent
239ad3b99f
commit
205754067c
@ -81,23 +81,23 @@ PHP
|
|||||||
*/
|
*/
|
||||||
public function refactor(Node $node): ?Node
|
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);
|
$this->useAddingCommander->analyseFileInfoUseStatements($node);
|
||||||
|
|
||||||
if ($node instanceof Name) {
|
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);
|
return $this->nameImporter->importName($node);
|
||||||
}
|
}
|
||||||
|
|
||||||
// process doc blocks
|
// process doc blocks
|
||||||
if ($this->shouldImportDocBlocks) {
|
if ($this->shouldImportDocBlocks) {
|
||||||
$this->docBlockManipulator->importNames($node);
|
$this->docBlockManipulator->importNames($node, $this->shouldImportRootNamespaceClasses);
|
||||||
return $node;
|
return $node;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -9,12 +9,21 @@ final class ImportRootNamespaceClassesDisabled
|
|||||||
*/
|
*/
|
||||||
private $date;
|
private $date;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var \Rector\CodingStyle\Tests\Rector\Namespace_\ImportFullyQualifiedNamesRector\Source\Response
|
||||||
|
*/
|
||||||
|
private $response;
|
||||||
|
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
/** @var \DateTime $currentDate */
|
/** @var \DateTime $currentDate */
|
||||||
$currentDate = new \DateTime();
|
$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->date = $currentDate;
|
||||||
|
$this->response = $response;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setDate(?\DateTime $date): void
|
public function setDate(?\DateTime $date): void
|
||||||
@ -26,4 +35,66 @@ final class ImportRootNamespaceClassesDisabled
|
|||||||
{
|
{
|
||||||
return $this->date;
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
?>
|
||||||
|
@ -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) {
|
if ($node->getDocComment() === null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$phpDocInfo = $this->createPhpDocInfoFromNode($node);
|
$phpDocInfo = $this->createPhpDocInfoFromNode($node);
|
||||||
$hasNodeChanged = $this->docBlockNameImporter->importNames($phpDocInfo, $node);
|
$hasNodeChanged = $this->docBlockNameImporter->importNames(
|
||||||
|
$phpDocInfo,
|
||||||
|
$node,
|
||||||
|
$shouldImportRootNamespaceClasses
|
||||||
|
);
|
||||||
|
|
||||||
if ($hasNodeChanged) {
|
if ($hasNodeChanged) {
|
||||||
$this->updateNodeWithPhpDocInfo($node, $phpDocInfo);
|
$this->updateNodeWithPhpDocInfo($node, $phpDocInfo);
|
||||||
|
@ -77,12 +77,16 @@ final class DocBlockNameImporter
|
|||||||
$this->importSkipper = $importSkipper;
|
$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();
|
$phpDocNode = $phpDocInfo->getPhpDocNode();
|
||||||
|
|
||||||
$this->phpDocNodeTraverser->traverseWithCallable($phpDocNode, function (PhpDocParserNode $docNode) use (
|
$this->phpDocNodeTraverser->traverseWithCallable($phpDocNode, function (PhpDocParserNode $docNode) use (
|
||||||
$phpParserNode
|
$phpParserNode,
|
||||||
|
$shouldImportRootNamespaceClasses
|
||||||
): PhpDocParserNode {
|
): PhpDocParserNode {
|
||||||
if (! $docNode instanceof IdentifierTypeNode) {
|
if (! $docNode instanceof IdentifierTypeNode) {
|
||||||
return $docNode;
|
return $docNode;
|
||||||
@ -93,6 +97,10 @@ final class DocBlockNameImporter
|
|||||||
return $docNode;
|
return $docNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (! $shouldImportRootNamespaceClasses && substr_count($staticType->getClassName(), '\\') === 0) {
|
||||||
|
return $docNode;
|
||||||
|
}
|
||||||
|
|
||||||
return $this->processFqnNameImport($phpParserNode, $docNode, $staticType);
|
return $this->processFqnNameImport($phpParserNode, $docNode, $staticType);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user