Skip to content

Commit

Permalink
[CodeQuality] Handle aliased object type from docblock on DynamicDocB…
Browse files Browse the repository at this point in the history
…lockPropertyToNativePropertyRector (#6493)

* [CodeQuality] Handle aliased object type on DynamicDocBlockPropertyToNativePropertyRector

* fix

* fix
  • Loading branch information
samsonasik authored Nov 24, 2024
1 parent 0d1dbd7 commit 9fae3bf
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 13 deletions.
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

0 comments on commit 9fae3bf

Please sign in to comment.