Skip to content

Commit

Permalink
Fix accidental class rename on docblock imports (#314)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Jun 27, 2021
1 parent a435716 commit 4b0dd17
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 4 deletions.
8 changes: 7 additions & 1 deletion .phpstorm.meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,18 @@
PhpDocAttributeKey::START_AND_END,
PhpDocAttributeKey::LAST_PHP_DOC_TOKEN_POSITION,
PhpDocAttributeKey::PARENT,
PhpDocAttributeKey::ORIG_NODE,
PhpDocAttributeKey::RESOLVED_CLASS,
);

expectedArguments(
\PHPStan\PhpDocParser\Ast\Node::setAttribute(),
\PHPStan\PhpDocParser\Ast\NodeAttributes::getAttribute(),
0,
PhpDocAttributeKey::START_AND_END,
PhpDocAttributeKey::LAST_PHP_DOC_TOKEN_POSITION,
PhpDocAttributeKey::PARENT,
PhpDocAttributeKey::ORIG_NODE,
PhpDocAttributeKey::RESOLVED_CLASS,
);

expectedArguments(
Expand All @@ -32,6 +36,8 @@
PhpDocAttributeKey::START_AND_END,
PhpDocAttributeKey::LAST_PHP_DOC_TOKEN_POSITION,
PhpDocAttributeKey::PARENT,
PhpDocAttributeKey::ORIG_NODE,
PhpDocAttributeKey::RESOLVED_CLASS,
);


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use Rector\BetterPhpDocParser\ValueObject\PhpDoc\DoctrineAnnotation\AbstractValuesAwareNode;
use Rector\BetterPhpDocParser\ValueObject\PhpDocAttributeKey;
use Stringable;

final class DoctrineAnnotationTagValueNode extends AbstractValuesAwareNode implements Stringable
Expand Down Expand Up @@ -49,6 +50,12 @@ public function __toString(): string

public function hasClassName(string $className): bool
{
return $this->identifierTypeNode->name === $className;
if ($this->identifierTypeNode->name === $className) {
return true;
}

// the name is not fully qualified in the original name, look for resolvd class attirubte
$resolvedClass = $this->identifierTypeNode->getAttribute(PhpDocAttributeKey::RESOLVED_CLASS);
return $resolvedClass === $className;
}
}
11 changes: 11 additions & 0 deletions packages/BetterPhpDocParser/PhpDocInfo/PhpDocInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use Rector\BetterPhpDocParser\PhpDoc\SpacelessPhpDocTagNode;
use Rector\BetterPhpDocParser\PhpDocNodeVisitor\ChangedPhpDocNodeVisitor;
use Rector\BetterPhpDocParser\ValueObject\Parser\BetterTokenIterator;
use Rector\BetterPhpDocParser\ValueObject\PhpDocAttributeKey;
use Rector\BetterPhpDocParser\ValueObject\Type\BracketsAwareUnionTypeNode;
use Rector\ChangesReporting\Collector\RectorChangeCollector;
use Rector\Core\Configuration\CurrentNodeProvider;
Expand Down Expand Up @@ -267,6 +268,16 @@ public function getByAnnotationClass(string $desiredClass): ?DoctrineAnnotationT
if ($this->isFnmatch($identifierTypeNode->name, $desiredClass)) {
return $doctrineAnnotationTagValueNode;
}

// FQN check
$resolvedClass = $identifierTypeNode->getAttribute(PhpDocAttributeKey::RESOLVED_CLASS);
if (!is_string($resolvedClass)) {
continue;
}

if ($this->isFnmatch($resolvedClass, $desiredClass)) {
return $doctrineAnnotationTagValueNode;
}
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,11 @@ private function transformGenericTagValueNodesToDoctrineAnnotationTagValueNodes(

$formerStartEnd = $genericTagValueNode->getAttribute(PhpDocAttributeKey::START_AND_END);

$identifierTypeNode = new IdentifierTypeNode($phpDocChildNode->name);
$identifierTypeNode->setAttribute(PhpDocAttributeKey::RESOLVED_CLASS, $fullyQualifiedAnnotationClass);

$doctrineAnnotationTagValueNode = new DoctrineAnnotationTagValueNode(
new IdentifierTypeNode($fullyQualifiedAnnotationClass),
$identifierTypeNode,
$genericTagValueNode->value,
$values,
SilentKeyMap::CLASS_NAMES_TO_SILENT_KEYS[$fullyQualifiedAnnotationClass] ?? null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ final class PhpDocAttributeKey
*/
public const START_AND_END = 'start_and_end';

/**
* Fully qualified name of identifier type class
* @var string
*/
public const RESOLVED_CLASS = 'resolved_class';

/**
* @var string
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use PHPStan\Type\ObjectType;
use Rector\BetterPhpDocParser\ValueObject\PhpDocAttributeKey;
use Rector\Core\Configuration\CurrentNodeProvider;
use Rector\Core\Configuration\RenamedClassesDataCollector;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\NodeTypeResolver\ValueObject\OldToNewType;
use Rector\StaticTypeMapper\StaticTypeMapper;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ private function processDoctrineAnnotationTagValueNode(
DoctrineAnnotationTagValueNode $doctrineAnnotationTagValueNode
): void {
$identifierTypeNode = $doctrineAnnotationTagValueNode->identifierTypeNode;

$staticType = $this->staticTypeMapper->mapPHPStanPhpDocTypeNodeToPHPStanType(
$identifierTypeNode,
$this->currentPhpParserNode
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Rector\Core\Tests\Issues\IssueDoubleNestedAnnotatoinDocBlock\Fixture;

use Rector\Core\Tests\Issues\IssueDoubleNestedAnnotatoinDocBlock\Source\SomeAnnotation;

/**
* @SomeAnnotation(
* @SomeAnnotation(key="value")
* )
*/
final class SomeFixture
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Rector\Core\Tests\Issues\IssueDoubleNestedAnnotatoinDocBlock;

use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;

final class IssueDoubleNestedAnnotatoinDocBlockTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(SmartFileInfo $fileInfo): void
{
$this->doTestFileInfo($fileInfo);
}

/**
* @return Iterator<SmartFileInfo>
*/
public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configure_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Rector\Core\Tests\Issues\IssueDoubleNestedAnnotatoinDocBlock\Source;

/**
* @annotation
*/
final class SomeAnnotation
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

use Rector\Core\Configuration\Option;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
$parameters = $containerConfigurator->parameters();
$parameters->set(Option::AUTO_IMPORT_NAMES, true);
};

0 comments on commit 4b0dd17

Please sign in to comment.