Skip to content

Commit

Permalink
[Naming] Fix PseudoNamespaceToNamespaceRector reporting on change (#2426
Browse files Browse the repository at this point in the history
)

* fix PseudoNamespaceToNamespaceRector reporting on change

* remove overly abstraction with visitor factories

* [ci-review] Rector Rectify

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
TomasVotruba and actions-user committed Jun 4, 2022
1 parent c3581f9 commit 6b99841
Show file tree
Hide file tree
Showing 14 changed files with 104 additions and 160 deletions.
1 change: 1 addition & 0 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
__DIR__ . '/../packages/BetterPhpDocParser/PhpDoc',
__DIR__ . '/../packages/PHPStanStaticTypeMapper/Enum',
__DIR__ . '/../packages/Caching/Cache.php',
__DIR__ . '/../packages/NodeTypeResolver/PhpDocNodeVisitor/UnderscoreRenamePhpDocNodeVisitor.php',

// used in PHPStan
__DIR__ . '/../packages/NodeTypeResolver/Reflection/BetterReflection/RectorBetterReflectionSourceLocatorFactory.php',
Expand Down

This file was deleted.

7 changes: 4 additions & 3 deletions packages/BetterPhpDocParser/Printer/PhpDocInfoPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use PHPStan\PhpDocParser\Lexer\Lexer;
use Rector\BetterPhpDocParser\PhpDoc\DoctrineAnnotationTagValueNode;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\BetterPhpDocParser\PhpDocNodeTraverser\ChangedPhpDocNodeTraverserFactory;
use Rector\BetterPhpDocParser\PhpDocNodeVisitor\ChangedPhpDocNodeVisitor;
use Rector\BetterPhpDocParser\ValueObject\PhpDocAttributeKey;
use Rector\BetterPhpDocParser\ValueObject\StartAndEnd;
Expand Down Expand Up @@ -83,9 +82,11 @@ public function __construct(
private readonly DocBlockInliner $docBlockInliner,
private readonly RemoveNodesStartAndEndResolver $removeNodesStartAndEndResolver,
private readonly ChangedPhpDocNodeVisitor $changedPhpDocNodeVisitor,
ChangedPhpDocNodeTraverserFactory $changedPhpDocNodeTraverserFactory
) {
$this->changedPhpDocNodeTraverser = $changedPhpDocNodeTraverserFactory->create();
$changedPhpDocNodeTraverser = new PhpDocNodeTraverser();
$changedPhpDocNodeTraverser->addPhpDocNodeVisitor($this->changedPhpDocNodeVisitor);

$this->changedPhpDocNodeTraverser = $changedPhpDocNodeTraverser;
}

public function printNew(PhpDocInfo $phpDocInfo): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
namespace Rector\NodeTypeResolver\PhpDoc\NodeAnalyzer;

use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\NodeTypeResolver\PhpDoc\PhpDocNodeTraverser\RenamingPhpDocNodeVisitorFactory;
use Rector\NodeTypeResolver\PhpDocNodeVisitor\ClassRenamePhpDocNodeVisitor;
use Rector\NodeTypeResolver\ValueObject\OldToNewType;
use Symplify\Astral\PhpDocParser\PhpDocNodeTraverser;

final class DocBlockClassRenamer
{
public function __construct(
private readonly ClassRenamePhpDocNodeVisitor $classRenamePhpDocNodeVisitor,
private readonly RenamingPhpDocNodeVisitorFactory $renamingPhpDocNodeVisitorFactory
) {
}

Expand All @@ -26,7 +25,9 @@ public function renamePhpDocType(PhpDocInfo $phpDocInfo, array $oldToNewTypes):
return;
}

$phpDocNodeTraverser = $this->renamingPhpDocNodeVisitorFactory->create();
$phpDocNodeTraverser = new PhpDocNodeTraverser();
$phpDocNodeTraverser->addPhpDocNodeVisitor($this->classRenamePhpDocNodeVisitor);

$this->classRenamePhpDocNodeVisitor->setOldToNewTypes($oldToNewTypes);

$phpDocNodeTraverser->traverse($phpDocInfo->getPhpDocNode());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@

use PhpParser\Node;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode;
use Rector\NodeTypeResolver\PhpDoc\PhpDocNodeTraverser\ImportingPhpDocNodeTraverserFactory;
use Rector\NodeTypeResolver\PhpDocNodeVisitor\NameImportingPhpDocNodeVisitor;
use Symplify\Astral\PhpDocParser\PhpDocNodeTraverser;

final class DocBlockNameImporter
{
public function __construct(
private readonly NameImportingPhpDocNodeVisitor $nameImportingPhpDocNodeVisitor,
private readonly ImportingPhpDocNodeTraverserFactory $importingPhpDocNodeTraverserFactory
) {
}

Expand All @@ -25,7 +24,8 @@ public function importNames(PhpDocNode $phpDocNode, Node $node): void

$this->nameImportingPhpDocNodeVisitor->setCurrentNode($node);

$phpDocNodeTraverser = $this->importingPhpDocNodeTraverserFactory->create();
$phpDocNodeTraverser = new PhpDocNodeTraverser();
$phpDocNodeTraverser->addPhpDocNodeVisitor($this->nameImportingPhpDocNodeVisitor);
$phpDocNodeTraverser->traverse($phpDocNode);
}
}

This file was deleted.

This file was deleted.

This file was deleted.

21 changes: 14 additions & 7 deletions packages/NodeTypeResolver/PhpDoc/PhpDocTypeRenamer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,36 @@

use PhpParser\Node;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\NodeTypeResolver\PhpDoc\PhpDocNodeTraverser\UnderscorePhpDocNodeTraverserFactory;
use Rector\NodeTypeResolver\PhpDocNodeVisitor\UnderscoreRenamePhpDocNodeVisitor;
use Rector\Renaming\ValueObject\PseudoNamespaceToNamespace;
use Rector\StaticTypeMapper\StaticTypeMapper;
use Symplify\Astral\PhpDocParser\PhpDocNodeTraverser;

final class PhpDocTypeRenamer
{
public function __construct(
private readonly UnderscorePhpDocNodeTraverserFactory $underscorePhpDocNodeTraverserFactory,
private readonly UnderscoreRenamePhpDocNodeVisitor $underscoreRenamePhpDocNodeVisitor
private readonly StaticTypeMapper $staticTypeMapper,
) {
}

public function changeUnderscoreType(
PhpDocInfo $phpDocInfo,
Node $node,
PseudoNamespaceToNamespace $pseudoNamespaceToNamespace
): void {
): bool {
$phpDocNode = $phpDocInfo->getPhpDocNode();

$this->underscoreRenamePhpDocNodeVisitor->setPseudoNamespaceToNamespace($pseudoNamespaceToNamespace);
$this->underscoreRenamePhpDocNodeVisitor->setCurrentPhpParserNode($node);
$underscoreRenamePhpDocNodeVisitor = new UnderscoreRenamePhpDocNodeVisitor(
$this->staticTypeMapper,
$pseudoNamespaceToNamespace,
$node,
);

$phpDocNodeTraverser = $this->underscorePhpDocNodeTraverserFactory->create();
$phpDocNodeTraverser = new PhpDocNodeTraverser();
$phpDocNodeTraverser->addPhpDocNodeVisitor($underscoreRenamePhpDocNodeVisitor);
$phpDocNodeTraverser->traverse($phpDocNode);

// has changed?
return $underscoreRenamePhpDocNodeVisitor->hasChanged();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function enterNode(Node $node): ?Node
}

$phpParserNode = $this->currentNodeProvider->getNode();
if (! $phpParserNode instanceof \PhpParser\Node) {
if (! $phpParserNode instanceof PhpParserNode) {
throw new ShouldNotHappenException();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,24 @@
use PHPStan\PhpDocParser\Ast\Node;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\Type\ObjectType;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Renaming\ValueObject\PseudoNamespaceToNamespace;
use Rector\StaticTypeMapper\StaticTypeMapper;
use Symplify\Astral\PhpDocParser\PhpDocNodeVisitor\AbstractPhpDocNodeVisitor;

final class UnderscoreRenamePhpDocNodeVisitor extends AbstractPhpDocNodeVisitor
{
private ?PseudoNamespaceToNamespace $pseudoNamespaceToNamespace = null;

private ?\PhpParser\Node $currentPhpParserNode = null;
private bool $hasChanged = false;

public function __construct(
private readonly StaticTypeMapper $staticTypeMapper
private readonly StaticTypeMapper $staticTypeMapper,
private readonly PseudoNamespaceToNamespace $pseudoNamespaceToNamespace,
private readonly \PhpParser\Node $phpNode,
) {
}

public function beforeTraverse(Node $node): void
{
if ($this->pseudoNamespaceToNamespace === null) {
throw new ShouldNotHappenException('Set PseudoNamespaceToNamespace first');
}

if (! $this->currentPhpParserNode instanceof \PhpParser\Node) {
throw new ShouldNotHappenException('Set "$currentPhpParserNode" first');
}
$this->hasChanged = false;
}

public function enterNode(Node $node): ?Node
Expand All @@ -41,32 +34,26 @@ public function enterNode(Node $node): ?Node
return null;
}

if ($this->shouldSkip($node, $this->currentPhpParserNode, $this->pseudoNamespaceToNamespace)) {
if ($this->shouldSkip($node, $this->phpNode, $this->pseudoNamespaceToNamespace)) {
return null;
}

/** @var IdentifierTypeNode $node */
$staticType = $this->staticTypeMapper->mapPHPStanPhpDocTypeNodeToPHPStanType(
$node,
$this->currentPhpParserNode
);
$staticType = $this->staticTypeMapper->mapPHPStanPhpDocTypeNodeToPHPStanType($node, $this->phpNode);
if (! $staticType instanceof ObjectType) {
return null;
}

$this->hasChanged = true;

// change underscore to \\
$slashedName = '\\' . Strings::replace($staticType->getClassName(), '#_#', '\\');
return new IdentifierTypeNode($slashedName);
}

public function setPseudoNamespaceToNamespace(PseudoNamespaceToNamespace $pseudoNamespaceToNamespace): void
{
$this->pseudoNamespaceToNamespace = $pseudoNamespaceToNamespace;
}

public function setCurrentPhpParserNode(\PhpParser\Node $node): void
public function hasChanged(): bool
{
$this->currentPhpParserNode = $node;
return $this->hasChanged;
}

private function shouldSkip(
Expand Down
3 changes: 3 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -719,3 +719,6 @@ parameters:
path: rules/DeadCode/Rector/StmtsAwareInterface/RemoveJustPropertyFetchForAssignRector.php

- '#Method "replaceTagByAnother\(\)" returns bool type, so the name should start with is/has/was#'
- '#Method "refactorPhpDoc\(\)" returns bool type, so the name should start with is/has/was#'

- '#Cognitive complexity for "Rector\\Renaming\\Rector\\FileWithoutNamespace\\PseudoNamespaceToNamespaceRector\:\:refactorStmts\(\)" is 11, keep it under 10#'
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@
use Rector\Renaming\ValueObject\PseudoNamespaceToNamespace;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig
->ruleWithConfiguration(PseudoNamespaceToNamespaceRector::class, [
new PseudoNamespaceToNamespace('PHPUnit_', ['PHPUnit_Framework_MockObject_MockObject']),
new PseudoNamespaceToNamespace('ChangeMe_', ['KeepMe_']),
new PseudoNamespaceToNamespace(
'Rector_Tests_Renaming_Rector_FileWithoutNamespace_PseudoNamespaceToNamespaceRector_Fixture_'
),
]);
$rectorConfig->ruleWithConfiguration(PseudoNamespaceToNamespaceRector::class, [
new PseudoNamespaceToNamespace('PHPUnit_', ['PHPUnit_Framework_MockObject_MockObject']),
new PseudoNamespaceToNamespace('ChangeMe_', ['KeepMe_']),
new PseudoNamespaceToNamespace(
'Rector_Tests_Renaming_Rector_FileWithoutNamespace_PseudoNamespaceToNamespaceRector_Fixture_'
),
]);
};

0 comments on commit 6b99841

Please sign in to comment.