fix PseudoNamespaceToNamespaceRector for docblocks

This commit is contained in:
Tomas Votruba 2019-03-15 23:03:18 +01:00
parent 4f5c7e7b80
commit cce3bc22bd
9 changed files with 127 additions and 48 deletions

View File

@ -8,6 +8,7 @@ use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTextNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\ThrowsTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
use PHPStan\PhpDocParser\Ast\Type\ArrayTypeNode;
use PHPStan\PhpDocParser\Ast\Type\IntersectionTypeNode;
@ -46,7 +47,8 @@ final class NodeTraverser
{
return $phpDocTagValueNode instanceof ReturnTagValueNode ||
$phpDocTagValueNode instanceof ParamTagValueNode ||
$phpDocTagValueNode instanceof VarTagValueNode;
$phpDocTagValueNode instanceof VarTagValueNode ||
$phpDocTagValueNode instanceof ThrowsTagValueNode;
}
private function traverseTypeNode(TypeNode $typeNode, callable $callable): TypeNode

View File

@ -14,9 +14,11 @@ use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
use PHPStan\PhpDocParser\Ast\Type\ArrayTypeNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode;
use Rector\BetterPhpDocParser\Annotation\AnnotationNaming;
use Rector\BetterPhpDocParser\Ast\NodeTraverser;
use Rector\BetterPhpDocParser\Attributes\Ast\AttributeAwareNodeFactory;
use Rector\BetterPhpDocParser\Attributes\Ast\PhpDoc\AttributeAwareParamTagValueNode;
use Rector\BetterPhpDocParser\Attributes\Ast\PhpDoc\AttributeAwarePhpDocNode;
@ -63,18 +65,25 @@ final class DocBlockManipulator
*/
private $attributeAwareNodeFactory;
/**
* @var NodeTraverser
*/
private $nodeTraverser;
public function __construct(
PhpDocInfoFactory $phpDocInfoFactory,
PhpDocInfoPrinter $phpDocInfoPrinter,
TypeAnalyzer $typeAnalyzer,
AttributeAwareNodeFactory $attributeAwareNodeFactory,
StringsTypePhpDocNodeDecorator $stringsTypePhpDocNodeDecorator
StringsTypePhpDocNodeDecorator $stringsTypePhpDocNodeDecorator,
NodeTraverser $nodeTraverser
) {
$this->phpDocInfoFactory = $phpDocInfoFactory;
$this->phpDocInfoPrinter = $phpDocInfoPrinter;
$this->typeAnalyzer = $typeAnalyzer;
$this->attributeAwareNodeFactory = $attributeAwareNodeFactory;
$this->stringsTypePhpDocNodeDecorator = $stringsTypePhpDocNodeDecorator;
$this->nodeTraverser = $nodeTraverser;
}
public function hasTag(Node $node, string $name): bool
@ -408,6 +417,46 @@ final class DocBlockManipulator
return $attributeAwarePhpDocNode;
}
/**
* @param string[]|null $excludedClasses
*/
public function changeUnderscoreType(Node $node, string $namespacePrefix, ?array $excludedClasses): void
{
if ($node->getDocComment() === null) {
return;
}
$phpDocInfo = $this->createPhpDocInfoFromNode($node);
$phpDocNode = $phpDocInfo->getPhpDocNode();
$this->nodeTraverser->traverseWithCallable($phpDocNode, function (AttributeAwareNodeInterface $node) use (
$namespacePrefix,
$excludedClasses
) {
if (! $node instanceof IdentifierTypeNode) {
return $node;
}
$name = ltrim($node->name, '\\');
if (! Strings::startsWith($name, $namespacePrefix)) {
return $node;
}
// excluded?
if (is_array($excludedClasses) && in_array($name, $excludedClasses, true)) {
return $node;
}
// change underscore to \\
$nameParts = explode('_', $name);
$node->name = '\\' . implode('\\', $nameParts);
return $node;
});
$this->updateNodeWithPhpDocInfo($node, $phpDocInfo);
}
private function updateNodeWithPhpDocInfo(Node $node, PhpDocInfo $phpDocInfo): void
{
// skip if has no doc comment
@ -446,6 +495,8 @@ final class DocBlockManipulator
private function replaceTypeNode(TypeNode $typeNode, string $oldType, string $newType): TypeNode
{
// @todo use $this->nodeTraverser->traverseWithCallable here matching "AttributeAwareIdentifierTypeNode"
if ($typeNode instanceof AttributeAwareIdentifierTypeNode) {
$nodeType = $this->resolveNodeType($typeNode);
@ -456,7 +507,6 @@ final class DocBlockManipulator
}
}
// @todo IntersectionTy
if ($typeNode instanceof UnionTypeNode) {
foreach ($typeNode->types as $key => $subTypeNode) {
$typeNode->types[$key] = $this->replaceTypeNode($subTypeNode, $oldType, $newType);

View File

@ -144,3 +144,4 @@ parameters:
- '#Call to an undefined method PHPStan\\Type\\Type\:\:getValue\(\)#'
- '#Method Rector\\PHPUnit\\Rector\\MethodCall\\ReplaceAssertArraySubsetRector\:\:matchArray\(\) should return PhpParser\\Node\\Expr\\Array_\|null but returns PhpParser\\Node\\Expr#'
- '#Parameter \#2 \$name of method Rector\\PhpSpecToPHPUnit\\Rector\\Class_\\PhpSpecClassToPHPUnitClassRector\:\:processTestMethod\(\) expects string, int\|string given#'
- '#Array \(array<PHPStan\\Type\\Type\>\) does not accept PHPStan\\PhpDocParser\\Ast\\Type\\TypeNode#'

View File

@ -8,7 +8,6 @@ use PhpParser\Node\FunctionLike;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\Stmt\Use_;
use PhpParser\Node\Stmt\UseUse;
@ -77,11 +76,11 @@ CODE_SAMPLE
*/
public function getNodeTypes(): array
{
return [Name::class, ClassMethod::class, Property::class, FunctionLike::class];
return [Name::class, Property::class, FunctionLike::class];
}
/**
* @param Name|ClassMethod|Property $node
* @param Name|FunctionLike|Property $node
*/
public function refactor(Node $node): ?Node
{

View File

@ -2,15 +2,14 @@
namespace Rector\Rector\Namespace_;
use PhpParser\Builder\Property;
use PhpParser\Node;
use PhpParser\Node\FunctionLike;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Namespace_;
use PhpParser\Node\Stmt\Property;
use Rector\Exception\ShouldNotHappenException;
use Rector\NodeTypeResolver\Node\Attribute;
use Rector\NodeTypeResolver\PhpDoc\NodeAnalyzer\DocBlockManipulator;
@ -89,43 +88,23 @@ CODE_SAMPLE
*/
public function getNodeTypes(): array
{
// property, method
return [Name::class, Identifier::class, ClassMethod::class, Property::class, FunctionLike::class];
return [Name::class, Identifier::class, Property::class, FunctionLike::class];
}
/**
* @param Name|Identifier $node
* @param Name|Identifier|Property|FunctionLike $node
*/
public function refactor(Node $node): ?Node
{
if ($node instanceof Name || $node instanceof Identifier) {
// no name → skip
if ($node->toString() === '') {
return null;
}
foreach ($this->namespacePrefixWithExcludedClasses as $namespacePrefix => $excludedClasses) {
if (! $this->isName($node, '#^' . $namespacePrefix . '*#')) {
continue;
}
if (is_array($excludedClasses) && $this->isNames($node, $excludedClasses)) {
return null;
}
if ($node instanceof Name) {
return $this->processName($node);
}
return $this->processIdentifier($node);
}
return null;
// replace on @var/@param/@return/@throws
foreach ($this->namespacePrefixWithExcludedClasses as $namespacePrefix => $excludedClasses) {
$this->docBlockManipulator->changeUnderscoreType($node, $namespacePrefix, $excludedClasses);
}
// replace on @var/@param/@return
// $this->docBlockManipulator->removeParamTagByName()
if ($node instanceof Name || $node instanceof Identifier) {
return $this->processNameOrIdentifier($node);
}
return null;
}
@ -185,7 +164,7 @@ CODE_SAMPLE
$newNamespace = implode('\\', $namespaceParts);
if ($this->newNamespace !== null && $this->newNamespace !== $newNamespace) {
throw new ShouldNotHappenException('There woulde are 2 different namespaces in one file');
throw new ShouldNotHappenException('There cannot be 2 different namespaces in one file');
}
$this->newNamespace = $newNamespace;
@ -194,4 +173,34 @@ CODE_SAMPLE
return $identifier;
}
/**
* @param Name|Identifier $node
* @return Name|Identifier
*/
private function processNameOrIdentifier(Node $node): ?Node
{
// no name → skip
if ($node->toString() === '') {
return null;
}
foreach ($this->namespacePrefixWithExcludedClasses as $namespacePrefix => $excludedClasses) {
if (! $this->isName($node, $namespacePrefix . '*')) {
continue;
}
if (is_array($excludedClasses) && $this->isNames($node, $excludedClasses)) {
return null;
}
if ($node instanceof Name) {
return $this->processName($node);
}
return $this->processIdentifier($node);
}
return null;
}
}

View File

@ -30,7 +30,7 @@ use Rector\Tests\Rector\Namespace_\PseudoNamespaceToNamespaceRector\Source\Keep_
class SomeTestCase
{
/**
* @return \ChangeMe_AnotherNamespace
* @return \ChangeMe\AnotherNamespace
*/
public function someMethod(): Keep_This
{

View File

@ -4,11 +4,6 @@ namespace OldNamespace\SubNamespace;
class AnnotationsTest
{
/**
* @var \PHPUnit_Framework_TestCase
*/
public $test;
/**
* @param \PHPUnit_Framework_TestCase $test
* @return \PHPUnit_Framework_TestCase
@ -27,11 +22,6 @@ namespace OldNamespace\SubNamespace;
class AnnotationsTest
{
/**
* @var \PHPUnit\Framework\TestCase
*/
public $test;
/**
* @param \PHPUnit\Framework\TestCase $test
* @return \PHPUnit\Framework\TestCase

View File

@ -0,0 +1,27 @@
<?php
namespace OldNamespace\SubNamespace;
class VarDoc
{
/**
* @var \PHPUnit_Framework_TestCase
*/
public $test;
}
?>
-----
<?php
namespace OldNamespace\SubNamespace;
class VarDoc
{
/**
* @var \PHPUnit\Framework\TestCase
*/
public $test;
}
?>

View File

@ -16,6 +16,7 @@ final class PseudoNamespaceToNamespaceRectorTest extends AbstractRectorTestCase
__DIR__ . '/Fixture/fixture4.php.inc',
__DIR__ . '/Fixture/fixture5.php.inc',
__DIR__ . '/Fixture/fixture6.php.inc',
__DIR__ . '/Fixture/var_doc.php.inc',
]);
}