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] Handle aliased object type from docblock on DynamicDocBlockPropertyToNativePropertyRector #6493

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
@@ -0,0 +1,36 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\Class_\DynamicDocBlockPropertyToNativePropertyRector\Fixture;

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

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

?>
-----
<?php

namespace Rector\Tests\CodeQuality\Rector\Class_\DynamicDocBlockPropertyToNativePropertyRector\Fixture;

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

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

?>
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 ?\Rector\Tests\CodeQuality\Rector\Class_\DynamicDocBlockPropertyToNativePropertyRector\Source\SomeDependency $someDependency;
private ?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 \Rector\Tests\CodeQuality\Rector\Class_\DynamicDocBlockPropertyToNativePropertyRector\Source\SomeDependency $someDependency;
private 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 ?\Rector\Tests\CodeQuality\Rector\Class_\DynamicDocBlockPropertyToNativePropertyRector\Source\SomeDependency $someDependency = null;
protected ?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 ?\Rector\Tests\CodeQuality\Rector\Class_\DynamicDocBlockPropertyToNativePropertyRector\Source\SomeDependency $someDependency;
private ?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 ?\Rector\Tests\CodeQuality\Rector\Class_\DynamicDocBlockPropertyToNativePropertyRector\Source\SomeDependency $someDependency;
private ?SomeDependency $someDependency;
public function run(): void
{
$this->someDependency = new SomeDependency();
Expand Down
6 changes: 3 additions & 3 deletions rules/TypeDeclaration/PHPStan/ObjectTypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,16 +153,16 @@ private function processAliasedObject(
): ?AliasedObjectType {
// A. is alias in use statement matching this class alias
if ($alias === $className) {
return new AliasedObjectType($alias, $fullyQualifiedName);
return new AliasedObjectType($className, $fullyQualifiedName);
}

// B. is aliased classes matching the class name
if ($useName === $className) {
return new AliasedObjectType($alias, $fullyQualifiedName);
return new AliasedObjectType($className, $fullyQualifiedName);
}

if (str_starts_with($className, $alias . '\\')) {
return new AliasedObjectType($alias, $fullyQualifiedName . ltrim($className, $alias));
return new AliasedObjectType($className, $fullyQualifiedName . ltrim($className, $alias));
}

return null;
Expand Down
6 changes: 1 addition & 5 deletions src/PHPStanStaticTypeMapper/TypeMapper/ObjectTypeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,7 @@ public function mapToPhpParserNode(Type $type, string $typeKind): ?Node
return new Name('self');
}

if ($type instanceof ShortenedObjectType) {
return new FullyQualified($type->getFullyQualifiedName());
}

if ($type instanceof AliasedObjectType) {
if ($type instanceof ShortenedObjectType || $type instanceof AliasedObjectType) {
return new Name($type->getClassName());
}

Expand Down