-
-
Notifications
You must be signed in to change notification settings - Fork 371
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Renaming] Apply rename fully qualified namespace docblock on RenameN…
…amespaceRector (#1917)
- Loading branch information
1 parent
84f7ebd
commit 888f483
Showing
7 changed files
with
301 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
packages/NodeTypeResolver/PhpDoc/NodeAnalyzer/DocBlockNamespaceRenamer.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\NodeTypeResolver\PhpDoc\NodeAnalyzer; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Stmt\ClassLike; | ||
use PhpParser\Node\Stmt\ClassMethod; | ||
use PhpParser\Node\Stmt\Expression; | ||
use PhpParser\Node\Stmt\Function_; | ||
use PhpParser\Node\Stmt\Property; | ||
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode; | ||
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagValueNode; | ||
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode; | ||
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode; | ||
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode; | ||
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory; | ||
use Rector\BetterPhpDocParser\ValueObject\PhpDoc\VariadicAwareParamTagValueNode; | ||
use Rector\BetterPhpDocParser\ValueObject\Type\BracketsAwareUnionTypeNode; | ||
use Rector\Core\PhpParser\Node\CustomNode\FileWithoutNamespace; | ||
use Rector\Naming\NamespaceMatcher; | ||
use Rector\Renaming\ValueObject\RenamedNamespace; | ||
|
||
final class DocBlockNamespaceRenamer | ||
{ | ||
/** | ||
* @var array<class-string<PhpDocTagValueNode>> | ||
*/ | ||
private const TO_BE_CHANGED = [ | ||
ReturnTagValueNode::class, | ||
VariadicAwareParamTagValueNode::class, | ||
VarTagValueNode::class, | ||
]; | ||
|
||
public function __construct( | ||
private readonly NamespaceMatcher $namespaceMatcher, | ||
private readonly PhpDocInfoFactory $phpDocInfoFactory | ||
) { | ||
} | ||
|
||
/** | ||
* @param array<string, string> $oldToNewNamespaces | ||
*/ | ||
public function renameFullyQualifiedNamespace( | ||
Property|ClassMethod|Function_|Expression|ClassLike|FileWithoutNamespace $node, | ||
array $oldToNewNamespaces | ||
): ?Node { | ||
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node); | ||
$phpDocNode = $phpDocInfo->getPhpDocNode(); | ||
$children = $phpDocNode->children; | ||
|
||
foreach ($children as $child) { | ||
if (! $child instanceof PhpDocTagNode) { | ||
continue; | ||
} | ||
|
||
$value = $child->value; | ||
if (! in_array($value::class, self::TO_BE_CHANGED, true)) { | ||
continue; | ||
} | ||
|
||
/** @var ReturnTagValueNode|VariadicAwareParamTagValueNode|VarTagValueNode $value */ | ||
$this->refactorDocblock($value, $oldToNewNamespaces); | ||
} | ||
|
||
if (! $phpDocInfo->hasChanged()) { | ||
return null; | ||
} | ||
|
||
return $node; | ||
} | ||
|
||
/** | ||
* @param array<string, string> $oldToNewNamespaces | ||
*/ | ||
private function refactorDocblock( | ||
ReturnTagValueNode|VariadicAwareParamTagValueNode|VarTagValueNode $value, | ||
array $oldToNewNamespaces | ||
): void { | ||
/** @var ReturnTagValueNode|VariadicAwareParamTagValueNode|VarTagValueNode $value */ | ||
$types = $value->type instanceof BracketsAwareUnionTypeNode | ||
? $value->type->types | ||
: [$value->type]; | ||
|
||
foreach ($types as $key => $type) { | ||
if (! $type instanceof IdentifierTypeNode) { | ||
continue; | ||
} | ||
|
||
$name = $type->name; | ||
$trimmedName = ltrim($type->name, '\\'); | ||
|
||
if ($name === $trimmedName) { | ||
continue; | ||
} | ||
|
||
$renamedNamespaceValueObject = $this->namespaceMatcher->matchRenamedNamespace( | ||
$trimmedName, | ||
$oldToNewNamespaces | ||
); | ||
if (! $renamedNamespaceValueObject instanceof RenamedNamespace) { | ||
continue; | ||
} | ||
|
||
$newType = new IdentifierTypeNode('\\' . $renamedNamespaceValueObject->getNameInNewNamespace()); | ||
|
||
if ($value->type instanceof BracketsAwareUnionTypeNode) { | ||
$types[$key] = $newType; | ||
} else { | ||
$value->type = $newType; | ||
} | ||
} | ||
|
||
if ($value->type instanceof BracketsAwareUnionTypeNode) { | ||
$value->type->types = $types; | ||
} | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...g/Rector/Namespace_/RenameNamespaceRector/Fixture/rename_namespace_docblock_param.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace OldNamespace\SubNamespace; | ||
|
||
use OldNamespace; | ||
|
||
class RenameNamespaceDocblockParam | ||
{ | ||
/** | ||
* @param \OldNamespace\SubNamespace\RenameNamespaceDocblockParam $argument | ||
*/ | ||
public function run(\OldNamespace\SubNamespace\RenameNamespaceDocblockParam $argument) | ||
{ | ||
return $argument; | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace NewNamespace\SubNamespace; | ||
|
||
use NewNamespace; | ||
|
||
class RenameNamespaceDocblockParam | ||
{ | ||
/** | ||
* @param \NewNamespace\SubNamespace\RenameNamespaceDocblockParam $argument | ||
*/ | ||
public function run(\NewNamespace\SubNamespace\RenameNamespaceDocblockParam $argument) | ||
{ | ||
return $argument; | ||
} | ||
} | ||
|
||
?> |
37 changes: 37 additions & 0 deletions
37
.../Rector/Namespace_/RenameNamespaceRector/Fixture/rename_namespace_docblock_return.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace OldNamespace\SubNamespace; | ||
|
||
use OldNamespace; | ||
|
||
class RenameNamespaceDocblockReturn | ||
{ | ||
/** | ||
* @return \OldNamespace\SubNamespace\RenameNamespaceDocblockReturn | ||
*/ | ||
public function run(\OldNamespace\SubNamespace\RenameNamespaceDocblockReturn $argument) | ||
{ | ||
return $argument; | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace NewNamespace\SubNamespace; | ||
|
||
use NewNamespace; | ||
|
||
class RenameNamespaceDocblockReturn | ||
{ | ||
/** | ||
* @return \NewNamespace\SubNamespace\RenameNamespaceDocblockReturn | ||
*/ | ||
public function run(\NewNamespace\SubNamespace\RenameNamespaceDocblockReturn $argument) | ||
{ | ||
return $argument; | ||
} | ||
} | ||
|
||
?> |
39 changes: 39 additions & 0 deletions
39
...g/Rector/Namespace_/RenameNamespaceRector/Fixture/rename_namespace_docblock_union.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
namespace OldNamespace\SubNamespace; | ||
|
||
use OldNamespace; | ||
|
||
class RenameNamespaceDocblockUnion | ||
{ | ||
public function run(?\OldNamespace\SubNamespace\RenameNamespaceDocblockUnion $argument) | ||
{ | ||
/** | ||
* @var \OldNamespace\SubNamespace\RenameNamespaceDocblockUnion|null $argument | ||
*/ | ||
$var = $argument; | ||
return $var; | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace NewNamespace\SubNamespace; | ||
|
||
use NewNamespace; | ||
|
||
class RenameNamespaceDocblockUnion | ||
{ | ||
public function run(?\NewNamespace\SubNamespace\RenameNamespaceDocblockUnion $argument) | ||
{ | ||
/** | ||
* @var \NewNamespace\SubNamespace\RenameNamespaceDocblockUnion|null $argument | ||
*/ | ||
$var = $argument; | ||
return $var; | ||
} | ||
} | ||
|
||
?> |
39 changes: 39 additions & 0 deletions
39
...ing/Rector/Namespace_/RenameNamespaceRector/Fixture/rename_namespace_docblock_var.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
namespace OldNamespace\SubNamespace; | ||
|
||
use OldNamespace; | ||
|
||
class RenameNamespaceDocblockVar | ||
{ | ||
public function run(\OldNamespace\SubNamespace\RenameNamespaceDocblockVar $argument) | ||
{ | ||
/** | ||
* @var \OldNamespace\SubNamespace\RenameNamespaceDocblockVar $argument | ||
*/ | ||
$var = $argument; | ||
return $var; | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace NewNamespace\SubNamespace; | ||
|
||
use NewNamespace; | ||
|
||
class RenameNamespaceDocblockVar | ||
{ | ||
public function run(\NewNamespace\SubNamespace\RenameNamespaceDocblockVar $argument) | ||
{ | ||
/** | ||
* @var \NewNamespace\SubNamespace\RenameNamespaceDocblockVar $argument | ||
*/ | ||
$var = $argument; | ||
return $var; | ||
} | ||
} | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters