Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CodeQuality][Renaming] Handle DynamicDocBlockPropertyToNativePropertyRector+RenameClassRector with aliased name #6506

Merged
merged 3 commits into from
Nov 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
5 changes: 1 addition & 4 deletions src/PHPStanStaticTypeMapper/TypeMapper/ObjectTypeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -72,9 +71,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) {
Expand Down
28 changes: 28 additions & 0 deletions tests/Issues/DynamicDocblockRename/DynamicDocblockRenameTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Issues\DynamicDocblockRename;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class DynamicDocblockRenameTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
36 changes: 36 additions & 0 deletions tests/Issues/DynamicDocblockRename/Fixture/fixture.php.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Rector\Tests\Issues\DynamicDocblockRename\Fixture;

use Rector\Tests\CodeQuality\Rector\Class_\DynamicDocBlockPropertyToNativePropertyRector\Source as SomeSource;

/**
* @property SomeSource\SomeDependency $someDependency
*/
#[\AllowDynamicProperties]
final class Fixture
{
public function run(): void
{
$this->someDependency = new SomeSource\SomeDependency();
}
}

?>
-----
<?php

namespace Rector\Tests\Issues\DynamicDocblockRename\Fixture;

use Rector\Tests\CodeQuality\Rector\Class_\DynamicDocBlockPropertyToNativePropertyRector\Source as SomeSource;

final class Fixture
{
private ?\stdClass $someDependency;
public function run(): void
{
$this->someDependency = new \stdClass();
}
}

?>
18 changes: 18 additions & 0 deletions tests/Issues/DynamicDocblockRename/config/configured_rule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

use Rector\CodeQuality\Rector\Class_\DynamicDocBlockPropertyToNativePropertyRector;
use Rector\Config\RectorConfig;
use Rector\Renaming\Rector\Name\RenameClassRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(DynamicDocBlockPropertyToNativePropertyRector::class);
$rectorConfig->ruleWithConfiguration(
RenameClassRector::class,
[
'Rector\Tests\CodeQuality\Rector\Class_\DynamicDocBlockPropertyToNativePropertyRector\Source\SomeDependency'
=> 'stdClass'
]
);
};