From 843e9d2bc652dfcc9b20d99f01bbb186d29e8986 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sun, 24 Nov 2024 21:21:04 +0700 Subject: [PATCH 1/3] [CodeQuality][Renaming] Handle DynamicDocBlockPropertyToNativePropertyRector+RenameClassRector with aliased name --- .../DynamicDocblockRenameTest.php | 28 +++++++++++++++ .../Fixture/fixture.php.inc | 36 +++++++++++++++++++ .../config/configured_rule.php | 18 ++++++++++ 3 files changed, 82 insertions(+) create mode 100644 tests/Issues/DynamicDocblockRename/DynamicDocblockRenameTest.php create mode 100644 tests/Issues/DynamicDocblockRename/Fixture/fixture.php.inc create mode 100644 tests/Issues/DynamicDocblockRename/config/configured_rule.php diff --git a/tests/Issues/DynamicDocblockRename/DynamicDocblockRenameTest.php b/tests/Issues/DynamicDocblockRename/DynamicDocblockRenameTest.php new file mode 100644 index 0000000000..c1980963f2 --- /dev/null +++ b/tests/Issues/DynamicDocblockRename/DynamicDocblockRenameTest.php @@ -0,0 +1,28 @@ +doTestFile($filePath); + } + + public static function provideData(): Iterator + { + return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); + } + + public function provideConfigFilePath(): string + { + return __DIR__ . '/config/configured_rule.php'; + } +} diff --git a/tests/Issues/DynamicDocblockRename/Fixture/fixture.php.inc b/tests/Issues/DynamicDocblockRename/Fixture/fixture.php.inc new file mode 100644 index 0000000000..c89d3711f5 --- /dev/null +++ b/tests/Issues/DynamicDocblockRename/Fixture/fixture.php.inc @@ -0,0 +1,36 @@ +someDependency = new SomeSource\SomeDependency(); + } +} + +?> +----- +someDependency = new \stdClass(); + } +} + +?> diff --git a/tests/Issues/DynamicDocblockRename/config/configured_rule.php b/tests/Issues/DynamicDocblockRename/config/configured_rule.php new file mode 100644 index 0000000000..19bb6bc7cd --- /dev/null +++ b/tests/Issues/DynamicDocblockRename/config/configured_rule.php @@ -0,0 +1,18 @@ +rule(DynamicDocBlockPropertyToNativePropertyRector::class); + $rectorConfig->ruleWithConfiguration( + RenameClassRector::class, + [ + 'Rector\Tests\CodeQuality\Rector\Class_\DynamicDocBlockPropertyToNativePropertyRector\Source\SomeDependency' + => 'stdClass' + ] + ); +}; From d2c7fae02e27e4d70d086c3f6d7d9d65c3f331d0 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sun, 24 Nov 2024 21:26:46 +0700 Subject: [PATCH 2/3] fix --- .../Fixture/aliased_class.php.inc | 2 +- .../Fixture/bare_class.php.inc | 2 +- .../Fixture/improve_existing_property_type.php.inc | 2 +- .../Fixture/include_null.php.inc | 2 +- .../Fixture/some_test.php.inc | 2 +- .../Fixture/with_other_existing_attribute.php.inc | 2 +- src/PHPStanStaticTypeMapper/TypeMapper/ObjectTypeMapper.php | 4 +--- 7 files changed, 7 insertions(+), 9 deletions(-) diff --git a/rules-tests/CodeQuality/Rector/Class_/DynamicDocBlockPropertyToNativePropertyRector/Fixture/aliased_class.php.inc b/rules-tests/CodeQuality/Rector/Class_/DynamicDocBlockPropertyToNativePropertyRector/Fixture/aliased_class.php.inc index 0bfb02d19b..935f20dd3f 100644 --- a/rules-tests/CodeQuality/Rector/Class_/DynamicDocBlockPropertyToNativePropertyRector/Fixture/aliased_class.php.inc +++ b/rules-tests/CodeQuality/Rector/Class_/DynamicDocBlockPropertyToNativePropertyRector/Fixture/aliased_class.php.inc @@ -26,7 +26,7 @@ use Rector\Tests\CodeQuality\Rector\Class_\DynamicDocBlockPropertyToNativeProper final class AliasedClass { - private ?SomeSource\SomeDependency $someDependency; + private ?\Rector\Tests\CodeQuality\Rector\Class_\DynamicDocBlockPropertyToNativePropertyRector\Source\SomeDependency $someDependency; public function run(): void { $this->someDependency = new SomeSource\SomeDependency(); diff --git a/rules-tests/CodeQuality/Rector/Class_/DynamicDocBlockPropertyToNativePropertyRector/Fixture/bare_class.php.inc b/rules-tests/CodeQuality/Rector/Class_/DynamicDocBlockPropertyToNativePropertyRector/Fixture/bare_class.php.inc index 850a717c86..bd35375ab1 100644 --- a/rules-tests/CodeQuality/Rector/Class_/DynamicDocBlockPropertyToNativePropertyRector/Fixture/bare_class.php.inc +++ b/rules-tests/CodeQuality/Rector/Class_/DynamicDocBlockPropertyToNativePropertyRector/Fixture/bare_class.php.inc @@ -26,7 +26,7 @@ use Rector\Tests\CodeQuality\Rector\Class_\DynamicDocBlockPropertyToNativeProper final class BareClass { - private ?SomeDependency $someDependency; + private ?\Rector\Tests\CodeQuality\Rector\Class_\DynamicDocBlockPropertyToNativePropertyRector\Source\SomeDependency $someDependency; public function run(): void { $this->someDependency = new SomeDependency(); diff --git a/rules-tests/CodeQuality/Rector/Class_/DynamicDocBlockPropertyToNativePropertyRector/Fixture/improve_existing_property_type.php.inc b/rules-tests/CodeQuality/Rector/Class_/DynamicDocBlockPropertyToNativePropertyRector/Fixture/improve_existing_property_type.php.inc index 737c89bcc6..1eef510f6b 100644 --- a/rules-tests/CodeQuality/Rector/Class_/DynamicDocBlockPropertyToNativePropertyRector/Fixture/improve_existing_property_type.php.inc +++ b/rules-tests/CodeQuality/Rector/Class_/DynamicDocBlockPropertyToNativePropertyRector/Fixture/improve_existing_property_type.php.inc @@ -31,7 +31,7 @@ use Rector\Tests\CodeQuality\Rector\Class_\DynamicDocBlockPropertyToNativeProper final class ImproveExistingPropertyType extends TestCase { - private SomeDependency $someDependency; + private \Rector\Tests\CodeQuality\Rector\Class_\DynamicDocBlockPropertyToNativePropertyRector\Source\SomeDependency $someDependency; protected function setUp(): void { parent::setUp(); diff --git a/rules-tests/CodeQuality/Rector/Class_/DynamicDocBlockPropertyToNativePropertyRector/Fixture/include_null.php.inc b/rules-tests/CodeQuality/Rector/Class_/DynamicDocBlockPropertyToNativePropertyRector/Fixture/include_null.php.inc index 5dae4a8961..582ef425fa 100644 --- a/rules-tests/CodeQuality/Rector/Class_/DynamicDocBlockPropertyToNativePropertyRector/Fixture/include_null.php.inc +++ b/rules-tests/CodeQuality/Rector/Class_/DynamicDocBlockPropertyToNativePropertyRector/Fixture/include_null.php.inc @@ -23,7 +23,7 @@ use Rector\Tests\CodeQuality\Rector\Class_\DynamicDocBlockPropertyToNativeProper final class IncludeNull { - protected ?SomeDependency $someDependency = null; + protected ?\Rector\Tests\CodeQuality\Rector\Class_\DynamicDocBlockPropertyToNativePropertyRector\Source\SomeDependency $someDependency = null; } ?> diff --git a/rules-tests/CodeQuality/Rector/Class_/DynamicDocBlockPropertyToNativePropertyRector/Fixture/some_test.php.inc b/rules-tests/CodeQuality/Rector/Class_/DynamicDocBlockPropertyToNativePropertyRector/Fixture/some_test.php.inc index cf1db2fa1c..85dfee2299 100644 --- a/rules-tests/CodeQuality/Rector/Class_/DynamicDocBlockPropertyToNativePropertyRector/Fixture/some_test.php.inc +++ b/rules-tests/CodeQuality/Rector/Class_/DynamicDocBlockPropertyToNativePropertyRector/Fixture/some_test.php.inc @@ -29,7 +29,7 @@ use Rector\Tests\CodeQuality\Rector\Class_\DynamicDocBlockPropertyToNativeProper final class SomeTest extends TestCase { - private ?SomeDependency $someDependency; + private ?\Rector\Tests\CodeQuality\Rector\Class_\DynamicDocBlockPropertyToNativePropertyRector\Source\SomeDependency $someDependency; protected function setUp(): void { parent::setUp(); diff --git a/rules-tests/CodeQuality/Rector/Class_/DynamicDocBlockPropertyToNativePropertyRector/Fixture/with_other_existing_attribute.php.inc b/rules-tests/CodeQuality/Rector/Class_/DynamicDocBlockPropertyToNativePropertyRector/Fixture/with_other_existing_attribute.php.inc index 4b1c1ce274..12fa3c20ea 100644 --- a/rules-tests/CodeQuality/Rector/Class_/DynamicDocBlockPropertyToNativePropertyRector/Fixture/with_other_existing_attribute.php.inc +++ b/rules-tests/CodeQuality/Rector/Class_/DynamicDocBlockPropertyToNativePropertyRector/Fixture/with_other_existing_attribute.php.inc @@ -28,7 +28,7 @@ use Rector\Tests\CodeQuality\Rector\Class_\DynamicDocBlockPropertyToNativeProper #[\OtherAttribute] final class WithOtherExistingAttribute { - private ?SomeDependency $someDependency; + private ?\Rector\Tests\CodeQuality\Rector\Class_\DynamicDocBlockPropertyToNativePropertyRector\Source\SomeDependency $someDependency; public function run(): void { $this->someDependency = new SomeDependency(); diff --git a/src/PHPStanStaticTypeMapper/TypeMapper/ObjectTypeMapper.php b/src/PHPStanStaticTypeMapper/TypeMapper/ObjectTypeMapper.php index d503103739..fbbb8d7919 100644 --- a/src/PHPStanStaticTypeMapper/TypeMapper/ObjectTypeMapper.php +++ b/src/PHPStanStaticTypeMapper/TypeMapper/ObjectTypeMapper.php @@ -72,9 +72,7 @@ public function mapToPhpParserNode(Type $type, string $typeKind): ?Node } if ($type instanceof ShortenedObjectType || $type instanceof AliasedObjectType) { - return new Name($type->getClassName(), [ - AttributeKey::NAMESPACED_NAME => $type->getFullyQualifiedName(), - ]); + return new FullyQualified($type->getFullyQualifiedName()); } if ($type instanceof FullyQualifiedObjectType) { From 28366ac6828e2921f78916de9226a43b2bf8f89c Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Sun, 24 Nov 2024 14:28:19 +0000 Subject: [PATCH 3/3] [ci-review] Rector Rectify --- src/PHPStanStaticTypeMapper/TypeMapper/ObjectTypeMapper.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/PHPStanStaticTypeMapper/TypeMapper/ObjectTypeMapper.php b/src/PHPStanStaticTypeMapper/TypeMapper/ObjectTypeMapper.php index fbbb8d7919..6ed4ddb195 100644 --- a/src/PHPStanStaticTypeMapper/TypeMapper/ObjectTypeMapper.php +++ b/src/PHPStanStaticTypeMapper/TypeMapper/ObjectTypeMapper.php @@ -13,7 +13,6 @@ use PHPStan\Type\ObjectType; use PHPStan\Type\Type; use PHPStan\Type\TypeTraverser; -use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface; use Rector\StaticTypeMapper\ValueObject\Type\AliasedObjectType; use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType;