-
-
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.
[DX] Make ClassAnnotationMatcher differentiate between known and unkn…
…own classes (#2319) Co-authored-by: Abdul Malik Ikhsan <samsonasik@gmail.com>
- Loading branch information
1 parent
40d9102
commit df5bd5f
Showing
11 changed files
with
266 additions
and
9 deletions.
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
...erPhpDocParser/PhpDocParser/ClassAnnotationMatcher/Fixture/ExistingClass/SiblingClass.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,8 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\BetterPhpDocParser\PhpDocParser\ClassAnnotationMatcher\Fixture\ExistingClass; | ||
|
||
class SiblingClass | ||
{ | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
...terPhpDocParser/PhpDocParser/ClassAnnotationMatcher/Fixture/ExistingClass/fixture.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,23 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\BetterPhpDocParser\PhpDocParser\ClassAnnotationMatcher\Fixture\ExistingClass; | ||
|
||
use Rector\Tests\BetterPhpDocParser\PhpDocParser\ClassAnnotationMatcher\Source\KnownClass; | ||
use Rector\Tests\BetterPhpDocParser\PhpDocParser\ClassAnnotationMatcher\Source\KnownClass as AliasedKnownClass; | ||
use SplHeap as AliasedSplHeap; | ||
|
||
class MyClass | ||
{ | ||
/** @var KnownClass */ | ||
private $knownClass; | ||
/** @var \Rector\Tests\BetterPhpDocParser\PhpDocParser\ClassAnnotationMatcher\Source\KnownClass */ | ||
private $knownInlinedClass; | ||
/** @var SiblingClass */ | ||
private $knownSiblingClass; | ||
/** @var AliasedKnownClass */ | ||
private $knownAliasedClass; | ||
/** @var \SplHeap */ | ||
private $knownGlobalClass; | ||
/** @var AliasedSplHeap */ | ||
private $knownAliasedGlobalClass; | ||
} |
20 changes: 20 additions & 0 deletions
20
...PhpDocParser/PhpDocParser/ClassAnnotationMatcher/Fixture/NonExistingClass/fixture.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,20 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\BetterPhpDocParser\PhpDocParser\ClassAnnotationMatcher\Fixture\NonExistingClass; | ||
|
||
use My\Namespace\OtherUnknownClass; | ||
use My\Namespace\OtherUnknownClass as AliasedUnknownClass; | ||
|
||
class MyClass | ||
{ | ||
/** @var UnknownClass */ | ||
private $unknownClass; | ||
/** @var \My\Namespace\UnknownClass */ | ||
private $unknownNamespacedClass; | ||
/** @var OtherUnknownClass */ | ||
private $unknownUsedClass; | ||
/** @var AliasedUnknownClass */ | ||
private $unknownAliasedUsedClass; | ||
/** @var \UnknownGlobalClass */ | ||
private $unknownGlobalClass; | ||
} |
81 changes: 81 additions & 0 deletions
81
...DocParser/PhpDocParser/ClassAnnotationMatcher/ResolveTagToKnownFullyQualifiedNameTest.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,81 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\BetterPhpDocParser\PhpDocParser\ClassAnnotationMatcher; | ||
|
||
use Iterator; | ||
use PhpParser\Node\Stmt\Property; | ||
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode; | ||
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory; | ||
use Rector\BetterPhpDocParser\PhpDocParser\ClassAnnotationMatcher; | ||
use Rector\Core\Exception\ShouldNotHappenException; | ||
use Rector\Core\PhpParser\Node\BetterNodeFinder; | ||
use Rector\NodeNameResolver\NodeNameResolver; | ||
use Rector\Testing\PHPUnit\AbstractTestCase; | ||
use Rector\Testing\TestingParser\TestingParser; | ||
use Symplify\EasyTesting\DataProvider\StaticFixtureFinder; | ||
use Symplify\SmartFileSystem\SmartFileInfo; | ||
|
||
final class ResolveTagToKnownFullyQualifiedNameTest extends AbstractTestCase | ||
{ | ||
private ClassAnnotationMatcher $classAnnotationMatcher; | ||
|
||
private TestingParser $testingParser; | ||
|
||
private BetterNodeFinder $nodeFinder; | ||
|
||
private PhpDocInfoFactory $phpDocInfoFactory; | ||
|
||
private NodeNameResolver $nodeNameResolver; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->boot(); | ||
|
||
$this->classAnnotationMatcher = $this->getService(ClassAnnotationMatcher::class); | ||
$this->testingParser = $this->getService(TestingParser::class); | ||
$this->nodeFinder = $this->getService(BetterNodeFinder::class); | ||
$this->phpDocInfoFactory = $this->getService(PhpDocInfoFactory::class); | ||
$this->nodeNameResolver = $this->getService(NodeNameResolver::class); | ||
} | ||
|
||
/** | ||
* @dataProvider provideData() | ||
*/ | ||
public function testResolvesClass(SmartFileInfo $file): void | ||
{ | ||
$nodes = $this->testingParser->parseFileToDecoratedNodes($file->getRelativeFilePath()); | ||
$properties = $this->nodeFinder->findInstancesOf($nodes, [Property::class]); | ||
|
||
foreach ($properties as $property) { | ||
/** @var Property $property */ | ||
$phpDoc = $this->phpDocInfoFactory->createFromNodeOrEmpty($property); | ||
/** @var VarTagValueNode $varTag */ | ||
$varTag = $phpDoc->getByType(VarTagValueNode::class)[0]; | ||
$value = $varTag->type->__toString(); | ||
$propertyName = strtolower($this->nodeNameResolver->getName($property)); | ||
|
||
$result = $this->classAnnotationMatcher->resolveTagToKnownFullyQualifiedName($value, $property); | ||
if (str_starts_with($propertyName, 'unknown')) { | ||
$this->assertNull($result); | ||
} elseif (str_contains($propertyName, 'aliased')) { | ||
$unaliasedClass = str_replace('Aliased', '', $value); | ||
$this->assertStringEndsWith($unaliasedClass, $result ?? ''); | ||
} elseif (str_starts_with($propertyName, 'known')) { | ||
$this->assertStringEndsWith($value, $result ?? ''); | ||
} else { | ||
throw new ShouldNotHappenException('All Variables should start with "known" or "unknown"!'); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @return Iterator<SmartFileInfo> | ||
*/ | ||
public function provideData(): Iterator | ||
{ | ||
$directory = __DIR__ . '/Fixture'; | ||
return StaticFixtureFinder::yieldDirectory($directory); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
packages-tests/BetterPhpDocParser/PhpDocParser/ClassAnnotationMatcher/Source/KnownClass.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,8 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\BetterPhpDocParser\PhpDocParser\ClassAnnotationMatcher\Source; | ||
|
||
class KnownClass | ||
{ | ||
|
||
} |
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
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
31 changes: 31 additions & 0 deletions
31
tests/Issues/DoNotReplaceUnknownClasses/DoNotReplaceUnknownClassesTest.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,31 @@ | ||
<?php | ||
|
||
namespace Rector\Core\Tests\Issues\DoNotReplaceUnknownClasses; | ||
|
||
use Iterator; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
use Symplify\SmartFileSystem\SmartFileInfo; | ||
|
||
class DoNotReplaceUnknownClassesTest 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/configured_rule.php'; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
tests/Issues/DoNotReplaceUnknownClasses/Fixture/skip_unknown_class_in_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,19 @@ | ||
<?php | ||
|
||
use My\Namespace\UnknownClass; | ||
|
||
final class SkipUnknownClassInVar | ||
{ | ||
private ?\My\Namespace\UnknownClass $otherClass = null; | ||
|
||
public function getOtherClass(): ?\My\Namespace\UnknownClass | ||
{ | ||
return $this->otherClass; | ||
} | ||
|
||
public function setOtherClass($otherClass): void | ||
{ | ||
/** @var UnknownClass $otherClass */ | ||
$this->otherClass = $otherClass; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
tests/Issues/DoNotReplaceUnknownClasses/Fixture/skip_unknown_target_entity.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,15 @@ | ||
<?php | ||
|
||
namespace Rector\Core\Tests\Issues\DoNotReplaceUnknownClasses\Fixture; | ||
|
||
use Doctrine\Common\Collections\Collection; | ||
use Doctrine\ORM\Mapping as ORM; | ||
use My\Namespace\UsedUnknownClass; | ||
|
||
final class SkipUnknownTargetEntity | ||
{ | ||
/** | ||
* @ORM\ManyToOne(targetEntity=\My\Namespace\UsedUnknownClass::class) | ||
*/ | ||
private readonly ?Collection $items; | ||
} |
14 changes: 14 additions & 0 deletions
14
tests/Issues/DoNotReplaceUnknownClasses/config/configured_rule.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,14 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\Config\RectorConfig; | ||
use Rector\Doctrine\Rector\Property\DoctrineTargetEntityStringToClassConstantRector; | ||
use Rector\TypeDeclaration\Rector\FunctionLike\ReturnTypeDeclarationRector; | ||
use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromAssignsRector; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->rule(DoctrineTargetEntityStringToClassConstantRector::class); | ||
$rectorConfig->rule(TypedPropertyFromAssignsRector::class); | ||
$rectorConfig->rule(ReturnTypeDeclarationRector::class); | ||
}; |